authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-22 20:07:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-26 15:57:07-07:00
log3faa550dc2d6b9a3a8e3be22fc927c8c5682d6c8
tree59d3607cc15c1dbe1fc1928cdafacd29a75c1e1a
parente45b10f3d453f3bd8326631ee786a2ef247e8953

stage2 async progress

After analyzing function body, check call instructions and determine whether it is an async function or not. LLVM backend: support lowering trivial async functions

4 files changed, 155 insertions(+), 44 deletions(-)

BRANCH_TODO+7
......@@ -1,3 +1,9 @@
1 * calling getFuncAsyncStatus is triggering machine code lowering too early,
2 because the function which does not yet know its own async status may get called
3 recursively by one of the callees.
4 - don't do any lowering to machine code until async status has been resolved for
5 the local call graph sub-tree.
6
17 * detect when a called function is async and make the caller async too
28 * generate the async frame type *after* lowering the function to LLVM IR
39 * calculate frame size after llvm lowering, ability to inspect with `@sizeOf`
......@@ -11,3 +17,4 @@
1117 * use function pointers instead of resume index to...
1218 - reduce the number of runtime branches from 2 to 1
1319 - pass function arguments as normal arguments to the first segment
20
src/Module.zig+60-9
......@@ -5692,10 +5692,9 @@ pub fn analyzeFnBody(mod: *Module, func_index: Fn.Index, arena: Allocator) SemaE
56925692 inner_block.error_return_trace_index = error_return_trace_index;
56935693
56945694 sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
5695 // TODO make these unreachable instead of @panic
5696 error.NeededSourceLocation => @panic("zig compiler bug: NeededSourceLocation"),
5697 error.GenericPoison => @panic("zig compiler bug: GenericPoison"),
5698 error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"),
5695 error.NeededSourceLocation => unreachable,
5696 error.GenericPoison => unreachable,
5697 error.ComptimeReturn => unreachable,
56995698 else => |e| return e,
57005699 };
57015700
......@@ -5717,11 +5716,10 @@ pub fn analyzeFnBody(mod: *Module, func_index: Fn.Index, arena: Allocator) SemaE
57175716 !sema.fn_ret_ty.isError(mod))
57185717 {
57195718 sema.setupErrorReturnTrace(&inner_block, last_arg_index) catch |err| switch (err) {
5720 // TODO make these unreachable instead of @panic
5721 error.NeededSourceLocation => @panic("zig compiler bug: NeededSourceLocation"),
5722 error.GenericPoison => @panic("zig compiler bug: GenericPoison"),
5723 error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"),
5724 error.ComptimeBreak => @panic("zig compiler bug: ComptimeBreak"),
5719 error.NeededSourceLocation => unreachable,
5720 error.GenericPoison => unreachable,
5721 error.ComptimeReturn => unreachable,
5722 error.ComptimeBreak => unreachable,
57255723 else => |e| return e,
57265724 };
57275725 }
......@@ -5742,6 +5740,59 @@ pub fn analyzeFnBody(mod: *Module, func_index: Fn.Index, arena: Allocator) SemaE
57425740 sema.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)] = main_block_index;
57435741
57445742 func.state = .success;
5743
5744 // Next we must look at all the function calls and determine two pieces of information:
5745 // * for each call, whether the called function is async
5746 // * whether this function making the calls is async
5747 // This happens *after* setting func.state to success above so that any
5748 // recursive check on this function will not cause an infinite loop.
5749 // Both of these pieces of information are needed by backends for machine code lowering.
5750 {
5751 const air = sema.getTmpAir();
5752 const air_tags = air.instructions.items(.tag);
5753 const air_datas = air.instructions.items(.data);
5754 for (air_tags, 0..) |air_tag, inst| {
5755 var is_suspend_point = false;
5756 const callee = switch (air_tag) {
5757 .call, .call_always_tail, .call_never_tail, .call_never_inline => c: {
5758 is_suspend_point = true;
5759 const pl_op = air_datas[inst].pl_op;
5760 break :c pl_op.operand;
5761 },
5762 .call_async => c: {
5763 const pl_op = air_datas[inst].pl_op;
5764 break :c pl_op.operand;
5765 },
5766 .call_async_alloc => c: {
5767 const ty_pl = air.instructions.items(.data)[inst].ty_pl;
5768 const extra = air.extraData(Air.AsyncCallAlloc, ty_pl.payload);
5769 break :c extra.data.callee;
5770 },
5771 else => continue,
5772 };
5773 const callee_val = (try air.value(callee, mod)) orelse continue;
5774 const callee_decl_index = switch (mod.intern_pool.indexToKey(callee_val.toIntern())) {
5775 .extern_func => continue, // extern functions cannot be async
5776 .func => |f| mod.funcPtr(f.index).owner_decl,
5777 else => unreachable,
5778 };
5779 const callee_decl = mod.declPtr(callee_decl_index);
5780 const callee_func_index = callee_decl.getOwnedFunctionIndex(mod).unwrap() orelse continue;
5781 const callee_status = sema.getFuncAsyncStatus(callee_func_index) catch |err| switch (err) {
5782 error.NeededSourceLocation => unreachable,
5783 error.GenericPoison => unreachable,
5784 error.ComptimeReturn => unreachable,
5785 error.ComptimeBreak => unreachable,
5786 error.AnalysisFail => continue, // treat this callee as non-async
5787 else => |e| return e,
5788 };
5789 if (is_suspend_point) switch (callee_status) {
5790 .unknown => unreachable,
5791 .not_async => continue,
5792 .yes_async => func.async_status = .yes_async,
5793 };
5794 }
5795 }
57455796 if (func.async_status == .unknown) {
57465797 func.async_status = .not_async;
57475798 }
src/Sema.zig+27-2
......@@ -9322,7 +9322,8 @@ fn funcCommon(
93229322 return sema.addType(fn_ty);
93239323 }
93249324
9325 const is_inline = fn_ty.fnCallingConvention(mod) == .Inline;
9325 const init_cc = fn_ty.fnCallingConvention(mod);
9326 const is_inline = init_cc == .Inline;
93269327 const anal_state: Module.Fn.Analysis = if (is_inline) .inline_only else .none;
93279328
93289329 const comptime_args: ?[*]TypedValue = if (sema.comptime_args_fn_inst == func_inst) blk: {
......@@ -9334,7 +9335,7 @@ fn funcCommon(
93349335 const generic_owner_decl = if (comptime_args == null) .none else new_func.generic_owner_decl;
93359336 new_func.* = .{
93369337 .state = anal_state,
9337 .async_status = .unknown,
9338 .async_status = initAsyncSatus(init_cc),
93389339 .zir_body_inst = func_inst,
93399340 .owner_decl = sema.owner_decl_index,
93409341 .generic_owner_decl = generic_owner_decl,
......@@ -9353,6 +9354,14 @@ fn funcCommon(
93539354 } })).toValue());
93549355}
93559356
9357fn initAsyncSatus(cc: std.builtin.CallingConvention) Module.Fn.AsyncStatus {
9358 return switch (cc) {
9359 .Unspecified => .unknown,
9360 .Async => .yes_async,
9361 else => .not_async,
9362 };
9363}
9364
93569365fn analyzeParameter(
93579366 sema: *Sema,
93589367 block: *Block,
......@@ -30557,6 +30566,22 @@ fn ensureFuncBodyAnalyzed(sema: *Sema, func: Module.Fn.Index) CompileError!void
3055730566 };
3055830567}
3055930568
30569pub fn getFuncAsyncStatus(sema: *Sema, func_index: Module.Fn.Index) CompileError!Module.Fn.AsyncStatus {
30570 const mod = sema.mod;
30571 const func = mod.funcPtr(func_index);
30572 switch (func.async_status) {
30573 .yes_async => return .yes_async,
30574 .not_async => return .not_async,
30575 .unknown => {
30576 try ensureFuncBodyAnalyzed(sema, func_index);
30577 switch (func.async_status) {
30578 .yes_async => return .yes_async,
30579 .not_async, .unknown => return .not_async,
30580 }
30581 },
30582 }
30583}
30584
3056030585fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {
3056130586 const mod = sema.mod;
3056230587 var anon_decl = try block.startAnonDecl();
src/codegen/llvm.zig+61-33
......@@ -961,7 +961,11 @@ pub const Object = struct {
961961 defer args.deinit();
962962
963963 {
964 var llvm_arg_i = @as(c_uint, @intFromBool(ret_ptr != null)) + @intFromBool(err_return_tracing);
964 var llvm_arg_i =
965 @as(c_uint, @intFromBool(ret_ptr != null)) +
966 @intFromBool(err_return_tracing) +
967 @intFromBool(func.isAsync());
968
965969 var it = iterateParamTypes(o, fn_info);
966970 while (it.next()) |lowering| switch (lowering) {
967971 .no_bits => continue,
......@@ -1215,25 +1219,27 @@ pub const Object = struct {
12151219 };
12161220 defer fg.deinit();
12171221
1218 if (func.isAsync()) {
1219 const frame_ty = try mod.asyncFrameType(func_index);
1220 const frame_size = frame_ty.abiSize(mod);
1221 const llvm_usize = dg.context.intType(target.ptrBitWidth());
1222 const size_val = llvm_usize.constInt(frame_size, .False);
1223 llvm_func.functionSetPrefixData(size_val);
1222 const llvm_usize = o.context.intType(target.ptrBitWidth());
12241223
1225 const async_preamble_bb = dg.context.appendBasicBlock(llvm_func, "AsyncSwitch");
1226 const bad_resume_bb = dg.context.appendBasicBlock(llvm_func, "BadResume");
1224 if (func.isAsync()) {
1225 const bad_resume_bb = o.context.appendBasicBlock(llvm_func, "BadResume");
12271226 builder.positionBuilderAtEnd(bad_resume_bb);
12281227 _ = builder.buildUnreachable(); // TODO make this a safety panic
12291228
1230 builder.positionBuilderAtEnd(async_preamble_bb);
1229 builder.positionBuilderAtEnd(entry_block);
12311230 const l = asyncFrameLayout();
1232 const frame_llvm_ty = try dg.lowerType(frame_ty);
1231 const frame_llvm_ty = try o.lowerAsyncFrameHeader(fn_info.return_type.toType());
12331232 const frame_ptr = llvm_func.getParam(0);
12341233 fg.resume_index_ptr = builder.buildStructGEP(frame_llvm_ty, frame_ptr, l.resume_index, "");
12351234 const resume_index = builder.buildLoad(llvm_usize, fg.resume_index_ptr, "");
12361235 fg.async_switch = builder.buildSwitch(resume_index, bad_resume_bb, 4);
1236
1237 const init_bb = o.context.appendBasicBlock(llvm_func, "Init");
1238 const new_block_index = fg.resume_block_index;
1239 fg.resume_block_index += 1;
1240 const new_block_index_llvm_val = llvm_usize.constInt(new_block_index, .False);
1241 fg.async_switch.addCase(new_block_index_llvm_val, init_bb);
1242 builder.positionBuilderAtEnd(init_bb);
12371243 }
12381244
12391245 fg.genBody(air.getMainBody()) catch |err| switch (err) {
......@@ -1246,6 +1252,12 @@ pub const Object = struct {
12461252 else => |e| return e,
12471253 };
12481254
1255 if (func.isAsync()) {
1256 const frame_size = 3 * (target.ptrBitWidth() / 8);
1257 const size_val = llvm_usize.constInt(frame_size, .False);
1258 llvm_func.functionSetPrefixData(size_val);
1259 }
1260
12491261 try o.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index));
12501262 }
12511263
......@@ -2499,16 +2511,20 @@ pub const Object = struct {
24992511 const mod = o.module;
25002512 const gpa = o.gpa;
25012513 const decl = mod.declPtr(decl_index);
2502 const zig_fn_type = decl.ty;
25032514 const gop = try o.decl_map.getOrPut(gpa, decl_index);
25042515 if (gop.found_existing) return gop.value_ptr.*;
25052516
25062517 assert(decl.has_tv);
2507 const fn_info = mod.typeToFunc(zig_fn_type).?;
2518 const func = decl.getOwnedFunction(mod).?;
2519 const zig_fn_type = decl.ty;
2520 const fn_info = info: {
2521 var info = mod.typeToFunc(zig_fn_type).?;
2522 if (func.isAsync()) info.cc = .Async;
2523 break :info info;
2524 };
25082525 const target = mod.getTarget();
25092526 const sret = firstParamSRet(fn_info, mod);
2510
2511 const fn_type = try o.lowerType(zig_fn_type);
2527 const fn_type = try o.lowerTypeFn(fn_info);
25122528
25132529 const fqn = try decl.getFullyQualifiedName(mod);
25142530
......@@ -2531,32 +2547,33 @@ pub const Object = struct {
25312547 }
25322548 }
25332549
2550 var llvm_param_i: u32 = 0;
2551
25342552 if (sret) {
2535 o.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0
2536 o.addArgAttr(llvm_fn, 0, "noalias");
2553 o.addArgAttr(llvm_fn, llvm_param_i, "nonnull"); // Sret pointers must not be address 0
2554 o.addArgAttr(llvm_fn, llvm_param_i, "noalias");
25372555
25382556 const raw_llvm_ret_ty = try o.lowerType(fn_info.return_type.toType());
25392557 llvm_fn.addSretAttr(raw_llvm_ret_ty);
2558
2559 llvm_param_i += 1;
25402560 }
25412561
25422562 const err_return_tracing = fn_info.return_type.toType().isError(mod) and
25432563 mod.comp.bin_file.options.error_return_tracing;
25442564
25452565 if (err_return_tracing) {
2546 o.addArgAttr(llvm_fn, @intFromBool(sret), "nonnull");
2566 o.addArgAttr(llvm_fn, llvm_param_i, "nonnull");
2567 llvm_param_i += 1;
25472568 }
25482569
25492570 switch (fn_info.cc) {
2550 .Unspecified, .Inline => {
2571 .Unspecified, .Inline, .Async => {
25512572 llvm_fn.setFunctionCallConv(.Fast);
25522573 },
25532574 .Naked => {
25542575 o.addFnAttr(llvm_fn, "naked");
25552576 },
2556 .Async => {
2557 llvm_fn.setFunctionCallConv(.Fast);
2558 @panic("TODO: LLVM backend lower async function");
2559 },
25602577 else => {
25612578 llvm_fn.setFunctionCallConv(toLlvmCallConv(fn_info.cc, target));
25622579 },
......@@ -2577,8 +2594,7 @@ pub const Object = struct {
25772594 // because functions with bodies are handled in `updateFunc`.
25782595 if (is_extern) {
25792596 var it = iterateParamTypes(o, fn_info);
2580 it.llvm_index += @intFromBool(sret);
2581 it.llvm_index += @intFromBool(err_return_tracing);
2597 it.llvm_index += llvm_param_i;
25822598 while (it.next()) |lowering| switch (lowering) {
25832599 .byval => {
25842600 const param_index = it.zig_index - 1;
......@@ -3052,7 +3068,7 @@ pub const Object = struct {
30523068 llvm_union_ty.structSetBody(&llvm_fields, llvm_fields_len, .False);
30533069 return llvm_union_ty;
30543070 },
3055 .Fn => return lowerTypeFn(o, t),
3071 .Fn => return lowerTypeFn(o, mod.typeToFunc(t).?),
30563072 .ComptimeInt => unreachable,
30573073 .ComptimeFloat => unreachable,
30583074 .Type => unreachable,
......@@ -3089,12 +3105,16 @@ pub const Object = struct {
30893105 }
30903106
30913107 fn lowerAsyncFrameHeader(o: *Object, ret_ty: Type) !*llvm.Type {
3108 const mod = o.module;
30923109 const opaque_ptr_ty = o.context.pointerType(0);
30933110 const l = asyncFrameLayout();
30943111 var fields: [4]*llvm.Type = undefined;
30953112 fields[l.fn_ptr] = opaque_ptr_ty;
30963113 fields[l.resume_index] = try o.lowerType(Type.usize);
30973114 fields[l.awaiter] = opaque_ptr_ty;
3115 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
3116 return o.context.structType(&fields, 3, .False);
3117 }
30983118 fields[l.ret_val] = try o.lowerType(ret_ty);
30993119 return o.context.structType(&fields, fields.len, .False);
31003120 }
......@@ -3122,23 +3142,29 @@ pub const Object = struct {
31223142 return llvm_struct_ty;
31233143 }
31243144
3125 fn lowerTypeFn(o: *Object, fn_ty: Type) Allocator.Error!*llvm.Type {
3145 fn lowerTypeFn(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!*llvm.Type {
31263146 const mod = o.module;
3127 const fn_info = mod.typeToFunc(fn_ty).?;
31283147 const llvm_ret_ty = try lowerFnRetTy(o, fn_info);
31293148
31303149 var llvm_params = std.ArrayList(*llvm.Type).init(o.gpa);
31313150 defer llvm_params.deinit();
31323151
3152 try llvm_params.ensureUnusedCapacity(3);
3153
31333154 if (firstParamSRet(fn_info, mod)) {
3134 try llvm_params.append(o.context.pointerType(0));
3155 llvm_params.appendAssumeCapacity(o.context.pointerType(0));
3156 }
3157
3158 if (fn_info.cc == .Async) {
3159 // frame_ptr
3160 llvm_params.appendAssumeCapacity(o.context.pointerType(0));
31353161 }
31363162
31373163 if (fn_info.return_type.toType().isError(mod) and
31383164 mod.comp.bin_file.options.error_return_tracing)
31393165 {
31403166 const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType());
3141 try llvm_params.append(try o.lowerType(ptr_ty));
3167 llvm_params.appendAssumeCapacity(try o.lowerType(ptr_ty));
31423168 }
31433169
31443170 var it = iterateParamTypes(o, fn_info);
......@@ -4860,9 +4886,11 @@ pub const FuncGen = struct {
48604886 }
48614887
48624888 fn genSuspendBegin(fg: *FuncGen, name_hint: [*:0]const u8) *llvm.BasicBlock {
4863 const target = fg.getTarget();
4864 const llvm_usize = fg.dg.context.intType(target.ptrBitWidth());
4865 const resume_bb = fg.context.appendBasicBlock(fg.llvm_func, name_hint);
4889 const o = fg.dg.object;
4890 const mod = o.module;
4891 const target = mod.getTarget();
4892 const llvm_usize = o.context.intType(target.ptrBitWidth());
4893 const resume_bb = o.context.appendBasicBlock(fg.llvm_func, name_hint);
48664894 const new_block_index = fg.resume_block_index;
48674895 fg.resume_block_index += 1;
48684896 const new_block_index_llvm_val = llvm_usize.constInt(new_block_index, .False);