From 3d58d7232ab6d1fd54523182beb99c31512bc4b9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Feb 2018 00:05:38 -0500 Subject: parse async fn calls and cancel expressions --- src/codegen.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 15648cbdec..4d9b0279d0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3088,6 +3088,10 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu return g->cur_err_ret_trace_val; } +static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrInstructionCancel *instruction) { + zig_panic("TODO ir_render_cancel"); +} + static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { switch (atomic_order) { case AtomicOrderUnordered: return LLVMAtomicOrderingUnordered; @@ -3862,6 +3866,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_align_cast(g, executable, (IrInstructionAlignCast *)instruction); case IrInstructionIdErrorReturnTrace: return ir_render_error_return_trace(g, executable, (IrInstructionErrorReturnTrace *)instruction); + case IrInstructionIdCancel: + return ir_render_cancel(g, executable, (IrInstructionCancel *)instruction); } zig_unreachable(); } @@ -5271,6 +5277,16 @@ static void define_builtin_types(CodeGen *g) { g->primitive_type_table.put(&entry->name, entry); } + { + TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); + TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdVoid); + entry->type_ref = u8_ptr_type->type_ref; + entry->zero_bits = false; + buf_init_from_str(&entry->name, "promise"); + entry->di_type = u8_ptr_type->di_type; + g->builtin_types.entry_promise = entry; + g->primitive_type_table.put(&entry->name, entry); + } } -- cgit v1.2.3 From a06f3c74fdc0d8bf0f42427a261ab32351753fcb Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Feb 2018 00:31:52 -0500 Subject: parse async fn definitions See #727 --- doc/langref.html.in | 2 +- src/all_types.hpp | 3 +++ src/analyze.cpp | 2 ++ src/codegen.cpp | 2 ++ src/ir.cpp | 49 +++++++++++++++++++++++++++++++++++++++---------- src/parser.cpp | 6 +++++- std/mem.zig | 2 +- 7 files changed, 53 insertions(+), 13 deletions(-) (limited to 'src/codegen.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 9123b1df74..05e804d78a 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -5645,7 +5645,7 @@ UseDecl = "use" Expression ";" ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";" -FnProto = option("nakedcc" | "stdcallcc" | "extern") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr +FnProto = option("nakedcc" | "stdcallcc" | "extern" | "async") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr FnDef = option("inline" | "export") FnProto Block diff --git a/src/all_types.hpp b/src/all_types.hpp index 694a2f64d9..b87899c526 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -406,6 +406,7 @@ enum CallingConvention { CallingConventionCold, CallingConventionNaked, CallingConventionStdcall, + CallingConventionAsync, }; struct AstNodeFnProto { @@ -2152,6 +2153,8 @@ struct IrInstructionCall { bool is_comptime; LLVMValueRef tmp_ptr; FnInline fn_inline; + bool is_async; + IrInstruction *async_allocator; }; struct IrInstructionConst { diff --git a/src/analyze.cpp b/src/analyze.cpp index 4545db9d78..4d6b218d0e 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -884,6 +884,7 @@ static const char *calling_convention_name(CallingConvention cc) { case CallingConventionCold: return "coldcc"; case CallingConventionNaked: return "nakedcc"; case CallingConventionStdcall: return "stdcallcc"; + case CallingConventionAsync: return "async"; } zig_unreachable(); } @@ -895,6 +896,7 @@ static const char *calling_convention_fn_type_str(CallingConvention cc) { case CallingConventionCold: return "coldcc "; case CallingConventionNaked: return "nakedcc "; case CallingConventionStdcall: return "stdcallcc "; + case CallingConventionAsync: return "async "; } zig_unreachable(); } diff --git a/src/codegen.cpp b/src/codegen.cpp index 4d9b0279d0..7c80c33754 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -381,6 +381,8 @@ static LLVMCallConv get_llvm_cc(CodeGen *g, CallingConvention cc) { } else { return LLVMCCallConv; } + case CallingConventionAsync: + return LLVMFastCallConv; } zig_unreachable(); } diff --git a/src/ir.cpp b/src/ir.cpp index d220efde79..7e95d98427 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -986,7 +986,7 @@ static IrInstruction *ir_build_union_field_ptr_from(IrBuilder *irb, IrInstructio static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) { IrInstructionCall *call_instruction = ir_build_instruction(irb, scope, source_node); call_instruction->fn_entry = fn_entry; @@ -995,21 +995,25 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc call_instruction->fn_inline = fn_inline; call_instruction->args = args; call_instruction->arg_count = arg_count; + call_instruction->is_async = is_async; + call_instruction->async_allocator = async_allocator; if (fn_ref) ir_ref_instruction(fn_ref, irb->current_basic_block); for (size_t i = 0; i < arg_count; i += 1) ir_ref_instruction(args[i], irb->current_basic_block); + if (async_allocator) + ir_ref_instruction(async_allocator, irb->current_basic_block); return &call_instruction->base; } static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) { IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope, - old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline); + old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline, is_async, async_allocator); ir_link_new_instruction(new_instruction, old_instruction); return new_instruction; } @@ -3754,7 +3758,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo } FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever; - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr); } case BuiltinFnIdTypeId: { @@ -3888,11 +3892,17 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node return args[i]; } - if (node->data.fn_call_expr.is_async) { - zig_panic("TODO ir_gen_fn_call for async fn calls"); + bool is_async = node->data.fn_call_expr.is_async; + IrInstruction *async_allocator = nullptr; + if (is_async) { + if (node->data.fn_call_expr.async_allocator) { + async_allocator = ir_gen_node(irb, node->data.fn_call_expr.async_allocator, scope); + if (async_allocator == irb->codegen->invalid_instruction) + return async_allocator; + } } - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator); } static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) { @@ -10584,6 +10594,11 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi buf_sprintf("exported function must specify calling convention")); add_error_note(ira->codegen, msg, fn_entry->proto_node, buf_sprintf("declared here")); } break; + case CallingConventionAsync: { + ErrorMsg *msg = ir_add_error(ira, target, + buf_sprintf("exported function cannot be async")); + add_error_note(ira->codegen, msg, fn_entry->proto_node, buf_sprintf("declared here")); + } break; case CallingConventionC: case CallingConventionNaked: case CallingConventionCold: @@ -10963,6 +10978,13 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } return ira->codegen->builtin_types.entry_invalid; } + if (fn_type_id->cc == CallingConventionAsync && !call_instruction->is_async) { + ErrorMsg *msg = ir_add_error(ira, fn_ref, buf_sprintf("must use async keyword to call async function")); + if (fn_proto_node) { + add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("declared here")); + } + return ira->codegen->builtin_types.entry_invalid; + } if (fn_type_id->is_var_args) { @@ -11258,7 +11280,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count; IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, - impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline); + impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline, false, nullptr); TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type; ir_add_alloca(ira, new_call_instruction, return_type); @@ -11324,12 +11346,16 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal assert(next_arg_index == call_param_count); + if (call_instruction->is_async) { + zig_panic("TODO handle async fn call"); + } + TypeTableEntry *return_type = fn_type_id->return_type; if (type_is_invalid(return_type)) return ira->codegen->builtin_types.entry_invalid; IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, - fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline); + fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr); ir_add_alloca(ira, new_call_instruction, return_type); return ir_finish_anal(ira, return_type); @@ -16491,7 +16517,10 @@ static TypeTableEntry *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruc } static TypeTableEntry *ir_analyze_instruction_cancel(IrAnalyze *ira, IrInstructionCancel *instruction) { - IrInstruction *casted_target = ir_implicit_cast(ira, instruction->target->other, ira->codegen->builtin_types.entry_promise); + IrInstruction *target_inst = instruction->target->other; + if (type_is_invalid(target_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *casted_target = ir_implicit_cast(ira, target_inst, ira->codegen->builtin_types.entry_promise); if (type_is_invalid(casted_target->value.type)) return ira->codegen->builtin_types.entry_invalid; diff --git a/src/parser.cpp b/src/parser.cpp index fc682ee62a..ee7141a910 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2333,7 +2333,7 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand } /* -FnProto = option("nakedcc" | "stdcallcc" | "extern") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr +FnProto = option("nakedcc" | "stdcallcc" | "extern" | "async") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr */ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool mandatory, VisibMod visib_mod) { Token *first_token = &pc->tokens->at(*token_index); @@ -2345,6 +2345,10 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m *token_index += 1; fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); cc = CallingConventionNaked; + } else if (first_token->id == TokenIdKeywordAsync) { + *token_index += 1; + fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); + cc = CallingConventionAsync; } else if (first_token->id == TokenIdKeywordStdcallCC) { *token_index += 1; fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); diff --git a/std/mem.zig b/std/mem.zig index 1dfef86a8f..2adb647ef6 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -124,7 +124,7 @@ pub const Allocator = struct { return self.allocator.allocFn(self.allocator, byte_count, alignment); } - fn free(self: &const AsyncAllocator, old_mem: []u8) { + fn free(self: &const AsyncAllocator, old_mem: []u8) void { return self.allocator.freeFn(self.allocator, old_mem); } }; -- cgit v1.2.3 From 65a51b401cfe17daee0c64404c8f564b0f282224 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Feb 2018 16:42:14 -0500 Subject: add promise type See #727 --- src/all_types.hpp | 8 ++++++++ src/analyze.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/analyze.hpp | 1 + src/codegen.cpp | 13 ++++++------- src/ir.cpp | 14 ++++++++++++++ 5 files changed, 80 insertions(+), 8 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index b87899c526..dbd8e0aa11 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1096,6 +1096,11 @@ struct TypeTableEntryBoundFn { TypeTableEntry *fn_type; }; +struct TypeTableEntryPromise { + // null if `promise` instead of `promise->T` + TypeTableEntry *result_type; +}; + enum TypeTableEntryId { TypeTableEntryIdInvalid, TypeTableEntryIdVar, @@ -1123,6 +1128,7 @@ enum TypeTableEntryId { TypeTableEntryIdBoundFn, TypeTableEntryIdArgTuple, TypeTableEntryIdOpaque, + TypeTableEntryIdPromise, }; struct TypeTableEntry { @@ -1149,11 +1155,13 @@ struct TypeTableEntry { TypeTableEntryUnion unionation; TypeTableEntryFn fn; TypeTableEntryBoundFn bound_fn; + TypeTableEntryPromise promise; } data; // use these fields to make sure we don't duplicate type table entries for the same type TypeTableEntry *pointer_parent[2]; // [0 - mut, 1 - const] TypeTableEntry *maybe_parent; + TypeTableEntry *promise_parent; // If we generate a constant name value for this type, we memoize it here. // The type of this is array ConstExprValue *cached_const_name_val; diff --git a/src/analyze.cpp b/src/analyze.cpp index 4d6b218d0e..e28a2d20f0 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -230,6 +230,7 @@ bool type_is_complete(TypeTableEntry *type_entry) { case TypeTableEntryIdBlock: case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: return true; } zig_unreachable(); @@ -267,6 +268,7 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) { case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: return true; } zig_unreachable(); @@ -339,6 +341,32 @@ TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) { return get_int_type(g, false, bits_needed_for_unsigned(x)); } +TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type) { + if (result_type != nullptr && result_type->promise_parent != nullptr) { + return result_type->promise_parent; + } else if (result_type == nullptr && g->builtin_types.entry_promise != nullptr) { + return g->builtin_types.entry_promise; + } + + TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); + TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPromise); + entry->type_ref = u8_ptr_type->type_ref; + entry->zero_bits = false; + entry->data.promise.result_type = result_type; + buf_init_from_str(&entry->name, "promise"); + if (result_type != nullptr) { + buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name)); + } + entry->di_type = u8_ptr_type->di_type; + + if (result_type != nullptr) { + result_type->promise_parent = entry; + } else if (result_type == nullptr) { + g->builtin_types.entry_promise = entry; + } + return entry; +} + TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const, bool is_volatile, uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count) { @@ -1203,6 +1231,7 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) { case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: return false; case TypeTableEntryIdVoid: case TypeTableEntryIdBool: @@ -1243,6 +1272,7 @@ static bool type_allowed_in_extern(CodeGen *g, TypeTableEntry *type_entry) { case TypeTableEntryIdBlock: case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: return false; case TypeTableEntryIdOpaque: case TypeTableEntryIdUnreachable: @@ -1383,7 +1413,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c case TypeTableEntryIdBoundFn: case TypeTableEntryIdMetaType: add_node_error(g, param_node->data.param_decl.type, - buf_sprintf("parameter of type '%s' must be declared inline", + buf_sprintf("parameter of type '%s' must be declared comptime", buf_ptr(&type_entry->name))); return g->builtin_types.entry_invalid; case TypeTableEntryIdVoid: @@ -1399,6 +1429,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c case TypeTableEntryIdEnum: case TypeTableEntryIdUnion: case TypeTableEntryIdFn: + case TypeTableEntryIdPromise: ensure_complete_type(g, type_entry); if (fn_type_id.cc == CallingConventionUnspecified && !type_is_copyable(g, type_entry)) { add_node_error(g, param_node->data.param_decl.type, @@ -1480,6 +1511,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c case TypeTableEntryIdEnum: case TypeTableEntryIdUnion: case TypeTableEntryIdFn: + case TypeTableEntryIdPromise: break; } @@ -3175,6 +3207,7 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt case TypeTableEntryIdUnion: case TypeTableEntryIdFn: case TypeTableEntryIdBoundFn: + case TypeTableEntryIdPromise: return type_entry; } zig_unreachable(); @@ -3553,6 +3586,7 @@ static bool is_container(TypeTableEntry *type_entry) { case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: return false; } zig_unreachable(); @@ -3603,6 +3637,7 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) { case TypeTableEntryIdVar: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: zig_unreachable(); } } @@ -4095,6 +4130,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) { case TypeTableEntryIdErrorSet: case TypeTableEntryIdFn: case TypeTableEntryIdEnum: + case TypeTableEntryIdPromise: return false; case TypeTableEntryIdArray: case TypeTableEntryIdStruct: @@ -4342,6 +4378,9 @@ static uint32_t hash_const_val(ConstExprValue *const_val) { } zig_unreachable(); } + case TypeTableEntryIdPromise: + // TODO better hashing algorithm + return 223048345; case TypeTableEntryIdUndefLit: return 162837799; case TypeTableEntryIdNullLit: @@ -4501,6 +4540,7 @@ bool type_requires_comptime(TypeTableEntry *type_entry) { case TypeTableEntryIdPointer: case TypeTableEntryIdVoid: case TypeTableEntryIdUnreachable: + case TypeTableEntryIdPromise: return false; } zig_unreachable(); @@ -4970,6 +5010,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) { case TypeTableEntryIdInvalid: case TypeTableEntryIdUnreachable: case TypeTableEntryIdVar: + case TypeTableEntryIdPromise: zig_unreachable(); } zig_unreachable(); @@ -5244,6 +5285,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { buf_appendf(buf, "(args value)"); return; } + case TypeTableEntryIdPromise: + zig_unreachable(); } zig_unreachable(); } @@ -5305,6 +5348,7 @@ uint32_t type_id_hash(TypeId x) { case TypeTableEntryIdBlock: case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: zig_unreachable(); case TypeTableEntryIdErrorUnion: return hash_ptr(x.data.error_union.err_set_type) ^ hash_ptr(x.data.error_union.payload_type); @@ -5342,6 +5386,7 @@ bool type_id_eql(TypeId a, TypeId b) { case TypeTableEntryIdUndefLit: case TypeTableEntryIdNullLit: case TypeTableEntryIdMaybe: + case TypeTableEntryIdPromise: case TypeTableEntryIdErrorSet: case TypeTableEntryIdEnum: case TypeTableEntryIdUnion: @@ -5469,6 +5514,7 @@ static const TypeTableEntryId all_type_ids[] = { TypeTableEntryIdBoundFn, TypeTableEntryIdArgTuple, TypeTableEntryIdOpaque, + TypeTableEntryIdPromise, }; TypeTableEntryId type_id_at_index(size_t index) { @@ -5533,6 +5579,8 @@ size_t type_id_index(TypeTableEntryId id) { return 22; case TypeTableEntryIdOpaque: return 23; + case TypeTableEntryIdPromise: + return 24; } zig_unreachable(); } @@ -5590,6 +5638,8 @@ const char *type_id_name(TypeTableEntryId id) { return "ArgTuple"; case TypeTableEntryIdOpaque: return "Opaque"; + case TypeTableEntryIdPromise: + return "Promise"; } zig_unreachable(); } diff --git a/src/analyze.hpp b/src/analyze.hpp index c0c89cf36b..34339c3688 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -35,6 +35,7 @@ TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry); TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name); TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[], TypeTableEntry *field_types[], size_t field_count); +TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type); TypeTableEntry *get_test_fn_type(CodeGen *g); bool handle_is_ptr(TypeTableEntry *type_entry); void find_libc_include_path(CodeGen *g); diff --git a/src/codegen.cpp b/src/codegen.cpp index 7c80c33754..291db7017b 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4017,6 +4017,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con case TypeTableEntryIdPointer: case TypeTableEntryIdFn: case TypeTableEntryIdMaybe: + case TypeTableEntryIdPromise: { LLVMValueRef ptr_val = gen_const_val(g, const_val, ""); LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref); @@ -4434,6 +4435,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c case TypeTableEntryIdVar: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: zig_unreachable(); } @@ -5280,13 +5282,7 @@ static void define_builtin_types(CodeGen *g) { g->primitive_type_table.put(&entry->name, entry); } { - TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); - TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdVoid); - entry->type_ref = u8_ptr_type->type_ref; - entry->zero_bits = false; - buf_init_from_str(&entry->name, "promise"); - entry->di_type = u8_ptr_type->di_type; - g->builtin_types.entry_promise = entry; + TypeTableEntry *entry = get_promise_type(g, nullptr); g->primitive_type_table.put(&entry->name, entry); } @@ -5916,6 +5912,7 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, TypeTableEntry case TypeTableEntryIdArgTuple: case TypeTableEntryIdErrorUnion: case TypeTableEntryIdErrorSet: + case TypeTableEntryIdPromise: zig_unreachable(); case TypeTableEntryIdVoid: case TypeTableEntryIdUnreachable: @@ -6102,6 +6099,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf case TypeTableEntryIdNullLit: case TypeTableEntryIdVar: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: zig_unreachable(); } } @@ -6262,6 +6260,7 @@ static void gen_h_file(CodeGen *g) { case TypeTableEntryIdArgTuple: case TypeTableEntryIdMaybe: case TypeTableEntryIdFn: + case TypeTableEntryIdPromise: zig_unreachable(); case TypeTableEntryIdEnum: assert(type_entry->data.enumeration.layout == ContainerLayoutExtern); diff --git a/src/ir.cpp b/src/ir.cpp index 7e95d98427..8d03a2276a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9589,6 +9589,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp case TypeTableEntryIdBlock: case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: if (!is_equality_cmp) { ir_add_error_node(ira, source_node, buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name))); @@ -10418,6 +10419,7 @@ static VarClassRequired get_var_class_required(TypeTableEntry *type_entry) { case TypeTableEntryIdVoid: case TypeTableEntryIdErrorSet: case TypeTableEntryIdFn: + case TypeTableEntryIdPromise: return VarClassRequiredAny; case TypeTableEntryIdNumLitFloat: case TypeTableEntryIdNumLitInt: @@ -10688,6 +10690,7 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: ir_add_error(ira, target, buf_sprintf("invalid export target '%s'", buf_ptr(&type_value->name))); break; @@ -10712,6 +10715,7 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: ir_add_error(ira, target, buf_sprintf("invalid export target type '%s'", buf_ptr(&target->value.type->name))); break; @@ -11481,6 +11485,7 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op case TypeTableEntryIdBlock: case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: + case TypeTableEntryIdPromise: { ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base); out_val->data.x_type = get_maybe_type(ira->codegen, type_entry); @@ -12710,6 +12715,7 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi case TypeTableEntryIdFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdOpaque: + case TypeTableEntryIdPromise: { ConstExprValue *out_val = ir_build_const_from(ira, &typeof_instruction->base); out_val->data.x_type = type_entry; @@ -12977,6 +12983,7 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira, case TypeTableEntryIdFn: case TypeTableEntryIdNamespace: case TypeTableEntryIdBoundFn: + case TypeTableEntryIdPromise: { type_ensure_zero_bits_known(ira->codegen, child_type); TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type, @@ -13085,6 +13092,7 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira, case TypeTableEntryIdFn: case TypeTableEntryIdNamespace: case TypeTableEntryIdBoundFn: + case TypeTableEntryIdPromise: { TypeTableEntry *result_type = get_array_type(ira->codegen, child_type, size); ConstExprValue *out_val = ir_build_const_from(ira, &array_type_instruction->base); @@ -13136,6 +13144,7 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira, case TypeTableEntryIdEnum: case TypeTableEntryIdUnion: case TypeTableEntryIdFn: + case TypeTableEntryIdPromise: { uint64_t size_in_bytes = type_size(ira->codegen, type_entry); ConstExprValue *out_val = ir_build_const_from(ira, &size_of_instruction->base); @@ -13465,6 +13474,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira, case TypeTableEntryIdNumLitFloat: case TypeTableEntryIdNumLitInt: case TypeTableEntryIdPointer: + case TypeTableEntryIdPromise: case TypeTableEntryIdFn: case TypeTableEntryIdNamespace: case TypeTableEntryIdErrorSet: @@ -14053,6 +14063,7 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_ case TypeTableEntryIdMetaType: case TypeTableEntryIdUnreachable: case TypeTableEntryIdPointer: + case TypeTableEntryIdPromise: case TypeTableEntryIdArray: case TypeTableEntryIdStruct: case TypeTableEntryIdNumLitFloat: @@ -15313,6 +15324,7 @@ static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruc case TypeTableEntryIdInt: case TypeTableEntryIdFloat: case TypeTableEntryIdPointer: + case TypeTableEntryIdPromise: case TypeTableEntryIdArray: case TypeTableEntryIdStruct: case TypeTableEntryIdMaybe: @@ -16008,6 +16020,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue case TypeTableEntryIdNumLitInt: case TypeTableEntryIdUndefLit: case TypeTableEntryIdNullLit: + case TypeTableEntryIdPromise: zig_unreachable(); case TypeTableEntryIdVoid: return; @@ -16075,6 +16088,7 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue case TypeTableEntryIdNumLitInt: case TypeTableEntryIdUndefLit: case TypeTableEntryIdNullLit: + case TypeTableEntryIdPromise: zig_unreachable(); case TypeTableEntryIdVoid: return; -- cgit v1.2.3 From 236bbe1183575d7644f943b59096f2eb275ffa3a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 21 Feb 2018 00:52:20 -0500 Subject: implement IR analysis for async function calls See #727 --- doc/langref.html.in | 2 +- src/all_types.hpp | 7 ++ src/analyze.cpp | 36 ++++++-- src/analyze.hpp | 1 - src/codegen.cpp | 11 +++ src/ir.cpp | 243 ++++++++++++++++++++++++++++++++++++++++++---------- src/ir_print.cpp | 16 ++++ src/parser.cpp | 10 ++- 8 files changed, 272 insertions(+), 54 deletions(-) (limited to 'src/codegen.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 05e804d78a..9c33f9e607 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -5645,7 +5645,7 @@ UseDecl = "use" Expression ";" ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";" -FnProto = option("nakedcc" | "stdcallcc" | "extern" | "async") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr +FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr FnDef = option("inline" | "export") FnProto Block diff --git a/src/all_types.hpp b/src/all_types.hpp index dbd8e0aa11..c4792c7921 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -428,6 +428,7 @@ struct AstNodeFnProto { AstNode *section_expr; bool auto_err_set; + AstNode *async_allocator_type; }; struct AstNodeFnDef { @@ -935,6 +936,7 @@ struct FnTypeId { bool is_var_args; CallingConvention cc; uint32_t alignment; + TypeTableEntry *async_allocator_type; }; uint32_t fn_type_id_hash(FnTypeId*); @@ -1958,6 +1960,7 @@ enum IrInstructionId { IrInstructionIdErrorReturnTrace, IrInstructionIdErrorUnion, IrInstructionIdCancel, + IrInstructionIdGetImplicitAllocator, }; struct IrInstruction { @@ -2803,6 +2806,10 @@ struct IrInstructionCancel { IrInstruction *target; }; +struct IrInstructionGetImplicitAllocator { + IrInstruction base; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/analyze.cpp b/src/analyze.cpp index e28a2d20f0..d83f195a85 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -954,8 +954,13 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { // populate the name of the type buf_resize(&fn_type->name, 0); - const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc); - buf_appendf(&fn_type->name, "%sfn(", cc_str); + if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) { + buf_appendf(&fn_type->name, "async(%s) ", buf_ptr(&fn_type_id->async_allocator_type->name)); + } else { + const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc); + buf_appendf(&fn_type->name, "%s", cc_str); + } + buf_appendf(&fn_type->name, "fn("); for (size_t i = 0; i < fn_type_id->param_count; i += 1) { FnTypeParamInfo *param_info = &fn_type_id->param_info[i]; @@ -1126,7 +1131,16 @@ TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) { TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) { TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn); fn_type->is_copyable = false; - buf_init_from_str(&fn_type->name, "fn("); + buf_resize(&fn_type->name, 0); + if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) { + const char *async_allocator_type_str = (fn_type->data.fn.fn_type_id.async_allocator_type == nullptr) ? + "var" : buf_ptr(&fn_type_id->async_allocator_type->name); + buf_appendf(&fn_type->name, "async(%s) ", async_allocator_type_str); + } else { + const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc); + buf_appendf(&fn_type->name, "%s", cc_str); + } + buf_appendf(&fn_type->name, "fn("); size_t i = 0; for (; i < fn_type_id->next_param_index; i += 1) { const char *comma_str = (i == 0) ? "" : ","; @@ -1515,6 +1529,16 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c break; } + if (fn_type_id.cc == CallingConventionAsync) { + if (fn_proto->async_allocator_type == nullptr) { + return get_generic_fn_type(g, &fn_type_id); + } + fn_type_id.async_allocator_type = analyze_type_expr(g, child_scope, fn_proto->async_allocator_type); + if (type_is_invalid(fn_type_id.async_allocator_type)) { + return g->builtin_types.entry_invalid; + } + } + return get_fn_type(g, &fn_type_id); } @@ -3676,7 +3700,7 @@ AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index) { return nullptr; } -void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars) { +static void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars) { TypeTableEntry *fn_type = fn_table_entry->type_entry; assert(!fn_type->data.fn.is_generic); FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id; @@ -4242,6 +4266,7 @@ uint32_t fn_type_id_hash(FnTypeId *id) { result += ((uint32_t)(id->cc)) * (uint32_t)3349388391; result += id->is_var_args ? (uint32_t)1931444534 : 0; result += hash_ptr(id->return_type); + result += hash_ptr(id->async_allocator_type); result += id->alignment * 0xd3b3f3e2; for (size_t i = 0; i < id->param_count; i += 1) { FnTypeParamInfo *info = &id->param_info[i]; @@ -4256,7 +4281,8 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) { a->return_type != b->return_type || a->is_var_args != b->is_var_args || a->param_count != b->param_count || - a->alignment != b->alignment) + a->alignment != b->alignment || + a->async_allocator_type != b->async_allocator_type) { return false; } diff --git a/src/analyze.hpp b/src/analyze.hpp index 34339c3688..a4036c597c 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -93,7 +93,6 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue * void eval_min_max_value_int(CodeGen *g, TypeTableEntry *int_type, BigInt *bigint, bool is_max); void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val); -void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars); void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_type_node); ScopeBlock *create_block_scope(AstNode *node, Scope *parent); diff --git a/src/codegen.cpp b/src/codegen.cpp index 291db7017b..d52cc2f9e2 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2521,6 +2521,10 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id; + if (fn_type_id->cc == CallingConventionAsync) { + zig_panic("TODO codegen async function call"); + } + TypeTableEntry *src_return_type = fn_type_id->return_type; bool ret_has_bits = type_has_bits(src_return_type); bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type); @@ -3094,6 +3098,10 @@ static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrIns zig_panic("TODO ir_render_cancel"); } +static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) { + zig_panic("TODO ir_render_get_implicit_allocator"); +} + static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { switch (atomic_order) { case AtomicOrderUnordered: return LLVMAtomicOrderingUnordered; @@ -3752,6 +3760,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, case IrInstructionIdExport: case IrInstructionIdErrorUnion: zig_unreachable(); + case IrInstructionIdReturn: return ir_render_return(g, executable, (IrInstructionReturn *)instruction); case IrInstructionIdDeclVar: @@ -3870,6 +3879,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_error_return_trace(g, executable, (IrInstructionErrorReturnTrace *)instruction); case IrInstructionIdCancel: return ir_render_cancel(g, executable, (IrInstructionCancel *)instruction); + case IrInstructionIdGetImplicitAllocator: + return ir_render_get_implicit_allocator(g, executable, (IrInstructionGetImplicitAllocator *)instruction); } zig_unreachable(); } diff --git a/src/ir.cpp b/src/ir.cpp index 8d03a2276a..a82e168986 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -65,6 +65,7 @@ enum ConstCastResultId { ConstCastResultIdFnArgNoAlias, ConstCastResultIdType, ConstCastResultIdUnresolvedInferredErrSet, + ConstCastResultIdAsyncAllocatorType, }; struct ConstCastErrSetMismatch { @@ -92,6 +93,7 @@ struct ConstCastOnly { ConstCastOnly *error_union_payload; ConstCastOnly *error_union_error_set; ConstCastOnly *return_type; + ConstCastOnly *async_allocator_type; ConstCastArg fn_arg; ConstCastArgNoAlias arg_no_alias; } data; @@ -104,6 +106,8 @@ static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *ins static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *expected_type); static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr); static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg); +static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name, + IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type); ConstExprValue *const_ptr_pointee(CodeGen *g, ConstExprValue *const_val) { assert(const_val->type->id == TypeTableEntryIdPointer); @@ -641,6 +645,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCancel *) { return IrInstructionIdCancel; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionGetImplicitAllocator *) { + return IrInstructionIdGetImplicitAllocator; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -954,15 +962,6 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As return &instruction->base; } -static IrInstruction *ir_build_struct_field_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, - IrInstruction *struct_ptr, TypeStructField *type_struct_field) -{ - IrInstruction *new_instruction = ir_build_struct_field_ptr(irb, old_instruction->scope, - old_instruction->source_node, struct_ptr, type_struct_field); - ir_link_new_instruction(new_instruction, old_instruction); - return new_instruction; -} - static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *union_ptr, TypeUnionField *field) { @@ -2415,6 +2414,12 @@ static IrInstruction *ir_build_cancel(IrBuilder *irb, Scope *scope, AstNode *sou return &instruction->base; } +static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node) { + IrInstructionGetImplicitAllocator *instruction = ir_build_instruction(irb, scope, source_node); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -6740,6 +6745,12 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry return result; } + if (expected_type == ira->codegen->builtin_types.entry_promise && + actual_type->id == TypeTableEntryIdPromise) + { + return result; + } + // fn if (expected_type->id == TypeTableEntryIdFn && actual_type->id == TypeTableEntryIdFn) @@ -6771,6 +6782,16 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry return result; } } + if (!expected_type->data.fn.is_generic && expected_type->data.fn.fn_type_id.cc == CallingConventionAsync) { + ConstCastOnly child = types_match_const_cast_only(ira, actual_type->data.fn.fn_type_id.async_allocator_type, + expected_type->data.fn.fn_type_id.async_allocator_type, source_node); + if (child.id != ConstCastResultIdOk) { + result.id = ConstCastResultIdAsyncAllocatorType; + result.data.async_allocator_type = allocate_nonzero(1); + *result.data.async_allocator_type = child; + return result; + } + } if (expected_type->data.fn.fn_type_id.param_count != actual_type->data.fn.fn_type_id.param_count) { result.id = ConstCastResultIdFnArgCount; return result; @@ -10768,6 +10789,58 @@ static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira, return ira->codegen->builtin_types.entry_type; } +IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr, FnTableEntry *parent_fn_entry) { + FnTypeId *parent_fn_type = &parent_fn_entry->type_entry->data.fn.fn_type_id; + if (parent_fn_type->cc != CallingConventionAsync) { + ir_add_error(ira, source_instr, buf_sprintf("async function call from non-async caller requires allocator parameter")); + return ira->codegen->invalid_instruction; + } + + assert(parent_fn_type->async_allocator_type != nullptr); + IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node); + result->value.type = parent_fn_type->async_allocator_type; + return result; +} + +static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, FnTableEntry *fn_entry, TypeTableEntry *fn_type, + IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst) +{ + Buf *alloc_field_name = buf_create_from_str("allocFn"); + //Buf *free_field_name = buf_create_from_str("freeFn"); + assert(async_allocator_inst->value.type->id == TypeTableEntryIdPointer); + TypeTableEntry *container_type = async_allocator_inst->value.type->data.pointer.child_type; + IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, alloc_field_name, &call_instruction->base, + async_allocator_inst, container_type); + if (type_is_invalid(field_ptr_inst->value.type)) { + return ira->codegen->invalid_instruction; + } + TypeTableEntry *ptr_to_alloc_fn_type = field_ptr_inst->value.type; + assert(ptr_to_alloc_fn_type->id == TypeTableEntryIdPointer); + + TypeTableEntry *alloc_fn_type = ptr_to_alloc_fn_type->data.pointer.child_type; + if (alloc_fn_type->id != TypeTableEntryIdFn) { + ir_add_error(ira, &call_instruction->base, + buf_sprintf("expected allocation function, found '%s'", buf_ptr(&alloc_fn_type->name))); + return ira->codegen->invalid_instruction; + } + + TypeTableEntry *alloc_fn_return_type = alloc_fn_type->data.fn.fn_type_id.return_type; + if (alloc_fn_return_type->id != TypeTableEntryIdErrorUnion) { + ir_add_error(ira, fn_ref, + buf_sprintf("expected allocation function to return error union, but it returns '%s'", buf_ptr(&alloc_fn_return_type->name))); + return ira->codegen->invalid_instruction; + } + TypeTableEntry *alloc_fn_error_set_type = alloc_fn_return_type->data.error_union.err_set_type; + TypeTableEntry *return_type = fn_type->data.fn.fn_type_id.return_type; + TypeTableEntry *promise_type = get_promise_type(ira->codegen, return_type); + TypeTableEntry *async_return_type = get_error_union_type(ira->codegen, alloc_fn_error_set_type, promise_type); + + IrInstruction *result = ir_build_call(&ira->new_irb, call_instruction->base.scope, call_instruction->base.source_node, + fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst); + result->value.type = async_return_type; + return result; +} + static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node, IrInstruction *arg, Scope **exec_scope, size_t *next_proto_i) { @@ -10989,6 +11062,13 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } return ira->codegen->builtin_types.entry_invalid; } + if (fn_type_id->cc != CallingConventionAsync && call_instruction->is_async) { + ErrorMsg *msg = ir_add_error(ira, fn_ref, buf_sprintf("cannot use async keyword to call non-async function")); + if (fn_proto_node) { + add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("declared here")); + } + return ira->codegen->builtin_types.entry_invalid; + } if (fn_type_id->is_var_args) { @@ -11115,6 +11195,11 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal buf_sprintf("calling a generic function requires compile-time known function value")); return ira->codegen->builtin_types.entry_invalid; } + if (call_instruction->is_async && fn_type_id->is_var_args) { + ir_add_error(ira, call_instruction->fn_ref, + buf_sprintf("compiler bug: TODO: implement var args async functions. https://github.com/zig-lang/zig/issues/557")); + return ira->codegen->builtin_types.entry_invalid; + } // Count the arguments of the function type id we are creating size_t new_fn_arg_count = first_arg_1_or_0; @@ -11263,6 +11348,35 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true, FnInlineAuto); } } + IrInstruction *async_allocator_inst = nullptr; + if (call_instruction->is_async) { + AstNode *async_allocator_type_node = fn_proto_node->data.fn_proto.async_allocator_type; + if (async_allocator_type_node != nullptr) { + TypeTableEntry *async_allocator_type = analyze_type_expr(ira->codegen, impl_fn->child_scope, async_allocator_type_node); + if (type_is_invalid(async_allocator_type)) + return ira->codegen->builtin_types.entry_invalid; + inst_fn_type_id.async_allocator_type = async_allocator_type; + } + IrInstruction *uncasted_async_allocator_inst; + if (call_instruction->async_allocator == nullptr) { + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, parent_fn_entry); + if (type_is_invalid(uncasted_async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } else { + uncasted_async_allocator_inst = call_instruction->async_allocator->other; + if (type_is_invalid(uncasted_async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } + if (inst_fn_type_id.async_allocator_type == nullptr) { + IrInstruction *casted_inst = ir_implicit_byval_const_ref_cast(ira, uncasted_async_allocator_inst); + if (type_is_invalid(casted_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + inst_fn_type_id.async_allocator_type = casted_inst->value.type; + } + async_allocator_inst = ir_implicit_cast(ira, uncasted_async_allocator_inst, inst_fn_type_id.async_allocator_type); + if (type_is_invalid(async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } auto existing_entry = ira->codegen->generic_table.put_unique(generic_id, impl_fn); if (existing_entry) { @@ -11282,17 +11396,24 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal ira->codegen->fn_defs.append(impl_fn); } - size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count; - IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, - impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline, false, nullptr); - TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type; - ir_add_alloca(ira, new_call_instruction, return_type); - if (return_type->id == TypeTableEntryIdErrorSet || return_type->id == TypeTableEntryIdErrorUnion) { parent_fn_entry->calls_errorable_function = true; } + size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count; + if (call_instruction->is_async) { + IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry, fn_ref, casted_args, impl_param_count, async_allocator_inst); + ir_link_new_instruction(result, &call_instruction->base); + return ir_finish_anal(ira, result->value.type); + } + + IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, + impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline, + call_instruction->is_async, async_allocator_inst); + + ir_add_alloca(ira, new_call_instruction, return_type); + return ir_finish_anal(ira, return_type); } @@ -11350,14 +11471,31 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal assert(next_arg_index == call_param_count); - if (call_instruction->is_async) { - zig_panic("TODO handle async fn call"); - } - TypeTableEntry *return_type = fn_type_id->return_type; if (type_is_invalid(return_type)) return ira->codegen->builtin_types.entry_invalid; + if (call_instruction->is_async) { + IrInstruction *uncasted_async_allocator_inst; + if (call_instruction->async_allocator == nullptr) { + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, parent_fn_entry); + if (type_is_invalid(uncasted_async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } else { + uncasted_async_allocator_inst = call_instruction->async_allocator->other; + if (type_is_invalid(uncasted_async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } + IrInstruction *async_allocator_inst = ir_implicit_cast(ira, uncasted_async_allocator_inst, fn_type_id->async_allocator_type); + if (type_is_invalid(async_allocator_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref, casted_args, call_param_count, async_allocator_inst); + ir_link_new_instruction(result, &call_instruction->base); + return ir_finish_anal(ira, result->value.type); + } + + IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr); @@ -12054,8 +12192,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc return return_type; } -static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira, - TypeTableEntry *bare_struct_type, Buf *field_name, IrInstructionFieldPtr *field_ptr_instruction, +static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira, + TypeTableEntry *bare_struct_type, Buf *field_name, IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type) { if (!is_slice(bare_struct_type)) { @@ -12063,17 +12201,17 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira, auto entry = container_scope->decl_table.maybe_get(field_name); Tld *tld = entry ? entry->value : nullptr; if (tld && tld->id == TldIdFn) { - resolve_top_level_decl(ira->codegen, tld, false, field_ptr_instruction->base.source_node); + resolve_top_level_decl(ira->codegen, tld, false, source_instr->source_node); if (tld->resolution == TldResolutionInvalid) - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; TldFn *tld_fn = (TldFn *)tld; FnTableEntry *fn_entry = tld_fn->fn_entry; if (type_is_invalid(fn_entry->type_entry)) - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; - IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope, - field_ptr_instruction->base.source_node, fn_entry, container_ptr); - return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value, true, false); + IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, source_instr->scope, + source_instr->source_node, fn_entry, container_ptr); + return ir_get_ref(ira, source_instr, bound_fn_value, true, false); } } const char *prefix_name; @@ -12088,19 +12226,19 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira, } else { prefix_name = ""; } - ir_add_error_node(ira, field_ptr_instruction->base.source_node, + ir_add_error_node(ira, source_instr->source_node, buf_sprintf("no member named '%s' in %s'%s'", buf_ptr(field_name), prefix_name, buf_ptr(&bare_struct_type->name))); - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; } -static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name, - IrInstructionFieldPtr *field_ptr_instruction, IrInstruction *container_ptr, TypeTableEntry *container_type) +static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name, + IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type) { TypeTableEntry *bare_type = container_ref_type(container_type); ensure_complete_type(ira->codegen, bare_type); if (type_is_invalid(bare_type)) - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; assert(container_ptr->value.type->id == TypeTableEntryIdPointer); bool is_const = container_ptr->value.type->data.pointer.is_const; @@ -12117,46 +12255,51 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field if (instr_is_comptime(container_ptr)) { ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad); if (!ptr_val) - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { ConstExprValue *struct_val = const_ptr_pointee(ira->codegen, ptr_val); if (type_is_invalid(struct_val->type)) - return ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index]; TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type, is_const, is_volatile, align_bytes, (uint32_t)(ptr_bit_offset + field->packed_bits_offset), (uint32_t)unaligned_bit_count_for_result_type); - ConstExprValue *const_val = ir_build_const_from(ira, &field_ptr_instruction->base); + IrInstruction *result = ir_get_const(ira, source_instr); + ConstExprValue *const_val = &result->value; const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct; const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut; const_val->data.x_ptr.data.base_struct.struct_val = struct_val; const_val->data.x_ptr.data.base_struct.field_index = field->src_index; - return ptr_type; + const_val->type = ptr_type; + return result; } } - ir_build_struct_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field); - return get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile, + IrInstruction *result = ir_build_struct_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, + container_ptr, field); + result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile, align_bytes, (uint32_t)(ptr_bit_offset + field->packed_bits_offset), (uint32_t)unaligned_bit_count_for_result_type); + return result; } else { return ir_analyze_container_member_access_inner(ira, bare_type, field_name, - field_ptr_instruction, container_ptr, container_type); + source_instr, container_ptr, container_type); } } else if (bare_type->id == TypeTableEntryIdEnum) { return ir_analyze_container_member_access_inner(ira, bare_type, field_name, - field_ptr_instruction, container_ptr, container_type); + source_instr, container_ptr, container_type); } else if (bare_type->id == TypeTableEntryIdUnion) { TypeUnionField *field = find_union_type_field(bare_type, field_name); if (field) { - ir_build_union_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field); - return get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile, + IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field); + result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile, get_abi_alignment(ira->codegen, field->type_entry), 0, 0); + return result; } else { return ir_analyze_container_member_access_inner(ira, bare_type, field_name, - field_ptr_instruction, container_ptr, container_type); + source_instr, container_ptr, container_type); } } else { zig_unreachable(); @@ -12266,9 +12409,13 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru if (container_type->id == TypeTableEntryIdPointer) { TypeTableEntry *bare_type = container_ref_type(container_type); IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr); - return ir_analyze_container_field_ptr(ira, field_name, field_ptr_instruction, container_child, bare_type); + IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_child, bare_type); + ir_link_new_instruction(result, &field_ptr_instruction->base); + return result->value.type; } else { - return ir_analyze_container_field_ptr(ira, field_name, field_ptr_instruction, container_ptr, container_type); + IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_ptr, container_type); + ir_link_new_instruction(result, &field_ptr_instruction->base); + return result->value.type; } } else if (container_type->id == TypeTableEntryIdArray) { if (buf_eql_str(field_name, "len")) { @@ -16539,7 +16686,8 @@ static TypeTableEntry *ir_analyze_instruction_cancel(IrAnalyze *ira, IrInstructi return ira->codegen->builtin_types.entry_invalid; IrInstruction *result = ir_build_cancel(&ira->new_irb, instruction->base.scope, instruction->base.source_node, casted_target); - result->value.type = casted_target->value.type; + result->value.type = ira->codegen->builtin_types.entry_void; + result->value.special = ConstValSpecialStatic; ir_link_new_instruction(result, &instruction->base); return result->value.type; } @@ -16559,6 +16707,7 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi case IrInstructionIdErrWrapCode: case IrInstructionIdErrWrapPayload: case IrInstructionIdCast: + case IrInstructionIdGetImplicitAllocator: zig_unreachable(); case IrInstructionIdReturn: return ir_analyze_instruction_return(ira, (IrInstructionReturn *)instruction); @@ -16936,7 +17085,9 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdTagType: case IrInstructionIdErrorReturnTrace: case IrInstructionIdErrorUnion: + case IrInstructionIdGetImplicitAllocator: return false; + case IrInstructionIdAsm: { IrInstructionAsm *asm_instruction = (IrInstructionAsm *)instruction; diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 5c0c3bab0d..32eb0d0a9f 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -198,6 +198,15 @@ static void ir_print_cast(IrPrint *irp, IrInstructionCast *cast_instruction) { } static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) { + if (call_instruction->is_async) { + fprintf(irp->f, "async"); + if (call_instruction->async_allocator != nullptr) { + fprintf(irp->f, "("); + ir_print_other_instruction(irp, call_instruction->async_allocator); + fprintf(irp->f, ")"); + } + fprintf(irp->f, " "); + } if (call_instruction->fn_entry) { fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name)); } else { @@ -1015,6 +1024,10 @@ static void ir_print_cancel(IrPrint *irp, IrInstructionCancel *instruction) { ir_print_other_instruction(irp, instruction->target); } +static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplicitAllocator *instruction) { + fprintf(irp->f, "@getImplicitAllocator()"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1338,6 +1351,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCancel: ir_print_cancel(irp, (IrInstructionCancel *)instruction); break; + case IrInstructionIdGetImplicitAllocator: + ir_print_get_implicit_allocator(irp, (IrInstructionGetImplicitAllocator *)instruction); + break; } fprintf(irp->f, "\n"); } diff --git a/src/parser.cpp b/src/parser.cpp index ee7141a910..e64c569e2f 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2333,7 +2333,7 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand } /* -FnProto = option("nakedcc" | "stdcallcc" | "extern" | "async") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr +FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr */ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool mandatory, VisibMod visib_mod) { Token *first_token = &pc->tokens->at(*token_index); @@ -2341,12 +2341,18 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m CallingConvention cc; bool is_extern = false; + AstNode *async_allocator_type_node = nullptr; if (first_token->id == TokenIdKeywordNakedCC) { *token_index += 1; fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); cc = CallingConventionNaked; } else if (first_token->id == TokenIdKeywordAsync) { *token_index += 1; + Token *next_token = &pc->tokens->at(*token_index); + if (next_token->id == TokenIdLParen) { + async_allocator_type_node = ast_parse_type_expr(pc, token_index, true); + ast_eat_token(pc, token_index, TokenIdRParen); + } fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn); cc = CallingConventionAsync; } else if (first_token->id == TokenIdKeywordStdcallCC) { @@ -2383,6 +2389,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m node->data.fn_proto.visib_mod = visib_mod; node->data.fn_proto.cc = cc; node->data.fn_proto.is_extern = is_extern; + node->data.fn_proto.async_allocator_type = async_allocator_type_node; Token *fn_name = &pc->tokens->at(*token_index); @@ -2798,6 +2805,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont visit_node_list(&node->data.fn_proto.params, visit, context); visit_field(&node->data.fn_proto.align_expr, visit, context); visit_field(&node->data.fn_proto.section_expr, visit, context); + visit_field(&node->data.fn_proto.async_allocator_type, visit, context); break; case NodeTypeFnDef: visit_field(&node->data.fn_def.fn_proto, visit, context); -- cgit v1.2.3 From b261da067248c4003b0008f6aaeefde43290a061 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 21 Feb 2018 23:28:35 -0500 Subject: add coroutine startup IR to async functions See #727 --- src/all_types.hpp | 26 ++++++++++ src/codegen.cpp | 26 ++++++++++ src/ir.cpp | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/ir_print.cpp | 34 ++++++++++++++ 4 files changed, 223 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index c4792c7921..b2d073f698 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -56,6 +56,7 @@ struct IrExecutable { IrAnalyze *analysis; Scope *begin_scope; ZigList tld_list; + IrInstruction *coro_handle; }; enum OutType { @@ -1961,6 +1962,10 @@ enum IrInstructionId { IrInstructionIdErrorUnion, IrInstructionIdCancel, IrInstructionIdGetImplicitAllocator, + IrInstructionIdCoroId, + IrInstructionIdCoroAlloc, + IrInstructionIdCoroSize, + IrInstructionIdCoroBegin, }; struct IrInstruction { @@ -2810,6 +2815,27 @@ struct IrInstructionGetImplicitAllocator { IrInstruction base; }; +struct IrInstructionCoroId { + IrInstruction base; +}; + +struct IrInstructionCoroAlloc { + IrInstruction base; + + IrInstruction *coro_id; +}; + +struct IrInstructionCoroSize { + IrInstruction base; +}; + +struct IrInstructionCoroBegin { + IrInstruction base; + + IrInstruction *coro_id; + IrInstruction *coro_mem_ptr; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/codegen.cpp b/src/codegen.cpp index d52cc2f9e2..e304d3cc24 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3696,6 +3696,23 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst return nullptr; } +static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) { + zig_panic("TODO ir_render_coro_id"); +} + +static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) { + zig_panic("TODO ir_render_coro_alloc"); +} + +static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) { + zig_panic("TODO ir_render_coro_size"); +} + +static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) { + zig_panic("TODO ir_render_coro_begin"); +} + + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -3881,12 +3898,21 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_cancel(g, executable, (IrInstructionCancel *)instruction); case IrInstructionIdGetImplicitAllocator: return ir_render_get_implicit_allocator(g, executable, (IrInstructionGetImplicitAllocator *)instruction); + case IrInstructionIdCoroId: + return ir_render_coro_id(g, executable, (IrInstructionCoroId *)instruction); + case IrInstructionIdCoroAlloc: + return ir_render_coro_alloc(g, executable, (IrInstructionCoroAlloc *)instruction); + case IrInstructionIdCoroSize: + return ir_render_coro_size(g, executable, (IrInstructionCoroSize *)instruction); + case IrInstructionIdCoroBegin: + return ir_render_coro_begin(g, executable, (IrInstructionCoroBegin *)instruction); } zig_unreachable(); } static void ir_render(CodeGen *g, FnTableEntry *fn_entry) { assert(fn_entry); + IrExecutable *executable = &fn_entry->analyzed_executable; assert(executable->basic_block_list.length > 0); for (size_t block_i = 0; block_i < executable->basic_block_list.length; block_i += 1) { diff --git a/src/ir.cpp b/src/ir.cpp index a82e168986..3c0d9d652f 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -45,6 +45,9 @@ static LVal make_lval_addr(bool is_const, bool is_volatile) { return { true, is_const, is_volatile }; } +static const char * ASYNC_ALLOC_FIELD_NAME = "allocFn"; +//static const char * ASYNC_FREE_FIELD_NAME = "freeFn"; + enum ConstCastResultId { ConstCastResultIdOk, ConstCastResultIdErrSet, @@ -649,6 +652,22 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionGetImplicitAlloc return IrInstructionIdGetImplicitAllocator; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroId *) { + return IrInstructionIdCoroId; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAlloc *) { + return IrInstructionIdCoroAlloc; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSize *) { + return IrInstructionIdCoroSize; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroBegin *) { + return IrInstructionIdCoroBegin; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -2420,6 +2439,38 @@ static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *sco return &instruction->base; } +static IrInstruction *ir_build_coro_id(IrBuilder *irb, Scope *scope, AstNode *source_node) { + IrInstructionCoroId *instruction = ir_build_instruction(irb, scope, source_node); + + return &instruction->base; +} + +static IrInstruction *ir_build_coro_alloc(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *coro_id) { + IrInstructionCoroAlloc *instruction = ir_build_instruction(irb, scope, source_node); + instruction->coro_id = coro_id; + + ir_ref_instruction(coro_id, irb->current_basic_block); + + return &instruction->base; +} + +static IrInstruction *ir_build_coro_size(IrBuilder *irb, Scope *scope, AstNode *source_node) { + IrInstructionCoroSize *instruction = ir_build_instruction(irb, scope, source_node); + + return &instruction->base; +} + +static IrInstruction *ir_build_coro_begin(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *coro_id, IrInstruction *coro_mem_ptr) { + IrInstructionCoroBegin *instruction = ir_build_instruction(irb, scope, source_node); + instruction->coro_id = coro_id; + instruction->coro_mem_ptr = coro_mem_ptr; + + ir_ref_instruction(coro_id, irb->current_basic_block); + ir_ref_instruction(coro_mem_ptr, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -5782,6 +5833,63 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec // Entry block gets a reference because we enter it to begin. ir_ref_bb(irb->current_basic_block); + FnTableEntry *fn_entry = exec_fn_entry(irb->exec); + bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + if (is_async) { + IrInstruction *is_comptime_false = ir_build_const_bool(irb, scope, node, false); + IrInstruction *coro_id = ir_build_coro_id(irb, scope, node); + IrInstruction *need_dyn_alloc = ir_build_coro_alloc(irb, scope, node, coro_id); + IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); + IrInstruction *u8_ptr_type = ir_build_const_type(irb, scope, node, + get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); + IrInstruction *null_ptr = ir_build_int_to_ptr(irb, scope, node, u8_ptr_type, zero); + + IrBasicBlock *dyn_alloc_block = ir_create_basic_block(irb, scope, "DynAlloc"); + IrBasicBlock *coro_begin_block = ir_create_basic_block(irb, scope, "CoroBegin"); + ir_build_cond_br(irb, scope, node, need_dyn_alloc, dyn_alloc_block, coro_begin_block, is_comptime_false); + + ir_set_cursor_at_end_and_append_block(irb, dyn_alloc_block); + IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); + IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); + Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); + IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, alloc_field_name); + IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); + IrInstruction *implicit_allocator = ir_build_load_ptr(irb, scope, node, implicit_allocator_ptr); + IrInstruction *alignment = ir_build_const_usize(irb, scope, node, irb->codegen->pointer_size_bytes * 2); + size_t arg_count = 3; + IrInstruction **args = allocate(arg_count); + args[0] = implicit_allocator; // self + args[1] = coro_size; // byte_count + args[2] = alignment; // alignment + IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, FnInlineAuto, false, nullptr); + IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); + IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result_ptr); + IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); + IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, scope, "AllocOk"); + ir_build_cond_br(irb, scope, node, alloc_result_is_err, alloc_err_block, alloc_ok_block, is_comptime_false); + + ir_set_cursor_at_end_and_append_block(irb, alloc_err_block); + IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, alloc_result_ptr); + ir_build_return(irb, scope, node, err_val); + + ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); + IrInstruction *unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false); + Buf *ptr_field_name = buf_create_from_str("ptr"); + IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, unwrapped_mem_ptr, ptr_field_name); + IrInstruction *coro_mem_ptr = ir_build_load_ptr(irb, scope, node, coro_mem_ptr_field); + ir_build_br(irb, scope, node, coro_begin_block, is_comptime_false); + + ir_set_cursor_at_end_and_append_block(irb, coro_begin_block); + IrBasicBlock **incoming_blocks = allocate(2); + IrInstruction **incoming_values = allocate(2); + incoming_blocks[0] = entry_block; + incoming_values[0] = null_ptr; + incoming_blocks[1] = dyn_alloc_block; + incoming_values[1] = coro_mem_ptr; + IrInstruction *coro_mem = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values); + irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem); + } + IrInstruction *result = ir_gen_node_extra(irb, node, scope, LVAL_NONE); assert(result); if (irb->exec->invalid) @@ -10805,7 +10913,7 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, FnTableEntry *fn_entry, TypeTableEntry *fn_type, IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst) { - Buf *alloc_field_name = buf_create_from_str("allocFn"); + Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); //Buf *free_field_name = buf_create_from_str("freeFn"); assert(async_allocator_inst->value.type->id == TypeTableEntryIdPointer); TypeTableEntry *container_type = async_allocator_inst->value.type->data.pointer.child_type; @@ -16692,6 +16800,22 @@ static TypeTableEntry *ir_analyze_instruction_cancel(IrAnalyze *ira, IrInstructi return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_coro_id(IrAnalyze *ira, IrInstructionCoroId *instruction) { + zig_panic("TODO ir_analyze_instruction_coro_id"); +} + +static TypeTableEntry *ir_analyze_instruction_coro_alloc(IrAnalyze *ira, IrInstructionCoroAlloc *instruction) { + zig_panic("TODO ir_analyze_instruction_coro_alloc"); +} + +static TypeTableEntry *ir_analyze_instruction_coro_size(IrAnalyze *ira, IrInstructionCoroSize *instruction) { + zig_panic("TODO ir_analyze_instruction_coro_size"); +} + +static TypeTableEntry *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstructionCoroBegin *instruction) { + zig_panic("TODO ir_analyze_instruction_coro_begin"); +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -16897,6 +17021,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_error_union(ira, (IrInstructionErrorUnion *)instruction); case IrInstructionIdCancel: return ir_analyze_instruction_cancel(ira, (IrInstructionCancel *)instruction); + case IrInstructionIdCoroId: + return ir_analyze_instruction_coro_id(ira, (IrInstructionCoroId *)instruction); + case IrInstructionIdCoroAlloc: + return ir_analyze_instruction_coro_alloc(ira, (IrInstructionCoroAlloc *)instruction); + case IrInstructionIdCoroSize: + return ir_analyze_instruction_coro_size(ira, (IrInstructionCoroSize *)instruction); + case IrInstructionIdCoroBegin: + return ir_analyze_instruction_coro_begin(ira, (IrInstructionCoroBegin *)instruction); } zig_unreachable(); } @@ -17011,6 +17143,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdSetAlignStack: case IrInstructionIdExport: case IrInstructionIdCancel: + case IrInstructionIdCoroId: + case IrInstructionIdCoroBegin: return true; case IrInstructionIdPhi: @@ -17086,6 +17220,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdErrorReturnTrace: case IrInstructionIdErrorUnion: case IrInstructionIdGetImplicitAllocator: + case IrInstructionIdCoroAlloc: + case IrInstructionIdCoroSize: return false; case IrInstructionIdAsm: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 32eb0d0a9f..2fcdb75a18 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1028,6 +1028,28 @@ static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplic fprintf(irp->f, "@getImplicitAllocator()"); } +static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) { + fprintf(irp->f, "@coroId()"); +} + +static void ir_print_coro_alloc(IrPrint *irp, IrInstructionCoroAlloc *instruction) { + fprintf(irp->f, "@coroAlloc("); + ir_print_other_instruction(irp, instruction->coro_id); + fprintf(irp->f, ")"); +} + +static void ir_print_coro_size(IrPrint *irp, IrInstructionCoroSize *instruction) { + fprintf(irp->f, "@coroSize()"); +} + +static void ir_print_coro_begin(IrPrint *irp, IrInstructionCoroBegin *instruction) { + fprintf(irp->f, "@coroBegin("); + ir_print_other_instruction(irp, instruction->coro_id); + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->coro_mem_ptr); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1354,6 +1376,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdGetImplicitAllocator: ir_print_get_implicit_allocator(irp, (IrInstructionGetImplicitAllocator *)instruction); break; + case IrInstructionIdCoroId: + ir_print_coro_id(irp, (IrInstructionCoroId *)instruction); + break; + case IrInstructionIdCoroAlloc: + ir_print_coro_alloc(irp, (IrInstructionCoroAlloc *)instruction); + break; + case IrInstructionIdCoroSize: + ir_print_coro_size(irp, (IrInstructionCoroSize *)instruction); + break; + case IrInstructionIdCoroBegin: + ir_print_coro_begin(irp, (IrInstructionCoroBegin *)instruction); + break; } fprintf(irp->f, "\n"); } -- cgit v1.2.3 From ca1b77b2d51408589659f652b1b1dbe2a25e149f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 22 Feb 2018 11:54:27 -0500 Subject: IR analysis for coro.begin See #727 --- src/all_types.hpp | 8 +++++++ src/codegen.cpp | 6 +++++ src/ir.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/ir_print.cpp | 9 ++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index b2d073f698..9575f6cdad 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1490,6 +1490,7 @@ struct CodeGen { TypeTableEntry *entry_u8; TypeTableEntry *entry_u16; TypeTableEntry *entry_u32; + TypeTableEntry *entry_u29; TypeTableEntry *entry_u64; TypeTableEntry *entry_u128; TypeTableEntry *entry_i8; @@ -1966,6 +1967,7 @@ enum IrInstructionId { IrInstructionIdCoroAlloc, IrInstructionIdCoroSize, IrInstructionIdCoroBegin, + IrInstructionIdCoroAllocFail, }; struct IrInstruction { @@ -2836,6 +2838,12 @@ struct IrInstructionCoroBegin { IrInstruction *coro_mem_ptr; }; +struct IrInstructionCoroAllocFail { + IrInstruction base; + + IrInstruction *err_val; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/codegen.cpp b/src/codegen.cpp index e304d3cc24..cb3a98bb88 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3712,6 +3712,9 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I zig_panic("TODO ir_render_coro_begin"); } +static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable, IrInstructionCoroAllocFail *instruction) { + zig_panic("TODO ir_render_coro_alloc_fail"); +} static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; @@ -3906,6 +3909,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_size(g, executable, (IrInstructionCoroSize *)instruction); case IrInstructionIdCoroBegin: return ir_render_coro_begin(g, executable, (IrInstructionCoroBegin *)instruction); + case IrInstructionIdCoroAllocFail: + return ir_render_coro_alloc_fail(g, executable, (IrInstructionCoroAllocFail *)instruction); } zig_unreachable(); } @@ -5282,6 +5287,7 @@ static void define_builtin_types(CodeGen *g) { g->builtin_types.entry_u8 = get_int_type(g, false, 8); g->builtin_types.entry_u16 = get_int_type(g, false, 16); + g->builtin_types.entry_u29 = get_int_type(g, false, 29); g->builtin_types.entry_u32 = get_int_type(g, false, 32); g->builtin_types.entry_u64 = get_int_type(g, false, 64); g->builtin_types.entry_u128 = get_int_type(g, false, 128); diff --git a/src/ir.cpp b/src/ir.cpp index 7235ad72f2..68d31a7712 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -668,6 +668,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroBegin *) { return IrInstructionIdCoroBegin; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocFail *) { + return IrInstructionIdCoroAllocFail; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -810,6 +814,14 @@ static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode return &const_instruction->base; } +static IrInstruction *ir_build_const_u29(IrBuilder *irb, Scope *scope, AstNode *source_node, uint32_t value) { + IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); + const_instruction->base.value.type = irb->codegen->builtin_types.entry_u29; + const_instruction->base.value.special = ConstValSpecialStatic; + bigint_init_unsigned(&const_instruction->base.value.data.x_bigint, value); + return &const_instruction->base; +} + static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node, TypeTableEntry *type_entry) { @@ -2471,6 +2483,17 @@ static IrInstruction *ir_build_coro_begin(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } +static IrInstruction *ir_build_coro_alloc_fail(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *err_val) { + IrInstructionCoroAllocFail *instruction = ir_build_instruction(irb, scope, source_node); + instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable; + instruction->base.value.special = ConstValSpecialStatic; + instruction->err_val = err_val; + + ir_ref_instruction(err_val, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -5854,23 +5877,22 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, alloc_field_name); IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); - IrInstruction *implicit_allocator = ir_build_load_ptr(irb, scope, node, implicit_allocator_ptr); - IrInstruction *alignment = ir_build_const_usize(irb, scope, node, irb->codegen->pointer_size_bytes * 2); + IrInstruction *alignment = ir_build_const_u29(irb, scope, node, irb->codegen->pointer_size_bytes * 2); size_t arg_count = 3; IrInstruction **args = allocate(arg_count); - args[0] = implicit_allocator; // self + args[0] = implicit_allocator_ptr; // self args[1] = coro_size; // byte_count args[2] = alignment; // alignment IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, FnInlineAuto, false, nullptr); IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); - IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result_ptr); + IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result); IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, scope, "AllocOk"); ir_build_cond_br(irb, scope, node, alloc_result_is_err, alloc_err_block, alloc_ok_block, is_comptime_false); ir_set_cursor_at_end_and_append_block(irb, alloc_err_block); IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, alloc_result_ptr); - ir_build_return(irb, scope, node, err_val); + ir_build_coro_alloc_fail(irb, scope, node, err_val); ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); IrInstruction *unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false); @@ -16826,18 +16848,47 @@ static TypeTableEntry *ir_analyze_instruction_coro_alloc(IrAnalyze *ira, IrInstr } static TypeTableEntry *ir_analyze_instruction_coro_size(IrAnalyze *ira, IrInstructionCoroSize *instruction) { - zig_panic("TODO ir_analyze_instruction_coro_size"); + IrInstruction *result = ir_build_coro_size(&ira->new_irb, instruction->base.scope, instruction->base.source_node); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_usize; + return result->value.type; } static TypeTableEntry *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstructionCoroBegin *instruction) { - zig_panic("TODO ir_analyze_instruction_coro_begin"); + IrInstruction *coro_id = instruction->coro_id->other; + if (type_is_invalid(coro_id->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *coro_mem_ptr = instruction->coro_mem_ptr->other; + if (type_is_invalid(coro_mem_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec); + assert(fn_entry != nullptr); + IrInstruction *result = ir_build_coro_begin(&ira->new_irb, instruction->base.scope, instruction->base.source_node, + coro_id, coro_mem_ptr); + ir_link_new_instruction(result, &instruction->base); + result->value.type = get_promise_type(ira->codegen, fn_entry->type_entry->data.fn.fn_type_id.return_type); + return result->value.type; } static TypeTableEntry *ir_analyze_instruction_get_implicit_allocator(IrAnalyze *ira, IrInstructionGetImplicitAllocator *instruction) { IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base); + ir_link_new_instruction(result, &instruction->base); return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_coro_alloc_fail(IrAnalyze *ira, IrInstructionCoroAllocFail *instruction) { + IrInstruction *err_val = instruction->err_val->other; + if (type_is_invalid(err_val->value.type)) + return ir_unreach_error(ira); + + IrInstruction *result = ir_build_coro_alloc_fail(&ira->new_irb, instruction->base.scope, instruction->base.source_node, err_val); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_unreachable; + return ir_finish_anal(ira, result->value.type); +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -17052,6 +17103,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_begin(ira, (IrInstructionCoroBegin *)instruction); case IrInstructionIdGetImplicitAllocator: return ir_analyze_instruction_get_implicit_allocator(ira, (IrInstructionGetImplicitAllocator *)instruction); + case IrInstructionIdCoroAllocFail: + return ir_analyze_instruction_coro_alloc_fail(ira, (IrInstructionCoroAllocFail *)instruction); } zig_unreachable(); } @@ -17168,6 +17221,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCancel: case IrInstructionIdCoroId: case IrInstructionIdCoroBegin: + case IrInstructionIdCoroAllocFail: return true; case IrInstructionIdPhi: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 186e2711a4..bb49273d5c 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1052,6 +1052,12 @@ static void ir_print_coro_begin(IrPrint *irp, IrInstructionCoroBegin *instructio fprintf(irp->f, ")"); } +static void ir_print_coro_alloc_fail(IrPrint *irp, IrInstructionCoroAllocFail *instruction) { + fprintf(irp->f, "@coroAllocFail("); + ir_print_other_instruction(irp, instruction->err_val); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1390,6 +1396,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCoroBegin: ir_print_coro_begin(irp, (IrInstructionCoroBegin *)instruction); break; + case IrInstructionIdCoroAllocFail: + ir_print_coro_alloc_fail(irp, (IrInstructionCoroAllocFail *)instruction); + break; } fprintf(irp->f, "\n"); } -- cgit v1.2.3 From 99985ad6fc0ff4ff09c0284c40023a9c826f8108 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 23 Feb 2018 03:03:06 -0500 Subject: implement Zig IR for async functions See #727 --- src/all_types.hpp | 36 +++++ src/analyze.cpp | 10 +- src/analyze.hpp | 1 + src/codegen.cpp | 46 +++++-- src/ir.cpp | 382 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/ir_print.cpp | 46 ++++++- 6 files changed, 467 insertions(+), 54 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 9575f6cdad..002a2d4a4c 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -56,7 +56,13 @@ struct IrExecutable { IrAnalyze *analysis; Scope *begin_scope; ZigList tld_list; + IrInstruction *coro_handle; + IrInstruction *coro_awaiter_field_ptr; + IrInstruction *coro_result_ptr_field_ptr; + IrInstruction *implicit_allocator_ptr; + IrBasicBlock *coro_early_final; + IrBasicBlock *coro_normal_final; }; enum OutType { @@ -1968,6 +1974,10 @@ enum IrInstructionId { IrInstructionIdCoroSize, IrInstructionIdCoroBegin, IrInstructionIdCoroAllocFail, + IrInstructionIdCoroSuspend, + IrInstructionIdCoroEnd, + IrInstructionIdCoroFree, + IrInstructionIdCoroResume, }; struct IrInstruction { @@ -2819,6 +2829,8 @@ struct IrInstructionGetImplicitAllocator { struct IrInstructionCoroId { IrInstruction base; + + IrInstruction *promise_ptr; }; struct IrInstructionCoroAlloc { @@ -2844,6 +2856,30 @@ struct IrInstructionCoroAllocFail { IrInstruction *err_val; }; +struct IrInstructionCoroSuspend { + IrInstruction base; + + IrInstruction *save_point; + IrInstruction *is_final; +}; + +struct IrInstructionCoroEnd { + IrInstruction base; +}; + +struct IrInstructionCoroFree { + IrInstruction base; + + IrInstruction *coro_id; + IrInstruction *coro_handle; +}; + +struct IrInstructionCoroResume { + IrInstruction base; + + IrInstruction *awaiter_handle; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/analyze.cpp b/src/analyze.cpp index d83f195a85..c00014d8ca 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -475,9 +475,7 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) { if (child_type->zero_bits) { entry->type_ref = LLVMInt1Type(); entry->di_type = g->builtin_types.entry_bool->di_type; - } else if (child_type->id == TypeTableEntryIdPointer || - child_type->id == TypeTableEntryIdFn) - { + } else if (type_is_codegen_pointer(child_type)) { // this is an optimization but also is necessary for calling C // functions where all pointers are maybe pointers // function types are technically pointers @@ -1262,7 +1260,7 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) { case TypeTableEntryIdMaybe: { TypeTableEntry *child_type = type_entry->data.maybe.child_type; - return child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn; + return type_is_codegen_pointer(child_type); } case TypeTableEntryIdEnum: return type_entry->data.enumeration.decl_node->data.container_decl.init_arg_expr != nullptr; @@ -1673,6 +1671,8 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f field->src_index = i; field->gen_index = i; + assert(type_has_bits(field->type_entry)); + auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field); assert(prev_entry == nullptr); } @@ -3669,9 +3669,11 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) { TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type) { if (type->id == TypeTableEntryIdPointer) return type; if (type->id == TypeTableEntryIdFn) return type; + if (type->id == TypeTableEntryIdPromise) return type; if (type->id == TypeTableEntryIdMaybe) { if (type->data.maybe.child_type->id == TypeTableEntryIdPointer) return type->data.maybe.child_type; if (type->data.maybe.child_type->id == TypeTableEntryIdFn) return type->data.maybe.child_type; + if (type->data.maybe.child_type->id == TypeTableEntryIdPromise) return type->data.maybe.child_type; } return nullptr; } diff --git a/src/analyze.hpp b/src/analyze.hpp index a4036c597c..2fe41f6572 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -51,6 +51,7 @@ VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name); Tld *find_decl(CodeGen *g, Scope *scope, Buf *name); void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node); bool type_is_codegen_pointer(TypeTableEntry *type); + TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type); uint32_t get_ptr_align(TypeTableEntry *type); TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry); diff --git a/src/codegen.cpp b/src/codegen.cpp index cb3a98bb88..6b1a2513c7 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -542,7 +542,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { if (!type_has_bits(return_type)) { // nothing to do - } else if (return_type->id == TypeTableEntryIdPointer || return_type->id == TypeTableEntryIdFn) { + } else if (type_is_codegen_pointer(return_type)) { addLLVMAttr(fn_table_entry->llvm_value, 0, "nonnull"); } else if (handle_is_ptr(return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc)) @@ -2789,7 +2789,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLV if (child_type->zero_bits) { return maybe_handle; } else { - bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn); + bool maybe_is_ptr = type_is_codegen_pointer(child_type); if (maybe_is_ptr) { return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), ""); } else { @@ -2829,7 +2829,7 @@ static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable, if (child_type->zero_bits) { return nullptr; } else { - bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn); + bool maybe_is_ptr = type_is_codegen_pointer(child_type); if (maybe_is_ptr) { return maybe_ptr; } else { @@ -3052,6 +3052,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I { align_bytes = target_type->data.maybe.child_type->data.fn.fn_type_id.alignment; ptr_val = target_val; + } else if (target_type->id == TypeTableEntryIdMaybe && + target_type->data.maybe.child_type->id == TypeTableEntryIdPromise) + { + zig_panic("TODO audit this function"); } else if (target_type->id == TypeTableEntryIdStruct && target_type->data.structure.is_slice) { TypeTableEntry *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry; align_bytes = slice_ptr_type->data.pointer.alignment; @@ -3522,9 +3526,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I } LLVMValueRef payload_val = ir_llvm_value(g, instruction->value); - if (child_type->id == TypeTableEntryIdPointer || - child_type->id == TypeTableEntryIdFn) - { + if (type_is_codegen_pointer(child_type)) { return payload_val; } @@ -3716,6 +3718,22 @@ static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executab zig_panic("TODO ir_render_coro_alloc_fail"); } +static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, IrInstructionCoroSuspend *instruction) { + zig_panic("TODO ir_render_coro_suspend"); +} + +static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrInstructionCoroEnd *instruction) { + zig_panic("TODO ir_render_coro_end"); +} + +static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, IrInstructionCoroFree *instruction) { + zig_panic("TODO ir_render_coro_free"); +} + +static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) { + zig_panic("TODO ir_render_coro_resume"); +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -3911,6 +3929,14 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_begin(g, executable, (IrInstructionCoroBegin *)instruction); case IrInstructionIdCoroAllocFail: return ir_render_coro_alloc_fail(g, executable, (IrInstructionCoroAllocFail *)instruction); + case IrInstructionIdCoroSuspend: + return ir_render_coro_suspend(g, executable, (IrInstructionCoroSuspend *)instruction); + case IrInstructionIdCoroEnd: + return ir_render_coro_end(g, executable, (IrInstructionCoroEnd *)instruction); + case IrInstructionIdCoroFree: + return ir_render_coro_free(g, executable, (IrInstructionCoroFree *)instruction); + case IrInstructionIdCoroResume: + return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction); } zig_unreachable(); } @@ -4155,9 +4181,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c TypeTableEntry *child_type = type_entry->data.maybe.child_type; if (child_type->zero_bits) { return LLVMConstInt(LLVMInt1Type(), const_val->data.x_maybe ? 1 : 0, false); - } else if (child_type->id == TypeTableEntryIdPointer || - child_type->id == TypeTableEntryIdFn) - { + } else if (type_is_codegen_pointer(child_type)) { if (const_val->data.x_maybe) { return gen_const_val(g, const_val->data.x_maybe, ""); } else { @@ -6085,9 +6109,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf if (child_type->zero_bits) { buf_init_from_str(out_buf, "bool"); return; - } else if (child_type->id == TypeTableEntryIdPointer || - child_type->id == TypeTableEntryIdFn) - { + } else if (type_is_codegen_pointer(child_type)) { return get_c_type(g, gen_h, child_type, out_buf); } else { zig_unreachable(); diff --git a/src/ir.cpp b/src/ir.cpp index 68d31a7712..183543b0fe 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -46,7 +46,10 @@ static LVal make_lval_addr(bool is_const, bool is_volatile) { } static const char * ASYNC_ALLOC_FIELD_NAME = "allocFn"; -//static const char * ASYNC_FREE_FIELD_NAME = "freeFn"; +static const char * ASYNC_FREE_FIELD_NAME = "freeFn"; +static const char * AWAITER_HANDLE_FIELD_NAME = "awaiter_handle"; +static const char * RESULT_FIELD_NAME = "result"; +static const char * RESULT_PTR_FIELD_NAME = "result_ptr"; enum ConstCastResultId { ConstCastResultIdOk, @@ -672,6 +675,22 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocFail *) return IrInstructionIdCoroAllocFail; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSuspend *) { + return IrInstructionIdCoroSuspend; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroEnd *) { + return IrInstructionIdCoroEnd; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroFree *) { + return IrInstructionIdCoroFree; +} + +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroResume *) { + return IrInstructionIdCoroResume; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -743,14 +762,6 @@ static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *sou return &return_instruction->base; } -static IrInstruction *ir_build_return_from(IrBuilder *irb, IrInstruction *old_instruction, - IrInstruction *return_value) -{ - IrInstruction *new_instruction = ir_build_return(irb, old_instruction->scope, old_instruction->source_node, return_value); - ir_link_new_instruction(new_instruction, old_instruction); - return new_instruction; -} - static IrInstruction *ir_create_const(IrBuilder *irb, Scope *scope, AstNode *source_node, TypeTableEntry *type_entry) { @@ -822,6 +833,14 @@ static IrInstruction *ir_build_const_u29(IrBuilder *irb, Scope *scope, AstNode * return &const_instruction->base; } +static IrInstruction *ir_build_const_u8(IrBuilder *irb, Scope *scope, AstNode *source_node, uint8_t value) { + IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); + const_instruction->base.value.type = irb->codegen->builtin_types.entry_u8; + const_instruction->base.value.special = ConstValSpecialStatic; + bigint_init_unsigned(&const_instruction->base.value.data.x_bigint, value); + return &const_instruction->base; +} + static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node, TypeTableEntry *type_entry) { @@ -909,6 +928,33 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, Ast return &const_instruction->base; } +static IrInstruction *ir_build_const_promise_init(IrBuilder *irb, Scope *scope, AstNode *source_node, + TypeTableEntry *return_type) +{ + TypeTableEntry *awaiter_handle_type = get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise); + TypeTableEntry *result_ptr_type = get_pointer_to_type(irb->codegen, return_type, false); + const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME}; + TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type}; + size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1; + TypeTableEntry *struct_type = get_struct_type(irb->codegen, "AsyncFramePromise", field_names, field_types, + field_count); + + IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); + const_instruction->base.value.type = struct_type; + const_instruction->base.value.special = ConstValSpecialStatic; + const_instruction->base.value.data.x_struct.fields = allocate(2); + const_instruction->base.value.data.x_struct.fields[0].type = awaiter_handle_type; + const_instruction->base.value.data.x_struct.fields[0].special = ConstValSpecialStatic; + const_instruction->base.value.data.x_struct.fields[0].data.x_maybe = nullptr; + if (field_count == 3) { + const_instruction->base.value.data.x_struct.fields[1].type = return_type; + const_instruction->base.value.data.x_struct.fields[1].special = ConstValSpecialUndef; + const_instruction->base.value.data.x_struct.fields[2].type = result_ptr_type; + const_instruction->base.value.data.x_struct.fields[2].special = ConstValSpecialUndef; + } + return &const_instruction->base; +} + static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrBinOp op_id, IrInstruction *op1, IrInstruction *op2, bool safety_check_on) { @@ -2451,8 +2497,11 @@ static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *sco return &instruction->base; } -static IrInstruction *ir_build_coro_id(IrBuilder *irb, Scope *scope, AstNode *source_node) { +static IrInstruction *ir_build_coro_id(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *promise_ptr) { IrInstructionCoroId *instruction = ir_build_instruction(irb, scope, source_node); + instruction->promise_ptr = promise_ptr; + + ir_ref_instruction(promise_ptr, irb->current_basic_block); return &instruction->base; } @@ -2494,6 +2543,48 @@ static IrInstruction *ir_build_coro_alloc_fail(IrBuilder *irb, Scope *scope, Ast return &instruction->base; } +static IrInstruction *ir_build_coro_suspend(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *save_point, IrInstruction *is_final) +{ + IrInstructionCoroSuspend *instruction = ir_build_instruction(irb, scope, source_node); + instruction->save_point = save_point; + instruction->is_final = is_final; + + if (save_point != nullptr) ir_ref_instruction(save_point, irb->current_basic_block); + ir_ref_instruction(is_final, irb->current_basic_block); + + return &instruction->base; +} + +static IrInstruction *ir_build_coro_end(IrBuilder *irb, Scope *scope, AstNode *source_node) { + IrInstructionCoroEnd *instruction = ir_build_instruction(irb, scope, source_node); + return &instruction->base; +} + +static IrInstruction *ir_build_coro_free(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *coro_id, IrInstruction *coro_handle) +{ + IrInstructionCoroFree *instruction = ir_build_instruction(irb, scope, source_node); + instruction->coro_id = coro_id; + instruction->coro_handle = coro_handle; + + ir_ref_instruction(coro_id, irb->current_basic_block); + ir_ref_instruction(coro_handle, irb->current_basic_block); + + return &instruction->base; +} + +static IrInstruction *ir_build_coro_resume(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *awaiter_handle) +{ + IrInstructionCoroResume *instruction = ir_build_instruction(irb, scope, source_node); + instruction->awaiter_handle = awaiter_handle; + + ir_ref_instruction(awaiter_handle, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -2566,6 +2657,29 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) { return nullptr; } +static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode *node, IrInstruction *return_value, + bool is_generated_code) +{ + FnTableEntry *fn_entry = exec_fn_entry(irb->exec); + bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + if (!is_async) { + IrInstruction *return_inst = ir_build_return(irb, scope, node, return_value); + return_inst->is_gen = is_generated_code; + return return_inst; + } + + if (irb->exec->coro_result_ptr_field_ptr) { + IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); + ir_build_store_ptr(irb, scope, node, result_ptr, return_value); + } + IrInstruction *maybe_await_handle = ir_build_load_ptr(irb, scope, node, irb->exec->coro_awaiter_field_ptr); + IrInstruction *is_non_null = ir_build_test_nonnull(irb, scope, node, maybe_await_handle); + IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, false); + return ir_build_cond_br(irb, scope, node, is_non_null, irb->exec->coro_normal_final, irb->exec->coro_early_final, + is_comptime); + // the above blocks are rendered by ir_gen after the rest of codegen +} + static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) { assert(node->type == NodeTypeReturnExpr); @@ -2615,18 +2729,22 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, } ir_mark_gen(ir_build_cond_br(irb, scope, node, is_err, err_block, ok_block, is_comptime)); + IrBasicBlock *ret_stmt_block = ir_create_basic_block(irb, scope, "RetStmt"); ir_set_cursor_at_end_and_append_block(irb, err_block); ir_gen_defers_for_block(irb, scope, outer_scope, true); - ir_build_return(irb, scope, node, return_value); + ir_build_br(irb, scope, node, ret_stmt_block, is_comptime); ir_set_cursor_at_end_and_append_block(irb, ok_block); ir_gen_defers_for_block(irb, scope, outer_scope, false); - return ir_build_return(irb, scope, node, return_value); + ir_build_br(irb, scope, node, ret_stmt_block, is_comptime); + + ir_set_cursor_at_end_and_append_block(irb, ret_stmt_block); + return ir_gen_async_return(irb, scope, node, return_value, false); } else { // generate unconditional defers ir_gen_defers_for_block(irb, scope, outer_scope, false); - return ir_build_return(irb, scope, node, return_value); + return ir_gen_async_return(irb, scope, node, return_value, false); } } case ReturnKindError: @@ -2646,7 +2764,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, ir_set_cursor_at_end_and_append_block(irb, return_block); ir_gen_defers_for_block(irb, scope, outer_scope, true); IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr); - ir_build_return(irb, scope, node, err_val); + ir_gen_async_return(irb, scope, node, err_val, false); ir_set_cursor_at_end_and_append_block(irb, continue_block); IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false); @@ -5842,6 +5960,7 @@ static void invalidate_exec(IrExecutable *exec) { invalidate_exec(exec->source_exec); } + bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) { assert(node->owner); @@ -5858,48 +5977,81 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec FnTableEntry *fn_entry = exec_fn_entry(irb->exec); bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + IrInstruction *u8_ptr_type; + IrInstruction *const_bool_false; + IrInstruction *coro_unwrapped_mem_ptr; + IrInstruction *coro_id; + IrInstruction *coro_promise_ptr; + IrInstruction *coro_result_field_ptr; + TypeTableEntry *return_type; + Buf *result_ptr_field_name; if (is_async) { - IrInstruction *is_comptime_false = ir_build_const_bool(irb, scope, node, false); - IrInstruction *coro_id = ir_build_coro_id(irb, scope, node); + // create the coro promise + const_bool_false = ir_build_const_bool(irb, scope, node, false); + VariableTableEntry *promise_var = ir_create_var(irb, node, scope, nullptr, false, false, true, const_bool_false); + //scope = promise_var->child_scope; + + return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type; + IrInstruction *promise_init = ir_build_const_promise_init(irb, scope, node, return_type); + ir_build_var_decl(irb, scope, node, promise_var, nullptr, nullptr, promise_init); + + coro_promise_ptr = ir_build_var_ptr(irb, scope, node, promise_var, false, false); + Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME); + irb->exec->coro_awaiter_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, + awaiter_handle_field_name); + if (type_has_bits(return_type)) { + Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME); + coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name); + result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME); + irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, + result_ptr_field_name); + ir_build_store_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr, coro_result_field_ptr); + } + + u8_ptr_type = ir_build_const_type(irb, scope, node, + get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); + IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr); + coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr); IrInstruction *need_dyn_alloc = ir_build_coro_alloc(irb, scope, node, coro_id); IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); - IrInstruction *u8_ptr_type = ir_build_const_type(irb, scope, node, - get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); IrInstruction *null_ptr = ir_build_int_to_ptr(irb, scope, node, u8_ptr_type, zero); IrBasicBlock *dyn_alloc_block = ir_create_basic_block(irb, scope, "DynAlloc"); IrBasicBlock *coro_begin_block = ir_create_basic_block(irb, scope, "CoroBegin"); - ir_build_cond_br(irb, scope, node, need_dyn_alloc, dyn_alloc_block, coro_begin_block, is_comptime_false); + ir_build_cond_br(irb, scope, node, need_dyn_alloc, dyn_alloc_block, coro_begin_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, dyn_alloc_block); IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); - IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); + irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); - IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, alloc_field_name); + IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, + alloc_field_name); IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); IrInstruction *alignment = ir_build_const_u29(irb, scope, node, irb->codegen->pointer_size_bytes * 2); size_t arg_count = 3; IrInstruction **args = allocate(arg_count); - args[0] = implicit_allocator_ptr; // self + args[0] = irb->exec->implicit_allocator_ptr; // self args[1] = coro_size; // byte_count args[2] = alignment; // alignment - IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, FnInlineAuto, false, nullptr); + IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, + FnInlineAuto, false, nullptr); IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result); IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, scope, "AllocOk"); - ir_build_cond_br(irb, scope, node, alloc_result_is_err, alloc_err_block, alloc_ok_block, is_comptime_false); + ir_build_cond_br(irb, scope, node, alloc_result_is_err, alloc_err_block, alloc_ok_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, alloc_err_block); IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, alloc_result_ptr); ir_build_coro_alloc_fail(irb, scope, node, err_val); ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); - IrInstruction *unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false); + coro_unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false); Buf *ptr_field_name = buf_create_from_str("ptr"); - IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, unwrapped_mem_ptr, ptr_field_name); + IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, coro_unwrapped_mem_ptr, + ptr_field_name); IrInstruction *coro_mem_ptr = ir_build_load_ptr(irb, scope, node, coro_mem_ptr_field); - ir_build_br(irb, scope, node, coro_begin_block, is_comptime_false); + ir_build_br(irb, scope, node, coro_begin_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, coro_begin_block); IrBasicBlock **incoming_blocks = allocate(2); @@ -5910,6 +6062,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec incoming_values[1] = coro_mem_ptr; IrInstruction *coro_mem = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values); irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem); + irb->exec->coro_early_final = ir_create_basic_block(irb, scope, "CoroEarlyFinal"); + irb->exec->coro_normal_final = ir_create_basic_block(irb, scope, "CoroNormalFinal"); } IrInstruction *result = ir_gen_node_extra(irb, node, scope, LVAL_NONE); @@ -5918,7 +6072,84 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec return false; if (!instr_is_unreachable(result)) { - ir_mark_gen(ir_build_return(irb, scope, result->source_node, result)); + ir_gen_async_return(irb, scope, result->source_node, result, true); + } + + if (is_async) { + IrBasicBlock *invalid_resume_block = ir_create_basic_block(irb, scope, "InvalidResume"); + IrBasicBlock *final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup"); + IrBasicBlock *suspend_block = ir_create_basic_block(irb, scope, "Suspend"); + IrBasicBlock *check_free_block = ir_create_basic_block(irb, scope, "CheckFree"); + + ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_early_final); + IrInstruction *const_bool_true = ir_build_const_bool(irb, scope, node, true); + IrInstruction *suspend_code = ir_build_coro_suspend(irb, scope, node, nullptr, const_bool_true); + IrInstructionSwitchBrCase *cases = allocate(2); + cases[0].value = ir_build_const_u8(irb, scope, node, 0); + cases[0].block = invalid_resume_block; + cases[1].value = ir_build_const_u8(irb, scope, node, 1); + cases[1].block = final_cleanup_block; + ir_build_switch_br(irb, scope, node, suspend_code, suspend_block, 2, cases, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, suspend_block); + ir_build_coro_end(irb, scope, node); + ir_build_return(irb, scope, node, irb->exec->coro_handle); + + ir_set_cursor_at_end_and_append_block(irb, invalid_resume_block); + ir_build_unreachable(irb, scope, node); + + ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_normal_final); + ir_build_br(irb, scope, node, check_free_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, final_cleanup_block); + if (type_has_bits(return_type)) { + IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); + IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, result_ptr); + IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, + coro_result_field_ptr); + IrInstruction *return_type_inst = ir_build_const_type(irb, scope, node, + fn_entry->type_entry->data.fn.fn_type_id.return_type); + IrInstruction *size_of_ret_val = ir_build_size_of(irb, scope, node, return_type_inst); + ir_build_memcpy(irb, scope, node, result_ptr_as_u8_ptr, return_value_ptr_as_u8_ptr, size_of_ret_val); + } + ir_build_br(irb, scope, node, check_free_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, check_free_block); + IrBasicBlock **incoming_blocks = allocate(2); + IrInstruction **incoming_values = allocate(2); + incoming_blocks[0] = final_cleanup_block; + incoming_values[0] = const_bool_false; + incoming_blocks[1] = irb->exec->coro_normal_final; + incoming_values[1] = const_bool_true; + IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values); + IrInstruction *mem_to_free = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle); + IrInstruction *is_non_null = ir_build_test_nonnull(irb, scope, node, mem_to_free); + IrBasicBlock *dyn_free_block = ir_create_basic_block(irb, scope, "DynFree"); + IrBasicBlock *end_free_block = ir_create_basic_block(irb, scope, "EndFree"); + ir_build_cond_br(irb, scope, node, is_non_null, dyn_free_block, end_free_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, dyn_free_block); + Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); + IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, + free_field_name); + IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); + size_t arg_count = 2; + IrInstruction **args = allocate(arg_count); + args[0] = irb->exec->implicit_allocator_ptr; // self + args[1] = ir_build_load_ptr(irb, scope, node, coro_unwrapped_mem_ptr); // old_mem + ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr); + ir_build_br(irb, scope, node, end_free_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, end_free_block); + IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume"); + ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, suspend_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, resume_block); + IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node, + irb->exec->coro_awaiter_field_ptr, false); + IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr); + ir_build_coro_resume(irb, scope, node, awaiter_handle); + ir_build_br(irb, scope, node, suspend_block, const_bool_false); } return true; @@ -9514,8 +9745,11 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira, ir_add_error(ira, casted_value, buf_sprintf("function returns address of local variable")); return ir_unreach_error(ira); } - ir_build_return_from(&ira->new_irb, &return_instruction->base, casted_value); - return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable); + IrInstruction *result = ir_build_return(&ira->new_irb, return_instruction->base.scope, + return_instruction->base.source_node, casted_value); + result->value.type = ira->codegen->builtin_types.entry_unreachable; + ir_link_new_instruction(result, &return_instruction->base); + return ir_finish_anal(ira, result->value.type); } static TypeTableEntry *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *const_instruction) { @@ -16624,11 +16858,8 @@ static TypeTableEntry *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstr TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize; - if (!(target->value.type->id == TypeTableEntryIdPointer || - target->value.type->id == TypeTableEntryIdFn || - (target->value.type->id == TypeTableEntryIdMaybe && - (target->value.type->data.maybe.child_type->id == TypeTableEntryIdPointer || - target->value.type->data.maybe.child_type->id == TypeTableEntryIdFn)))) + if (!(type_is_codegen_pointer(target->value.type) || (target->value.type->id == TypeTableEntryIdMaybe && + type_is_codegen_pointer(target->value.type->data.maybe.child_type)))) { ir_add_error(ira, target, buf_sprintf("expected pointer, found '%s'", buf_ptr(&target->value.type->name))); @@ -16829,7 +17060,12 @@ static TypeTableEntry *ir_analyze_instruction_cancel(IrAnalyze *ira, IrInstructi } static TypeTableEntry *ir_analyze_instruction_coro_id(IrAnalyze *ira, IrInstructionCoroId *instruction) { - IrInstruction *result = ir_build_coro_id(&ira->new_irb, instruction->base.scope, instruction->base.source_node); + IrInstruction *promise_ptr = instruction->promise_ptr->other; + if (type_is_invalid(promise_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_id(&ira->new_irb, instruction->base.scope, instruction->base.source_node, + promise_ptr); ir_link_new_instruction(result, &instruction->base); result->value.type = ira->codegen->builtin_types.entry_usize; return result->value.type; @@ -16889,6 +17125,63 @@ static TypeTableEntry *ir_analyze_instruction_coro_alloc_fail(IrAnalyze *ira, Ir return ir_finish_anal(ira, result->value.type); } +static TypeTableEntry *ir_analyze_instruction_coro_suspend(IrAnalyze *ira, IrInstructionCoroSuspend *instruction) { + IrInstruction *save_point = nullptr; + if (instruction->save_point != nullptr) { + save_point = instruction->save_point->other; + if (type_is_invalid(save_point->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } + + IrInstruction *is_final = instruction->is_final->other; + if (type_is_invalid(is_final->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_suspend(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, save_point, is_final); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_u8; + return result->value.type; +} + +static TypeTableEntry *ir_analyze_instruction_coro_end(IrAnalyze *ira, IrInstructionCoroEnd *instruction) { + IrInstruction *result = ir_build_coro_end(&ira->new_irb, instruction->base.scope, + instruction->base.source_node); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_void; + return result->value.type; +} + +static TypeTableEntry *ir_analyze_instruction_coro_free(IrAnalyze *ira, IrInstructionCoroFree *instruction) { + IrInstruction *coro_id = instruction->coro_id->other; + if (type_is_invalid(coro_id->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *coro_handle = instruction->coro_handle->other; + if (type_is_invalid(coro_handle->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_free(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, coro_id, coro_handle); + ir_link_new_instruction(result, &instruction->base); + TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_u8, false); + result->value.type = get_maybe_type(ira->codegen, ptr_type); + return result->value.type; +} + +static TypeTableEntry *ir_analyze_instruction_coro_resume(IrAnalyze *ira, IrInstructionCoroResume *instruction) { + IrInstruction *awaiter_handle = instruction->awaiter_handle->other; + if (type_is_invalid(awaiter_handle->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_resume(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, awaiter_handle); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_void; + return result->value.type; +} + + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -17105,6 +17398,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_get_implicit_allocator(ira, (IrInstructionGetImplicitAllocator *)instruction); case IrInstructionIdCoroAllocFail: return ir_analyze_instruction_coro_alloc_fail(ira, (IrInstructionCoroAllocFail *)instruction); + case IrInstructionIdCoroSuspend: + return ir_analyze_instruction_coro_suspend(ira, (IrInstructionCoroSuspend *)instruction); + case IrInstructionIdCoroEnd: + return ir_analyze_instruction_coro_end(ira, (IrInstructionCoroEnd *)instruction); + case IrInstructionIdCoroFree: + return ir_analyze_instruction_coro_free(ira, (IrInstructionCoroFree *)instruction); + case IrInstructionIdCoroResume: + return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction); } zig_unreachable(); } @@ -17134,7 +17435,10 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl IrAnalyze *ira = allocate(1); old_exec->analysis = ira; ira->codegen = codegen; - ira->explicit_return_type = expected_type; + + FnTableEntry *fn_entry = exec_fn_entry(old_exec); + bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + ira->explicit_return_type = is_async ? get_promise_type(codegen, expected_type) : expected_type; ira->old_irb.codegen = codegen; ira->old_irb.exec = old_exec; @@ -17222,6 +17526,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroId: case IrInstructionIdCoroBegin: case IrInstructionIdCoroAllocFail: + case IrInstructionIdCoroEnd: + case IrInstructionIdCoroResume: return true; case IrInstructionIdPhi: @@ -17299,6 +17605,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdGetImplicitAllocator: case IrInstructionIdCoroAlloc: case IrInstructionIdCoroSize: + case IrInstructionIdCoroSuspend: + case IrInstructionIdCoroFree: return false; case IrInstructionIdAsm: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index bb49273d5c..ca7eb25879 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1031,7 +1031,9 @@ static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplic } static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) { - fprintf(irp->f, "@coroId()"); + fprintf(irp->f, "@coroId("); + ir_print_other_instruction(irp, instruction->promise_ptr); + fprintf(irp->f, ")"); } static void ir_print_coro_alloc(IrPrint *irp, IrInstructionCoroAlloc *instruction) { @@ -1058,6 +1060,36 @@ static void ir_print_coro_alloc_fail(IrPrint *irp, IrInstructionCoroAllocFail *i fprintf(irp->f, ")"); } +static void ir_print_coro_suspend(IrPrint *irp, IrInstructionCoroSuspend *instruction) { + fprintf(irp->f, "@coroSuspend("); + if (instruction->save_point != nullptr) { + ir_print_other_instruction(irp, instruction->save_point); + } else { + fprintf(irp->f, "null"); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->is_final); + fprintf(irp->f, ")"); +} + +static void ir_print_coro_end(IrPrint *irp, IrInstructionCoroEnd *instruction) { + fprintf(irp->f, "@coroEnd()"); +} + +static void ir_print_coro_free(IrPrint *irp, IrInstructionCoroFree *instruction) { + fprintf(irp->f, "@coroFree("); + ir_print_other_instruction(irp, instruction->coro_id); + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->coro_handle); + fprintf(irp->f, ")"); +} + +static void ir_print_coro_resume(IrPrint *irp, IrInstructionCoroResume *instruction) { + fprintf(irp->f, "@coroResume("); + ir_print_other_instruction(irp, instruction->awaiter_handle); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1399,6 +1431,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCoroAllocFail: ir_print_coro_alloc_fail(irp, (IrInstructionCoroAllocFail *)instruction); break; + case IrInstructionIdCoroSuspend: + ir_print_coro_suspend(irp, (IrInstructionCoroSuspend *)instruction); + break; + case IrInstructionIdCoroEnd: + ir_print_coro_end(irp, (IrInstructionCoroEnd *)instruction); + break; + case IrInstructionIdCoroFree: + ir_print_coro_free(irp, (IrInstructionCoroFree *)instruction); + break; + case IrInstructionIdCoroResume: + ir_print_coro_resume(irp, (IrInstructionCoroResume *)instruction); + break; } fprintf(irp->f, "\n"); } -- cgit v1.2.3 From 05bf666eb690d1a1328234cc408960133dba9563 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 02:47:31 -0500 Subject: codegen for calling an async function See #727 --- src/analyze.cpp | 34 +++++++++++++++++++++++++++++----- src/codegen.cpp | 22 ++++++++++++++++++---- src/ir.cpp | 2 ++ 3 files changed, 49 insertions(+), 9 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index cf5d9e0eab..2126f5ba07 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -986,20 +986,25 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { if (!skip_debug_info) { bool first_arg_return = calling_convention_does_first_arg_return(fn_type_id->cc) && handle_is_ptr(fn_type_id->return_type); + bool is_async = fn_type_id->cc == CallingConventionAsync; bool prefix_arg_error_return_trace = g->have_err_ret_tracing && (fn_type_id->return_type->id == TypeTableEntryIdErrorUnion || fn_type_id->return_type->id == TypeTableEntryIdErrorSet); // +1 for maybe making the first argument the return value - // +1 for maybe last argument the error return trace - LLVMTypeRef *gen_param_types = allocate(2 + fn_type_id->param_count); + // +1 for maybe first argument the error return trace + // +2 for maybe arguments async allocator and error code pointer + LLVMTypeRef *gen_param_types = allocate(4 + fn_type_id->param_count); // +1 because 0 is the return type and // +1 for maybe making first arg ret val and - // +1 for maybe last argument the error return trace - ZigLLVMDIType **param_di_types = allocate(3 + fn_type_id->param_count); + // +1 for maybe first argument the error return trace + // +2 for maybe arguments async allocator and error code pointer + ZigLLVMDIType **param_di_types = allocate(5 + fn_type_id->param_count); param_di_types[0] = fn_type_id->return_type->di_type; size_t gen_param_index = 0; TypeTableEntry *gen_return_type; - if (!type_has_bits(fn_type_id->return_type)) { + if (is_async) { + gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); + } else if (!type_has_bits(fn_type_id->return_type)) { gen_return_type = g->builtin_types.entry_void; } else if (first_arg_return) { TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false); @@ -1020,6 +1025,25 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { // after the gen_param_index += 1 because 0 is the return type param_di_types[gen_param_index] = gen_type->di_type; } + if (is_async) { + { + // async allocator param + TypeTableEntry *gen_type = fn_type_id->async_allocator_type; + gen_param_types[gen_param_index] = gen_type->type_ref; + gen_param_index += 1; + // after the gen_param_index += 1 because 0 is the return type + param_di_types[gen_param_index] = gen_type->di_type; + } + + { + // error code pointer + TypeTableEntry *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); + gen_param_types[gen_param_index] = gen_type->type_ref; + gen_param_index += 1; + // after the gen_param_index += 1 because 0 is the return type + param_di_types[gen_param_index] = gen_type->di_type; + } + } fn_type->data.fn.gen_param_info = allocate(fn_type_id->param_count); for (size_t i = 0; i < fn_type_id->param_count; i += 1) { diff --git a/src/codegen.cpp b/src/codegen.cpp index 6b1a2513c7..ec14d85064 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2521,13 +2521,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id; - if (fn_type_id->cc == CallingConventionAsync) { - zig_panic("TODO codegen async function call"); - } TypeTableEntry *src_return_type = fn_type_id->return_type; bool ret_has_bits = type_has_bits(src_return_type); - bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type); + + bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) && + calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc); bool prefix_arg_err_ret_stack = g->have_err_ret_tracing && (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet); size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0); bool is_var_args = fn_type_id->is_var_args; @@ -2541,6 +2540,15 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr gen_param_values[gen_param_index] = g->cur_err_ret_trace_val; gen_param_index += 1; } + if (instruction->is_async) { + gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->async_allocator); + gen_param_index += 1; + + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, ""); + LLVMBuildStore(g->builder, LLVMConstNull(g->builtin_types.entry_global_error_set->type_ref), err_val_ptr); + gen_param_values[gen_param_index] = err_val_ptr; + gen_param_index += 1; + } for (size_t call_i = 0; call_i < instruction->arg_count; call_i += 1) { IrInstruction *param_instruction = instruction->args[call_i]; TypeTableEntry *param_type = param_instruction->value.type; @@ -2578,6 +2586,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } } + if (instruction->is_async) { + LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, ""); + LLVMBuildStore(g->builder, result, payload_ptr); + return instruction->tmp_ptr; + } + if (src_return_type->id == TypeTableEntryIdUnreachable) { return LLVMBuildUnreachable(g->builder); } else if (!ret_has_bits) { diff --git a/src/ir.cpp b/src/ir.cpp index cc57e20c62..f484f32e01 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11775,6 +11775,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal if (call_instruction->is_async) { IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry, fn_ref, casted_args, impl_param_count, async_allocator_inst); ir_link_new_instruction(result, &call_instruction->base); + ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); } @@ -11862,6 +11863,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref, casted_args, call_param_count, async_allocator_inst); ir_link_new_instruction(result, &call_instruction->base); + ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); } -- cgit v1.2.3 From 7567448b91fd7012bf61d3f5532bfd86304899ae Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 14:47:58 -0500 Subject: codegen for cancel See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 002a2d4a4c..e9b4561eba 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1609,6 +1609,7 @@ struct CodeGen { LLVMValueRef trap_fn_val; LLVMValueRef return_address_fn_val; LLVMValueRef frame_address_fn_val; + LLVMValueRef coro_destroy_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index ec14d85064..783f5fd8fe 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -927,6 +927,21 @@ static LLVMValueRef get_memcpy_fn_val(CodeGen *g) { return g->memcpy_fn_val; } +static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) { + if (g->coro_destroy_fn_val) + return g->coro_destroy_fn_val; + + LLVMTypeRef param_types[] = { + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false); + Buf *name = buf_sprintf("llvm.coro.destroy"); + g->coro_destroy_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_destroy_fn_val)); + + return g->coro_destroy_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3113,7 +3128,9 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu } static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrInstructionCancel *instruction) { - zig_panic("TODO ir_render_cancel"); + LLVMValueRef target_handle = ir_llvm_value(g, instruction->target); + LLVMBuildCall(g->builder, get_coro_destroy_fn_val(g), &target_handle, 1, ""); + return nullptr; } static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) { -- cgit v1.2.3 From 9f6c5a20de03a59bfcaead703fe9490a6d622f84 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 15:10:29 -0500 Subject: codegen for coro_id instruction See #727 --- src/all_types.hpp | 1 + src/analyze.cpp | 4 ++++ src/analyze.hpp | 2 ++ src/codegen.cpp | 30 +++++++++++++++++++++++++++++- src/ir.cpp | 3 ++- src/zig_llvm.cpp | 3 +++ src/zig_llvm.h | 2 ++ 7 files changed, 43 insertions(+), 2 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index e9b4561eba..8e362cd64e 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1610,6 +1610,7 @@ struct CodeGen { LLVMValueRef return_address_fn_val; LLVMValueRef frame_address_fn_val; LLVMValueRef coro_destroy_fn_val; + LLVMValueRef coro_id_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/analyze.cpp b/src/analyze.cpp index 2126f5ba07..2787906c64 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -5776,3 +5776,7 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) { assert(err_set_type->data.error_set.infer_fn == nullptr); return err_set_type->data.error_set.err_count == UINT32_MAX; } + +uint32_t get_coro_frame_align_bytes(CodeGen *g) { + return g->pointer_size_bytes * 2; +} diff --git a/src/analyze.hpp b/src/analyze.hpp index 2fe41f6572..926793c58a 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -191,4 +191,6 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry); TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry); +uint32_t get_coro_frame_align_bytes(CodeGen *g); + #endif diff --git a/src/codegen.cpp b/src/codegen.cpp index 783f5fd8fe..21ad715977 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -942,6 +942,24 @@ static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) { return g->coro_destroy_fn_val; } +static LLVMValueRef get_coro_id_fn_val(CodeGen *g) { + if (g->coro_id_fn_val) + return g->coro_id_fn_val; + + LLVMTypeRef param_types[] = { + LLVMInt32Type(), + LLVMPointerType(LLVMInt8Type(), 0), + LLVMPointerType(LLVMInt8Type(), 0), + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 4, false); + Buf *name = buf_sprintf("llvm.coro.id"); + g->coro_id_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_id_fn_val)); + + return g->coro_id_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3730,7 +3748,17 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst } static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) { - zig_panic("TODO ir_render_coro_id"); + LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr); + LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false); + LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref), + LLVMPointerType(LLVMInt8Type(), 0)); + LLVMValueRef params[] = { + align_val, + promise_ptr, + null, + null, + }; + return LLVMBuildCall(g->builder, get_coro_id_fn_val(g), params, 4, ""); } static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) { diff --git a/src/ir.cpp b/src/ir.cpp index f484f32e01..5ab2b149d6 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6027,7 +6027,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, alloc_field_name); IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); - IrInstruction *alignment = ir_build_const_u29(irb, scope, node, irb->codegen->pointer_size_bytes * 2); + IrInstruction *alignment = ir_build_const_u29(irb, scope, node, + get_coro_frame_align_bytes(irb->codegen)); size_t arg_count = 3; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 97c07ab820..34defc6dc6 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -182,6 +182,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM return false; } +ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) { + return wrap(Type::getTokenTy(*unwrap(context_ref))); +} LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name) diff --git a/src/zig_llvm.h b/src/zig_llvm.h index 4ae25ef6fd..01a78a6af4 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType { ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug); +ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref); + enum ZigLLVM_FnInline { ZigLLVM_FnInlineAuto, ZigLLVM_FnInlineAlways, -- cgit v1.2.3 From 93cbd4eeb97f30062311d6e083dd056b8fb8b021 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 15:20:31 -0500 Subject: codegen for coro_alloc and coro_size instructions See #727 --- src/all_types.hpp | 2 ++ src/codegen.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 8e362cd64e..f95cb8425a 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1611,6 +1611,8 @@ struct CodeGen { LLVMValueRef frame_address_fn_val; LLVMValueRef coro_destroy_fn_val; LLVMValueRef coro_id_fn_val; + LLVMValueRef coro_alloc_fn_val; + LLVMValueRef coro_size_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index 21ad715977..c7b6ce1f7c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -960,6 +960,33 @@ static LLVMValueRef get_coro_id_fn_val(CodeGen *g) { return g->coro_id_fn_val; } +static LLVMValueRef get_coro_alloc_fn_val(CodeGen *g) { + if (g->coro_alloc_fn_val) + return g->coro_alloc_fn_val; + + LLVMTypeRef param_types[] = { + ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 1, false); + Buf *name = buf_sprintf("llvm.coro.alloc"); + g->coro_alloc_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_alloc_fn_val)); + + return g->coro_alloc_fn_val; +} + +static LLVMValueRef get_coro_size_fn_val(CodeGen *g) { + if (g->coro_size_fn_val) + return g->coro_size_fn_val; + + LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false); + Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8); + g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_size_fn_val)); + + return g->coro_size_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3762,11 +3789,12 @@ static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrIn } static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) { - zig_panic("TODO ir_render_coro_alloc"); + LLVMValueRef token = ir_llvm_value(g, instruction->coro_id); + return LLVMBuildCall(g->builder, get_coro_alloc_fn_val(g), &token, 1, ""); } static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) { - zig_panic("TODO ir_render_coro_size"); + return LLVMBuildCall(g->builder, get_coro_size_fn_val(g), nullptr, 0, ""); } static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) { -- cgit v1.2.3 From bced3fb64cbb9006886bc237d959ce0f2ca3c3f7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:05:10 -0500 Subject: codegen for get_implicit_allocator instruction See #727 --- src/codegen.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index c7b6ce1f7c..0c8e601bc0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2568,6 +2568,11 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI } } +static bool get_prefix_arg_err_ret_stack(CodeGen *g, TypeTableEntry *src_return_type) { + return g->have_err_ret_tracing && + (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet); +} + static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) { LLVMValueRef fn_val; TypeTableEntry *fn_type; @@ -2587,7 +2592,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc); - bool prefix_arg_err_ret_stack = g->have_err_ret_tracing && (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet); + bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0); bool is_var_args = fn_type_id->is_var_args; LLVMValueRef *gen_param_values = allocate(actual_param_count); @@ -3178,8 +3183,13 @@ static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrIns return nullptr; } -static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) { - zig_panic("TODO ir_render_get_implicit_allocator"); +static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, + IrInstructionGetImplicitAllocator *instruction) +{ + TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; + bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); + size_t allocator_arg_index = prefix_arg_err_ret_stack ? 1 : 0; + return LLVMGetParam(g->cur_fn_val, allocator_arg_index); } static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { -- cgit v1.2.3 From 79f1ff574b3badf7cf0a0cd7632c529801b4c611 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:15:14 -0500 Subject: codegen for coro_alloc_fail instruction See #727 --- src/codegen.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 0c8e601bc0..60667ac465 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3811,8 +3811,24 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I zig_panic("TODO ir_render_coro_begin"); } -static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable, IrInstructionCoroAllocFail *instruction) { - zig_panic("TODO ir_render_coro_alloc_fail"); +static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable, + IrInstructionCoroAllocFail *instruction) +{ + TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; + bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); + size_t err_code_ptr_arg_index = prefix_arg_err_ret_stack ? 2 : 1; + LLVMValueRef err_code_ptr_val = LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index); + LLVMValueRef err_code = ir_llvm_value(g, instruction->err_val); + LLVMBuildStore(g->builder, err_code, err_code_ptr_val); + + LLVMValueRef return_value; + if (ir_want_runtime_safety(g, &instruction->base)) { + return_value = LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0)); + } else { + return_value = LLVMGetUndef(LLVMPointerType(LLVMInt8Type(), 0)); + } + LLVMBuildRet(g->builder, return_value); + return nullptr; } static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, IrInstructionCoroSuspend *instruction) { -- cgit v1.2.3 From d0f2eca106f556fe5826d96b08ffdf1be1293915 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:22:19 -0500 Subject: codegen for coro_begin instruction See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index f95cb8425a..d2bfd25446 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1613,6 +1613,7 @@ struct CodeGen { LLVMValueRef coro_id_fn_val; LLVMValueRef coro_alloc_fn_val; LLVMValueRef coro_size_fn_val; + LLVMValueRef coro_begin_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index 60667ac465..c1d6346253 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -987,6 +987,22 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) { return g->coro_size_fn_val; } +static LLVMValueRef get_coro_begin_fn_val(CodeGen *g) { + if (g->coro_begin_fn_val) + return g->coro_begin_fn_val; + + LLVMTypeRef param_types[] = { + ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 2, false); + Buf *name = buf_sprintf("llvm.coro.begin"); + g->coro_begin_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_begin_fn_val)); + + return g->coro_begin_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3808,7 +3824,13 @@ static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, Ir } static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) { - zig_panic("TODO ir_render_coro_begin"); + LLVMValueRef coro_id = ir_llvm_value(g, instruction->coro_id); + LLVMValueRef coro_mem_ptr = ir_llvm_value(g, instruction->coro_mem_ptr); + LLVMValueRef params[] = { + coro_id, + coro_mem_ptr, + }; + return LLVMBuildCall(g->builder, get_coro_begin_fn_val(g), params, 2, ""); } static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable, -- cgit v1.2.3 From 0cf327eb17360cddcdfab623db049aca5afd7010 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:29:07 -0500 Subject: codegen for coro_suspend instruction See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index d2bfd25446..7efede5333 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1614,6 +1614,7 @@ struct CodeGen { LLVMValueRef coro_alloc_fn_val; LLVMValueRef coro_size_fn_val; LLVMValueRef coro_begin_fn_val; + LLVMValueRef coro_suspend_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index c1d6346253..5643333f99 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1003,6 +1003,22 @@ static LLVMValueRef get_coro_begin_fn_val(CodeGen *g) { return g->coro_begin_fn_val; } +static LLVMValueRef get_coro_suspend_fn_val(CodeGen *g) { + if (g->coro_suspend_fn_val) + return g->coro_suspend_fn_val; + + LLVMTypeRef param_types[] = { + ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), + LLVMInt1Type(), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt8Type(), param_types, 2, false); + Buf *name = buf_sprintf("llvm.coro.suspend"); + g->coro_suspend_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_suspend_fn_val)); + + return g->coro_suspend_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3854,7 +3870,18 @@ static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executab } static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, IrInstructionCoroSuspend *instruction) { - zig_panic("TODO ir_render_coro_suspend"); + LLVMValueRef save_point; + if (instruction->save_point == nullptr) { + save_point = LLVMConstNull(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext())); + } else { + save_point = ir_llvm_value(g, instruction->save_point); + } + LLVMValueRef is_final = ir_llvm_value(g, instruction->is_final); + LLVMValueRef params[] = { + save_point, + is_final, + }; + return LLVMBuildCall(g->builder, get_coro_suspend_fn_val(g), params, 2, ""); } static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrInstructionCoroEnd *instruction) { -- cgit v1.2.3 From d2d2ba10e9688e6760e75290a29dc055d04a0296 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:40:00 -0500 Subject: codegen for coro_end instruction See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 7efede5333..b7a0625926 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1615,6 +1615,7 @@ struct CodeGen { LLVMValueRef coro_size_fn_val; LLVMValueRef coro_begin_fn_val; LLVMValueRef coro_suspend_fn_val; + LLVMValueRef coro_end_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index 5643333f99..4cc9880fea 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1019,6 +1019,22 @@ static LLVMValueRef get_coro_suspend_fn_val(CodeGen *g) { return g->coro_suspend_fn_val; } +static LLVMValueRef get_coro_end_fn_val(CodeGen *g) { + if (g->coro_end_fn_val) + return g->coro_end_fn_val; + + LLVMTypeRef param_types[] = { + LLVMPointerType(LLVMInt8Type(), 0), + LLVMInt1Type(), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 2, false); + Buf *name = buf_sprintf("llvm.coro.end"); + g->coro_end_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_end_fn_val)); + + return g->coro_end_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3885,7 +3901,11 @@ static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, } static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrInstructionCoroEnd *instruction) { - zig_panic("TODO ir_render_coro_end"); + LLVMValueRef params[] = { + LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0)), + LLVMConstNull(LLVMInt1Type()), + }; + return LLVMBuildCall(g->builder, get_coro_end_fn_val(g), params, 2, ""); } static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, IrInstructionCoroFree *instruction) { -- cgit v1.2.3 From 4eac75914bcdf9648518d1837f48e07e35744dc1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 16:46:01 -0500 Subject: codegen for coro_free instruction See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index b7a0625926..d4ec5ac427 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1616,6 +1616,7 @@ struct CodeGen { LLVMValueRef coro_begin_fn_val; LLVMValueRef coro_suspend_fn_val; LLVMValueRef coro_end_fn_val; + LLVMValueRef coro_free_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index 4cc9880fea..163e9d804b 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1035,6 +1035,22 @@ static LLVMValueRef get_coro_end_fn_val(CodeGen *g) { return g->coro_end_fn_val; } +static LLVMValueRef get_coro_free_fn_val(CodeGen *g) { + if (g->coro_free_fn_val) + return g->coro_free_fn_val; + + LLVMTypeRef param_types[] = { + ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 2, false); + Buf *name = buf_sprintf("llvm.coro.free"); + g->coro_free_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_free_fn_val)); + + return g->coro_free_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3909,7 +3925,13 @@ static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrI } static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, IrInstructionCoroFree *instruction) { - zig_panic("TODO ir_render_coro_free"); + LLVMValueRef coro_id = ir_llvm_value(g, instruction->coro_id); + LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle); + LLVMValueRef params[] = { + coro_id, + coro_handle, + }; + return LLVMBuildCall(g->builder, get_coro_free_fn_val(g), params, 2, ""); } static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) { -- cgit v1.2.3 From 83f89064490350991806aea02ea6ba4b948c0376 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Feb 2018 17:34:05 -0500 Subject: codegen for coro_resume instruction See #727 --- src/all_types.hpp | 1 + src/codegen.cpp | 18 +++++++++++++++++- src/ir.cpp | 9 +++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index d4ec5ac427..3cf5676dfe 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1617,6 +1617,7 @@ struct CodeGen { LLVMValueRef coro_suspend_fn_val; LLVMValueRef coro_end_fn_val; LLVMValueRef coro_free_fn_val; + LLVMValueRef coro_resume_fn_val; bool error_during_imports; const char **clang_argv; diff --git a/src/codegen.cpp b/src/codegen.cpp index 163e9d804b..0c4f66daa4 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1051,6 +1051,21 @@ static LLVMValueRef get_coro_free_fn_val(CodeGen *g) { return g->coro_free_fn_val; } +static LLVMValueRef get_coro_resume_fn_val(CodeGen *g) { + if (g->coro_resume_fn_val) + return g->coro_resume_fn_val; + + LLVMTypeRef param_types[] = { + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false); + Buf *name = buf_sprintf("llvm.coro.resume"); + g->coro_resume_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_resume_fn_val)); + + return g->coro_resume_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3935,7 +3950,8 @@ static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, Ir } static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) { - zig_panic("TODO ir_render_coro_resume"); + LLVMValueRef awaiter_handle = ir_llvm_value(g, instruction->awaiter_handle); + return LLVMBuildCall(g->builder, get_coro_resume_fn_val(g), &awaiter_handle, 1, ""); } static void set_debug_location(CodeGen *g, IrInstruction *instruction) { diff --git a/src/ir.cpp b/src/ir.cpp index 5ab2b149d6..81bde2e793 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6143,14 +6143,19 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_set_cursor_at_end_and_append_block(irb, end_free_block); IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume"); - ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, suspend_block, const_bool_false); + IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "Return"); + ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, return_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, resume_block); IrInstruction *unwrapped_await_handle_ptr = ir_build_unwrap_maybe(irb, scope, node, irb->exec->coro_awaiter_field_ptr, false); IrInstruction *awaiter_handle = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr); ir_build_coro_resume(irb, scope, node, awaiter_handle); - ir_build_br(irb, scope, node, suspend_block, const_bool_false); + ir_build_br(irb, scope, node, return_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, return_block); + IrInstruction *undef = ir_build_const_undefined(irb, scope, node); + ir_build_return(irb, scope, node, undef); } return true; -- cgit v1.2.3 From 3e86fb500dc918618a2ccaa5d942de98bd5fea47 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Feb 2018 02:46:21 -0500 Subject: implement coroutine suspend see #727 --- src/all_types.hpp | 10 ++++++ src/codegen.cpp | 22 ++++++++++++ src/ir.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++----- src/ir_print.cpp | 9 +++++ 4 files changed, 139 insertions(+), 8 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index d2705d8ec6..63292dd8ec 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -63,6 +63,8 @@ struct IrExecutable { IrInstruction *implicit_allocator_ptr; IrBasicBlock *coro_early_final; IrBasicBlock *coro_normal_final; + IrBasicBlock *coro_suspend_block; + IrBasicBlock *coro_final_cleanup_block; }; enum OutType { @@ -1631,6 +1633,7 @@ struct CodeGen { LLVMValueRef coro_end_fn_val; LLVMValueRef coro_free_fn_val; LLVMValueRef coro_resume_fn_val; + LLVMValueRef coro_save_fn_val; bool error_during_imports; const char **clang_argv; @@ -2000,6 +2003,7 @@ enum IrInstructionId { IrInstructionIdCoroEnd, IrInstructionIdCoroFree, IrInstructionIdCoroResume, + IrInstructionIdCoroSave, }; struct IrInstruction { @@ -2902,6 +2906,12 @@ struct IrInstructionCoroResume { IrInstruction *awaiter_handle; }; +struct IrInstructionCoroSave { + IrInstruction base; + + IrInstruction *coro_handle; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/codegen.cpp b/src/codegen.cpp index 0c4f66daa4..f82c686b85 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1066,6 +1066,21 @@ static LLVMValueRef get_coro_resume_fn_val(CodeGen *g) { return g->coro_resume_fn_val; } +static LLVMValueRef get_coro_save_fn_val(CodeGen *g) { + if (g->coro_save_fn_val) + return g->coro_save_fn_val; + + LLVMTypeRef param_types[] = { + LLVMPointerType(LLVMInt8Type(), 0), + }; + LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 1, false); + Buf *name = buf_sprintf("llvm.coro.save"); + g->coro_save_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_save_fn_val)); + + return g->coro_save_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -3954,6 +3969,11 @@ static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, return LLVMBuildCall(g->builder, get_coro_resume_fn_val(g), &awaiter_handle, 1, ""); } +static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, IrInstructionCoroSave *instruction) { + LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle); + return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, ""); +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -4157,6 +4177,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_free(g, executable, (IrInstructionCoroFree *)instruction); case IrInstructionIdCoroResume: return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction); + case IrInstructionIdCoroSave: + return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction); } zig_unreachable(); } diff --git a/src/ir.cpp b/src/ir.cpp index 7ed66b92bd..2600f5e948 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -691,6 +691,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroResume *) { return IrInstructionIdCoroResume; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSave *) { + return IrInstructionIdCoroSave; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -2585,6 +2589,17 @@ static IrInstruction *ir_build_coro_resume(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } +static IrInstruction *ir_build_coro_save(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *coro_handle) +{ + IrInstructionCoroSave *instruction = ir_build_instruction(irb, scope, source_node); + instruction->coro_handle = coro_handle; + + ir_ref_instruction(coro_handle, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -5847,7 +5862,67 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNode *node) { assert(node->type == NodeTypeSuspend); - zig_panic("TODO: generate suspend"); + FnTableEntry *fn_entry = exec_fn_entry(irb->exec); + if (!fn_entry) { + add_node_error(irb->codegen, node, buf_sprintf("suspend outside function definition")); + return irb->codegen->invalid_instruction; + } + if (fn_entry->type_entry->data.fn.fn_type_id.cc != CallingConventionAsync) { + add_node_error(irb->codegen, node, buf_sprintf("suspend in non-async function")); + return irb->codegen->invalid_instruction; + } + + ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(parent_scope); + if (scope_defer_expr) { + if (!scope_defer_expr->reported_err) { + add_node_error(irb->codegen, node, buf_sprintf("cannot suspend inside defer expression")); + scope_defer_expr->reported_err = true; + } + return irb->codegen->invalid_instruction; + } + + Scope *outer_scope = irb->exec->begin_scope; + + + IrInstruction *suspend_code; + IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false); + if (node->data.suspend.block == nullptr) { + suspend_code = ir_build_coro_suspend(irb, parent_scope, node, nullptr, const_bool_false); + } else { + assert(node->data.suspend.promise_symbol != nullptr); + assert(node->data.suspend.promise_symbol->type == NodeTypeSymbol); + Buf *promise_symbol_name = node->data.suspend.promise_symbol->data.symbol_expr.symbol; + Scope *child_scope; + if (!buf_eql_str(promise_symbol_name, "_")) { + VariableTableEntry *promise_var = ir_create_var(irb, node, parent_scope, promise_symbol_name, + true, true, false, const_bool_false); + ir_build_var_decl(irb, parent_scope, node, promise_var, nullptr, nullptr, irb->exec->coro_handle); + child_scope = promise_var->child_scope; + } else { + child_scope = parent_scope; + } + IrInstruction *save_token = ir_build_coro_save(irb, child_scope, node, irb->exec->coro_handle); + ir_gen_node(irb, node->data.suspend.block, child_scope); + suspend_code = ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false); + } + + IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); + IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); + + IrInstructionSwitchBrCase *cases = allocate(2); + cases[0].value = ir_build_const_u8(irb, parent_scope, node, 0); + cases[0].block = resume_block; + cases[1].value = ir_build_const_u8(irb, parent_scope, node, 1); + cases[1].block = cleanup_block; + ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, + 2, cases, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, cleanup_block); + ir_gen_defers_for_block(irb, parent_scope, outer_scope, true); + ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, resume_block); + return ir_build_const_void(irb, parent_scope, node); } static 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 irb->exec->coro_early_final = ir_create_basic_block(irb, scope, "CoroEarlyFinal"); irb->exec->coro_normal_final = ir_create_basic_block(irb, scope, "CoroNormalFinal"); + irb->exec->coro_suspend_block = ir_create_basic_block(irb, scope, "Suspend"); + irb->exec->coro_final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup"); } 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 if (is_async) { IrBasicBlock *invalid_resume_block = ir_create_basic_block(irb, scope, "InvalidResume"); - IrBasicBlock *final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup"); - IrBasicBlock *suspend_block = ir_create_basic_block(irb, scope, "Suspend"); IrBasicBlock *check_free_block = ir_create_basic_block(irb, scope, "CheckFree"); 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 cases[0].value = ir_build_const_u8(irb, scope, node, 0); cases[0].block = invalid_resume_block; cases[1].value = ir_build_const_u8(irb, scope, node, 1); - cases[1].block = final_cleanup_block; - ir_build_switch_br(irb, scope, node, suspend_code, suspend_block, 2, cases, const_bool_false); + cases[1].block = irb->exec->coro_final_cleanup_block; + ir_build_switch_br(irb, scope, node, suspend_code, irb->exec->coro_suspend_block, 2, cases, const_bool_false); - ir_set_cursor_at_end_and_append_block(irb, suspend_block); + ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_suspend_block); ir_build_coro_end(irb, scope, node); ir_build_return(irb, scope, node, irb->exec->coro_handle); @@ -6136,7 +6211,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_normal_final); ir_build_br(irb, scope, node, check_free_block, const_bool_false); - ir_set_cursor_at_end_and_append_block(irb, final_cleanup_block); + ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_final_cleanup_block); if (type_has_bits(return_type)) { IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); 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 ir_set_cursor_at_end_and_append_block(irb, check_free_block); IrBasicBlock **incoming_blocks = allocate(2); IrInstruction **incoming_values = allocate(2); - incoming_blocks[0] = final_cleanup_block; + incoming_blocks[0] = irb->exec->coro_final_cleanup_block; incoming_values[0] = const_bool_false; incoming_blocks[1] = irb->exec->coro_normal_final; incoming_values[1] = const_bool_true; @@ -17219,6 +17294,18 @@ static TypeTableEntry *ir_analyze_instruction_coro_resume(IrAnalyze *ira, IrInst return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_coro_save(IrAnalyze *ira, IrInstructionCoroSave *instruction) { + IrInstruction *coro_handle = instruction->coro_handle->other; + if (type_is_invalid(coro_handle->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_save(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, coro_handle); + ir_link_new_instruction(result, &instruction->base); + result->value.type = ira->codegen->builtin_types.entry_usize; + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { @@ -17444,6 +17531,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_free(ira, (IrInstructionCoroFree *)instruction); case IrInstructionIdCoroResume: return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction); + case IrInstructionIdCoroSave: + return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction); } zig_unreachable(); } @@ -17566,6 +17655,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroAllocFail: case IrInstructionIdCoroEnd: case IrInstructionIdCoroResume: + case IrInstructionIdCoroSave: return true; case IrInstructionIdPhi: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index ca7eb25879..2e367672a5 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1090,6 +1090,12 @@ static void ir_print_coro_resume(IrPrint *irp, IrInstructionCoroResume *instruct fprintf(irp->f, ")"); } +static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction) { + fprintf(irp->f, "@coroSave("); + ir_print_other_instruction(irp, instruction->coro_handle); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1443,6 +1449,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCoroResume: ir_print_coro_resume(irp, (IrInstructionCoroResume *)instruction); break; + case IrInstructionIdCoroSave: + ir_print_coro_save(irp, (IrInstructionCoroSave *)instruction); + break; } fprintf(irp->f, "\n"); } -- cgit v1.2.3 From 4ac6c4d6bfb8f7ada2799ddb5ce3a9797be0518d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Feb 2018 21:14:15 -0500 Subject: workaround llvm coro transformations by making alloc and free functions be parameters to async functions instead of using getelementptr in the DynAlloc block See #727 --- src/all_types.hpp | 11 +++++ src/analyze.cpp | 34 ++++++++++++-- src/codegen.cpp | 37 ++++++++++++--- src/ir.cpp | 136 +++++++++++++++++++++++++++++++++++++----------------- src/ir_print.cpp | 14 +++++- std/mem.zig | 16 ------- 6 files changed, 179 insertions(+), 69 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 63292dd8ec..4220523126 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2208,7 +2208,10 @@ struct IrInstructionCall { LLVMValueRef tmp_ptr; FnInline fn_inline; bool is_async; + IrInstruction *async_allocator; + IrInstruction *alloc_fn; + IrInstruction *free_fn; }; struct IrInstructionConst { @@ -2849,8 +2852,16 @@ struct IrInstructionCancel { IrInstruction *target; }; +enum ImplicitAllocatorId { + ImplicitAllocatorIdContext, + ImplicitAllocatorIdAlloc, + ImplicitAllocatorIdFree, +}; + struct IrInstructionGetImplicitAllocator { IrInstruction base; + + ImplicitAllocatorId id; }; struct IrInstructionCoroId { diff --git a/src/analyze.cpp b/src/analyze.cpp index ce9e99f8fa..26924cc7db 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1006,13 +1006,13 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { fn_type_id->return_type->id == TypeTableEntryIdErrorSet); // +1 for maybe making the first argument the return value // +1 for maybe first argument the error return trace - // +2 for maybe arguments async allocator and error code pointer - LLVMTypeRef *gen_param_types = allocate(4 + fn_type_id->param_count); + // +4 for maybe arguments async allocator and error code pointer + LLVMTypeRef *gen_param_types = allocate(6 + fn_type_id->param_count); // +1 because 0 is the return type and // +1 for maybe making first arg ret val and // +1 for maybe first argument the error return trace - // +2 for maybe arguments async allocator and error code pointer - ZigLLVMDIType **param_di_types = allocate(5 + fn_type_id->param_count); + // +4 for maybe arguments async allocator and error code pointer + ZigLLVMDIType **param_di_types = allocate(7 + fn_type_id->param_count); param_di_types[0] = fn_type_id->return_type->di_type; size_t gen_param_index = 0; TypeTableEntry *gen_return_type; @@ -1049,6 +1049,32 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { param_di_types[gen_param_index] = gen_type->di_type; } + { + // async alloc fn param + assert(fn_type_id->async_allocator_type->id == TypeTableEntryIdPointer); + TypeTableEntry *struct_type = fn_type_id->async_allocator_type->data.pointer.child_type; + TypeStructField *alloc_fn_field = find_struct_type_field(struct_type, buf_create_from_str("allocFn")); + assert(alloc_fn_field->type_entry->id == TypeTableEntryIdFn); + TypeTableEntry *gen_type = alloc_fn_field->type_entry; + gen_param_types[gen_param_index] = gen_type->type_ref; + gen_param_index += 1; + // after the gen_param_index += 1 because 0 is the return type + param_di_types[gen_param_index] = gen_type->di_type; + } + + { + // async free fn param + assert(fn_type_id->async_allocator_type->id == TypeTableEntryIdPointer); + TypeTableEntry *struct_type = fn_type_id->async_allocator_type->data.pointer.child_type; + TypeStructField *free_fn_field = find_struct_type_field(struct_type, buf_create_from_str("freeFn")); + assert(free_fn_field->type_entry->id == TypeTableEntryIdFn); + TypeTableEntry *gen_type = free_fn_field->type_entry; + gen_param_types[gen_param_index] = gen_type->type_ref; + gen_param_index += 1; + // after the gen_param_index += 1 because 0 is the return type + param_di_types[gen_param_index] = gen_type->di_type; + } + { // error code pointer TypeTableEntry *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); diff --git a/src/codegen.cpp b/src/codegen.cpp index f82c686b85..21fc28e4af 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2667,6 +2667,18 @@ static bool get_prefix_arg_err_ret_stack(CodeGen *g, TypeTableEntry *src_return_ (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet); } +static size_t get_async_allocator_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { + // 0 1 2 3 4 5 + // err_ret_stack allocator_ptr alloc free err_code other_args... + return get_prefix_arg_err_ret_stack(g, src_return_type) ? 1 : 0; +} + +static size_t get_async_err_code_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { + // 0 1 2 3 4 5 + // err_ret_stack allocator_ptr alloc free err_code other_args... + return 3 + get_async_allocator_arg_index(g, src_return_type); +} + static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) { LLVMValueRef fn_val; TypeTableEntry *fn_type; @@ -2687,7 +2699,8 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc); bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); - size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0); + // +4 for the async args + size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0) + 4; bool is_var_args = fn_type_id->is_var_args; LLVMValueRef *gen_param_values = allocate(actual_param_count); size_t gen_param_index = 0; @@ -2703,6 +2716,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->async_allocator); gen_param_index += 1; + gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->alloc_fn); + gen_param_index += 1; + + gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->free_fn); + gen_param_index += 1; + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, ""); LLVMBuildStore(g->builder, LLVMConstNull(g->builtin_types.entry_global_error_set->type_ref), err_val_ptr); gen_param_values[gen_param_index] = err_val_ptr; @@ -3281,9 +3300,16 @@ static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *e IrInstructionGetImplicitAllocator *instruction) { TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; - bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); - size_t allocator_arg_index = prefix_arg_err_ret_stack ? 1 : 0; - return LLVMGetParam(g->cur_fn_val, allocator_arg_index); + size_t allocator_arg_index = get_async_allocator_arg_index(g, src_return_type); + switch (instruction->id) { + case ImplicitAllocatorIdContext: + return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 0); + case ImplicitAllocatorIdAlloc: + return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 1); + case ImplicitAllocatorIdFree: + return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 2); + } + zig_unreachable(); } static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { @@ -3915,8 +3941,7 @@ static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executab IrInstructionCoroAllocFail *instruction) { TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; - bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); - size_t err_code_ptr_arg_index = prefix_arg_err_ret_stack ? 2 : 1; + size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, src_return_type); LLVMValueRef err_code_ptr_val = LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index); LLVMValueRef err_code = ir_llvm_value(g, instruction->err_val); LLVMBuildStore(g->builder, err_code, err_code_ptr_val); diff --git a/src/ir.cpp b/src/ir.cpp index 2600f5e948..bca133b8e5 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1066,7 +1066,7 @@ static IrInstruction *ir_build_union_field_ptr_from(IrBuilder *irb, IrInstructio static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *alloc_fn, IrInstruction *free_fn) { IrInstructionCall *call_instruction = ir_build_instruction(irb, scope, source_node); call_instruction->fn_entry = fn_entry; @@ -1077,6 +1077,8 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc call_instruction->arg_count = arg_count; call_instruction->is_async = is_async; call_instruction->async_allocator = async_allocator; + call_instruction->alloc_fn = alloc_fn; + call_instruction->free_fn = free_fn; if (fn_ref) ir_ref_instruction(fn_ref, irb->current_basic_block); @@ -1084,16 +1086,20 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc ir_ref_instruction(args[i], irb->current_basic_block); if (async_allocator) ir_ref_instruction(async_allocator, irb->current_basic_block); + if (alloc_fn) + ir_ref_instruction(alloc_fn, irb->current_basic_block); + if (free_fn) + ir_ref_instruction(free_fn, irb->current_basic_block); return &call_instruction->base; } static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *alloc_fn, IrInstruction *free_fn) { IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope, - old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline, is_async, async_allocator); + old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline, is_async, async_allocator, alloc_fn, free_fn); ir_link_new_instruction(new_instruction, old_instruction); return new_instruction; } @@ -2495,8 +2501,9 @@ static IrInstruction *ir_build_cancel(IrBuilder *irb, Scope *scope, AstNode *sou return &instruction->base; } -static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node) { +static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node, ImplicitAllocatorId id) { IrInstructionGetImplicitAllocator *instruction = ir_build_instruction(irb, scope, source_node); + instruction->id = id; return &instruction->base; } @@ -3970,7 +3977,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo } FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever; - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr, nullptr, nullptr); } case BuiltinFnIdTypeId: { @@ -4106,15 +4113,25 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node bool is_async = node->data.fn_call_expr.is_async; IrInstruction *async_allocator = nullptr; + IrInstruction *alloc_fn = nullptr; + IrInstruction *free_fn = nullptr; if (is_async) { if (node->data.fn_call_expr.async_allocator) { async_allocator = ir_gen_node(irb, node->data.fn_call_expr.async_allocator, scope); if (async_allocator == irb->codegen->invalid_instruction) return async_allocator; + + Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); + IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, async_allocator, alloc_field_name); + alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); + + Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); + IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, async_allocator, free_field_name); + free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); } } - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator, alloc_fn, free_fn); } static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) { @@ -6106,20 +6123,16 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_set_cursor_at_end_and_append_block(irb, dyn_alloc_block); IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); - irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); - Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); - IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, - alloc_field_name); - IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); - IrInstruction *alignment = ir_build_const_u29(irb, scope, node, - get_coro_frame_align_bytes(irb->codegen)); + irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdContext); + IrInstruction *alloc_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdAlloc); + IrInstruction *alignment = ir_build_const_u29(irb, scope, node, get_coro_frame_align_bytes(irb->codegen)); size_t arg_count = 3; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self args[1] = coro_size; // byte_count args[2] = alignment; // alignment IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, - FnInlineAuto, false, nullptr); + FnInlineAuto, false, nullptr, nullptr, nullptr); IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result); IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); @@ -6237,15 +6250,12 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_build_cond_br(irb, scope, node, coro_need_dyn_alloc, dyn_free_block, end_free_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, dyn_free_block); - Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); - IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, - free_field_name); - IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); + IrInstruction *free_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdFree); size_t arg_count = 2; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self args[1] = ir_build_load_ptr(irb, scope, node, coro_unwrapped_mem_ptr); // old_mem - ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr); + ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr, nullptr, nullptr); ir_build_br(irb, scope, node, end_free_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, end_free_block); @@ -11266,7 +11276,7 @@ static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira, return ira->codegen->builtin_types.entry_type; } -IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr) { +IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr, ImplicitAllocatorId id) { FnTableEntry *parent_fn_entry = exec_fn_entry(ira->new_irb.exec); if (parent_fn_entry == nullptr) { ir_add_error(ira, source_instr, buf_sprintf("no implicit allocator available")); @@ -11280,27 +11290,39 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i } assert(parent_fn_type->async_allocator_type != nullptr); - IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node); - result->value.type = parent_fn_type->async_allocator_type; + IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node, id); + switch (id) { + case ImplicitAllocatorIdContext: + result->value.type = parent_fn_type->async_allocator_type; + break; + case ImplicitAllocatorIdAlloc: + { + assert(parent_fn_type->async_allocator_type->id == TypeTableEntryIdPointer); + TypeTableEntry *struct_type = parent_fn_type->async_allocator_type->data.pointer.child_type; + TypeStructField *alloc_fn_field = find_struct_type_field(struct_type, buf_create_from_str(ASYNC_ALLOC_FIELD_NAME)); + assert(alloc_fn_field->type_entry->id == TypeTableEntryIdFn); + result->value.type = alloc_fn_field->type_entry; + break; + } + case ImplicitAllocatorIdFree: + { + assert(parent_fn_type->async_allocator_type->id == TypeTableEntryIdPointer); + TypeTableEntry *struct_type = parent_fn_type->async_allocator_type->data.pointer.child_type; + TypeStructField *free_fn_field = find_struct_type_field(struct_type, buf_create_from_str(ASYNC_FREE_FIELD_NAME)); + assert(free_fn_field->type_entry->id == TypeTableEntryIdFn); + result->value.type = free_fn_field->type_entry; + break; + } + } return result; } static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, FnTableEntry *fn_entry, TypeTableEntry *fn_type, - IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst) + IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst, IrInstruction *alloc_fn, IrInstruction *free_fn) { - Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); - //Buf *free_field_name = buf_create_from_str("freeFn"); assert(async_allocator_inst->value.type->id == TypeTableEntryIdPointer); - TypeTableEntry *container_type = async_allocator_inst->value.type->data.pointer.child_type; - IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, alloc_field_name, &call_instruction->base, - async_allocator_inst, container_type); - if (type_is_invalid(field_ptr_inst->value.type)) { - return ira->codegen->invalid_instruction; - } - TypeTableEntry *ptr_to_alloc_fn_type = field_ptr_inst->value.type; - assert(ptr_to_alloc_fn_type->id == TypeTableEntryIdPointer); - TypeTableEntry *alloc_fn_type = ptr_to_alloc_fn_type->data.pointer.child_type; + TypeTableEntry *alloc_fn_type = alloc_fn->value.type; if (alloc_fn_type->id != TypeTableEntryIdFn) { ir_add_error(ira, &call_instruction->base, buf_sprintf("expected allocation function, found '%s'", buf_ptr(&alloc_fn_type->name))); @@ -11319,7 +11341,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c TypeTableEntry *async_return_type = get_error_union_type(ira->codegen, alloc_fn_error_set_type, promise_type); IrInstruction *result = ir_build_call(&ira->new_irb, call_instruction->base.scope, call_instruction->base.source_node, - fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst); + fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst, alloc_fn, free_fn); result->value.type = async_return_type; return result; } @@ -11842,7 +11864,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } IrInstruction *uncasted_async_allocator_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdContext); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; } else { @@ -11886,15 +11908,25 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count; if (call_instruction->is_async) { - IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry, fn_ref, casted_args, impl_param_count, async_allocator_inst); + IrInstruction *alloc_fn = call_instruction->alloc_fn->other; + if (type_is_invalid(alloc_fn->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *free_fn = call_instruction->free_fn->other; + if (type_is_invalid(free_fn->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry, fn_ref, casted_args, impl_param_count, + async_allocator_inst, alloc_fn, free_fn); ir_link_new_instruction(result, &call_instruction->base); ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); } + assert(async_allocator_inst == nullptr); IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline, - call_instruction->is_async, async_allocator_inst); + call_instruction->is_async, nullptr, nullptr, nullptr); ir_add_alloca(ira, new_call_instruction, return_type); @@ -11961,20 +11993,40 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal if (call_instruction->is_async) { IrInstruction *uncasted_async_allocator_inst; + IrInstruction *uncasted_alloc_fn_inst; + IrInstruction *uncasted_free_fn_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdContext); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; + + uncasted_alloc_fn_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdAlloc); + if (type_is_invalid(uncasted_alloc_fn_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + uncasted_free_fn_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdFree); + if (type_is_invalid(uncasted_free_fn_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; } else { uncasted_async_allocator_inst = call_instruction->async_allocator->other; if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; + + uncasted_alloc_fn_inst = call_instruction->alloc_fn->other; + if (type_is_invalid(uncasted_alloc_fn_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + uncasted_free_fn_inst = call_instruction->free_fn->other; + if (type_is_invalid(uncasted_free_fn_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + } IrInstruction *async_allocator_inst = ir_implicit_cast(ira, uncasted_async_allocator_inst, fn_type_id->async_allocator_type); if (type_is_invalid(async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; - IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref, casted_args, call_param_count, async_allocator_inst); + IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref, casted_args, call_param_count, + async_allocator_inst, uncasted_alloc_fn_inst, uncasted_free_fn_inst); ir_link_new_instruction(result, &call_instruction->base); ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); @@ -11982,7 +12034,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, - fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr); + fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr, nullptr, nullptr); ir_add_alloca(ira, new_call_instruction, return_type); return ir_finish_anal(ira, return_type); @@ -17222,7 +17274,7 @@ static TypeTableEntry *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstr } static TypeTableEntry *ir_analyze_instruction_get_implicit_allocator(IrAnalyze *ira, IrInstructionGetImplicitAllocator *instruction) { - IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base); + IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base, instruction->id); ir_link_new_instruction(result, &instruction->base); return result->value.type; } diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 2e367672a5..1314123110 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1027,7 +1027,19 @@ static void ir_print_cancel(IrPrint *irp, IrInstructionCancel *instruction) { } static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplicitAllocator *instruction) { - fprintf(irp->f, "@getImplicitAllocator()"); + fprintf(irp->f, "@getImplicitAllocator("); + switch (instruction->id) { + case ImplicitAllocatorIdContext: + fprintf(irp->f, "Context"); + break; + case ImplicitAllocatorIdAlloc: + fprintf(irp->f, "Alloc"); + break; + case ImplicitAllocatorIdFree: + fprintf(irp->f, "Free"); + break; + } + fprintf(irp->f, ")"); } static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) { diff --git a/std/mem.zig b/std/mem.zig index 2adb647ef6..07521bfcb8 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -116,22 +116,6 @@ pub const Allocator = struct { const non_const_ptr = @intToPtr(&u8, @ptrToInt(bytes.ptr)); self.freeFn(self, non_const_ptr[0..bytes.len]); } - - pub const AsyncAllocator = struct { - allocator: &Allocator, - - fn alloc(self: &const AsyncAllocator, byte_count: usize, alignment: u29) Error![]u8 { - return self.allocator.allocFn(self.allocator, byte_count, alignment); - } - - fn free(self: &const AsyncAllocator, old_mem: []u8) void { - return self.allocator.freeFn(self.allocator, old_mem); - } - }; - - fn toAsync(self: &Allocator) AsyncAllocator { - return AsyncAllocator { .allocator = self }; - } }; /// Copy all of source into dest at position 0. -- cgit v1.2.3 From c2f5634fb3df51622cf74f23b4ae0d4a7d2bbbe9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Feb 2018 10:00:07 -0500 Subject: another llvm workaround for getelementptr --- src/all_types.hpp | 11 ++++++++- src/analyze.cpp | 8 +++++++ src/codegen.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 6 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 4220523126..52be88496b 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1461,6 +1461,14 @@ struct LinkLib { bool provided_explicitly; }; +struct WorkaroundStructGEPId { + LLVMTypeRef struct_ptr_type; + uint32_t index; +}; + +uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x); +bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b); + struct CodeGen { LLVMModuleRef module; ZigList errors; @@ -1491,7 +1499,7 @@ struct CodeGen { HashMap exported_symbol_names; HashMap external_prototypes; HashMap string_literals_table; - + HashMap workaround_struct_gep_table; ZigList import_queue; size_t import_queue_index; @@ -1603,6 +1611,7 @@ struct CodeGen { LLVMValueRef cur_ret_ptr; LLVMValueRef cur_fn_val; LLVMValueRef cur_err_ret_trace_val; + bool cur_workaround_gep_on; bool c_want_stdint; bool c_want_stdbool; AstNode *root_export_decl; diff --git a/src/analyze.cpp b/src/analyze.cpp index 26924cc7db..9c5e4ffdde 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -5821,3 +5821,11 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) { uint32_t get_coro_frame_align_bytes(CodeGen *g) { return g->pointer_size_bytes * 2; } + +uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x) { + return ptr_hash(x.struct_ptr_type) ^ x.index; +} + +bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b) { + return a.struct_ptr_type == b.struct_ptr_type && a.index == b.index; +} diff --git a/src/codegen.cpp b/src/codegen.cpp index 21fc28e4af..54ad1fe311 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out g->exported_symbol_names.init(8); g->external_prototypes.init(8); g->string_literals_table.init(16); + g->workaround_struct_gep_table.init(8); g->is_test_build = false; g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib); buf_resize(&g->global_asm, 0); @@ -2781,6 +2782,52 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } } +static LLVMValueRef get_workaround_struct_gep_fn_val(CodeGen *g, LLVMTypeRef struct_ptr_type, uint32_t index) { + WorkaroundStructGEPId hash_id = {struct_ptr_type, index}; + auto existing_entry = g->workaround_struct_gep_table.maybe_get(hash_id); + if (existing_entry) + return existing_entry->value; + + LLVMTypeRef arg_types[] = { + struct_ptr_type, + }; + LLVMTypeRef result_type = LLVMStructGetTypeAtIndex(LLVMGetElementType(struct_ptr_type), index); + LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(result_type, 0), arg_types, 1, false); + + Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_workaround_llvm_struct_gep"), false); + LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref); + LLVMSetLinkage(fn_val, LLVMInternalLinkage); + LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified)); + addLLVMFnAttr(fn_val, "nounwind"); + addLLVMArgAttr(fn_val, (unsigned)0, "nonnull"); + + LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry"); + LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder); + LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder); + LLVMPositionBuilderAtEnd(g->builder, entry_block); + ZigLLVMClearCurrentDebugLocation(g->builder); + + LLVMValueRef result = LLVMBuildStructGEP(g->builder, LLVMGetParam(fn_val, 0), index, ""); + LLVMBuildRet(g->builder, result); + + LLVMPositionBuilderAtEnd(g->builder, prev_block); + LLVMSetCurrentDebugLocation(g->builder, prev_debug_location); + + g->workaround_struct_gep_table.put(hash_id, fn_val); + return fn_val; +} + +static LLVMValueRef gen_workaround_struct_gep(CodeGen *g, LLVMValueRef struct_ptr, uint32_t field_index) { + if (g->cur_workaround_gep_on) { + // We need to generate a normal StructGEP but due to llvm bugs we have to workaround it by + // putting the GEP in a function call + LLVMValueRef fn_val = get_workaround_struct_gep_fn_val(g, LLVMTypeOf(struct_ptr), field_index); + return LLVMBuildCall(g->builder, fn_val, &struct_ptr, 1, ""); + } else { + return LLVMBuildStructGEP(g->builder, struct_ptr, field_index, ""); + } +} + static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable, IrInstructionStructFieldPtr *instruction) { @@ -2799,7 +2846,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa } assert(field->gen_index != SIZE_MAX); - return LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, ""); + return gen_workaround_struct_gep(g, struct_ptr, field->gen_index); } static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executable, @@ -3655,7 +3702,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI LLVMValueRef err_val; if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); + LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { err_val = err_union_handle; @@ -3674,7 +3721,12 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type); if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); + LLVMValueRef err_val_ptr; + if (g->cur_workaround_gep_on) { + err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); + } else { + err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); + } return gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { return err_union_handle; @@ -3696,7 +3748,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu if (ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on && g->errors_by_index.length > 1) { LLVMValueRef err_val; if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); + LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { err_val = err_union_handle; @@ -3714,7 +3766,11 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu } if (type_has_bits(payload_type)) { - return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, ""); + if (g->cur_workaround_gep_on) { + return gen_workaround_struct_gep(g, err_union_handle, err_union_payload_index); + } else { + return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, ""); + } } else { return nullptr; } @@ -3934,6 +3990,7 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I coro_id, coro_mem_ptr, }; + g->cur_workaround_gep_on = false; return LLVMBuildCall(g->builder, get_coro_begin_fn_val(g), params, 2, ""); } @@ -5073,6 +5130,7 @@ static void do_code_gen(CodeGen *g) { LLVMValueRef fn = fn_llvm_value(g, fn_table_entry); g->cur_fn = fn_table_entry; g->cur_fn_val = fn; + g->cur_workaround_gep_on = fn_table_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; TypeTableEntry *return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type; if (handle_is_ptr(return_type)) { g->cur_ret_ptr = LLVMGetParam(fn, 0); -- cgit v1.2.3 From 6e2a67724c576420c1c0f005ccebdc6b0e708724 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Feb 2018 14:58:02 -0500 Subject: Revert "another llvm workaround for getelementptr" This reverts commit c2f5634fb3df51622cf74f23b4ae0d4a7d2bbbe9. It doesn't work. With this, LLVM moves the allocate fn call to after llvm.coro.begin --- src/all_types.hpp | 11 +-------- src/analyze.cpp | 8 ------- src/codegen.cpp | 68 ++++--------------------------------------------------- 3 files changed, 6 insertions(+), 81 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 52be88496b..4220523126 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1461,14 +1461,6 @@ struct LinkLib { bool provided_explicitly; }; -struct WorkaroundStructGEPId { - LLVMTypeRef struct_ptr_type; - uint32_t index; -}; - -uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x); -bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b); - struct CodeGen { LLVMModuleRef module; ZigList errors; @@ -1499,7 +1491,7 @@ struct CodeGen { HashMap exported_symbol_names; HashMap external_prototypes; HashMap string_literals_table; - HashMap workaround_struct_gep_table; + ZigList import_queue; size_t import_queue_index; @@ -1611,7 +1603,6 @@ struct CodeGen { LLVMValueRef cur_ret_ptr; LLVMValueRef cur_fn_val; LLVMValueRef cur_err_ret_trace_val; - bool cur_workaround_gep_on; bool c_want_stdint; bool c_want_stdbool; AstNode *root_export_decl; diff --git a/src/analyze.cpp b/src/analyze.cpp index 9c5e4ffdde..26924cc7db 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -5821,11 +5821,3 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) { uint32_t get_coro_frame_align_bytes(CodeGen *g) { return g->pointer_size_bytes * 2; } - -uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x) { - return ptr_hash(x.struct_ptr_type) ^ x.index; -} - -bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b) { - return a.struct_ptr_type == b.struct_ptr_type && a.index == b.index; -} diff --git a/src/codegen.cpp b/src/codegen.cpp index 54ad1fe311..21fc28e4af 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -88,7 +88,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out g->exported_symbol_names.init(8); g->external_prototypes.init(8); g->string_literals_table.init(16); - g->workaround_struct_gep_table.init(8); g->is_test_build = false; g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib); buf_resize(&g->global_asm, 0); @@ -2782,52 +2781,6 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } } -static LLVMValueRef get_workaround_struct_gep_fn_val(CodeGen *g, LLVMTypeRef struct_ptr_type, uint32_t index) { - WorkaroundStructGEPId hash_id = {struct_ptr_type, index}; - auto existing_entry = g->workaround_struct_gep_table.maybe_get(hash_id); - if (existing_entry) - return existing_entry->value; - - LLVMTypeRef arg_types[] = { - struct_ptr_type, - }; - LLVMTypeRef result_type = LLVMStructGetTypeAtIndex(LLVMGetElementType(struct_ptr_type), index); - LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(result_type, 0), arg_types, 1, false); - - Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_workaround_llvm_struct_gep"), false); - LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref); - LLVMSetLinkage(fn_val, LLVMInternalLinkage); - LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified)); - addLLVMFnAttr(fn_val, "nounwind"); - addLLVMArgAttr(fn_val, (unsigned)0, "nonnull"); - - LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry"); - LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder); - LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder); - LLVMPositionBuilderAtEnd(g->builder, entry_block); - ZigLLVMClearCurrentDebugLocation(g->builder); - - LLVMValueRef result = LLVMBuildStructGEP(g->builder, LLVMGetParam(fn_val, 0), index, ""); - LLVMBuildRet(g->builder, result); - - LLVMPositionBuilderAtEnd(g->builder, prev_block); - LLVMSetCurrentDebugLocation(g->builder, prev_debug_location); - - g->workaround_struct_gep_table.put(hash_id, fn_val); - return fn_val; -} - -static LLVMValueRef gen_workaround_struct_gep(CodeGen *g, LLVMValueRef struct_ptr, uint32_t field_index) { - if (g->cur_workaround_gep_on) { - // We need to generate a normal StructGEP but due to llvm bugs we have to workaround it by - // putting the GEP in a function call - LLVMValueRef fn_val = get_workaround_struct_gep_fn_val(g, LLVMTypeOf(struct_ptr), field_index); - return LLVMBuildCall(g->builder, fn_val, &struct_ptr, 1, ""); - } else { - return LLVMBuildStructGEP(g->builder, struct_ptr, field_index, ""); - } -} - static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable, IrInstructionStructFieldPtr *instruction) { @@ -2846,7 +2799,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa } assert(field->gen_index != SIZE_MAX); - return gen_workaround_struct_gep(g, struct_ptr, field->gen_index); + return LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, ""); } static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executable, @@ -3702,7 +3655,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI LLVMValueRef err_val; if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { err_val = err_union_handle; @@ -3721,12 +3674,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type); if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr; - if (g->cur_workaround_gep_on) { - err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); - } else { - err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); - } + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); return gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { return err_union_handle; @@ -3748,7 +3696,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu if (ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on && g->errors_by_index.length > 1) { LLVMValueRef err_val; if (type_has_bits(payload_type)) { - LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index); + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, ""); err_val = gen_load_untyped(g, err_val_ptr, 0, false, ""); } else { err_val = err_union_handle; @@ -3766,11 +3714,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu } if (type_has_bits(payload_type)) { - if (g->cur_workaround_gep_on) { - return gen_workaround_struct_gep(g, err_union_handle, err_union_payload_index); - } else { - return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, ""); - } + return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, ""); } else { return nullptr; } @@ -3990,7 +3934,6 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I coro_id, coro_mem_ptr, }; - g->cur_workaround_gep_on = false; return LLVMBuildCall(g->builder, get_coro_begin_fn_val(g), params, 2, ""); } @@ -5130,7 +5073,6 @@ static void do_code_gen(CodeGen *g) { LLVMValueRef fn = fn_llvm_value(g, fn_table_entry); g->cur_fn = fn_table_entry; g->cur_fn_val = fn; - g->cur_workaround_gep_on = fn_table_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; TypeTableEntry *return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type; if (handle_is_ptr(return_type)) { g->cur_ret_ptr = LLVMGetParam(fn, 0); -- cgit v1.2.3 From 132e604aa399a3bcb91996e550cf8972bd88422c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Feb 2018 17:12:53 -0500 Subject: llvm coroutine workaround: sret functions return sret pointer --- src/analyze.cpp | 5 ++++- src/codegen.cpp | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 26924cc7db..09c5566c5f 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1026,7 +1026,10 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { gen_param_index += 1; // after the gen_param_index += 1 because 0 is the return type param_di_types[gen_param_index] = gen_type->di_type; - gen_return_type = g->builtin_types.entry_void; + + // as a workaround for LLVM coroutines not understanding instruction dependencies, + // we return the sret pointer argument instead of returning void + gen_return_type = gen_type; } else { gen_return_type = fn_type_id->return_type; } diff --git a/src/codegen.cpp b/src/codegen.cpp index 21fc28e4af..9f69f29b80 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -547,7 +547,10 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { } else if (handle_is_ptr(return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc)) { - addLLVMArgAttr(fn_table_entry->llvm_value, 0, "sret"); + // We do not add the sret attribute, because it would require the return type to be void, + // and we want the return value to return the sret pointer, to work around LLVM Coroutine + // transformation passes not understanding the data dependency. + //addLLVMArgAttr(fn_table_entry->llvm_value, 0, "sret"); addLLVMArgAttr(fn_table_entry->llvm_value, 0, "nonnull"); } @@ -1616,7 +1619,9 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns if (calling_convention_does_first_arg_return(g->cur_fn->type_entry->data.fn.fn_type_id.cc)) { assert(g->cur_ret_ptr); gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value); - LLVMBuildRetVoid(g->builder); + // as a workaround for LLVM coroutines not understanding instruction dependencies, + // we return the sret pointer argument instead of returning void + LLVMBuildRet(g->builder, g->cur_ret_ptr); } else { LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, ""); LLVMBuildRet(g->builder, by_val_value); @@ -2775,7 +2780,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } else if (!ret_has_bits) { return nullptr; } else if (first_arg_ret) { - return instruction->tmp_ptr; + // instead of returning instruction->tmp_ptr here, we trust that the function returned the first arg. + // this is a workaround for llvm coroutines not understanding the data dependency + return result; } else { return result; } -- cgit v1.2.3 From 138d6f909321fb4fddeaa172357336c384c64eda Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Feb 2018 17:46:13 -0500 Subject: revert workaround for alloc and free as coro params reverts 4ac6c4d6bfb8f7ada2799ddb5ce3a9797be0518d the workaround didn't work --- src/all_types.hpp | 10 ----- src/analyze.cpp | 34 ++------------ src/codegen.cpp | 30 ++++--------- src/ir.cpp | 132 +++++++++++++++++------------------------------------- src/ir_print.cpp | 14 +----- 5 files changed, 55 insertions(+), 165 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 4220523126..5073ffaceb 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2210,8 +2210,6 @@ struct IrInstructionCall { bool is_async; IrInstruction *async_allocator; - IrInstruction *alloc_fn; - IrInstruction *free_fn; }; struct IrInstructionConst { @@ -2852,16 +2850,8 @@ struct IrInstructionCancel { IrInstruction *target; }; -enum ImplicitAllocatorId { - ImplicitAllocatorIdContext, - ImplicitAllocatorIdAlloc, - ImplicitAllocatorIdFree, -}; - struct IrInstructionGetImplicitAllocator { IrInstruction base; - - ImplicitAllocatorId id; }; struct IrInstructionCoroId { diff --git a/src/analyze.cpp b/src/analyze.cpp index 09c5566c5f..f95c9396cb 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1006,13 +1006,13 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { fn_type_id->return_type->id == TypeTableEntryIdErrorSet); // +1 for maybe making the first argument the return value // +1 for maybe first argument the error return trace - // +4 for maybe arguments async allocator and error code pointer - LLVMTypeRef *gen_param_types = allocate(6 + fn_type_id->param_count); + // +2 for maybe arguments async allocator and error code pointer + LLVMTypeRef *gen_param_types = allocate(4 + fn_type_id->param_count); // +1 because 0 is the return type and // +1 for maybe making first arg ret val and // +1 for maybe first argument the error return trace - // +4 for maybe arguments async allocator and error code pointer - ZigLLVMDIType **param_di_types = allocate(7 + fn_type_id->param_count); + // +2 for maybe arguments async allocator and error code pointer + ZigLLVMDIType **param_di_types = allocate(5 + fn_type_id->param_count); param_di_types[0] = fn_type_id->return_type->di_type; size_t gen_param_index = 0; TypeTableEntry *gen_return_type; @@ -1052,32 +1052,6 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { param_di_types[gen_param_index] = gen_type->di_type; } - { - // async alloc fn param - assert(fn_type_id->async_allocator_type->id == TypeTableEntryIdPointer); - TypeTableEntry *struct_type = fn_type_id->async_allocator_type->data.pointer.child_type; - TypeStructField *alloc_fn_field = find_struct_type_field(struct_type, buf_create_from_str("allocFn")); - assert(alloc_fn_field->type_entry->id == TypeTableEntryIdFn); - TypeTableEntry *gen_type = alloc_fn_field->type_entry; - gen_param_types[gen_param_index] = gen_type->type_ref; - gen_param_index += 1; - // after the gen_param_index += 1 because 0 is the return type - param_di_types[gen_param_index] = gen_type->di_type; - } - - { - // async free fn param - assert(fn_type_id->async_allocator_type->id == TypeTableEntryIdPointer); - TypeTableEntry *struct_type = fn_type_id->async_allocator_type->data.pointer.child_type; - TypeStructField *free_fn_field = find_struct_type_field(struct_type, buf_create_from_str("freeFn")); - assert(free_fn_field->type_entry->id == TypeTableEntryIdFn); - TypeTableEntry *gen_type = free_fn_field->type_entry; - gen_param_types[gen_param_index] = gen_type->type_ref; - gen_param_index += 1; - // after the gen_param_index += 1 because 0 is the return type - param_di_types[gen_param_index] = gen_type->di_type; - } - { // error code pointer TypeTableEntry *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); diff --git a/src/codegen.cpp b/src/codegen.cpp index 9f69f29b80..7fda1eaa3f 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2673,15 +2673,15 @@ static bool get_prefix_arg_err_ret_stack(CodeGen *g, TypeTableEntry *src_return_ } static size_t get_async_allocator_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { - // 0 1 2 3 4 5 - // err_ret_stack allocator_ptr alloc free err_code other_args... + // 0 1 2 3 + // err_ret_stack allocator_ptr err_code other_args... return get_prefix_arg_err_ret_stack(g, src_return_type) ? 1 : 0; } static size_t get_async_err_code_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { - // 0 1 2 3 4 5 - // err_ret_stack allocator_ptr alloc free err_code other_args... - return 3 + get_async_allocator_arg_index(g, src_return_type); + // 0 1 2 3 + // err_ret_stack allocator_ptr err_code other_args... + return 1 + get_async_allocator_arg_index(g, src_return_type); } static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) { @@ -2704,8 +2704,8 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc); bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); - // +4 for the async args - size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0) + 4; + // +2 for the async args + size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0) + 2; bool is_var_args = fn_type_id->is_var_args; LLVMValueRef *gen_param_values = allocate(actual_param_count); size_t gen_param_index = 0; @@ -2721,12 +2721,6 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->async_allocator); gen_param_index += 1; - gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->alloc_fn); - gen_param_index += 1; - - gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->free_fn); - gen_param_index += 1; - LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, ""); LLVMBuildStore(g->builder, LLVMConstNull(g->builtin_types.entry_global_error_set->type_ref), err_val_ptr); gen_param_values[gen_param_index] = err_val_ptr; @@ -3308,15 +3302,7 @@ static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *e { TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; size_t allocator_arg_index = get_async_allocator_arg_index(g, src_return_type); - switch (instruction->id) { - case ImplicitAllocatorIdContext: - return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 0); - case ImplicitAllocatorIdAlloc: - return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 1); - case ImplicitAllocatorIdFree: - return LLVMGetParam(g->cur_fn_val, allocator_arg_index + 2); - } - zig_unreachable(); + return LLVMGetParam(g->cur_fn_val, allocator_arg_index); } static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { diff --git a/src/ir.cpp b/src/ir.cpp index 00a7233897..9d213d1ddb 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1066,7 +1066,7 @@ static IrInstruction *ir_build_union_field_ptr_from(IrBuilder *irb, IrInstructio static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *alloc_fn, IrInstruction *free_fn) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) { IrInstructionCall *call_instruction = ir_build_instruction(irb, scope, source_node); call_instruction->fn_entry = fn_entry; @@ -1077,8 +1077,6 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc call_instruction->arg_count = arg_count; call_instruction->is_async = is_async; call_instruction->async_allocator = async_allocator; - call_instruction->alloc_fn = alloc_fn; - call_instruction->free_fn = free_fn; if (fn_ref) ir_ref_instruction(fn_ref, irb->current_basic_block); @@ -1086,20 +1084,16 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc ir_ref_instruction(args[i], irb->current_basic_block); if (async_allocator) ir_ref_instruction(async_allocator, irb->current_basic_block); - if (alloc_fn) - ir_ref_instruction(alloc_fn, irb->current_basic_block); - if (free_fn) - ir_ref_instruction(free_fn, irb->current_basic_block); return &call_instruction->base; } static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction, FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args, - bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *alloc_fn, IrInstruction *free_fn) + bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator) { IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope, - old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline, is_async, async_allocator, alloc_fn, free_fn); + old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, fn_inline, is_async, async_allocator); ir_link_new_instruction(new_instruction, old_instruction); return new_instruction; } @@ -2501,9 +2495,8 @@ static IrInstruction *ir_build_cancel(IrBuilder *irb, Scope *scope, AstNode *sou return &instruction->base; } -static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node, ImplicitAllocatorId id) { +static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node) { IrInstructionGetImplicitAllocator *instruction = ir_build_instruction(irb, scope, source_node); - instruction->id = id; return &instruction->base; } @@ -3977,7 +3970,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo } FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever; - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr, nullptr, nullptr); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr); } case BuiltinFnIdTypeId: { @@ -4113,25 +4106,15 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node bool is_async = node->data.fn_call_expr.is_async; IrInstruction *async_allocator = nullptr; - IrInstruction *alloc_fn = nullptr; - IrInstruction *free_fn = nullptr; if (is_async) { if (node->data.fn_call_expr.async_allocator) { async_allocator = ir_gen_node(irb, node->data.fn_call_expr.async_allocator, scope); if (async_allocator == irb->codegen->invalid_instruction) return async_allocator; - - Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); - IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, async_allocator, alloc_field_name); - alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); - - Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); - IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, async_allocator, free_field_name); - free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); } } - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator, alloc_fn, free_fn); + return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator); } static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) { @@ -6113,16 +6096,20 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr); coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr); IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); - irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdContext); - IrInstruction *alloc_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdAlloc); - IrInstruction *alignment = ir_build_const_u29(irb, scope, node, get_coro_frame_align_bytes(irb->codegen)); + irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); + Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); + IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, + alloc_field_name); + IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); + IrInstruction *alignment = ir_build_const_u29(irb, scope, node, + get_coro_frame_align_bytes(irb->codegen)); size_t arg_count = 3; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self args[1] = coro_size; // byte_count args[2] = alignment; // alignment IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, - FnInlineAuto, false, nullptr, nullptr, nullptr); + FnInlineAuto, false, nullptr); IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result); IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); @@ -6216,12 +6203,15 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec incoming_values[1] = const_bool_true; IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values); - IrInstruction *free_fn = ir_build_get_implicit_allocator(irb, scope, node, ImplicitAllocatorIdFree); + Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); + IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, + free_field_name); + IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); size_t arg_count = 2; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self args[1] = ir_build_load_ptr(irb, scope, node, coro_unwrapped_mem_ptr); // old_mem - ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr, nullptr, nullptr); + ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr); IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume"); IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "Return"); @@ -11240,7 +11230,7 @@ static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira, return ira->codegen->builtin_types.entry_type; } -IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr, ImplicitAllocatorId id) { +IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr) { FnTableEntry *parent_fn_entry = exec_fn_entry(ira->new_irb.exec); if (parent_fn_entry == nullptr) { ir_add_error(ira, source_instr, buf_sprintf("no implicit allocator available")); @@ -11254,39 +11244,27 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i } assert(parent_fn_type->async_allocator_type != nullptr); - IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node, id); - switch (id) { - case ImplicitAllocatorIdContext: - result->value.type = parent_fn_type->async_allocator_type; - break; - case ImplicitAllocatorIdAlloc: - { - assert(parent_fn_type->async_allocator_type->id == TypeTableEntryIdPointer); - TypeTableEntry *struct_type = parent_fn_type->async_allocator_type->data.pointer.child_type; - TypeStructField *alloc_fn_field = find_struct_type_field(struct_type, buf_create_from_str(ASYNC_ALLOC_FIELD_NAME)); - assert(alloc_fn_field->type_entry->id == TypeTableEntryIdFn); - result->value.type = alloc_fn_field->type_entry; - break; - } - case ImplicitAllocatorIdFree: - { - assert(parent_fn_type->async_allocator_type->id == TypeTableEntryIdPointer); - TypeTableEntry *struct_type = parent_fn_type->async_allocator_type->data.pointer.child_type; - TypeStructField *free_fn_field = find_struct_type_field(struct_type, buf_create_from_str(ASYNC_FREE_FIELD_NAME)); - assert(free_fn_field->type_entry->id == TypeTableEntryIdFn); - result->value.type = free_fn_field->type_entry; - break; - } - } + IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node); + result->value.type = parent_fn_type->async_allocator_type; return result; } static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, FnTableEntry *fn_entry, TypeTableEntry *fn_type, - IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst, IrInstruction *alloc_fn, IrInstruction *free_fn) + IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count, IrInstruction *async_allocator_inst) { + Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); + //Buf *free_field_name = buf_create_from_str("freeFn"); assert(async_allocator_inst->value.type->id == TypeTableEntryIdPointer); + TypeTableEntry *container_type = async_allocator_inst->value.type->data.pointer.child_type; + IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, alloc_field_name, &call_instruction->base, + async_allocator_inst, container_type); + if (type_is_invalid(field_ptr_inst->value.type)) { + return ira->codegen->invalid_instruction; + } + TypeTableEntry *ptr_to_alloc_fn_type = field_ptr_inst->value.type; + assert(ptr_to_alloc_fn_type->id == TypeTableEntryIdPointer); - TypeTableEntry *alloc_fn_type = alloc_fn->value.type; + TypeTableEntry *alloc_fn_type = ptr_to_alloc_fn_type->data.pointer.child_type; if (alloc_fn_type->id != TypeTableEntryIdFn) { ir_add_error(ira, &call_instruction->base, buf_sprintf("expected allocation function, found '%s'", buf_ptr(&alloc_fn_type->name))); @@ -11305,7 +11283,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c TypeTableEntry *async_return_type = get_error_union_type(ira->codegen, alloc_fn_error_set_type, promise_type); IrInstruction *result = ir_build_call(&ira->new_irb, call_instruction->base.scope, call_instruction->base.source_node, - fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst, alloc_fn, free_fn); + fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst); result->value.type = async_return_type; return result; } @@ -11828,7 +11806,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } IrInstruction *uncasted_async_allocator_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdContext); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; } else { @@ -11872,16 +11850,8 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count; if (call_instruction->is_async) { - IrInstruction *alloc_fn = call_instruction->alloc_fn->other; - if (type_is_invalid(alloc_fn->value.type)) - return ira->codegen->builtin_types.entry_invalid; - - IrInstruction *free_fn = call_instruction->free_fn->other; - if (type_is_invalid(free_fn->value.type)) - return ira->codegen->builtin_types.entry_invalid; - IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry, fn_ref, casted_args, impl_param_count, - async_allocator_inst, alloc_fn, free_fn); + async_allocator_inst); ir_link_new_instruction(result, &call_instruction->base); ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); @@ -11890,7 +11860,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal assert(async_allocator_inst == nullptr); IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline, - call_instruction->is_async, nullptr, nullptr, nullptr); + call_instruction->is_async, nullptr); ir_add_alloca(ira, new_call_instruction, return_type); @@ -11957,40 +11927,22 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal if (call_instruction->is_async) { IrInstruction *uncasted_async_allocator_inst; - IrInstruction *uncasted_alloc_fn_inst; - IrInstruction *uncasted_free_fn_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdContext); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; - - uncasted_alloc_fn_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdAlloc); - if (type_is_invalid(uncasted_alloc_fn_inst->value.type)) - return ira->codegen->builtin_types.entry_invalid; - - uncasted_free_fn_inst = ir_get_implicit_allocator(ira, &call_instruction->base, ImplicitAllocatorIdFree); - if (type_is_invalid(uncasted_free_fn_inst->value.type)) - return ira->codegen->builtin_types.entry_invalid; } else { uncasted_async_allocator_inst = call_instruction->async_allocator->other; if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; - uncasted_alloc_fn_inst = call_instruction->alloc_fn->other; - if (type_is_invalid(uncasted_alloc_fn_inst->value.type)) - return ira->codegen->builtin_types.entry_invalid; - - uncasted_free_fn_inst = call_instruction->free_fn->other; - if (type_is_invalid(uncasted_free_fn_inst->value.type)) - return ira->codegen->builtin_types.entry_invalid; - } IrInstruction *async_allocator_inst = ir_implicit_cast(ira, uncasted_async_allocator_inst, fn_type_id->async_allocator_type); if (type_is_invalid(async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref, casted_args, call_param_count, - async_allocator_inst, uncasted_alloc_fn_inst, uncasted_free_fn_inst); + async_allocator_inst); ir_link_new_instruction(result, &call_instruction->base); ir_add_alloca(ira, result, result->value.type); return ir_finish_anal(ira, result->value.type); @@ -11998,7 +11950,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base, - fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr, nullptr, nullptr); + fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr); ir_add_alloca(ira, new_call_instruction, return_type); return ir_finish_anal(ira, return_type); @@ -17238,7 +17190,7 @@ static TypeTableEntry *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstr } static TypeTableEntry *ir_analyze_instruction_get_implicit_allocator(IrAnalyze *ira, IrInstructionGetImplicitAllocator *instruction) { - IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base, instruction->id); + IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base); ir_link_new_instruction(result, &instruction->base); return result->value.type; } diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 1314123110..2e367672a5 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1027,19 +1027,7 @@ static void ir_print_cancel(IrPrint *irp, IrInstructionCancel *instruction) { } static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplicitAllocator *instruction) { - fprintf(irp->f, "@getImplicitAllocator("); - switch (instruction->id) { - case ImplicitAllocatorIdContext: - fprintf(irp->f, "Context"); - break; - case ImplicitAllocatorIdAlloc: - fprintf(irp->f, "Alloc"); - break; - case ImplicitAllocatorIdFree: - fprintf(irp->f, "Free"); - break; - } - fprintf(irp->f, ")"); + fprintf(irp->f, "@getImplicitAllocator()"); } static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) { -- cgit v1.2.3 From d24345386274e3abcbcc676fe65bda127c06ce8e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Feb 2018 17:47:18 -0500 Subject: Revert "llvm coroutine workaround: sret functions return sret pointer" This reverts commit 132e604aa399a3bcb91996e550cf8972bd88422c. this workaround didn't work either --- src/analyze.cpp | 5 +---- src/codegen.cpp | 13 +++---------- 2 files changed, 4 insertions(+), 14 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index f95c9396cb..ce9e99f8fa 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1026,10 +1026,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { gen_param_index += 1; // after the gen_param_index += 1 because 0 is the return type param_di_types[gen_param_index] = gen_type->di_type; - - // as a workaround for LLVM coroutines not understanding instruction dependencies, - // we return the sret pointer argument instead of returning void - gen_return_type = gen_type; + gen_return_type = g->builtin_types.entry_void; } else { gen_return_type = fn_type_id->return_type; } diff --git a/src/codegen.cpp b/src/codegen.cpp index 7fda1eaa3f..d650daa6cb 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -547,10 +547,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { } else if (handle_is_ptr(return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc)) { - // We do not add the sret attribute, because it would require the return type to be void, - // and we want the return value to return the sret pointer, to work around LLVM Coroutine - // transformation passes not understanding the data dependency. - //addLLVMArgAttr(fn_table_entry->llvm_value, 0, "sret"); + addLLVMArgAttr(fn_table_entry->llvm_value, 0, "sret"); addLLVMArgAttr(fn_table_entry->llvm_value, 0, "nonnull"); } @@ -1619,9 +1616,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns if (calling_convention_does_first_arg_return(g->cur_fn->type_entry->data.fn.fn_type_id.cc)) { assert(g->cur_ret_ptr); gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value); - // as a workaround for LLVM coroutines not understanding instruction dependencies, - // we return the sret pointer argument instead of returning void - LLVMBuildRet(g->builder, g->cur_ret_ptr); + LLVMBuildRetVoid(g->builder); } else { LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, ""); LLVMBuildRet(g->builder, by_val_value); @@ -2774,9 +2769,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr } else if (!ret_has_bits) { return nullptr; } else if (first_arg_ret) { - // instead of returning instruction->tmp_ptr here, we trust that the function returned the first arg. - // this is a workaround for llvm coroutines not understanding the data dependency - return result; + return instruction->tmp_ptr; } else { return result; } -- cgit v1.2.3 From 026aebf2ea567c15eebf9ddb9180f7d0e2ec7a9d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 04:01:22 -0500 Subject: another workaround for llvm coroutines this one doesn't work either --- src/all_types.hpp | 9 ++++ src/analyze.cpp | 10 ++-- src/analyze.hpp | 1 + src/codegen.cpp | 155 +++++++++++++++++++++++++++++++++++++++++++++++++----- src/ir.cpp | 87 ++++++++++++++++++------------ src/ir_print.cpp | 11 ++++ 6 files changed, 222 insertions(+), 51 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 5073ffaceb..9b4fa6d36c 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1634,6 +1634,7 @@ struct CodeGen { LLVMValueRef coro_free_fn_val; LLVMValueRef coro_resume_fn_val; LLVMValueRef coro_save_fn_val; + LLVMValueRef coro_alloc_helper_fn_val; bool error_during_imports; const char **clang_argv; @@ -2004,6 +2005,7 @@ enum IrInstructionId { IrInstructionIdCoroFree, IrInstructionIdCoroResume, IrInstructionIdCoroSave, + IrInstructionIdCoroAllocHelper, }; struct IrInstruction { @@ -2913,6 +2915,13 @@ struct IrInstructionCoroSave { IrInstruction *coro_handle; }; +struct IrInstructionCoroAllocHelper { + IrInstruction base; + + IrInstruction *alloc_fn; + IrInstruction *coro_size; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/analyze.cpp b/src/analyze.cpp index ce9e99f8fa..be01f6b5f8 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1001,9 +1001,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { bool first_arg_return = calling_convention_does_first_arg_return(fn_type_id->cc) && handle_is_ptr(fn_type_id->return_type); bool is_async = fn_type_id->cc == CallingConventionAsync; - bool prefix_arg_error_return_trace = g->have_err_ret_tracing && - (fn_type_id->return_type->id == TypeTableEntryIdErrorUnion || - fn_type_id->return_type->id == TypeTableEntryIdErrorSet); + bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id); // +1 for maybe making the first argument the return value // +1 for maybe first argument the error return trace // +2 for maybe arguments async allocator and error code pointer @@ -5795,3 +5793,9 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) { uint32_t get_coro_frame_align_bytes(CodeGen *g) { return g->pointer_size_bytes * 2; } + +bool fn_type_can_fail(FnTypeId *fn_type_id) { + TypeTableEntry *return_type = fn_type_id->return_type; + return return_type->id == TypeTableEntryIdErrorUnion || return_type->id == TypeTableEntryIdErrorSet || + fn_type_id->cc == CallingConventionAsync; +} diff --git a/src/analyze.hpp b/src/analyze.hpp index 926793c58a..068f321bfb 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -192,5 +192,6 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry); TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry); uint32_t get_coro_frame_align_bytes(CodeGen *g); +bool fn_type_can_fail(FnTypeId *fn_type_id); #endif diff --git a/src/codegen.cpp b/src/codegen.cpp index d650daa6cb..ec047ad9ec 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -412,10 +412,10 @@ static uint32_t get_err_ret_trace_arg_index(CodeGen *g, FnTableEntry *fn_table_e return UINT32_MAX; } TypeTableEntry *fn_type = fn_table_entry->type_entry; - TypeTableEntry *return_type = fn_type->data.fn.fn_type_id.return_type; - if (return_type->id != TypeTableEntryIdErrorUnion && return_type->id != TypeTableEntryIdErrorSet) { + if (!fn_type_can_fail(&fn_type->data.fn.fn_type_id)) { return UINT32_MAX; } + TypeTableEntry *return_type = fn_type->data.fn.fn_type_id.return_type; bool first_arg_ret = type_has_bits(return_type) && handle_is_ptr(return_type); return first_arg_ret ? 1 : 0; } @@ -2662,21 +2662,23 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI } } -static bool get_prefix_arg_err_ret_stack(CodeGen *g, TypeTableEntry *src_return_type) { +static bool get_prefix_arg_err_ret_stack(CodeGen *g, FnTypeId *fn_type_id) { return g->have_err_ret_tracing && - (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet); + (fn_type_id->return_type->id == TypeTableEntryIdErrorUnion || + fn_type_id->return_type->id == TypeTableEntryIdErrorSet || + fn_type_id->cc == CallingConventionAsync); } -static size_t get_async_allocator_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { +static size_t get_async_allocator_arg_index(CodeGen *g, FnTypeId *fn_type_id) { // 0 1 2 3 // err_ret_stack allocator_ptr err_code other_args... - return get_prefix_arg_err_ret_stack(g, src_return_type) ? 1 : 0; + return get_prefix_arg_err_ret_stack(g, fn_type_id) ? 1 : 0; } -static size_t get_async_err_code_arg_index(CodeGen *g, TypeTableEntry *src_return_type) { +static size_t get_async_err_code_arg_index(CodeGen *g, FnTypeId *fn_type_id) { // 0 1 2 3 // err_ret_stack allocator_ptr err_code other_args... - return 1 + get_async_allocator_arg_index(g, src_return_type); + return 1 + get_async_allocator_arg_index(g, fn_type_id); } static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) { @@ -2698,7 +2700,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) && calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc); - bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, src_return_type); + bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, fn_type_id); // +2 for the async args size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0) + 2; bool is_var_args = fn_type_id->is_var_args; @@ -2717,7 +2719,6 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr gen_param_index += 1; LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, ""); - LLVMBuildStore(g->builder, LLVMConstNull(g->builtin_types.entry_global_error_set->type_ref), err_val_ptr); gen_param_values[gen_param_index] = err_val_ptr; gen_param_index += 1; } @@ -3293,8 +3294,7 @@ static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrIns static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) { - TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; - size_t allocator_arg_index = get_async_allocator_arg_index(g, src_return_type); + size_t allocator_arg_index = get_async_allocator_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id); return LLVMGetParam(g->cur_fn_val, allocator_arg_index); } @@ -3926,8 +3926,7 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable, IrInstructionCoroAllocFail *instruction) { - TypeTableEntry *src_return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type; - size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, src_return_type); + size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id); LLVMValueRef err_code_ptr_val = LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index); LLVMValueRef err_code = ir_llvm_value(g, instruction->err_val); LLVMBuildStore(g->builder, err_code, err_code_ptr_val); @@ -3985,6 +3984,132 @@ static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, Ir return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, ""); } +static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) { + if (g->coro_alloc_helper_fn_val != nullptr) + return g->coro_alloc_fn_val; + + assert(fn_type->id == TypeTableEntryIdFn); + + TypeTableEntry *ptr_to_err_code_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); + + LLVMTypeRef alloc_raw_fn_type_ref = LLVMGetElementType(alloc_fn_type_ref); + LLVMTypeRef *alloc_fn_arg_types = allocate(LLVMCountParamTypes(alloc_raw_fn_type_ref)); + LLVMGetParamTypes(alloc_raw_fn_type_ref, alloc_fn_arg_types); + + ZigList arg_types = {}; + arg_types.append(alloc_fn_type_ref); + if (g->have_err_ret_tracing) { + arg_types.append(alloc_fn_arg_types[1]); + } + arg_types.append(alloc_fn_arg_types[g->have_err_ret_tracing ? 2 : 1]); + arg_types.append(ptr_to_err_code_type->type_ref); + arg_types.append(g->builtin_types.entry_usize->type_ref); + + LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), + arg_types.items, arg_types.length, false); + + Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_coro_alloc_helper"), false); + LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref); + LLVMSetLinkage(fn_val, LLVMInternalLinkage); + LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified)); + addLLVMFnAttr(fn_val, "nounwind"); + addLLVMArgAttr(fn_val, (unsigned)0, "nonnull"); + addLLVMArgAttr(fn_val, (unsigned)1, "nonnull"); + + LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder); + LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder); + FnTableEntry *prev_cur_fn = g->cur_fn; + LLVMValueRef prev_cur_fn_val = g->cur_fn_val; + + LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry"); + LLVMPositionBuilderAtEnd(g->builder, entry_block); + ZigLLVMClearCurrentDebugLocation(g->builder); + g->cur_fn = nullptr; + g->cur_fn_val = fn_val; + + LLVMValueRef sret_ptr = LLVMBuildAlloca(g->builder, LLVMGetElementType(alloc_fn_arg_types[0]), ""); + + size_t next_arg = 0; + LLVMValueRef alloc_fn_val = LLVMGetParam(fn_val, next_arg); + next_arg += 1; + + LLVMValueRef stack_trace_val; + if (g->have_err_ret_tracing) { + stack_trace_val = LLVMGetParam(fn_val, next_arg); + next_arg += 1; + } + + LLVMValueRef allocator_val = LLVMGetParam(fn_val, next_arg); + next_arg += 1; + LLVMValueRef err_code_ptr = LLVMGetParam(fn_val, next_arg); + next_arg += 1; + LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg); + next_arg += 1; + LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref, + 2 * g->pointer_size_bytes, false); + + ZigList args = {}; + args.append(sret_ptr); + if (g->have_err_ret_tracing) { + args.append(stack_trace_val); + } + args.append(allocator_val); + args.append(coro_size); + args.append(alignment_val); + ZigLLVMBuildCall(g->builder, alloc_fn_val, args.items, args.length, + get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, ""); + LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, sret_ptr, err_union_err_index, ""); + LLVMValueRef err_val = LLVMBuildLoad(g->builder, err_val_ptr, ""); + LLVMBuildStore(g->builder, err_val, err_code_ptr); + LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, LLVMConstNull(LLVMTypeOf(err_val)), ""); + LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(fn_val, "AllocOk"); + LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(fn_val, "AllocFail"); + LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block); + + LLVMPositionBuilderAtEnd(g->builder, ok_block); + LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, sret_ptr, err_union_payload_index, ""); + TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); + TypeTableEntry *slice_type = get_slice_type(g, u8_ptr_type); + size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index; + LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, payload_ptr, ptr_field_index, ""); + LLVMValueRef ptr_val = LLVMBuildLoad(g->builder, ptr_field_ptr, ""); + LLVMBuildRet(g->builder, ptr_val); + + LLVMPositionBuilderAtEnd(g->builder, fail_block); + LLVMBuildRet(g->builder, LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0))); + + g->cur_fn = prev_cur_fn; + g->cur_fn_val = prev_cur_fn_val; + LLVMPositionBuilderAtEnd(g->builder, prev_block); + LLVMSetCurrentDebugLocation(g->builder, prev_debug_location); + + g->coro_alloc_helper_fn_val = fn_val; + return fn_val; +} + +static LLVMValueRef ir_render_coro_alloc_helper(CodeGen *g, IrExecutable *executable, + IrInstructionCoroAllocHelper *instruction) +{ + LLVMValueRef alloc_fn = ir_llvm_value(g, instruction->alloc_fn); + LLVMValueRef coro_size = ir_llvm_value(g, instruction->coro_size); + LLVMValueRef fn_val = get_coro_alloc_helper_fn_val(g, LLVMTypeOf(alloc_fn), instruction->alloc_fn->value.type); + size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id); + size_t allocator_arg_index = get_async_allocator_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id); + + ZigList params = {}; + params.append(alloc_fn); + uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, g->cur_fn); + if (err_ret_trace_arg_index != UINT32_MAX) { + params.append(LLVMGetParam(g->cur_fn_val, err_ret_trace_arg_index)); + } + params.append(LLVMGetParam(g->cur_fn_val, allocator_arg_index)); + params.append(LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index)); + params.append(coro_size); + + return ZigLLVMBuildCall(g->builder, fn_val, params.items, params.length, + get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, ""); +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -4190,6 +4315,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction); case IrInstructionIdCoroSave: return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction); + case IrInstructionIdCoroAllocHelper: + return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction); } zig_unreachable(); } diff --git a/src/ir.cpp b/src/ir.cpp index 9d213d1ddb..d2c28582d7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -695,6 +695,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSave *) { return IrInstructionIdCoroSave; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocHelper *) { + return IrInstructionIdCoroAllocHelper; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -829,14 +833,6 @@ static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode return &const_instruction->base; } -static IrInstruction *ir_build_const_u29(IrBuilder *irb, Scope *scope, AstNode *source_node, uint32_t value) { - IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); - const_instruction->base.value.type = irb->codegen->builtin_types.entry_u29; - const_instruction->base.value.special = ConstValSpecialStatic; - bigint_init_unsigned(&const_instruction->base.value.data.x_bigint, value); - return &const_instruction->base; -} - static IrInstruction *ir_build_const_u8(IrBuilder *irb, Scope *scope, AstNode *source_node, uint8_t value) { IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); const_instruction->base.value.type = irb->codegen->builtin_types.entry_u8; @@ -2600,6 +2596,19 @@ static IrInstruction *ir_build_coro_save(IrBuilder *irb, Scope *scope, AstNode * return &instruction->base; } +static IrInstruction *ir_build_coro_alloc_helper(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *alloc_fn, IrInstruction *coro_size) +{ + IrInstructionCoroAllocHelper *instruction = ir_build_instruction(irb, scope, source_node); + instruction->alloc_fn = alloc_fn; + instruction->coro_size = coro_size; + + ir_ref_instruction(alloc_fn, irb->current_basic_block); + ir_ref_instruction(coro_size, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -6074,10 +6083,11 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; IrInstruction *u8_ptr_type; IrInstruction *const_bool_false; - IrInstruction *coro_unwrapped_mem_ptr; + IrInstruction *coro_size; IrInstruction *coro_id; IrInstruction *coro_promise_ptr; IrInstruction *coro_result_field_ptr; + IrInstruction *coro_mem_ptr; TypeTableEntry *return_type; Buf *result_ptr_field_name; if (is_async) { @@ -6095,39 +6105,25 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr); coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr); - IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); + coro_size = ir_build_coro_size(irb, scope, node); irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, alloc_field_name); IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); - IrInstruction *alignment = ir_build_const_u29(irb, scope, node, - get_coro_frame_align_bytes(irb->codegen)); - size_t arg_count = 3; - IrInstruction **args = allocate(arg_count); - args[0] = irb->exec->implicit_allocator_ptr; // self - args[1] = coro_size; // byte_count - args[2] = alignment; // alignment - IrInstruction *alloc_result = ir_build_call(irb, scope, node, nullptr, alloc_fn, arg_count, args, false, - FnInlineAuto, false, nullptr); - IrInstruction *alloc_result_ptr = ir_build_ref(irb, scope, node, alloc_result, true, false); - IrInstruction *alloc_result_is_err = ir_build_test_err(irb, scope, node, alloc_result); + IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, scope, node, alloc_fn, coro_size); + IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, scope, node, maybe_coro_mem_ptr); IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError"); IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, scope, "AllocOk"); - ir_build_cond_br(irb, scope, node, alloc_result_is_err, alloc_err_block, alloc_ok_block, const_bool_false); + ir_build_cond_br(irb, scope, node, alloc_result_is_ok, alloc_ok_block, alloc_err_block, const_bool_false); ir_set_cursor_at_end_and_append_block(irb, alloc_err_block); - IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, alloc_result_ptr); - ir_build_coro_alloc_fail(irb, scope, node, err_val); + IrInstruction *undef = ir_build_const_undefined(irb, scope, node); + ir_build_return(irb, scope, node, undef); ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); - coro_unwrapped_mem_ptr = ir_build_unwrap_err_payload(irb, scope, node, alloc_result_ptr, false); - Buf *ptr_field_name = buf_create_from_str("ptr"); - IrInstruction *coro_mem_ptr_field = ir_build_field_ptr(irb, scope, node, coro_unwrapped_mem_ptr, - ptr_field_name); - IrInstruction *coro_mem = ir_build_load_ptr(irb, scope, node, coro_mem_ptr_field); - - irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem); + coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, maybe_coro_mem_ptr); + irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem_ptr); Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME); irb->exec->coro_awaiter_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, @@ -6207,10 +6203,13 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, free_field_name); IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); + IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); + IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false); + IrInstruction *mem_slice = ir_build_slice(irb, scope, node, coro_mem_ptr_ref, zero, coro_size, false); size_t arg_count = 2; IrInstruction **args = allocate(arg_count); args[0] = irb->exec->implicit_allocator_ptr; // self - args[1] = ir_build_load_ptr(irb, scope, node, coro_unwrapped_mem_ptr); // old_mem + args[1] = mem_slice; // old_mem ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr); IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume"); @@ -11844,7 +11843,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type; - if (return_type->id == TypeTableEntryIdErrorSet || return_type->id == TypeTableEntryIdErrorUnion) { + if (fn_type_can_fail(&impl_fn->type_entry->data.fn.fn_type_id)) { parent_fn_entry->calls_errorable_function = true; } @@ -11870,7 +11869,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal FnTableEntry *parent_fn_entry = exec_fn_entry(ira->new_irb.exec); assert(fn_type_id->return_type != nullptr); assert(parent_fn_entry != nullptr); - if (fn_type_id->return_type->id == TypeTableEntryIdErrorSet || fn_type_id->return_type->id == TypeTableEntryIdErrorUnion) { + if (fn_type_can_fail(fn_type_id)) { parent_fn_entry->calls_errorable_function = true; } @@ -17274,6 +17273,23 @@ static TypeTableEntry *ir_analyze_instruction_coro_save(IrAnalyze *ira, IrInstru return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_coro_alloc_helper(IrAnalyze *ira, IrInstructionCoroAllocHelper *instruction) { + IrInstruction *alloc_fn = instruction->alloc_fn->other; + if (type_is_invalid(alloc_fn->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *coro_size = instruction->coro_size->other; + if (type_is_invalid(coro_size->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *result = ir_build_coro_alloc_helper(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, alloc_fn, coro_size); + ir_link_new_instruction(result, &instruction->base); + TypeTableEntry *u8_ptr_type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_u8, false); + result->value.type = get_maybe_type(ira->codegen, u8_ptr_type); + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { @@ -17501,6 +17517,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction); case IrInstructionIdCoroSave: return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction); + case IrInstructionIdCoroAllocHelper: + return ir_analyze_instruction_coro_alloc_helper(ira, (IrInstructionCoroAllocHelper *)instruction); } zig_unreachable(); } @@ -17624,6 +17642,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroEnd: case IrInstructionIdCoroResume: case IrInstructionIdCoroSave: + case IrInstructionIdCoroAllocHelper: return true; case IrInstructionIdPhi: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 2e367672a5..b9e02988c2 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1096,6 +1096,14 @@ static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction) fprintf(irp->f, ")"); } +static void ir_print_coro_alloc_helper(IrPrint *irp, IrInstructionCoroAllocHelper *instruction) { + fprintf(irp->f, "@coroAllocHelper("); + ir_print_other_instruction(irp, instruction->alloc_fn); + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->coro_size); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1452,6 +1460,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCoroSave: ir_print_coro_save(irp, (IrInstructionCoroSave *)instruction); break; + case IrInstructionIdCoroAllocHelper: + ir_print_coro_alloc_helper(irp, (IrInstructionCoroAllocHelper *)instruction); + break; } fprintf(irp->f, "\n"); } -- cgit v1.2.3 From ad2a29ccf25af189fc180cba6843c20b9dd029d1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 16:47:13 -0500 Subject: break the data dependencies that llvm coro transforms cant handle my simple coro test program builds now see #727 --- src/all_types.hpp | 9 ++++++- src/codegen.cpp | 1 + src/ir.cpp | 73 +++++++++++++++++++++++++++++++++++++++---------------- src/ir_print.cpp | 11 ++++++++- 4 files changed, 71 insertions(+), 23 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 9b4fa6d36c..1551f07c70 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -60,11 +60,11 @@ struct IrExecutable { IrInstruction *coro_handle; IrInstruction *coro_awaiter_field_ptr; IrInstruction *coro_result_ptr_field_ptr; - IrInstruction *implicit_allocator_ptr; IrBasicBlock *coro_early_final; IrBasicBlock *coro_normal_final; IrBasicBlock *coro_suspend_block; IrBasicBlock *coro_final_cleanup_block; + VariableTableEntry *coro_allocator_var; }; enum OutType { @@ -2852,8 +2852,15 @@ struct IrInstructionCancel { IrInstruction *target; }; +enum ImplicitAllocatorId { + ImplicitAllocatorIdArg, + ImplicitAllocatorIdLocalVar, +}; + struct IrInstructionGetImplicitAllocator { IrInstruction base; + + ImplicitAllocatorId id; }; struct IrInstructionCoroId { diff --git a/src/codegen.cpp b/src/codegen.cpp index ec047ad9ec..a5ae9996b5 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3294,6 +3294,7 @@ static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrIns static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable, IrInstructionGetImplicitAllocator *instruction) { + assert(instruction->id == ImplicitAllocatorIdArg); size_t allocator_arg_index = get_async_allocator_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id); return LLVMGetParam(g->cur_fn_val, allocator_arg_index); } diff --git a/src/ir.cpp b/src/ir.cpp index d2c28582d7..e9f29d1caa 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -114,6 +114,8 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg); static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name, IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type); +static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, + VariableTableEntry *var, bool is_const_ptr, bool is_volatile_ptr); ConstExprValue *const_ptr_pointee(CodeGen *g, ConstExprValue *const_val) { assert(const_val->type->id == TypeTableEntryIdPointer); @@ -2491,8 +2493,11 @@ static IrInstruction *ir_build_cancel(IrBuilder *irb, Scope *scope, AstNode *sou return &instruction->base; } -static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node) { +static IrInstruction *ir_build_get_implicit_allocator(IrBuilder *irb, Scope *scope, AstNode *source_node, + ImplicitAllocatorId id) +{ IrInstructionGetImplicitAllocator *instruction = ir_build_instruction(irb, scope, source_node); + instruction->id = id; return &instruction->base; } @@ -6081,15 +6086,13 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec FnTableEntry *fn_entry = exec_fn_entry(irb->exec); bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; + IrInstruction *coro_id; IrInstruction *u8_ptr_type; IrInstruction *const_bool_false; - IrInstruction *coro_size; - IrInstruction *coro_id; - IrInstruction *coro_promise_ptr; IrInstruction *coro_result_field_ptr; - IrInstruction *coro_mem_ptr; TypeTableEntry *return_type; Buf *result_ptr_field_name; + VariableTableEntry *coro_size_var; if (is_async) { // create the coro promise const_bool_false = ir_build_const_bool(irb, scope, node, false); @@ -6099,17 +6102,21 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type; IrInstruction *promise_init = ir_build_const_promise_init(irb, scope, node, return_type); ir_build_var_decl(irb, scope, node, promise_var, nullptr, nullptr, promise_init); - coro_promise_ptr = ir_build_var_ptr(irb, scope, node, promise_var, false, false); + IrInstruction *coro_promise_ptr = ir_build_var_ptr(irb, scope, node, promise_var, false, false); u8_ptr_type = ir_build_const_type(irb, scope, node, get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr); coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr); - coro_size = ir_build_coro_size(irb, scope, node); - irb->exec->implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node); + coro_size_var = ir_create_var(irb, node, scope, nullptr, false, false, true, const_bool_false); + IrInstruction *coro_size = ir_build_coro_size(irb, scope, node); + ir_build_var_decl(irb, scope, node, coro_size_var, nullptr, nullptr, coro_size); + IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node, + ImplicitAllocatorIdArg); + irb->exec->coro_allocator_var = ir_create_var(irb, node, scope, nullptr, true, true, true, const_bool_false); + ir_build_var_decl(irb, scope, node, irb->exec->coro_allocator_var, nullptr, nullptr, implicit_allocator_ptr); Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME); - IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, - alloc_field_name); + IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, alloc_field_name); IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr); IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, scope, node, alloc_fn, coro_size); IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, scope, node, maybe_coro_mem_ptr); @@ -6122,7 +6129,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_build_return(irb, scope, node, undef); ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); - coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, maybe_coro_mem_ptr); + IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, maybe_coro_mem_ptr); irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem_ptr); Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME); @@ -6200,15 +6207,20 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values); Buf *free_field_name = buf_create_from_str(ASYNC_FREE_FIELD_NAME); - IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr, - free_field_name); + IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node, + ImplicitAllocatorIdLocalVar); + IrInstruction *free_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, free_field_name); IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); + IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle); + IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_mem_ptr_maybe); IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false); + IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var, true, false); + IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr); IrInstruction *mem_slice = ir_build_slice(irb, scope, node, coro_mem_ptr_ref, zero, coro_size, false); size_t arg_count = 2; IrInstruction **args = allocate(arg_count); - args[0] = irb->exec->implicit_allocator_ptr; // self + args[0] = implicit_allocator_ptr; // self args[1] = mem_slice; // old_mem ir_build_call(irb, scope, node, nullptr, free_fn, arg_count, args, false, FnInlineAuto, false, nullptr); @@ -11229,7 +11241,7 @@ static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira, return ira->codegen->builtin_types.entry_type; } -IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr) { +IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_instr, ImplicitAllocatorId id) { FnTableEntry *parent_fn_entry = exec_fn_entry(ira->new_irb.exec); if (parent_fn_entry == nullptr) { ir_add_error(ira, source_instr, buf_sprintf("no implicit allocator available")); @@ -11243,9 +11255,26 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i } assert(parent_fn_type->async_allocator_type != nullptr); - IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, source_instr->source_node); - result->value.type = parent_fn_type->async_allocator_type; - return result; + + switch (id) { + case ImplicitAllocatorIdArg: + { + IrInstruction *result = ir_build_get_implicit_allocator(&ira->new_irb, source_instr->scope, + source_instr->source_node, ImplicitAllocatorIdArg); + result->value.type = parent_fn_type->async_allocator_type; + return result; + } + case ImplicitAllocatorIdLocalVar: + { + VariableTableEntry *coro_allocator_var = ira->old_irb.exec->coro_allocator_var; + assert(coro_allocator_var != nullptr); + IrInstruction *var_ptr_inst = ir_get_var_ptr(ira, source_instr, coro_allocator_var, true, false); + IrInstruction *result = ir_get_deref(ira, source_instr, var_ptr_inst); + assert(result->value.type != nullptr); + return result; + } + } + zig_unreachable(); } static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, FnTableEntry *fn_entry, TypeTableEntry *fn_type, @@ -11805,7 +11834,8 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } IrInstruction *uncasted_async_allocator_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, + ImplicitAllocatorIdLocalVar); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; } else { @@ -11927,7 +11957,8 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal if (call_instruction->is_async) { IrInstruction *uncasted_async_allocator_inst; if (call_instruction->async_allocator == nullptr) { - uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base); + uncasted_async_allocator_inst = ir_get_implicit_allocator(ira, &call_instruction->base, + ImplicitAllocatorIdLocalVar); if (type_is_invalid(uncasted_async_allocator_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; } else { @@ -17189,7 +17220,7 @@ static TypeTableEntry *ir_analyze_instruction_coro_begin(IrAnalyze *ira, IrInstr } static TypeTableEntry *ir_analyze_instruction_get_implicit_allocator(IrAnalyze *ira, IrInstructionGetImplicitAllocator *instruction) { - IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base); + IrInstruction *result = ir_get_implicit_allocator(ira, &instruction->base, instruction->id); ir_link_new_instruction(result, &instruction->base); return result->value.type; } diff --git a/src/ir_print.cpp b/src/ir_print.cpp index b9e02988c2..a68b5a46df 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1027,7 +1027,16 @@ static void ir_print_cancel(IrPrint *irp, IrInstructionCancel *instruction) { } static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplicitAllocator *instruction) { - fprintf(irp->f, "@getImplicitAllocator()"); + fprintf(irp->f, "@getImplicitAllocator("); + switch (instruction->id) { + case ImplicitAllocatorIdArg: + fprintf(irp->f, "Arg"); + break; + case ImplicitAllocatorIdLocalVar: + fprintf(irp->f, "LocalVar"); + break; + } + fprintf(irp->f, ")"); } static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) { -- cgit v1.2.3 From 807a5e94e976f03058426e04dceef449a5bf7ed8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 21:19:51 -0500 Subject: add atomicrmw builtin function --- doc/langref.html.in | 21 ++++++- src/all_types.hpp | 27 +++++++++ src/codegen.cpp | 50 +++++++++++++++++ src/ir.cpp | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ir_print.cpp | 29 ++++++++++ std/debug/index.zig | 18 +++--- test/cases/atomics.zig | 15 ++++- 7 files changed, 297 insertions(+), 12 deletions(-) (limited to 'src/codegen.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 2d4bead65e..25a90e3361 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -3775,6 +3775,25 @@ pub fn main() void { {#header_open|@ArgType#}

TODO

{#header_close#} + {#header_open|@atomicRmw#} +
@atomicRmw(comptime T: type, ptr: &T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) -> T
+

+ This builtin function atomically modifies memory and then returns the previous value. +

+

+ T must be a pointer type, a bool, + or an integer whose bit count meets these requirements: +

+
    +
  • At least 8
  • +
  • At most the same as usize
  • +
  • Power of 2
  • +
+

+ TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe + we can remove this restriction +

+ {#header_close#} {#header_open|@bitCast#}
@bitCast(comptime DestType: type, value: var) -> DestType

@@ -5859,7 +5878,7 @@ hljs.registerLanguage("zig", function(t) { a = t.IR + "\\s*\\(", c = { keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong", - built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate", + built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate atomicRmw", literal: "true false null undefined" }, n = [e, t.CLCM, t.CBCM, s, r]; diff --git a/src/all_types.hpp b/src/all_types.hpp index 1551f07c70..d727f4a862 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1338,6 +1338,7 @@ enum BuiltinFnId { BuiltinFnIdArgType, BuiltinFnIdExport, BuiltinFnIdErrorReturnTrace, + BuiltinFnIdAtomicRmw, }; struct BuiltinFnEntry { @@ -1857,6 +1858,19 @@ enum AtomicOrder { AtomicOrderSeqCst, }; +// synchronized with the code in define_builtin_compile_vars +enum AtomicRmwOp { + AtomicRmwOp_xchg, + AtomicRmwOp_add, + AtomicRmwOp_sub, + AtomicRmwOp_and, + AtomicRmwOp_nand, + AtomicRmwOp_or, + AtomicRmwOp_xor, + AtomicRmwOp_max, + AtomicRmwOp_min, +}; + // A basic block contains no branching. Branches send control flow // to another basic block. // Phi instructions must be first in a basic block. @@ -2006,6 +2020,7 @@ enum IrInstructionId { IrInstructionIdCoroResume, IrInstructionIdCoroSave, IrInstructionIdCoroAllocHelper, + IrInstructionIdAtomicRmw, }; struct IrInstruction { @@ -2929,6 +2944,18 @@ struct IrInstructionCoroAllocHelper { IrInstruction *coro_size; }; +struct IrInstructionAtomicRmw { + IrInstruction base; + + IrInstruction *operand_type; + IrInstruction *ptr; + IrInstruction *op; + AtomicRmwOp resolved_op; + IrInstruction *operand; + IrInstruction *ordering; + AtomicOrder resolved_ordering; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/codegen.cpp b/src/codegen.cpp index a5ae9996b5..89dc23f428 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3311,6 +3311,23 @@ static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) { zig_unreachable(); } +static LLVMAtomicRMWBinOp to_LLVMAtomicRMWBinOp(AtomicRmwOp op, bool is_signed) { + switch (op) { + case AtomicRmwOp_xchg: return LLVMAtomicRMWBinOpXchg; + case AtomicRmwOp_add: return LLVMAtomicRMWBinOpAdd; + case AtomicRmwOp_sub: return LLVMAtomicRMWBinOpSub; + case AtomicRmwOp_and: return LLVMAtomicRMWBinOpAnd; + case AtomicRmwOp_nand: return LLVMAtomicRMWBinOpNand; + case AtomicRmwOp_or: return LLVMAtomicRMWBinOpOr; + case AtomicRmwOp_xor: return LLVMAtomicRMWBinOpXor; + case AtomicRmwOp_max: + return is_signed ? LLVMAtomicRMWBinOpMax : LLVMAtomicRMWBinOpUMax; + case AtomicRmwOp_min: + return is_signed ? LLVMAtomicRMWBinOpMin : LLVMAtomicRMWBinOpUMin; + } + zig_unreachable(); +} + static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrInstructionCmpxchg *instruction) { LLVMValueRef ptr_val = ir_llvm_value(g, instruction->ptr); LLVMValueRef cmp_val = ir_llvm_value(g, instruction->cmp_value); @@ -4111,6 +4128,22 @@ static LLVMValueRef ir_render_coro_alloc_helper(CodeGen *g, IrExecutable *execut get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, ""); } +static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable, + IrInstructionAtomicRmw *instruction) +{ + bool is_signed; + if (instruction->operand->value.type->id == TypeTableEntryIdInt) { + is_signed = instruction->operand->value.type->data.integral.is_signed; + } else { + is_signed = false; + } + LLVMAtomicRMWBinOp op = to_LLVMAtomicRMWBinOp(instruction->resolved_op, is_signed); + LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->resolved_ordering); + LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr); + LLVMValueRef operand = ir_llvm_value(g, instruction->operand); + return LLVMBuildAtomicRMW(g->builder, op, ptr, operand, ordering, false); +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -4318,6 +4351,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction); case IrInstructionIdCoroAllocHelper: return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction); + case IrInstructionIdAtomicRmw: + return ir_render_atomic_rmw(g, executable, (IrInstructionAtomicRmw *)instruction); } zig_unreachable(); } @@ -5810,6 +5845,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdArgType, "ArgType", 2); create_builtin_fn(g, BuiltinFnIdExport, "export", 3); create_builtin_fn(g, BuiltinFnIdErrorReturnTrace, "errorReturnTrace", 0); + create_builtin_fn(g, BuiltinFnIdAtomicRmw, "atomicRmw", 5); } static const char *bool_to_str(bool b) { @@ -5939,6 +5975,20 @@ static void define_builtin_compile_vars(CodeGen *g) { " SeqCst,\n" "};\n\n"); } + { + buf_appendf(contents, + "pub const AtomicRmwOp = enum {\n" + " Xchg,\n" + " Add,\n" + " Sub,\n" + " And,\n" + " Nand,\n" + " Or,\n" + " Xor,\n" + " Max,\n" + " Min,\n" + "};\n\n"); + } { buf_appendf(contents, "pub const Mode = enum {\n" diff --git a/src/ir.cpp b/src/ir.cpp index 06a23af07c..dcca05ad02 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -701,6 +701,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocHelper return IrInstructionIdCoroAllocHelper; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionAtomicRmw *) { + return IrInstructionIdAtomicRmw; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -2614,6 +2618,28 @@ static IrInstruction *ir_build_coro_alloc_helper(IrBuilder *irb, Scope *scope, A return &instruction->base; } +static IrInstruction *ir_build_atomic_rmw(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *operand_type, IrInstruction *ptr, IrInstruction *op, IrInstruction *operand, + IrInstruction *ordering, AtomicRmwOp resolved_op, AtomicOrder resolved_ordering) +{ + IrInstructionAtomicRmw *instruction = ir_build_instruction(irb, scope, source_node); + instruction->operand_type = operand_type; + instruction->ptr = ptr; + instruction->op = op; + instruction->operand = operand; + instruction->ordering = ordering; + instruction->resolved_op = resolved_op; + instruction->resolved_ordering = resolved_ordering; + + if (operand_type != nullptr) ir_ref_instruction(operand_type, irb->current_basic_block); + ir_ref_instruction(ptr, irb->current_basic_block); + if (op != nullptr) ir_ref_instruction(op, irb->current_basic_block); + ir_ref_instruction(operand, irb->current_basic_block); + if (ordering != nullptr) ir_ref_instruction(ordering, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -4094,6 +4120,38 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo { return ir_build_error_return_trace(irb, scope, node); } + case BuiltinFnIdAtomicRmw: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + AstNode *arg2_node = node->data.fn_call_expr.params.at(2); + IrInstruction *arg2_value = ir_gen_node(irb, arg2_node, scope); + if (arg2_value == irb->codegen->invalid_instruction) + return arg2_value; + + AstNode *arg3_node = node->data.fn_call_expr.params.at(3); + IrInstruction *arg3_value = ir_gen_node(irb, arg3_node, scope); + if (arg3_value == irb->codegen->invalid_instruction) + return arg3_value; + + AstNode *arg4_node = node->data.fn_call_expr.params.at(4); + IrInstruction *arg4_value = ir_gen_node(irb, arg4_node, scope); + if (arg4_value == irb->codegen->invalid_instruction) + return arg4_value; + + return ir_build_atomic_rmw(irb, scope, node, arg0_value, arg1_value, arg2_value, arg3_value, + arg4_value, + // these 2 values don't mean anything since we passed non-null values for other args + AtomicRmwOp_xchg, AtomicOrderMonotonic); + } } zig_unreachable(); } @@ -9730,6 +9788,26 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic return true; } +static bool ir_resolve_atomic_rmw_op(IrAnalyze *ira, IrInstruction *value, AtomicRmwOp *out) { + if (type_is_invalid(value->value.type)) + return false; + + ConstExprValue *atomic_rmw_op_val = get_builtin_value(ira->codegen, "AtomicRmwOp"); + assert(atomic_rmw_op_val->type->id == TypeTableEntryIdMetaType); + TypeTableEntry *atomic_rmw_op_type = atomic_rmw_op_val->data.x_type; + + IrInstruction *casted_value = ir_implicit_cast(ira, value, atomic_rmw_op_type); + if (type_is_invalid(casted_value->value.type)) + return false; + + ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad); + if (!const_val) + return false; + + *out = (AtomicRmwOp)bigint_as_unsigned(&const_val->data.x_enum_tag); + return true; +} + static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, GlobalLinkageId *out) { if (type_is_invalid(value->value.type)) return false; @@ -17316,6 +17394,74 @@ static TypeTableEntry *ir_analyze_instruction_coro_alloc_helper(IrAnalyze *ira, return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstructionAtomicRmw *instruction) { + TypeTableEntry *operand_type = ir_resolve_type(ira, instruction->operand_type->other); + if (type_is_invalid(operand_type)) { + return ira->codegen->builtin_types.entry_invalid; + } + if (operand_type->id == TypeTableEntryIdInt) { + if (operand_type->data.integral.bit_count < 8) { + ir_add_error(ira, &instruction->base, + buf_sprintf("expected integer type 8 bits or larger, found %" PRIu32 "-bit integer type", + operand_type->data.integral.bit_count)); + return ira->codegen->builtin_types.entry_invalid; + } + if (operand_type->data.integral.bit_count > ira->codegen->pointer_size_bytes * 8) { + ir_add_error(ira, &instruction->base, + buf_sprintf("expected integer type pointer size or smaller, found %" PRIu32 "-bit integer type", + operand_type->data.integral.bit_count)); + return ira->codegen->builtin_types.entry_invalid; + } + if (!is_power_of_2(operand_type->data.integral.bit_count)) { + ir_add_error(ira, &instruction->base, + buf_sprintf("%" PRIu32 "-bit integer type is not a power of 2", operand_type->data.integral.bit_count)); + return ira->codegen->builtin_types.entry_invalid; + } + } else if (get_codegen_ptr_type(operand_type) == nullptr) { + ir_add_error(ira, &instruction->base, + buf_sprintf("expected integer or pointer type, found '%s'", buf_ptr(&operand_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + + IrInstruction *ptr_inst = instruction->ptr->other; + if (type_is_invalid(ptr_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, operand_type, false); + IrInstruction *casted_ptr = ir_implicit_cast(ira, ptr_inst, ptr_type); + if (type_is_invalid(casted_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + AtomicRmwOp op; + if (!ir_resolve_atomic_rmw_op(ira, instruction->op->other, &op)) { + return ira->codegen->builtin_types.entry_invalid; + } + + IrInstruction *operand = instruction->operand->other; + if (type_is_invalid(operand->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *casted_operand = ir_implicit_cast(ira, operand, operand_type); + if (type_is_invalid(casted_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + AtomicOrder ordering; + if (!ir_resolve_atomic_order(ira, instruction->ordering->other, &ordering)) + return ira->codegen->builtin_types.entry_invalid; + + if (instr_is_comptime(casted_operand) && instr_is_comptime(casted_ptr) && casted_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) + { + zig_panic("TODO compile-time execution of atomicRmw"); + } + + IrInstruction *result = ir_build_atomic_rmw(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, nullptr, casted_ptr, nullptr, casted_operand, nullptr, + op, ordering); + ir_link_new_instruction(result, &instruction->base); + result->value.type = operand_type; + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { @@ -17545,6 +17691,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction); case IrInstructionIdCoroAllocHelper: return ir_analyze_instruction_coro_alloc_helper(ira, (IrInstructionCoroAllocHelper *)instruction); + case IrInstructionIdAtomicRmw: + return ir_analyze_instruction_atomic_rmw(ira, (IrInstructionAtomicRmw *)instruction); } zig_unreachable(); } @@ -17748,6 +17896,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroSize: case IrInstructionIdCoroSuspend: case IrInstructionIdCoroFree: + case IrInstructionIdAtomicRmw: return false; case IrInstructionIdAsm: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index a68b5a46df..e23183bb38 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1113,6 +1113,32 @@ static void ir_print_coro_alloc_helper(IrPrint *irp, IrInstructionCoroAllocHelpe fprintf(irp->f, ")"); } +static void ir_print_atomic_rmw(IrPrint *irp, IrInstructionAtomicRmw *instruction) { + fprintf(irp->f, "@atomicRmw("); + if (instruction->operand_type != nullptr) { + ir_print_other_instruction(irp, instruction->operand_type); + } else { + fprintf(irp->f, "[TODO print]"); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->ptr); + fprintf(irp->f, ","); + if (instruction->op != nullptr) { + ir_print_other_instruction(irp, instruction->op); + } else { + fprintf(irp->f, "[TODO print]"); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->operand); + fprintf(irp->f, ","); + if (instruction->ordering != nullptr) { + ir_print_other_instruction(irp, instruction->ordering); + } else { + fprintf(irp->f, "[TODO print]"); + } + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1472,6 +1498,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdCoroAllocHelper: ir_print_coro_alloc_helper(irp, (IrInstructionCoroAllocHelper *)instruction); break; + case IrInstructionIdAtomicRmw: + ir_print_atomic_rmw(irp, (IrInstructionAtomicRmw *)instruction); + break; } fprintf(irp->f, "\n"); } diff --git a/std/debug/index.zig b/std/debug/index.zig index cc4832b1ea..8731834fa5 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -97,21 +97,18 @@ pub fn assertOrPanic(ok: bool) void { } } -var panicking = false; +var panicking: u8 = 0; // TODO make this a bool /// This is the default panic implementation. pub fn panic(comptime format: []const u8, args: ...) noreturn { - // TODO an intrinsic that labels this as unlikely to be reached + @setCold(true); - // TODO - // if (@atomicRmw(AtomicOp.XChg, &panicking, true, AtomicOrder.SeqCst)) { } - if (panicking) { + if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) { // Panicked during a panic. + // TODO detect if a different thread caused the panic, because in that case // we would want to return here instead of calling abort, so that the thread // which first called panic can finish printing a stack trace. os.abort(); - } else { - panicking = true; } const stderr = getStderrStream() catch os.abort(); @@ -122,10 +119,11 @@ pub fn panic(comptime format: []const u8, args: ...) noreturn { } pub fn panicWithTrace(trace: &const builtin.StackTrace, comptime format: []const u8, args: ...) noreturn { - if (panicking) { + @setCold(true); + + if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) { + // See TODO in above function os.abort(); - } else { - panicking = true; } const stderr = getStderrStream() catch os.abort(); stderr.print(format ++ "\n", args) catch os.abort(); diff --git a/test/cases/atomics.zig b/test/cases/atomics.zig index a796488d3b..e8e81b76e6 100644 --- a/test/cases/atomics.zig +++ b/test/cases/atomics.zig @@ -1,5 +1,7 @@ const assert = @import("std").debug.assert; -const AtomicOrder = @import("builtin").AtomicOrder; +const builtin = @import("builtin"); +const AtomicRmwOp = builtin.AtomicRmwOp; +const AtomicOrder = builtin.AtomicOrder; test "cmpxchg" { var x: i32 = 1234; @@ -12,3 +14,14 @@ test "fence" { @fence(AtomicOrder.SeqCst); x = 5678; } + +test "atomicrmw" { + var data: u8 = 200; + testAtomicRmw(&data); + assert(data == 42); +} + +fn testAtomicRmw(ptr: &u8) void { + const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst); + assert(prev_value == 200); +} -- cgit v1.2.3 From c6227661568a9e8cad9d28bd7a11cb76c4f9c1c1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 21:48:20 -0500 Subject: async function fulfills promise atomically --- src/codegen.cpp | 17 ++++++++++++++--- src/ir.cpp | 24 +++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 89dc23f428..315699b826 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4132,8 +4132,9 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable, IrInstructionAtomicRmw *instruction) { bool is_signed; - if (instruction->operand->value.type->id == TypeTableEntryIdInt) { - is_signed = instruction->operand->value.type->data.integral.is_signed; + TypeTableEntry *operand_type = instruction->operand->value.type; + if (operand_type->id == TypeTableEntryIdInt) { + is_signed = operand_type->data.integral.is_signed; } else { is_signed = false; } @@ -4141,7 +4142,17 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable, LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->resolved_ordering); LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr); LLVMValueRef operand = ir_llvm_value(g, instruction->operand); - return LLVMBuildAtomicRMW(g->builder, op, ptr, operand, ordering, false); + + if (get_codegen_ptr_type(operand_type) == nullptr) { + return LLVMBuildAtomicRMW(g->builder, op, ptr, operand, ordering, false); + } + + // it's a pointer but we need to treat it as an int + LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr, + LLVMPointerType(g->builtin_types.entry_usize->type_ref, 0), ""); + LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->type_ref, ""); + LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false); + return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, ""); } static void set_debug_location(CodeGen *g, IrInstruction *instruction) { diff --git a/src/ir.cpp b/src/ir.cpp index dcca05ad02..dc845bdaf7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -2727,7 +2727,13 @@ static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); ir_build_store_ptr(irb, scope, node, result_ptr, return_value); } - IrInstruction *maybe_await_handle = ir_build_load_ptr(irb, scope, node, irb->exec->coro_awaiter_field_ptr); + IrInstruction *promise_type_val = ir_build_const_type(irb, scope, node, + get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise)); + // TODO replace replacement_value with @intToPtr(?promise, 0x1) when it doesn't crash zig + IrInstruction *replacement_value = irb->exec->coro_handle; + IrInstruction *maybe_await_handle = ir_build_atomic_rmw(irb, scope, node, + promise_type_val, irb->exec->coro_awaiter_field_ptr, nullptr, replacement_value, nullptr, + AtomicRmwOp_xchg, AtomicOrderSeqCst); IrInstruction *is_non_null = ir_build_test_nonnull(irb, scope, node, maybe_await_handle); IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, false); return ir_build_cond_br(irb, scope, node, is_non_null, irb->exec->coro_normal_final, irb->exec->coro_early_final, @@ -17433,8 +17439,12 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr return ira->codegen->builtin_types.entry_invalid; AtomicRmwOp op; - if (!ir_resolve_atomic_rmw_op(ira, instruction->op->other, &op)) { - return ira->codegen->builtin_types.entry_invalid; + if (instruction->op == nullptr) { + op = instruction->resolved_op; + } else { + if (!ir_resolve_atomic_rmw_op(ira, instruction->op->other, &op)) { + return ira->codegen->builtin_types.entry_invalid; + } } IrInstruction *operand = instruction->operand->other; @@ -17446,8 +17456,12 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr return ira->codegen->builtin_types.entry_invalid; AtomicOrder ordering; - if (!ir_resolve_atomic_order(ira, instruction->ordering->other, &ordering)) - return ira->codegen->builtin_types.entry_invalid; + if (instruction->ordering == nullptr) { + ordering = instruction->resolved_ordering; + } else { + if (!ir_resolve_atomic_order(ira, instruction->ordering->other, &ordering)) + return ira->codegen->builtin_types.entry_invalid; + } if (instr_is_comptime(casted_operand) && instr_is_comptime(casted_ptr) && casted_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) { -- cgit v1.2.3 From 8429d4ceac4eb99fbe8aeca2ebe864dfd5b40470 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 22:18:48 -0500 Subject: implement coroutine resume --- doc/langref.html.in | 4 +++- src/all_types.hpp | 6 ++++++ src/analyze.cpp | 1 + src/ast_render.cpp | 8 ++++++++ src/codegen.cpp | 2 +- src/ir.cpp | 18 +++++++++++++++++- src/parser.cpp | 27 ++++++++++++++++++++++++++- test/cases/coroutines.zig | 27 +++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 4 deletions(-) (limited to 'src/codegen.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 25a90e3361..83d5e65bba 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -5682,7 +5682,7 @@ ErrorSetExpr = (PrefixOpExpression "!" PrefixOpExpression) | PrefixOpExpression BlockOrExpression = Block | Expression -Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression +Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpression AsmExpression = "asm" option("volatile") "(" String option(AsmOutput) ")" @@ -5730,6 +5730,8 @@ BreakExpression = "break" option(":" Symbol) option(Expression) CancelExpression = "cancel" Expression; +ResumeExpression = "resume" Expression; + Defer(body) = ("defer" | "deferror") body IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body)) diff --git a/src/all_types.hpp b/src/all_types.hpp index d727f4a862..d2c7875943 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -403,6 +403,7 @@ enum NodeType { NodeTypeTestExpr, NodeTypeErrorSetDecl, NodeTypeCancel, + NodeTypeResume, NodeTypeAwaitExpr, NodeTypeSuspend, }; @@ -849,6 +850,10 @@ struct AstNodeCancelExpr { AstNode *expr; }; +struct AstNodeResumeExpr { + AstNode *expr; +}; + struct AstNodeContinueExpr { Buf *name; }; @@ -930,6 +935,7 @@ struct AstNode { AstNodeVarLiteral var_literal; AstNodeErrorSetDecl err_set_decl; AstNodeCancelExpr cancel_expr; + AstNodeResumeExpr resume_expr; AstNodeAwaitExpr await_expr; AstNodeSuspend suspend; } data; diff --git a/src/analyze.cpp b/src/analyze.cpp index be01f6b5f8..8842b4967e 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3212,6 +3212,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) { case NodeTypeTestExpr: case NodeTypeErrorSetDecl: case NodeTypeCancel: + case NodeTypeResume: case NodeTypeAwaitExpr: case NodeTypeSuspend: zig_unreachable(); diff --git a/src/ast_render.cpp b/src/ast_render.cpp index 5f3e1998fd..6318ba3cff 100644 --- a/src/ast_render.cpp +++ b/src/ast_render.cpp @@ -246,6 +246,8 @@ static const char *node_type_str(NodeType node_type) { return "ErrorSetDecl"; case NodeTypeCancel: return "Cancel"; + case NodeTypeResume: + return "Resume"; case NodeTypeAwaitExpr: return "AwaitExpr"; case NodeTypeSuspend: @@ -1049,6 +1051,12 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { render_node_grouped(ar, node->data.cancel_expr.expr); break; } + case NodeTypeResume: + { + fprintf(ar->f, "resume "); + render_node_grouped(ar, node->data.resume_expr.expr); + break; + } case NodeTypeAwaitExpr: { fprintf(ar->f, "await "); diff --git a/src/codegen.cpp b/src/codegen.cpp index 315699b826..59956c9279 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4004,7 +4004,7 @@ static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, Ir static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) { if (g->coro_alloc_helper_fn_val != nullptr) - return g->coro_alloc_fn_val; + return g->coro_alloc_helper_fn_val; assert(fn_type->id == TypeTableEntryIdFn); diff --git a/src/ir.cpp b/src/ir.cpp index dc845bdaf7..4222196f37 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5927,6 +5927,16 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *parent_scope, AstNode return ir_build_cancel(irb, parent_scope, node, target_inst); } +static IrInstruction *ir_gen_resume(IrBuilder *irb, Scope *parent_scope, AstNode *node) { + assert(node->type == NodeTypeResume); + + IrInstruction *target_inst = ir_gen_node(irb, node->data.resume_expr.expr, parent_scope); + if (target_inst == irb->codegen->invalid_instruction) + return irb->codegen->invalid_instruction; + + return ir_build_coro_resume(irb, parent_scope, node, target_inst); +} + static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) { assert(node->type == NodeTypeAwaitExpr); @@ -6101,6 +6111,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop return ir_lval_wrap(irb, scope, ir_gen_err_set_decl(irb, scope, node), lval); case NodeTypeCancel: return ir_lval_wrap(irb, scope, ir_gen_cancel(irb, scope, node), lval); + case NodeTypeResume: + return ir_lval_wrap(irb, scope, ir_gen_resume(irb, scope, node), lval); case NodeTypeAwaitExpr: return ir_lval_wrap(irb, scope, ir_gen_await_expr(irb, scope, node), lval); case NodeTypeSuspend: @@ -17364,8 +17376,12 @@ static TypeTableEntry *ir_analyze_instruction_coro_resume(IrAnalyze *ira, IrInst if (type_is_invalid(awaiter_handle->value.type)) return ira->codegen->builtin_types.entry_invalid; + IrInstruction *casted_target = ir_implicit_cast(ira, awaiter_handle, ira->codegen->builtin_types.entry_promise); + if (type_is_invalid(casted_target->value.type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *result = ir_build_coro_resume(&ira->new_irb, instruction->base.scope, - instruction->base.source_node, awaiter_handle); + instruction->base.source_node, casted_target); ir_link_new_instruction(result, &instruction->base); result->value.type = ira->codegen->builtin_types.entry_void; return result->value.type; diff --git a/src/parser.cpp b/src/parser.cpp index 763273fd0a..38994c79fc 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1638,6 +1638,24 @@ static AstNode *ast_parse_cancel_expr(ParseContext *pc, size_t *token_index) { return node; } +/* +ResumeExpression = "resume" Expression; +*/ +static AstNode *ast_parse_resume_expr(ParseContext *pc, size_t *token_index) { + Token *token = &pc->tokens->at(*token_index); + + if (token->id != TokenIdKeywordResume) { + return nullptr; + } + *token_index += 1; + + AstNode *node = ast_create_node(pc, NodeTypeResume, token); + + node->data.resume_expr.expr = ast_parse_expression(pc, token_index, false); + + return node; +} + /* Defer(body) = ("defer" | "errdefer") body */ @@ -2266,7 +2284,7 @@ static AstNode *ast_parse_block_or_expression(ParseContext *pc, size_t *token_in } /* -Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression +Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpression */ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool mandatory) { Token *token = &pc->tokens->at(*token_index); @@ -2287,6 +2305,10 @@ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool if (cancel_expr) return cancel_expr; + AstNode *resume_expr = ast_parse_resume_expr(pc, token_index); + if (resume_expr) + return resume_expr; + AstNode *ass_expr = ast_parse_ass_expr(pc, token_index, false); if (ass_expr) return ass_expr; @@ -3060,6 +3082,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont case NodeTypeCancel: visit_field(&node->data.cancel_expr.expr, visit, context); break; + case NodeTypeResume: + visit_field(&node->data.resume_expr.expr, visit, context); + break; case NodeTypeAwaitExpr: visit_field(&node->data.await_expr.expr, visit, context); break; diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig index f5e70774fa..2a5505360c 100644 --- a/test/cases/coroutines.zig +++ b/test/cases/coroutines.zig @@ -14,3 +14,30 @@ async fn simpleAsyncFn() void { suspend; x += 1; } + +test "coroutine suspend, resume, cancel" { + seq('a'); + const p = (async(std.debug.global_allocator) testAsyncSeq()) catch unreachable; + seq('c'); + resume p; + seq('f'); + cancel p; + seq('g'); + + assert(std.mem.eql(u8, points, "abcdefg")); +} + +async fn testAsyncSeq() void { + defer seq('e'); + + seq('b'); + suspend; + seq('d'); +} +var points = []u8{0} ** "abcdefg".len; +var index: usize = 0; + +fn seq(c: u8) void { + points[index] = c; + index += 1; +} -- cgit v1.2.3 From 253d988e7c00f7ad0cc1b5f913562cb5c1712c91 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 1 Mar 2018 03:28:13 -0500 Subject: implementation of await but it has bugs --- src/all_types.hpp | 23 ++++++ src/analyze.cpp | 18 +++++ src/analyze.hpp | 1 + src/codegen.cpp | 32 +++++++- src/ir.cpp | 185 +++++++++++++++++++++++++++++++++++++++++----- src/ir_print.cpp | 24 +++++- test/cases/coroutines.zig | 6 +- 7 files changed, 266 insertions(+), 23 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index d2c7875943..503d45fd9b 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1192,6 +1192,7 @@ struct TypeTableEntry { TypeTableEntry *pointer_parent[2]; // [0 - mut, 1 - const] TypeTableEntry *maybe_parent; TypeTableEntry *promise_parent; + TypeTableEntry *promise_frame_parent; // If we generate a constant name value for this type, we memoize it here. // The type of this is array ConstExprValue *cached_const_name_val; @@ -1641,6 +1642,7 @@ struct CodeGen { LLVMValueRef coro_free_fn_val; LLVMValueRef coro_resume_fn_val; LLVMValueRef coro_save_fn_val; + LLVMValueRef coro_promise_fn_val; LLVMValueRef coro_alloc_helper_fn_val; bool error_during_imports; @@ -2025,8 +2027,10 @@ enum IrInstructionId { IrInstructionIdCoroFree, IrInstructionIdCoroResume, IrInstructionIdCoroSave, + IrInstructionIdCoroPromise, IrInstructionIdCoroAllocHelper, IrInstructionIdAtomicRmw, + IrInstructionIdPromiseResultType, }; struct IrInstruction { @@ -2943,6 +2947,12 @@ struct IrInstructionCoroSave { IrInstruction *coro_handle; }; +struct IrInstructionCoroPromise { + IrInstruction base; + + IrInstruction *coro_handle; +}; + struct IrInstructionCoroAllocHelper { IrInstruction base; @@ -2962,6 +2972,12 @@ struct IrInstructionAtomicRmw { AtomicOrder resolved_ordering; }; +struct IrInstructionPromiseResultType { + IrInstruction base; + + IrInstruction *promise_type; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; @@ -2971,6 +2987,13 @@ static const size_t maybe_null_index = 1; static const size_t err_union_err_index = 0; static const size_t err_union_payload_index = 1; +#define ASYNC_ALLOC_FIELD_NAME "allocFn" +#define ASYNC_FREE_FIELD_NAME "freeFn" +#define AWAITER_HANDLE_FIELD_NAME "awaiter_handle" +#define RESULT_FIELD_NAME "result" +#define RESULT_PTR_FIELD_NAME "result_ptr" + + enum FloatMode { FloatModeOptimized, FloatModeStrict, diff --git a/src/analyze.cpp b/src/analyze.cpp index 8842b4967e..d66130ef6d 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -457,6 +457,23 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool return get_pointer_to_type_extra(g, child_type, is_const, false, get_abi_alignment(g, child_type), 0, 0); } +TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type) { + if (return_type->promise_frame_parent != nullptr) { + return return_type->promise_frame_parent; + } + + TypeTableEntry *awaiter_handle_type = get_maybe_type(g, g->builtin_types.entry_promise); + TypeTableEntry *result_ptr_type = get_pointer_to_type(g, return_type, false); + const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME}; + TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type}; + size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1; + Buf *name = buf_sprintf("AsyncFramePromise(%s)", buf_ptr(&return_type->name)); + TypeTableEntry *entry = get_struct_type(g, buf_ptr(name), field_names, field_types, field_count); + + return_type->promise_frame_parent = entry; + return entry; +} + TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) { if (child_type->maybe_parent) { TypeTableEntry *entry = child_type->maybe_parent; @@ -5800,3 +5817,4 @@ bool fn_type_can_fail(FnTypeId *fn_type_id) { return return_type->id == TypeTableEntryIdErrorUnion || return_type->id == TypeTableEntryIdErrorSet || fn_type_id->cc == CallingConventionAsync; } + diff --git a/src/analyze.hpp b/src/analyze.hpp index 068f321bfb..e9f89aa638 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -36,6 +36,7 @@ TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[], TypeTableEntry *field_types[], size_t field_count); TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type); +TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type); TypeTableEntry *get_test_fn_type(CodeGen *g); bool handle_is_ptr(TypeTableEntry *type_entry); void find_libc_include_path(CodeGen *g); diff --git a/src/codegen.cpp b/src/codegen.cpp index 59956c9279..6534515edc 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1081,6 +1081,23 @@ static LLVMValueRef get_coro_save_fn_val(CodeGen *g) { return g->coro_save_fn_val; } +static LLVMValueRef get_coro_promise_fn_val(CodeGen *g) { + if (g->coro_promise_fn_val) + return g->coro_promise_fn_val; + + LLVMTypeRef param_types[] = { + LLVMPointerType(LLVMInt8Type(), 0), + LLVMInt32Type(), + LLVMInt1Type(), + }; + LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 3, false); + Buf *name = buf_sprintf("llvm.coro.promise"); + g->coro_promise_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type); + assert(LLVMGetIntrinsicID(g->coro_promise_fn_val)); + + return g->coro_promise_fn_val; +} + static LLVMValueRef get_return_address_fn_val(CodeGen *g) { if (g->return_address_fn_val) return g->return_address_fn_val; @@ -4002,6 +4019,16 @@ static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, Ir return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, ""); } +static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable, IrInstructionCoroPromise *instruction) { + LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle); + LLVMValueRef params[] = { + coro_handle, + LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false), + LLVMConstNull(LLVMInt1Type()), + }; + return LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, ""); +} + static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) { if (g->coro_alloc_helper_fn_val != nullptr) return g->coro_alloc_helper_fn_val; @@ -4064,7 +4091,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg); next_arg += 1; LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref, - 2 * g->pointer_size_bytes, false); + get_coro_frame_align_bytes(g), false); ZigList args = {}; args.append(sret_ptr); @@ -4218,6 +4245,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, case IrInstructionIdTagType: case IrInstructionIdExport: case IrInstructionIdErrorUnion: + case IrInstructionIdPromiseResultType: zig_unreachable(); case IrInstructionIdReturn: @@ -4360,6 +4388,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction); case IrInstructionIdCoroSave: return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction); + case IrInstructionIdCoroPromise: + return ir_render_coro_promise(g, executable, (IrInstructionCoroPromise *)instruction); case IrInstructionIdCoroAllocHelper: return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction); case IrInstructionIdAtomicRmw: diff --git a/src/ir.cpp b/src/ir.cpp index 4222196f37..51c75ca23b 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -45,12 +45,6 @@ static LVal make_lval_addr(bool is_const, bool is_volatile) { return { true, is_const, is_volatile }; } -static const char * ASYNC_ALLOC_FIELD_NAME = "allocFn"; -static const char * ASYNC_FREE_FIELD_NAME = "freeFn"; -static const char * AWAITER_HANDLE_FIELD_NAME = "awaiter_handle"; -static const char * RESULT_FIELD_NAME = "result"; -static const char * RESULT_PTR_FIELD_NAME = "result_ptr"; - enum ConstCastResultId { ConstCastResultIdOk, ConstCastResultIdErrSet, @@ -697,6 +691,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroSave *) { return IrInstructionIdCoroSave; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroPromise *) { + return IrInstructionIdCoroPromise; +} + static constexpr IrInstructionId ir_instruction_id(IrInstructionCoroAllocHelper *) { return IrInstructionIdCoroAllocHelper; } @@ -705,6 +703,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAtomicRmw *) { return IrInstructionIdAtomicRmw; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionPromiseResultType *) { + return IrInstructionIdPromiseResultType; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -937,25 +939,19 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, Ast static IrInstruction *ir_build_const_promise_init(IrBuilder *irb, Scope *scope, AstNode *source_node, TypeTableEntry *return_type) { - TypeTableEntry *awaiter_handle_type = get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise); - TypeTableEntry *result_ptr_type = get_pointer_to_type(irb->codegen, return_type, false); - const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME}; - TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type}; - size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1; - TypeTableEntry *struct_type = get_struct_type(irb->codegen, "AsyncFramePromise", field_names, field_types, - field_count); + TypeTableEntry *struct_type = get_promise_frame_type(irb->codegen, return_type); IrInstructionConst *const_instruction = ir_build_instruction(irb, scope, source_node); const_instruction->base.value.type = struct_type; const_instruction->base.value.special = ConstValSpecialStatic; - const_instruction->base.value.data.x_struct.fields = allocate(field_count); - const_instruction->base.value.data.x_struct.fields[0].type = awaiter_handle_type; + const_instruction->base.value.data.x_struct.fields = allocate(struct_type->data.structure.src_field_count); + const_instruction->base.value.data.x_struct.fields[0].type = struct_type->data.structure.fields[0].type_entry; const_instruction->base.value.data.x_struct.fields[0].special = ConstValSpecialStatic; const_instruction->base.value.data.x_struct.fields[0].data.x_maybe = nullptr; - if (field_count == 3) { + if (struct_type->data.structure.src_field_count > 1) { const_instruction->base.value.data.x_struct.fields[1].type = return_type; const_instruction->base.value.data.x_struct.fields[1].special = ConstValSpecialUndef; - const_instruction->base.value.data.x_struct.fields[2].type = result_ptr_type; + const_instruction->base.value.data.x_struct.fields[2].type = struct_type->data.structure.fields[2].type_entry; const_instruction->base.value.data.x_struct.fields[2].special = ConstValSpecialUndef; } return &const_instruction->base; @@ -2605,6 +2601,17 @@ static IrInstruction *ir_build_coro_save(IrBuilder *irb, Scope *scope, AstNode * return &instruction->base; } +static IrInstruction *ir_build_coro_promise(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *coro_handle) +{ + IrInstructionCoroPromise *instruction = ir_build_instruction(irb, scope, source_node); + instruction->coro_handle = coro_handle; + + ir_ref_instruction(coro_handle, irb->current_basic_block); + + return &instruction->base; +} + static IrInstruction *ir_build_coro_alloc_helper(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *alloc_fn, IrInstruction *coro_size) { @@ -2640,6 +2647,17 @@ static IrInstruction *ir_build_atomic_rmw(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } +static IrInstruction *ir_build_promise_result_type(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *promise_type) +{ + IrInstructionPromiseResultType *instruction = ir_build_instruction(irb, scope, source_node); + instruction->promise_type = promise_type; + + ir_ref_instruction(promise_type, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -5944,7 +5962,93 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast if (target_inst == irb->codegen->invalid_instruction) return irb->codegen->invalid_instruction; - zig_panic("TODO: generate await expr"); + FnTableEntry *fn_entry = exec_fn_entry(irb->exec); + if (!fn_entry) { + add_node_error(irb->codegen, node, buf_sprintf("await outside function definition")); + return irb->codegen->invalid_instruction; + } + if (fn_entry->type_entry->data.fn.fn_type_id.cc != CallingConventionAsync) { + add_node_error(irb->codegen, node, buf_sprintf("await in non-async function")); + return irb->codegen->invalid_instruction; + } + + ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(parent_scope); + if (scope_defer_expr) { + if (!scope_defer_expr->reported_err) { + add_node_error(irb->codegen, node, buf_sprintf("cannot await inside defer expression")); + scope_defer_expr->reported_err = true; + } + return irb->codegen->invalid_instruction; + } + + Scope *outer_scope = irb->exec->begin_scope; + + IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, parent_scope, node, target_inst); + Buf *result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME); + IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_ptr_field_name); + + Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME); + IrInstruction *awaiter_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, + awaiter_handle_field_name); + + IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false); + VariableTableEntry *result_var = ir_create_var(irb, node, parent_scope, nullptr, + false, false, true, const_bool_false); + IrInstruction *undefined_value = ir_build_const_undefined(irb, parent_scope, node); + IrInstruction *target_promise_type = ir_build_typeof(irb, parent_scope, node, target_inst); + IrInstruction *promise_result_type = ir_build_promise_result_type(irb, parent_scope, node, target_promise_type); + ir_build_var_decl(irb, parent_scope, node, result_var, promise_result_type, nullptr, undefined_value); + IrInstruction *my_result_var_ptr = ir_build_var_ptr(irb, parent_scope, node, result_var, false, false); + ir_build_store_ptr(irb, parent_scope, node, result_ptr_field_ptr, my_result_var_ptr); + IrInstruction *save_token = ir_build_coro_save(irb, parent_scope, node, irb->exec->coro_handle); + IrInstruction *promise_type_val = ir_build_const_type(irb, parent_scope, node, + get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise)); + IrInstruction *maybe_await_handle = ir_build_atomic_rmw(irb, parent_scope, node, + promise_type_val, awaiter_field_ptr, nullptr, irb->exec->coro_handle, nullptr, + AtomicRmwOp_xchg, AtomicOrderSeqCst); + IrInstruction *is_non_null = ir_build_test_nonnull(irb, parent_scope, node, maybe_await_handle); + IrBasicBlock *yes_suspend_block = ir_create_basic_block(irb, parent_scope, "YesSuspend"); + IrBasicBlock *no_suspend_block = ir_create_basic_block(irb, parent_scope, "NoSuspend"); + IrBasicBlock *merge_block = ir_create_basic_block(irb, parent_scope, "Merge"); + ir_build_cond_br(irb, parent_scope, node, is_non_null, no_suspend_block, yes_suspend_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, no_suspend_block); + Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME); + IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_field_name); + IrInstruction *no_suspend_result = ir_build_load_ptr(irb, parent_scope, node, promise_result_ptr); + ir_build_cancel(irb, parent_scope, node, target_inst); + ir_build_br(irb, parent_scope, node, merge_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, yes_suspend_block); + ir_build_coro_resume(irb, parent_scope, node, target_inst); + IrInstruction *suspend_code = ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false); + IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); + IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); + + IrInstructionSwitchBrCase *cases = allocate(2); + cases[0].value = ir_build_const_u8(irb, parent_scope, node, 0); + cases[0].block = resume_block; + cases[1].value = ir_build_const_u8(irb, parent_scope, node, 1); + cases[1].block = cleanup_block; + ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, + 2, cases, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, cleanup_block); + ir_gen_defers_for_block(irb, parent_scope, outer_scope, true); + ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, resume_block); + IrInstruction *yes_suspend_result = ir_build_load_ptr(irb, parent_scope, node, my_result_var_ptr); + ir_build_br(irb, parent_scope, node, merge_block, const_bool_false); + + ir_set_cursor_at_end_and_append_block(irb, merge_block); + IrBasicBlock **incoming_blocks = allocate(2); + IrInstruction **incoming_values = allocate(2); + incoming_blocks[0] = resume_block; + incoming_values[0] = yes_suspend_result; + incoming_blocks[1] = no_suspend_block; + incoming_values[1] = no_suspend_result; + return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values); } static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNode *node) { @@ -17399,6 +17503,29 @@ static TypeTableEntry *ir_analyze_instruction_coro_save(IrAnalyze *ira, IrInstru return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_coro_promise(IrAnalyze *ira, IrInstructionCoroPromise *instruction) { + IrInstruction *coro_handle = instruction->coro_handle->other; + if (type_is_invalid(coro_handle->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + if (coro_handle->value.type->id != TypeTableEntryIdPromise || + coro_handle->value.type->data.promise.result_type == nullptr) + { + ir_add_error(ira, &instruction->base, buf_sprintf("expected promise->T, found '%s'", + buf_ptr(&coro_handle->value.type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + + TypeTableEntry *coro_frame_type = get_promise_frame_type(ira->codegen, + coro_handle->value.type->data.promise.result_type); + + IrInstruction *result = ir_build_coro_promise(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, coro_handle); + ir_link_new_instruction(result, &instruction->base); + result->value.type = get_pointer_to_type(ira->codegen, coro_frame_type, false); + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_coro_alloc_helper(IrAnalyze *ira, IrInstructionCoroAllocHelper *instruction) { IrInstruction *alloc_fn = instruction->alloc_fn->other; if (type_is_invalid(alloc_fn->value.type)) @@ -17492,6 +17619,22 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_promise_result_type(IrAnalyze *ira, IrInstructionPromiseResultType *instruction) { + TypeTableEntry *promise_type = ir_resolve_type(ira, instruction->promise_type->other); + if (type_is_invalid(promise_type)) + return ira->codegen->builtin_types.entry_invalid; + + if (promise_type->id != TypeTableEntryIdPromise || promise_type->data.promise.result_type == nullptr) { + ir_add_error(ira, &instruction->base, buf_sprintf("expected promise->T, found '%s'", + buf_ptr(&promise_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); + out_val->data.x_type = promise_type->data.promise.result_type; + return ira->codegen->builtin_types.entry_type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { @@ -17719,10 +17862,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_resume(ira, (IrInstructionCoroResume *)instruction); case IrInstructionIdCoroSave: return ir_analyze_instruction_coro_save(ira, (IrInstructionCoroSave *)instruction); + case IrInstructionIdCoroPromise: + return ir_analyze_instruction_coro_promise(ira, (IrInstructionCoroPromise *)instruction); case IrInstructionIdCoroAllocHelper: return ir_analyze_instruction_coro_alloc_helper(ira, (IrInstructionCoroAllocHelper *)instruction); case IrInstructionIdAtomicRmw: return ir_analyze_instruction_atomic_rmw(ira, (IrInstructionAtomicRmw *)instruction); + case IrInstructionIdPromiseResultType: + return ir_analyze_instruction_promise_result_type(ira, (IrInstructionPromiseResultType *)instruction); } zig_unreachable(); } @@ -17927,6 +18074,8 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroSuspend: case IrInstructionIdCoroFree: case IrInstructionIdAtomicRmw: + case IrInstructionIdCoroPromise: + case IrInstructionIdPromiseResultType: return false; case IrInstructionIdAsm: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index e23183bb38..194225935a 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -839,7 +839,11 @@ static void ir_print_ptr_to_int(IrPrint *irp, IrInstructionPtrToInt *instruction static void ir_print_int_to_ptr(IrPrint *irp, IrInstructionIntToPtr *instruction) { fprintf(irp->f, "@intToPtr("); - ir_print_other_instruction(irp, instruction->dest_type); + if (instruction->dest_type == nullptr) { + fprintf(irp->f, "(null)"); + } else { + ir_print_other_instruction(irp, instruction->dest_type); + } fprintf(irp->f, ","); ir_print_other_instruction(irp, instruction->target); fprintf(irp->f, ")"); @@ -1105,6 +1109,18 @@ static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction) fprintf(irp->f, ")"); } +static void ir_print_coro_promise(IrPrint *irp, IrInstructionCoroPromise *instruction) { + fprintf(irp->f, "@coroPromise("); + ir_print_other_instruction(irp, instruction->coro_handle); + fprintf(irp->f, ")"); +} + +static void ir_print_promise_result_type(IrPrint *irp, IrInstructionPromiseResultType *instruction) { + fprintf(irp->f, "@PromiseResultType("); + ir_print_other_instruction(irp, instruction->promise_type); + fprintf(irp->f, ")"); +} + static void ir_print_coro_alloc_helper(IrPrint *irp, IrInstructionCoroAllocHelper *instruction) { fprintf(irp->f, "@coroAllocHelper("); ir_print_other_instruction(irp, instruction->alloc_fn); @@ -1501,6 +1517,12 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdAtomicRmw: ir_print_atomic_rmw(irp, (IrInstructionAtomicRmw *)instruction); break; + case IrInstructionIdCoroPromise: + ir_print_coro_promise(irp, (IrInstructionCoroPromise *)instruction); + break; + case IrInstructionIdPromiseResultType: + ir_print_promise_result_type(irp, (IrInstructionPromiseResultType *)instruction); + break; } fprintf(irp->f, "\n"); } diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig index b2bed7a8a0..8f1909a64f 100644 --- a/test/cases/coroutines.zig +++ b/test/cases/coroutines.zig @@ -4,7 +4,7 @@ const assert = std.debug.assert; var x: i32 = 1; test "create a coroutine and cancel it" { - const p = try (async(std.debug.global_allocator) simpleAsyncFn()); + const p = try async(std.debug.global_allocator) simpleAsyncFn(); cancel p; assert(x == 2); } @@ -17,7 +17,7 @@ async fn simpleAsyncFn() void { test "coroutine suspend, resume, cancel" { seq('a'); - const p = (async(std.debug.global_allocator) testAsyncSeq()) catch unreachable; + const p = try async(std.debug.global_allocator) testAsyncSeq(); seq('c'); resume p; seq('f'); @@ -43,7 +43,7 @@ fn seq(c: u8) void { } test "coroutine suspend with block" { - const p = (async(std.debug.global_allocator) testSuspendBlock()) catch unreachable; + const p = try async(std.debug.global_allocator) testSuspendBlock(); std.debug.assert(!result); resume a_promise; std.debug.assert(result); -- cgit v1.2.3 From a7c87ae1e4b621291a844df678cbe0fbfb531029 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 1 Mar 2018 10:23:47 -0500 Subject: fix not casting result of llvm.coro.promise --- src/codegen.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 6534515edc..0cfd27322f 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4026,7 +4026,8 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable, LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false), LLVMConstNull(LLVMInt1Type()), }; - return LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, ""); + LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, ""); + return LLVMBuildBitCast(g->builder, uncasted_result, instruction->base.value.type->type_ref, ""); } static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) { -- cgit v1.2.3