Skip to content

Commit 4e43bde

Browse files
committed
workaround for llvm: delete coroutine allocation elision
maybe this can be reverted, but it seems to be related to llvm's coro transformations crashing. See #727
1 parent 4ac6c4d commit 4e43bde

File tree

1 file changed

+3
-39
lines changed

1 file changed

+3
-39
lines changed

src/ir.cpp

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6095,7 +6095,6 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
60956095
IrInstruction *coro_id;
60966096
IrInstruction *coro_promise_ptr;
60976097
IrInstruction *coro_result_field_ptr;
6098-
IrInstruction *coro_need_dyn_alloc;
60996098
TypeTableEntry *return_type;
61006099
Buf *result_ptr_field_name;
61016100
if (is_async) {
@@ -6113,15 +6112,6 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61136112
get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false));
61146113
IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr);
61156114
coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr);
6116-
coro_need_dyn_alloc = ir_build_coro_alloc(irb, scope, node, coro_id);
6117-
IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
6118-
IrInstruction *null_ptr = ir_build_int_to_ptr(irb, scope, node, u8_ptr_type, zero);
6119-
6120-
IrBasicBlock *dyn_alloc_block = ir_create_basic_block(irb, scope, "DynAlloc");
6121-
IrBasicBlock *coro_begin_block = ir_create_basic_block(irb, scope, "CoroBegin");
6122-
ir_build_cond_br(irb, scope, node, coro_need_dyn_alloc, dyn_alloc_block, coro_begin_block, const_bool_false);
6123-
6124-
ir_set_cursor_at_end_and_append_block(irb, dyn_alloc_block);
61256115
IrInstruction *coro_size = ir_build_coro_size(irb, scope, node);
61266116
irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdContext);
61276117
IrInstruction *alloc_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdAlloc);
@@ -6144,31 +6134,11 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61446134
ir_build_coro_alloc_fail(irb, scope, node, err_val);
61456135

61466136
ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block);
6147-
IrInstruction *unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false);
6137+
coro_unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false);
61486138
Buf *ptr_field_name = buf_create_from_str("ptr");
6149-
IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, unwrapped_mem_ptr,
6139+
IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, coro_unwrapped_mem_ptr,
61506140
ptr_field_name);
6151-
IrInstruction *coro_mem_ptr = ir_build_load_ptr(irb, scope, node, coro_mem_ptr_field);
6152-
ir_build_br(irb, scope, node, coro_begin_block, const_bool_false);
6153-
6154-
ir_set_cursor_at_end_and_append_block(irb, coro_begin_block);
6155-
6156-
IrBasicBlock **coro_mem_incoming_blocks = allocate<IrBasicBlock *>(2);
6157-
IrInstruction **coro_mem_incoming_values = allocate<IrInstruction *>(2);
6158-
coro_mem_incoming_blocks[0] = entry_block;
6159-
coro_mem_incoming_values[0] = null_ptr;
6160-
coro_mem_incoming_blocks[1] = alloc_ok_block;
6161-
coro_mem_incoming_values[1] = coro_mem_ptr;
6162-
IrInstruction *coro_mem = ir_build_phi(irb, scope, node, 2, coro_mem_incoming_blocks, coro_mem_incoming_values);
6163-
6164-
IrBasicBlock **unwrapped_mem_ptr_incoming_blocks = allocate<IrBasicBlock *>(2);
6165-
IrInstruction **unwrapped_mem_ptr_incoming_values = allocate<IrInstruction *>(2);
6166-
unwrapped_mem_ptr_incoming_blocks[0] = entry_block;
6167-
unwrapped_mem_ptr_incoming_values[0] = ir_build_const_undefined(irb, scope, node);
6168-
unwrapped_mem_ptr_incoming_blocks[1] = alloc_ok_block;
6169-
unwrapped_mem_ptr_incoming_values[1] = unwrapped_mem_ptr;
6170-
coro_unwrapped_mem_ptr = ir_build_phi(irb, scope, node, 2,
6171-
unwrapped_mem_ptr_incoming_blocks, unwrapped_mem_ptr_incoming_values);
6141+
IrInstruction *coro_mem = ir_build_load_ptr(irb, scope, node, coro_mem_ptr_field);
61726142

61736143
irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem);
61746144

@@ -6245,20 +6215,14 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
62456215
incoming_blocks[1] = irb->exec->coro_normal_final;
62466216
incoming_values[1] = const_bool_true;
62476217
IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
6248-
IrBasicBlock *dyn_free_block = ir_create_basic_block(irb, scope, "DynFree");
6249-
IrBasicBlock *end_free_block = ir_create_basic_block(irb, scope, "EndFree");
6250-
ir_build_cond_br(irb, scope, node, coro_need_dyn_alloc, dyn_free_block, end_free_block, const_bool_false);
62516218

6252-
ir_set_cursor_at_end_and_append_block(irb, dyn_free_block);
62536219
IrInstruction *free_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdFree);
62546220
size_t arg_count = 2;
62556221
IrInstruction **args = allocate<IrInstruction *>(arg_count);
62566222
args[0] = irb->exec->implicit_allocator_ptr; // self
62576223
args[1] = ir_build_load_ptr(irb, scope, node, coro_unwrapped_mem_ptr); // old_mem
62586224
ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr, nullptr, nullptr);
6259-
ir_build_br(irb, scope, node, end_free_block, const_bool_false);
62606225

6261-
ir_set_cursor_at_end_and_append_block(irb, end_free_block);
62626226
IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume");
62636227
IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "Return");
62646228
ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, return_block, const_bool_false);

0 commit comments

Comments
 (0)