authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-29 21:51:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-29 21:51:37-04:00
log03910925f06f6127e81de47ff22ce4d24ca565b2
tree280f12a0b8e28667b4120d1fbc0e00861d4fdfdd
parent8e939916347888e755d737c579042b034e215aa8
signaturelock-open Commit is signed but in an unrecognized format.

await does not force async if callee is blocking

closes #3067

7 files changed, 169 insertions(+), 68 deletions(-)

src/all_types.hpp+4
......@@ -36,6 +36,7 @@ struct IrInstruction;
3636struct IrInstructionCast;
3737struct IrInstructionAllocaGen;
3838struct IrInstructionCallGen;
39struct IrInstructionAwaitGen;
3940struct IrBasicBlock;
4041struct ScopeDecls;
4142struct ZigWindowsSDK;
......@@ -1486,6 +1487,7 @@ struct ZigFn {
14861487 AstNode **param_source_nodes;
14871488 Buf **param_names;
14881489 IrInstruction *err_code_spill;
1490 AstNode *assumed_non_async;
14891491
14901492 AstNode *fn_no_inline_set_node;
14911493 AstNode *fn_static_eval_set_node;
......@@ -1503,6 +1505,7 @@ struct ZigFn {
15031505
15041506 ZigList<GlobalExport> export_list;
15051507 ZigList<IrInstructionCallGen *> call_list;
1508 ZigList<IrInstructionAwaitGen *> await_list;
15061509
15071510 LLVMValueRef valgrind_client_request_array;
15081511
......@@ -3717,6 +3720,7 @@ struct IrInstructionAwaitGen {
37173720
37183721 IrInstruction *frame;
37193722 IrInstruction *result_loc;
3723 ZigFn *target_fn;
37203724};
37213725
37223726struct IrInstructionResume {
src/analyze.cpp+75-33
......@@ -31,6 +31,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
3131static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status);
3232static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, ScopeDecls *dest_decls_scope);
3333static void resolve_use_decl(CodeGen *g, TldUsingNamespace *tld_using_namespace, ScopeDecls *dest_decls_scope);
34static void analyze_fn_async(CodeGen *g, ZigFn *fn, bool resolve_frame);
3435
3536// nullptr means not analyzed yet; this one means currently being analyzed
3637static const AstNode *inferred_async_checking = reinterpret_cast<AstNode *>(0x1);
......@@ -4196,6 +4197,54 @@ static void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn) {
41964197 }
41974198}
41984199
4200// ErrorNone - not async
4201// ErrorIsAsync - yes async
4202// ErrorSemanticAnalyzeFail - compile error emitted result is invalid
4203static Error analyze_callee_async(CodeGen *g, ZigFn *fn, ZigFn *callee, AstNode *call_node,
4204 bool must_not_be_async)
4205{
4206 if (callee->type_entry->data.fn.fn_type_id.cc != CallingConventionUnspecified)
4207 return ErrorNone;
4208 if (callee->anal_state == FnAnalStateReady) {
4209 analyze_fn_body(g, callee);
4210 if (callee->anal_state == FnAnalStateInvalid) {
4211 return ErrorSemanticAnalyzeFail;
4212 }
4213 }
4214 bool callee_is_async;
4215 if (callee->anal_state == FnAnalStateComplete) {
4216 analyze_fn_async(g, callee, true);
4217 if (callee->anal_state == FnAnalStateInvalid) {
4218 return ErrorSemanticAnalyzeFail;
4219 }
4220 callee_is_async = fn_is_async(callee);
4221 } else {
4222 // If it's already been determined, use that value. Otherwise
4223 // assume non-async, emit an error later if it turned out to be async.
4224 if (callee->inferred_async_node == nullptr ||
4225 callee->inferred_async_node == inferred_async_checking)
4226 {
4227 callee->assumed_non_async = call_node;
4228 callee_is_async = false;
4229 } else {
4230 callee_is_async = callee->inferred_async_node != inferred_async_none;
4231 }
4232 }
4233 if (callee_is_async) {
4234 fn->inferred_async_node = call_node;
4235 fn->inferred_async_fn = callee;
4236 if (must_not_be_async) {
4237 ErrorMsg *msg = add_node_error(g, fn->proto_node,
4238 buf_sprintf("function with calling convention '%s' cannot be async",
4239 calling_convention_name(fn->type_entry->data.fn.fn_type_id.cc)));
4240 add_async_error_notes(g, msg, fn);
4241 return ErrorSemanticAnalyzeFail;
4242 }
4243 return ErrorIsAsync;
4244 }
4245 return ErrorNone;
4246}
4247
41994248// This function resolves functions being inferred async.
42004249static void analyze_fn_async(CodeGen *g, ZigFn *fn, bool resolve_frame) {
42014250 if (fn->inferred_async_node == inferred_async_checking) {
......@@ -4222,47 +4271,40 @@ static void analyze_fn_async(CodeGen *g, ZigFn *fn, bool resolve_frame) {
42224271
42234272 for (size_t i = 0; i < fn->call_list.length; i += 1) {
42244273 IrInstructionCallGen *call = fn->call_list.at(i);
4225 ZigFn *callee = call->fn_entry;
4226 if (callee == nullptr) {
4274 if (call->fn_entry == nullptr) {
42274275 // TODO function pointer call here, could be anything
42284276 continue;
42294277 }
4230
4231 if (callee->type_entry->data.fn.fn_type_id.cc != CallingConventionUnspecified)
4232 continue;
4233 if (callee->anal_state == FnAnalStateReady) {
4234 analyze_fn_body(g, callee);
4235 if (callee->anal_state == FnAnalStateInvalid) {
4278 switch (analyze_callee_async(g, fn, call->fn_entry, call->base.source_node, must_not_be_async)) {
4279 case ErrorSemanticAnalyzeFail:
42364280 fn->anal_state = FnAnalStateInvalid;
42374281 return;
4238 }
4239 }
4240 if (callee->anal_state != FnAnalStateComplete) {
4241 add_node_error(g, call->base.source_node,
4242 buf_sprintf("call to function '%s' depends on itself", buf_ptr(&callee->symbol_name)));
4243 fn->anal_state = FnAnalStateInvalid;
4244 return;
4245 }
4246 analyze_fn_async(g, callee, true);
4247 if (callee->anal_state == FnAnalStateInvalid) {
4248 fn->anal_state = FnAnalStateInvalid;
4249 return;
4282 case ErrorNone:
4283 continue;
4284 case ErrorIsAsync:
4285 if (resolve_frame) {
4286 resolve_async_fn_frame(g, fn);
4287 }
4288 return;
4289 default:
4290 zig_unreachable();
42504291 }
4251 if (fn_is_async(callee)) {
4252 fn->inferred_async_node = call->base.source_node;
4253 fn->inferred_async_fn = callee;
4254 if (must_not_be_async) {
4255 ErrorMsg *msg = add_node_error(g, fn->proto_node,
4256 buf_sprintf("function with calling convention '%s' cannot be async",
4257 calling_convention_name(fn->type_entry->data.fn.fn_type_id.cc)));
4258 add_async_error_notes(g, msg, fn);
4292 }
4293 for (size_t i = 0; i < fn->await_list.length; i += 1) {
4294 IrInstructionAwaitGen *await = fn->await_list.at(i);
4295 switch (analyze_callee_async(g, fn, await->target_fn, await->base.source_node, must_not_be_async)) {
4296 case ErrorSemanticAnalyzeFail:
42594297 fn->anal_state = FnAnalStateInvalid;
42604298 return;
4261 }
4262 if (resolve_frame) {
4263 resolve_async_fn_frame(g, fn);
4264 }
4265 return;
4299 case ErrorNone:
4300 continue;
4301 case ErrorIsAsync:
4302 if (resolve_frame) {
4303 resolve_async_fn_frame(g, fn);
4304 }
4305 return;
4306 default:
4307 zig_unreachable();
42664308 }
42674309 }
42684310 fn->inferred_async_node = inferred_async_none;
src/codegen.cpp+53-25
......@@ -3924,7 +3924,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
39243924 LLVMBuildStore(g->builder, awaiter_init_val, awaiter_ptr);
39253925
39263926 if (ret_has_bits) {
3927 LLVMValueRef ret_ptr = LLVMBuildStructGEP(g->builder, frame_result_loc, frame_ret_start + 2, "");
3927 ret_ptr = LLVMBuildStructGEP(g->builder, frame_result_loc, frame_ret_start + 2, "");
39283928 LLVMValueRef ret_ptr_ptr = LLVMBuildStructGEP(g->builder, frame_result_loc, frame_ret_start, "");
39293929 LLVMBuildStore(g->builder, ret_ptr, ret_ptr_ptr);
39303930
......@@ -4067,6 +4067,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
40674067 LLVMValueRef store_instr = LLVMBuildStore(g->builder, result, result_loc);
40684068 LLVMSetAlignment(store_instr, get_ptr_align(g, instruction->result_loc->value.type));
40694069 return result_loc;
4070 } else if (!callee_is_async && instruction->is_async) {
4071 LLVMBuildStore(g->builder, result, ret_ptr);
4072 return result_loc;
40704073 } else {
40714074 return result;
40724075 }
......@@ -5498,6 +5501,44 @@ static LLVMValueRef ir_render_suspend_finish(CodeGen *g, IrExecutable *executabl
54985501 return nullptr;
54995502}
55005503
5504static LLVMValueRef gen_await_early_return(CodeGen *g, IrInstruction *source_instr,
5505 LLVMValueRef target_frame_ptr, ZigType *result_type, ZigType *ptr_result_type,
5506 LLVMValueRef result_loc, bool non_async)
5507{
5508 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
5509 LLVMValueRef their_result_ptr = nullptr;
5510 if (type_has_bits(result_type) && (non_async || result_loc != nullptr)) {
5511 LLVMValueRef their_result_ptr_ptr = LLVMBuildStructGEP(g->builder, target_frame_ptr, frame_ret_start, "");
5512 their_result_ptr = LLVMBuildLoad(g->builder, their_result_ptr_ptr, "");
5513 if (result_loc != nullptr) {
5514 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
5515 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, result_loc, ptr_u8, "");
5516 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, their_result_ptr, ptr_u8, "");
5517 bool is_volatile = false;
5518 uint32_t abi_align = get_abi_alignment(g, result_type);
5519 LLVMValueRef byte_count_val = LLVMConstInt(usize_type_ref, type_size(g, result_type), false);
5520 ZigLLVMBuildMemCpy(g->builder,
5521 dest_ptr_casted, abi_align,
5522 src_ptr_casted, abi_align, byte_count_val, is_volatile);
5523 }
5524 }
5525 if (codegen_fn_has_err_ret_tracing_arg(g, result_type)) {
5526 LLVMValueRef their_trace_ptr_ptr = LLVMBuildStructGEP(g->builder, target_frame_ptr,
5527 frame_index_trace_arg(g, result_type), "");
5528 LLVMValueRef src_trace_ptr = LLVMBuildLoad(g->builder, their_trace_ptr_ptr, "");
5529 LLVMValueRef dest_trace_ptr = get_cur_err_ret_trace_val(g, source_instr->scope);
5530 LLVMValueRef args[] = { dest_trace_ptr, src_trace_ptr };
5531 ZigLLVMBuildCall(g->builder, get_merge_err_ret_traces_fn_val(g), args, 2,
5532 get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
5533 }
5534 if (non_async && type_has_bits(result_type)) {
5535 LLVMValueRef result_ptr = (result_loc == nullptr) ? their_result_ptr : result_loc;
5536 return get_handle_value(g, result_ptr, result_type, ptr_result_type);
5537 } else {
5538 return nullptr;
5539 }
5540}
5541
55015542static LLVMValueRef ir_render_await(CodeGen *g, IrExecutable *executable, IrInstructionAwaitGen *instruction) {
55025543 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
55035544 LLVMValueRef zero = LLVMConstNull(usize_type_ref);
......@@ -5505,6 +5546,14 @@ static LLVMValueRef ir_render_await(CodeGen *g, IrExecutable *executable, IrInst
55055546 ZigType *result_type = instruction->base.value.type;
55065547 ZigType *ptr_result_type = get_pointer_to_type(g, result_type, true);
55075548
5549 LLVMValueRef result_loc = (instruction->result_loc == nullptr) ?
5550 nullptr : ir_llvm_value(g, instruction->result_loc);
5551
5552 if (instruction->target_fn != nullptr && !fn_is_async(instruction->target_fn)) {
5553 return gen_await_early_return(g, &instruction->base, target_frame_ptr, result_type,
5554 ptr_result_type, result_loc, true);
5555 }
5556
55085557 // Prepare to be suspended
55095558 LLVMBasicBlockRef resume_bb = gen_suspend_begin(g, "AwaitResume");
55105559 LLVMBasicBlockRef end_bb = LLVMAppendBasicBlock(g->cur_fn_val, "AwaitEnd");
......@@ -5512,9 +5561,8 @@ static LLVMValueRef ir_render_await(CodeGen *g, IrExecutable *executable, IrInst
55125561 // At this point resuming the function will continue from resume_bb.
55135562 // This code is as if it is running inside the suspend block.
55145563
5564
55155565 // supply the awaiter return pointer
5516 LLVMValueRef result_loc = (instruction->result_loc == nullptr) ?
5517 nullptr : ir_llvm_value(g, instruction->result_loc);
55185566 if (type_has_bits(result_type)) {
55195567 LLVMValueRef awaiter_ret_ptr_ptr = LLVMBuildStructGEP(g->builder, target_frame_ptr, frame_ret_start + 1, "");
55205568 if (result_loc == nullptr) {
......@@ -5562,28 +5610,8 @@ static LLVMValueRef ir_render_await(CodeGen *g, IrExecutable *executable, IrInst
55625610 // Early return: The async function has already completed. We must copy the result and
55635611 // the error return trace if applicable.
55645612 LLVMPositionBuilderAtEnd(g->builder, early_return_block);
5565 if (type_has_bits(result_type) && result_loc != nullptr) {
5566 LLVMValueRef their_result_ptr_ptr = LLVMBuildStructGEP(g->builder, target_frame_ptr, frame_ret_start, "");
5567 LLVMValueRef their_result_ptr = LLVMBuildLoad(g->builder, their_result_ptr_ptr, "");
5568 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
5569 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, result_loc, ptr_u8, "");
5570 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, their_result_ptr, ptr_u8, "");
5571 bool is_volatile = false;
5572 uint32_t abi_align = get_abi_alignment(g, result_type);
5573 LLVMValueRef byte_count_val = LLVMConstInt(usize_type_ref, type_size(g, result_type), false);
5574 ZigLLVMBuildMemCpy(g->builder,
5575 dest_ptr_casted, abi_align,
5576 src_ptr_casted, abi_align, byte_count_val, is_volatile);
5577 }
5578 if (codegen_fn_has_err_ret_tracing_arg(g, result_type)) {
5579 LLVMValueRef their_trace_ptr_ptr = LLVMBuildStructGEP(g->builder, target_frame_ptr,
5580 frame_index_trace_arg(g, result_type), "");
5581 LLVMValueRef src_trace_ptr = LLVMBuildLoad(g->builder, their_trace_ptr_ptr, "");
5582 LLVMValueRef dest_trace_ptr = get_cur_err_ret_trace_val(g, instruction->base.scope);
5583 LLVMValueRef args[] = { dest_trace_ptr, src_trace_ptr };
5584 ZigLLVMBuildCall(g->builder, get_merge_err_ret_traces_fn_val(g), args, 2,
5585 get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
5586 }
5613 gen_await_early_return(g, &instruction->base, target_frame_ptr, result_type, ptr_result_type,
5614 result_loc, false);
55875615 LLVMBuildBr(g->builder, end_bb);
55885616
55895617 LLVMPositionBuilderAtEnd(g->builder, resume_bb);
src/error.cpp+1
......@@ -56,6 +56,7 @@ const char *err_str(Error err) {
5656 case ErrorNoSpaceLeft: return "no space left";
5757 case ErrorNoCCompilerInstalled: return "no C compiler installed";
5858 case ErrorNotLazy: return "not lazy";
59 case ErrorIsAsync: return "is async";
5960 }
6061 return "(invalid error)";
6162}
src/ir.cpp+25-10
......@@ -3268,7 +3268,7 @@ static IrInstruction *ir_build_await_src(IrBuilder *irb, Scope *scope, AstNode *
32683268 return &instruction->base;
32693269}
32703270
3271static IrInstruction *ir_build_await_gen(IrAnalyze *ira, IrInstruction *source_instruction,
3271static IrInstructionAwaitGen *ir_build_await_gen(IrAnalyze *ira, IrInstruction *source_instruction,
32723272 IrInstruction *frame, ZigType *result_type, IrInstruction *result_loc)
32733273{
32743274 IrInstructionAwaitGen *instruction = ir_build_instruction<IrInstructionAwaitGen>(&ira->new_irb,
......@@ -3280,7 +3280,7 @@ static IrInstruction *ir_build_await_gen(IrAnalyze *ira, IrInstruction *source_i
32803280 ir_ref_instruction(frame, ira->new_irb.current_basic_block);
32813281 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
32823282
3283 return &instruction->base;
3283 return instruction;
32843284}
32853285
32863286static IrInstruction *ir_build_resume(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *frame) {
......@@ -24763,18 +24763,22 @@ static IrInstruction *ir_analyze_instruction_suspend_finish(IrAnalyze *ira,
2476324763}
2476424764
2476524765static IrInstruction *analyze_frame_ptr_to_anyframe_T(IrAnalyze *ira, IrInstruction *source_instr,
24766 IrInstruction *frame_ptr)
24766 IrInstruction *frame_ptr, ZigFn **target_fn)
2476724767{
2476824768 if (type_is_invalid(frame_ptr->value.type))
2476924769 return ira->codegen->invalid_instruction;
2477024770
24771 *target_fn = nullptr;
24772
2477124773 ZigType *result_type;
2477224774 IrInstruction *frame;
2477324775 if (frame_ptr->value.type->id == ZigTypeIdPointer &&
2477424776 frame_ptr->value.type->data.pointer.ptr_len == PtrLenSingle &&
2477524777 frame_ptr->value.type->data.pointer.child_type->id == ZigTypeIdFnFrame)
2477624778 {
24777 result_type = frame_ptr->value.type->data.pointer.child_type->data.frame.fn->type_entry->data.fn.fn_type_id.return_type;
24779 ZigFn *func = frame_ptr->value.type->data.pointer.child_type->data.frame.fn;
24780 result_type = func->type_entry->data.fn.fn_type_id.return_type;
24781 *target_fn = func;
2477824782 frame = frame_ptr;
2477924783 } else {
2478024784 frame = ir_get_deref(ira, source_instr, frame_ptr, nullptr);
......@@ -24782,7 +24786,9 @@ static IrInstruction *analyze_frame_ptr_to_anyframe_T(IrAnalyze *ira, IrInstruct
2478224786 frame->value.type->data.pointer.ptr_len == PtrLenSingle &&
2478324787 frame->value.type->data.pointer.child_type->id == ZigTypeIdFnFrame)
2478424788 {
24785 result_type = frame->value.type->data.pointer.child_type->data.frame.fn->type_entry->data.fn.fn_type_id.return_type;
24789 ZigFn *func = frame->value.type->data.pointer.child_type->data.frame.fn;
24790 result_type = func->type_entry->data.fn.fn_type_id.return_type;
24791 *target_fn = func;
2478624792 } else if (frame->value.type->id != ZigTypeIdAnyFrame ||
2478724793 frame->value.type->data.any_frame.result_type == nullptr)
2478824794 {
......@@ -24803,7 +24809,11 @@ static IrInstruction *analyze_frame_ptr_to_anyframe_T(IrAnalyze *ira, IrInstruct
2480324809}
2480424810
2480524811static IrInstruction *ir_analyze_instruction_await(IrAnalyze *ira, IrInstructionAwaitSrc *instruction) {
24806 IrInstruction *frame = analyze_frame_ptr_to_anyframe_T(ira, &instruction->base, instruction->frame->child);
24812 IrInstruction *operand = instruction->frame->child;
24813 if (type_is_invalid(operand->value.type))
24814 return ira->codegen->invalid_instruction;
24815 ZigFn *target_fn;
24816 IrInstruction *frame = analyze_frame_ptr_to_anyframe_T(ira, &instruction->base, operand, &target_fn);
2480724817 if (type_is_invalid(frame->value.type))
2480824818 return ira->codegen->invalid_instruction;
2480924819
......@@ -24812,8 +24822,11 @@ static IrInstruction *ir_analyze_instruction_await(IrAnalyze *ira, IrInstruction
2481224822 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
2481324823 ir_assert(fn_entry != nullptr, &instruction->base);
2481424824
24815 if (fn_entry->inferred_async_node == nullptr) {
24816 fn_entry->inferred_async_node = instruction->base.source_node;
24825 // If it's not @Frame(func) then it's definitely a suspend point
24826 if (target_fn == nullptr) {
24827 if (fn_entry->inferred_async_node == nullptr) {
24828 fn_entry->inferred_async_node = instruction->base.source_node;
24829 }
2481724830 }
2481824831
2481924832 if (type_can_fail(result_type)) {
......@@ -24830,8 +24843,10 @@ static IrInstruction *ir_analyze_instruction_await(IrAnalyze *ira, IrInstruction
2483024843 result_loc = nullptr;
2483124844 }
2483224845
24833 IrInstruction *result = ir_build_await_gen(ira, &instruction->base, frame, result_type, result_loc);
24834 return ir_finish_anal(ira, result);
24846 IrInstructionAwaitGen *result = ir_build_await_gen(ira, &instruction->base, frame, result_type, result_loc);
24847 result->target_fn = target_fn;
24848 fn_entry->await_list.append(result);
24849 return ir_finish_anal(ira, &result->base);
2483524850}
2483624851
2483724852static IrInstruction *ir_analyze_instruction_resume(IrAnalyze *ira, IrInstructionResume *instruction) {
src/userland.h+1
......@@ -76,6 +76,7 @@ enum Error {
7676 ErrorBrokenPipe,
7777 ErrorNoSpaceLeft,
7878 ErrorNotLazy,
79 ErrorIsAsync,
7980};
8081
8182// ABI warning
test/stage1/behavior/async_fn.zig+10
......@@ -844,3 +844,13 @@ test "cast fn to async fn when it is inferred to be async" {
844844 resume S.frame;
845845 expect(S.ok);
846846}
847
848test "await does not force async if callee is blocking" {
849 const S = struct {
850 fn simple() i32 {
851 return 1234;
852 }
853 };
854 var x = async S.simple();
855 expect(await x == 1234);
856}