authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-07 17:37:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-07 17:37:17-04:00
log229323e13a074e94ef8a58409c81cfd9ac807dd8
tree07e39a8d4656fcd930b381466d07e67bb8bb837b
parentd3cf040c900c14feec427f3835f247b6a4c616bb
signaturelock-open Commit is signed but in an unrecognized format.

fix suspensions inside for loops generating invalid LLVM IR

closes #3076

5 files changed, 59 insertions(+), 12 deletions(-)

src/all_types.hpp+2
......@@ -25,6 +25,7 @@ struct ZigFn;
2525struct Scope;
2626struct ScopeBlock;
2727struct ScopeFnDef;
28struct ScopeExpr;
2829struct ZigType;
2930struct ZigVar;
3031struct ErrorTableEntry;
......@@ -2230,6 +2231,7 @@ struct ScopeLoop {
22302231 ZigList<IrInstruction *> *incoming_values;
22312232 ZigList<IrBasicBlock *> *incoming_blocks;
22322233 ResultLocPeerParent *peer_parent;
2234 ScopeExpr *spill_scope;
22332235};
22342236
22352237// This scope blocks certain things from working such as comptime continue
src/analyze.cpp+20-6
......@@ -227,7 +227,7 @@ Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent) {
227227 return &scope->base;
228228}
229229
230Scope *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
230ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
231231 ScopeExpr *scope = allocate<ScopeExpr>(1);
232232 init_scope(g, &scope->base, ScopeIdExpr, node, parent);
233233 ScopeExpr *parent_expr = find_expr_scope(parent);
......@@ -238,7 +238,7 @@ Scope *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
238238 parent_expr->children_ptr[parent_expr->children_len] = scope;
239239 parent_expr->children_len = new_len;
240240 }
241 return &scope->base;
241 return scope;
242242}
243243
244244ZigType *get_scope_import(Scope *scope) {
......@@ -5713,7 +5713,6 @@ static void mark_suspension_point(Scope *scope) {
57135713 case ScopeIdCImport:
57145714 case ScopeIdSuspend:
57155715 case ScopeIdTypeOf:
5716 case ScopeIdBlock:
57175716 return;
57185717 case ScopeIdLoop:
57195718 case ScopeIdRuntime:
......@@ -5730,6 +5729,14 @@ static void mark_suspension_point(Scope *scope) {
57305729 child_expr_scope = parent_expr_scope;
57315730 continue;
57325731 }
5732 case ScopeIdBlock:
5733 if (scope->parent->parent->id == ScopeIdLoop) {
5734 ScopeLoop *loop_scope = reinterpret_cast<ScopeLoop *>(scope->parent->parent);
5735 if (loop_scope->spill_scope != nullptr) {
5736 loop_scope->spill_scope->need_spill = MemoizedBoolTrue;
5737 }
5738 }
5739 return;
57335740 }
57345741 }
57355742}
......@@ -5928,6 +5935,15 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
59285935 await->result_loc = ir_create_alloca(g, await->base.scope, await->base.source_node, fn,
59295936 await->base.value.type, "");
59305937 }
5938 for (size_t block_i = 0; block_i < fn->analyzed_executable.basic_block_list.length; block_i += 1) {
5939 IrBasicBlock *block = fn->analyzed_executable.basic_block_list.at(block_i);
5940 for (size_t instr_i = 0; instr_i < block->instruction_list.length; instr_i += 1) {
5941 IrInstruction *instruction = block->instruction_list.at(instr_i);
5942 if (instruction->id == IrInstructionIdSuspendFinish) {
5943 mark_suspension_point(instruction->scope);
5944 }
5945 }
5946 }
59315947 // Now that we've marked all the expr scopes that have to spill, we go over the instructions
59325948 // and spill the relevant ones.
59335949 for (size_t block_i = 0; block_i < fn->analyzed_executable.basic_block_list.length; block_i += 1) {
......@@ -6395,9 +6411,7 @@ void eval_min_max_value(CodeGen *g, ZigType *type_entry, ConstExprValue *const_v
63956411}
63966412
63976413static void render_const_val_ptr(CodeGen *g, Buf *buf, ConstExprValue *const_val, ZigType *type_entry) {
6398 assert(type_entry->id == ZigTypeIdPointer);
6399
6400 if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) {
6414 if (type_entry->id == ZigTypeIdPointer && type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) {
64016415 buf_append_buf(buf, &type_entry->name);
64026416 return;
64036417 }
src/analyze.hpp+1-1
......@@ -114,7 +114,7 @@ ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *
114114Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
115115Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
116116Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent);
117Scope *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
117ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
118118
119119void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str);
120120ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str);
src/ir.cpp+9-5
......@@ -6474,6 +6474,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
64746474 IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node,
64756475 ir_should_inline(irb->exec, parent_scope) || node->data.for_expr.is_inline);
64766476
6477 ScopeExpr *spill_scope = create_expr_scope(irb->codegen, node, parent_scope);
6478
64776479 AstNode *index_var_source_node;
64786480 ZigVar *index_var;
64796481 const char *index_var_name;
......@@ -6504,11 +6506,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
65046506
65056507 Buf *len_field_name = buf_create_from_str("len");
65066508 IrInstruction *len_ref = ir_build_field_ptr(irb, parent_scope, node, array_val_ptr, len_field_name, false);
6507 IrInstruction *len_val = ir_build_load_ptr(irb, parent_scope, node, len_ref);
6509 IrInstruction *len_val = ir_build_load_ptr(irb, &spill_scope->base, node, len_ref);
65086510 ir_build_br(irb, parent_scope, node, cond_block, is_comptime);
65096511
65106512 ir_set_cursor_at_end_and_append_block(irb, cond_block);
6511 IrInstruction *index_val = ir_build_load_ptr(irb, parent_scope, node, index_ptr);
6513 IrInstruction *index_val = ir_build_load_ptr(irb, &spill_scope->base, node, index_ptr);
65126514 IrInstruction *cond = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
65136515 IrBasicBlock *after_cond_block = irb->current_basic_block;
65146516 IrInstruction *void_else_value = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, parent_scope, node));
......@@ -6518,7 +6520,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
65186520 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc, is_comptime);
65196521
65206522 ir_set_cursor_at_end_and_append_block(irb, body_block);
6521 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false,
6523 Scope *elem_ptr_scope = node->data.for_expr.elem_is_ptr ? parent_scope : &spill_scope->base;
6524 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, elem_ptr_scope, node, array_val_ptr, index_val, false,
65226525 PtrLenSingle, nullptr);
65236526 // TODO make it an error to write to element variable or i variable.
65246527 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
......@@ -6526,7 +6529,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
65266529 Scope *child_scope = elem_var->child_scope;
65276530
65286531 IrInstruction *var_ptr = node->data.for_expr.elem_is_ptr ?
6529 ir_build_ref(irb, parent_scope, elem_node, elem_ptr, true, false) : elem_ptr;
6532 ir_build_ref(irb, &spill_scope->base, elem_node, elem_ptr, true, false) : elem_ptr;
65306533 ir_build_var_decl_src(irb, parent_scope, elem_node, elem_var, nullptr, var_ptr);
65316534
65326535 ZigList<IrInstruction *> incoming_values = {0};
......@@ -6539,6 +6542,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
65396542 loop_scope->incoming_values = &incoming_values;
65406543 loop_scope->lval = LValNone;
65416544 loop_scope->peer_parent = peer_parent;
6545 loop_scope->spill_scope = spill_scope;
65426546
65436547 // Note the body block of the loop is not the place that lval and result_loc are used -
65446548 // it's actually in break statements, handled similarly to return statements.
......@@ -8166,7 +8170,7 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
81668170 {
81678171 child_scope = scope;
81688172 } else {
8169 child_scope = create_expr_scope(irb->codegen, node, scope);
8173 child_scope = &create_expr_scope(irb->codegen, node, scope)->base;
81708174 }
81718175 IrInstruction *result = ir_gen_node_raw(irb, node, child_scope, lval, result_loc);
81728176 if (result == irb->codegen->invalid_instruction) {
test/stage1/behavior/async_fn.zig+27
......@@ -1151,3 +1151,30 @@ test "async fn call used in expression after a fn call" {
11511151 };
11521152 _ = async S.atest();
11531153}
1154
1155test "suspend in for loop" {
1156 const S = struct {
1157 var global_frame: ?anyframe = null;
1158
1159 fn doTheTest() void {
1160 _ = async atest();
1161 while (global_frame) |f| resume f;
1162 }
1163
1164 fn atest() void {
1165 expect(func([_]u8{ 1, 2, 3 }) == 6);
1166 }
1167 fn func(stuff: []const u8) u32 {
1168 global_frame = @frame();
1169 var sum: u32 = 0;
1170 for (stuff) |x| {
1171 suspend;
1172 sum += x;
1173 }
1174 global_frame = null;
1175 return sum;
1176 }
1177 };
1178 S.doTheTest();
1179}
1180