Skip to content

Commit da3d59d

Browse files
bors[bot]dafaust
andauthored
Merge #1087
1087: Use loop to initialize repeat arrays r=philberty a=dafaust This PR changes how we compile initializers for arrays of repeating elements. I use the same approach outlined in the comments of the linked issue, with some tweaks. It is very similar to how the D language front-end compiles, the new function `Gcc_backend::array_initializer` is heavily inspired by the D front-end's `build_array_set` This fixes the issue where the compiler tries to allocate a vec containing all elements of the array to be constructed, and therefore explodes on huge constructions (e.g. `let x = [0u8; 4 * 1024 * 1024 * 1024 * 1024]`) However, we can only initialize non-const arrays in this way. For arrays in const contexts we must initialize them at compile time, and therefore continue using the old method. Fixes: #1068 Co-authored-by: David Faust <[email protected]>
2 parents b829e7c + 5559bdc commit da3d59d

File tree

7 files changed

+156
-11
lines changed

7 files changed

+156
-11
lines changed

gcc/rust/backend/rust-compile-context.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ class Context
250250
fn_stack.push_back (fncontext{fn, ret_addr});
251251
}
252252
void pop_fn () { fn_stack.pop_back (); }
253+
254+
bool in_fn () { return fn_stack.size () != 0; }
255+
256+
// Note: it is undefined behavior to call peek_fn () if fn_stack is empty.
253257
fncontext peek_fn () { return fn_stack.back (); }
254258

255259
void push_type (tree t) { type_decls.push_back (t); }
@@ -301,6 +305,14 @@ class Context
301305
return pop;
302306
}
303307

308+
void push_const_context (void) { const_context++; }
309+
void pop_const_context (void)
310+
{
311+
if (const_context > 0)
312+
const_context--;
313+
}
314+
bool const_context_p (void) { return (const_context > 0); }
315+
304316
std::string mangle_item (const TyTy::BaseType *ty,
305317
const Resolver::CanonicalPath &path) const
306318
{
@@ -341,6 +353,9 @@ class Context
341353
std::vector<::Bvariable *> var_decls;
342354
std::vector<tree> const_decls;
343355
std::vector<tree> func_decls;
356+
357+
// Nonzero iff we are currently compiling something inside a constant context.
358+
unsigned int const_context = 0;
344359
};
345360

346361
} // namespace Compile

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,19 +1163,54 @@ CompileExpr::array_copied_expr (Location expr_locus,
11631163
unsigned HOST_WIDE_INT len
11641164
= wi::ext (max - min + 1, precision, sign).to_uhwi ();
11651165

1166-
// create the constructor
1167-
size_t idx = 0;
1168-
std::vector<unsigned long> indexes;
1169-
std::vector<tree> constructor;
1170-
for (unsigned HOST_WIDE_INT i = 0; i < len; i++)
1171-
{
1172-
constructor.push_back (translated_expr);
1173-
indexes.push_back (idx++);
1166+
// In a const context we must initialize the entire array, which entails
1167+
// allocating for each element. If the user wants a huge array, we will OOM
1168+
// and die horribly.
1169+
if (ctx->const_context_p ())
1170+
{
1171+
size_t idx = 0;
1172+
std::vector<unsigned long> indexes;
1173+
std::vector<tree> constructor;
1174+
for (unsigned HOST_WIDE_INT i = 0; i < len; i++)
1175+
{
1176+
constructor.push_back (translated_expr);
1177+
indexes.push_back (idx++);
1178+
}
1179+
1180+
return ctx->get_backend ()->array_constructor_expression (array_type,
1181+
indexes,
1182+
constructor,
1183+
expr_locus);
11741184
}
11751185

1176-
return ctx->get_backend ()->array_constructor_expression (array_type, indexes,
1177-
constructor,
1178-
expr_locus);
1186+
else
1187+
{
1188+
// Create a new block scope in which to initialize the array
1189+
tree fndecl = NULL_TREE;
1190+
if (ctx->in_fn ())
1191+
fndecl = ctx->peek_fn ().fndecl;
1192+
1193+
std::vector<Bvariable *> locals;
1194+
tree enclosing_scope = ctx->peek_enclosing_scope ();
1195+
tree init_block
1196+
= ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
1197+
expr_locus, expr_locus);
1198+
ctx->push_block (init_block);
1199+
1200+
tree tmp;
1201+
tree stmts
1202+
= ctx->get_backend ()->array_initializer (fndecl, init_block,
1203+
array_type, capacity_expr,
1204+
translated_expr, &tmp,
1205+
expr_locus);
1206+
ctx->add_statement (stmts);
1207+
1208+
tree block = ctx->pop_block ();
1209+
1210+
// The result is a compound expression which creates a temporary array,
1211+
// initializes all the elements in a loop, and then yeilds the array.
1212+
return ctx->get_backend ()->compound_expression (block, tmp, expr_locus);
1213+
}
11791214
}
11801215

11811216
tree

gcc/rust/backend/rust-compile-item.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ CompileItem::visit (HIR::ConstantItem &constant)
9292
rust_assert (ok);
9393

9494
HIR::Expr *const_value_expr = constant.get_expr ();
95+
ctx->push_const_context ();
9596
tree const_expr
9697
= compile_constant_item (ctx, resolved_type, canonical_path,
9798
const_value_expr, constant.get_locus ());
99+
ctx->pop_const_context ();
98100

99101
ctx->push_const (const_expr);
100102
ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);

gcc/rust/rust-backend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ class Backend
275275
const std::vector<tree> &vals, Location)
276276
= 0;
277277

278+
virtual tree array_initializer (tree, tree, tree, tree, tree, tree *,
279+
Location)
280+
= 0;
281+
278282
// Return an expression for ARRAY[INDEX] as an l-value. ARRAY is a valid
279283
// fixed-length array, not a slice.
280284
virtual tree array_index_expression (tree array, tree index, Location) = 0;

gcc/rust/rust-gcc.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class Gcc_backend : public Backend
246246
tree array_constructor_expression (tree, const std::vector<unsigned long> &,
247247
const std::vector<tree> &, Location);
248248

249+
tree array_initializer (tree, tree, tree, tree, tree, tree *, Location);
250+
249251
tree array_index_expression (tree array, tree index, Location);
250252

251253
tree call_expression (tree caller, tree fn, const std::vector<tree> &args,
@@ -1694,6 +1696,77 @@ Gcc_backend::array_constructor_expression (
16941696
return ret;
16951697
}
16961698

1699+
// Build insns to create an array, initialize all elements of the array to
1700+
// value, and return it
1701+
tree
1702+
Gcc_backend::array_initializer (tree fndecl, tree block, tree array_type,
1703+
tree length, tree value, tree *tmp,
1704+
Location locus)
1705+
{
1706+
std::vector<tree> stmts;
1707+
1708+
// Temporary array we initialize with the desired value.
1709+
tree t = NULL_TREE;
1710+
Bvariable *tmp_array = this->temporary_variable (fndecl, block, array_type,
1711+
NULL_TREE, true, locus, &t);
1712+
tree arr = tmp_array->get_tree (locus);
1713+
stmts.push_back (t);
1714+
1715+
// Temporary for the array length used for initialization loop guard.
1716+
Bvariable *tmp_len = this->temporary_variable (fndecl, block, size_type_node,
1717+
length, true, locus, &t);
1718+
tree len = tmp_len->get_tree (locus);
1719+
stmts.push_back (t);
1720+
1721+
// Temporary variable for pointer used to initialize elements.
1722+
tree ptr_type = this->pointer_type (TREE_TYPE (array_type));
1723+
tree ptr_init
1724+
= build1_loc (locus.gcc_location (), ADDR_EXPR, ptr_type,
1725+
this->array_index_expression (arr, integer_zero_node, locus));
1726+
Bvariable *tmp_ptr = this->temporary_variable (fndecl, block, ptr_type,
1727+
ptr_init, false, locus, &t);
1728+
tree ptr = tmp_ptr->get_tree (locus);
1729+
stmts.push_back (t);
1730+
1731+
// push statement list for the loop
1732+
std::vector<tree> loop_stmts;
1733+
1734+
// Loop exit condition:
1735+
// if (length == 0) break;
1736+
t = this->comparison_expression (ComparisonOperator::EQUAL, len,
1737+
this->zero_expression (TREE_TYPE (len)),
1738+
locus);
1739+
1740+
t = this->exit_expression (t, locus);
1741+
loop_stmts.push_back (t);
1742+
1743+
// Assign value to the current pointer position
1744+
// *ptr = value;
1745+
t = this->assignment_statement (build_fold_indirect_ref (ptr), value, locus);
1746+
loop_stmts.push_back (t);
1747+
1748+
// Move pointer to next element
1749+
// ptr++;
1750+
tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptr_type));
1751+
t = build2 (POSTINCREMENT_EXPR, ptr_type, ptr, convert (ptr_type, size));
1752+
loop_stmts.push_back (t);
1753+
1754+
// Decrement loop counter.
1755+
// length--;
1756+
t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (len), len,
1757+
convert (TREE_TYPE (len), integer_one_node));
1758+
loop_stmts.push_back (t);
1759+
1760+
// pop statments and finish loop
1761+
tree loop_body = this->statement_list (loop_stmts);
1762+
stmts.push_back (this->loop_expression (loop_body, locus));
1763+
1764+
// Return the temporary in the provided pointer and the statement list which
1765+
// initializes it.
1766+
*tmp = tmp_array->get_tree (locus);
1767+
return this->statement_list (stmts);
1768+
}
1769+
16971770
// Return an expression representing ARRAY[INDEX]
16981771

16991772
tree
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
// Checks that we don't try to allocate a 4TB array during compilation
3+
fn main () {
4+
let x = [0; 4 * 1024 * 1024 * 1024 * 1024];
5+
// { dg-warning "unused name" "" { target *-*-* } .-1 }
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// Checks that we don't try to allocate a 4TB array during compilation
3+
fn foo() -> [u8; 4 * 1024 * 1024 * 1024 * 1024] {
4+
[0; 4 * 1024 * 1024 * 1024 * 1024]
5+
}
6+
7+
fn main () {
8+
let x = foo ();
9+
// { dg-warning "unused name" "" { target *-*-* } .-1 }
10+
}

0 commit comments

Comments
 (0)