authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-26 02:46:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-26 02:46:21-05:00
log3e86fb500dc918618a2ccaa5d942de98bd5fea47
tree7498a99a75c71b2ff0fca06c74ec18aa318dd47c
parentc60496a297a0d4c53ad0e22850ad62b0b4a2d841

implement coroutine suspend

see #727

4 files changed, 139 insertions(+), 8 deletions(-)

src/all_types.hpp+10
......@@ -63,6 +63,8 @@ struct IrExecutable {
6363 IrInstruction *implicit_allocator_ptr;
6464 IrBasicBlock *coro_early_final;
6565 IrBasicBlock *coro_normal_final;
66 IrBasicBlock *coro_suspend_block;
67 IrBasicBlock *coro_final_cleanup_block;
6668};
6769
6870enum OutType {
......@@ -1631,6 +1633,7 @@ struct CodeGen {
16311633 LLVMValueRef coro_end_fn_val;
16321634 LLVMValueRef coro_free_fn_val;
16331635 LLVMValueRef coro_resume_fn_val;
1636 LLVMValueRef coro_save_fn_val;
16341637 bool error_during_imports;
16351638
16361639 const char **clang_argv;
......@@ -2000,6 +2003,7 @@ enum IrInstructionId {
20002003 IrInstructionIdCoroEnd,
20012004 IrInstructionIdCoroFree,
20022005 IrInstructionIdCoroResume,
2006 IrInstructionIdCoroSave,
20032007};
20042008
20052009struct IrInstruction {
......@@ -2902,6 +2906,12 @@ struct IrInstructionCoroResume {
29022906 IrInstruction *awaiter_handle;
29032907};
29042908
2909struct IrInstructionCoroSave {
2910 IrInstruction base;
2911
2912 IrInstruction *coro_handle;
2913};
2914
29052915static const size_t slice_ptr_index = 0;
29062916static const size_t slice_len_index = 1;
29072917
src/codegen.cpp+22
......@@ -1066,6 +1066,21 @@ static LLVMValueRef get_coro_resume_fn_val(CodeGen *g) {
10661066 return g->coro_resume_fn_val;
10671067}
10681068
1069static LLVMValueRef get_coro_save_fn_val(CodeGen *g) {
1070 if (g->coro_save_fn_val)
1071 return g->coro_save_fn_val;
1072
1073 LLVMTypeRef param_types[] = {
1074 LLVMPointerType(LLVMInt8Type(), 0),
1075 };
1076 LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 1, false);
1077 Buf *name = buf_sprintf("llvm.coro.save");
1078 g->coro_save_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
1079 assert(LLVMGetIntrinsicID(g->coro_save_fn_val));
1080
1081 return g->coro_save_fn_val;
1082}
1083
10691084static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
10701085 if (g->return_address_fn_val)
10711086 return g->return_address_fn_val;
......@@ -3954,6 +3969,11 @@ static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable,
39543969 return LLVMBuildCall(g->builder, get_coro_resume_fn_val(g), &awaiter_handle, 1, "");
39553970}
39563971
3972static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, IrInstructionCoroSave *instruction) {
3973 LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle);
3974 return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, "");
3975}
3976
39573977static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
39583978 AstNode *source_node = instruction->source_node;
39593979 Scope *scope = instruction->scope;
......@@ -4157,6 +4177,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
41574177 return ir_render_coro_free(g, executable, (IrInstructionCoroFree *)instruction);
41584178 case IrInstructionIdCoroResume:
41594179 return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction);
4180 case IrInstructionIdCoroSave:
4181 return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction);
41604182 }
41614183 zig_unreachable();
41624184}
src/ir.cpp+98-8
......@@ -691,6 +691,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroResume *) {
691691 return IrInstructionIdCoroResume;
692692}
693693
694static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSave *) {
695 return IrInstructionIdCoroSave;
696}
697
694698template<typename T>
695699static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
696700 T *special_instruction = allocate<T>(1);
......@@ -2585,6 +2589,17 @@ static IrInstruction *ir_build_coro_resume(IrBuilder *irb, Scope *scope, AstNode
25852589 return &instruction->base;
25862590}
25872591
2592static IrInstruction *ir_build_coro_save(IrBuilder *irb, Scope *scope, AstNode *source_node,
2593 IrInstruction *coro_handle)
2594{
2595 IrInstructionCoroSave *instruction = ir_build_instruction<IrInstructionCoroSave>(irb, scope, source_node);
2596 instruction->coro_handle = coro_handle;
2597
2598 ir_ref_instruction(coro_handle, irb->current_basic_block);
2599
2600 return &instruction->base;
2601}
2602
25882603static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
25892604 results[ReturnKindUnconditional] = 0;
25902605 results[ReturnKindError] = 0;
......@@ -5847,7 +5862,67 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast
58475862static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
58485863 assert(node->type == NodeTypeSuspend);
58495864
5850 zig_panic("TODO: generate suspend");
5865 FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
5866 if (!fn_entry) {
5867 add_node_error(irb->codegen, node, buf_sprintf("suspend outside function definition"));
5868 return irb->codegen->invalid_instruction;
5869 }
5870 if (fn_entry->type_entry->data.fn.fn_type_id.cc != CallingConventionAsync) {
5871 add_node_error(irb->codegen, node, buf_sprintf("suspend in non-async function"));
5872 return irb->codegen->invalid_instruction;
5873 }
5874
5875 ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(parent_scope);
5876 if (scope_defer_expr) {
5877 if (!scope_defer_expr->reported_err) {
5878 add_node_error(irb->codegen, node, buf_sprintf("cannot suspend inside defer expression"));
5879 scope_defer_expr->reported_err = true;
5880 }
5881 return irb->codegen->invalid_instruction;
5882 }
5883
5884 Scope *outer_scope = irb->exec->begin_scope;
5885
5886
5887 IrInstruction *suspend_code;
5888 IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false);
5889 if (node->data.suspend.block == nullptr) {
5890 suspend_code = ir_build_coro_suspend(irb, parent_scope, node, nullptr, const_bool_false);
5891 } else {
5892 assert(node->data.suspend.promise_symbol != nullptr);
5893 assert(node->data.suspend.promise_symbol->type == NodeTypeSymbol);
5894 Buf *promise_symbol_name = node->data.suspend.promise_symbol->data.symbol_expr.symbol;
5895 Scope *child_scope;
5896 if (!buf_eql_str(promise_symbol_name, "_")) {
5897 VariableTableEntry *promise_var = ir_create_var(irb, node, parent_scope, promise_symbol_name,
5898 true, true, false, const_bool_false);
5899 ir_build_var_decl(irb, parent_scope, node, promise_var, nullptr, nullptr, irb->exec->coro_handle);
5900 child_scope = promise_var->child_scope;
5901 } else {
5902 child_scope = parent_scope;
5903 }
5904 IrInstruction *save_token = ir_build_coro_save(irb, child_scope, node, irb->exec->coro_handle);
5905 ir_gen_node(irb, node->data.suspend.block, child_scope);
5906 suspend_code = ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false);
5907 }
5908
5909 IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup");
5910 IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume");
5911
5912 IrInstructionSwitchBrCase *cases = allocate<IrInstructionSwitchBrCase>(2);
5913 cases[0].value = ir_build_const_u8(irb, parent_scope, node, 0);
5914 cases[0].block = resume_block;
5915 cases[1].value = ir_build_const_u8(irb, parent_scope, node, 1);
5916 cases[1].block = cleanup_block;
5917 ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block,
5918 2, cases, const_bool_false);
5919
5920 ir_set_cursor_at_end_and_append_block(irb, cleanup_block);
5921 ir_gen_defers_for_block(irb, parent_scope, outer_scope, true);
5922 ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false);
5923
5924 ir_set_cursor_at_end_and_append_block(irb, resume_block);
5925 return ir_build_const_void(irb, parent_scope, node);
58515926}
58525927
58535928static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
......@@ -6099,6 +6174,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
60996174
61006175 irb->exec->coro_early_final = ir_create_basic_block(irb, scope, "CoroEarlyFinal");
61016176 irb->exec->coro_normal_final = ir_create_basic_block(irb, scope, "CoroNormalFinal");
6177 irb->exec->coro_suspend_block = ir_create_basic_block(irb, scope, "Suspend");
6178 irb->exec->coro_final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup");
61026179 }
61036180
61046181 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LVAL_NONE);
......@@ -6112,8 +6189,6 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61126189
61136190 if (is_async) {
61146191 IrBasicBlock *invalid_resume_block = ir_create_basic_block(irb, scope, "InvalidResume");
6115 IrBasicBlock *final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup");
6116 IrBasicBlock *suspend_block = ir_create_basic_block(irb, scope, "Suspend");
61176192 IrBasicBlock *check_free_block = ir_create_basic_block(irb, scope, "CheckFree");
61186193
61196194 ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_early_final);
......@@ -6123,10 +6198,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61236198 cases[0].value = ir_build_const_u8(irb, scope, node, 0);
61246199 cases[0].block = invalid_resume_block;
61256200 cases[1].value = ir_build_const_u8(irb, scope, node, 1);
6126 cases[1].block = final_cleanup_block;
6127 ir_build_switch_br(irb, scope, node, suspend_code, suspend_block, 2, cases, const_bool_false);
6201 cases[1].block = irb->exec->coro_final_cleanup_block;
6202 ir_build_switch_br(irb, scope, node, suspend_code, irb->exec->coro_suspend_block, 2, cases, const_bool_false);
61286203
6129 ir_set_cursor_at_end_and_append_block(irb, suspend_block);
6204 ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_suspend_block);
61306205 ir_build_coro_end(irb, scope, node);
61316206 ir_build_return(irb, scope, node, irb->exec->coro_handle);
61326207
......@@ -6136,7 +6211,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61366211 ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_normal_final);
61376212 ir_build_br(irb, scope, node, check_free_block, const_bool_false);
61386213
6139 ir_set_cursor_at_end_and_append_block(irb, final_cleanup_block);
6214 ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_final_cleanup_block);
61406215 if (type_has_bits(return_type)) {
61416216 IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr);
61426217 IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, result_ptr);
......@@ -6152,7 +6227,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
61526227 ir_set_cursor_at_end_and_append_block(irb, check_free_block);
61536228 IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
61546229 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
6155 incoming_blocks[0] = final_cleanup_block;
6230 incoming_blocks[0] = irb->exec->coro_final_cleanup_block;
61566231 incoming_values[0] = const_bool_false;
61576232 incoming_blocks[1] = irb->exec->coro_normal_final;
61586233 incoming_values[1] = const_bool_true;
......@@ -17219,6 +17294,18 @@ static TypeTableEntry *ir_analyze_instruction_coro_resume(IrAnalyze *ira, IrInst
1721917294 return result->value.type;
1722017295}
1722117296
17297static TypeTableEntry *ir_analyze_instruction_coro_save(IrAnalyze *ira, IrInstructionCoroSave *instruction) {
17298 IrInstruction *coro_handle = instruction->coro_handle->other;
17299 if (type_is_invalid(coro_handle->value.type))
17300 return ira->codegen->builtin_types.entry_invalid;
17301
17302 IrInstruction *result = ir_build_coro_save(&ira->new_irb, instruction->base.scope,
17303 instruction->base.source_node, coro_handle);
17304 ir_link_new_instruction(result, &instruction->base);
17305 result->value.type = ira->codegen->builtin_types.entry_usize;
17306 return result->value.type;
17307}
17308
1722217309
1722317310static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
1722417311 switch (instruction->id) {
......@@ -17444,6 +17531,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1744417531 return ir_analyze_instruction_coro_free(ira, (IrInstructionCoroFree *)instruction);
1744517532 case IrInstructionIdCoroResume:
1744617533 return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction);
17534 case IrInstructionIdCoroSave:
17535 return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction);
1744717536 }
1744817537 zig_unreachable();
1744917538}
......@@ -17566,6 +17655,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1756617655 case IrInstructionIdCoroAllocFail:
1756717656 case IrInstructionIdCoroEnd:
1756817657 case IrInstructionIdCoroResume:
17658 case IrInstructionIdCoroSave:
1756917659 return true;
1757017660
1757117661 case IrInstructionIdPhi:
src/ir_print.cpp+9
......@@ -1090,6 +1090,12 @@ static void ir_print_coro_resume(IrPrint *irp, IrInstructionCoroResume *instruct
10901090 fprintf(irp->f, ")");
10911091}
10921092
1093static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction) {
1094 fprintf(irp->f, "@coroSave(");
1095 ir_print_other_instruction(irp, instruction->coro_handle);
1096 fprintf(irp->f, ")");
1097}
1098
10931099static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
10941100 ir_print_prefix(irp, instruction);
10951101 switch (instruction->id) {
......@@ -1443,6 +1449,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
14431449 case IrInstructionIdCoroResume:
14441450 ir_print_coro_resume(irp, (IrInstructionCoroResume *)instruction);
14451451 break;
1452 case IrInstructionIdCoroSave:
1453 ir_print_coro_save(irp, (IrInstructionCoroSave *)instruction);
1454 break;
14461455 }
14471456 fprintf(irp->f, "\n");
14481457}