@@ -7112,6 +7112,12 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
71127112 IrInstruction *dest_err_ret_trace_ptr = ir_build_load_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr);
71137113 ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr, dest_err_ret_trace_ptr);
71147114 }
7115+ // Before we destroy the coroutine frame, we need to load the target promise into
7116+ // a register or local variable which does not get spilled into the frame,
7117+ // otherwise llvm tries to access memory inside the destroyed frame.
7118+ IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node,
7119+ irb->exec->await_handle_var_ptr, false);
7120+ IrInstruction *await_handle_in_block = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);
71157121 ir_build_br(irb, scope, node, check_free_block, const_bool_false);
71167122
71177123 ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_final_cleanup_block);
@@ -7126,6 +7132,14 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
71267132 incoming_values[1] = const_bool_true;
71277133 IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
71287134
7135+ IrBasicBlock **merge_incoming_blocks = allocate<IrBasicBlock *>(2);
7136+ IrInstruction **merge_incoming_values = allocate<IrInstruction *>(2);
7137+ merge_incoming_blocks[0] = irb->exec->coro_final_cleanup_block;
7138+ merge_incoming_values[0] = ir_build_const_undefined(irb, scope, node);
7139+ merge_incoming_blocks[1] = irb->exec->coro_normal_final;
7140+ merge_incoming_values[1] = await_handle_in_block;
7141+ IrInstruction *awaiter_handle = ir_build_phi(irb, scope, node, 2, merge_incoming_blocks, merge_incoming_values);
7142+
71297143 Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME);
71307144 IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node,
71317145 ImplicitAllocatorIdLocalVar);
@@ -7152,9 +7166,6 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
71527166 ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, irb->exec->coro_suspend_block, const_bool_false);
71537167
71547168 ir_set_cursor_at_end_and_append_block(irb, resume_block);
7155- IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node,
7156- irb->exec->await_handle_var_ptr, false);
7157- IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);
71587169 ir_build_coro_resume(irb, scope, node, awaiter_handle);
71597170 ir_build_br(irb, scope, node, irb->exec->coro_suspend_block, const_bool_false);
71607171 }
0 commit comments