From bf28a47cf23a60539f7cc6cb87309d1a7fa01c18 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 14 Jul 2022 19:48:42 +0200 Subject: [PATCH 1/4] wasm: pass correct abi-size for scalar values When returning an aggregate type that contains a scalar value (nested), its abi-size is passed by bits, rather than bytes to `buildOpcode`. --- src/arch/wasm/CodeGen.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index ab73e23783..1fbf9f5785 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1674,7 +1674,7 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { try self.emitWValue(operand); const opcode = buildOpcode(.{ .op = .load, - .width = @intCast(u8, scalar_type.abiSize(self.target)), + .width = @intCast(u8, scalar_type.abiSize(self.target) * 8), .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned, .valtype1 = typeToValtype(scalar_type, self.target), }); From 200b2e4ee108d388e0db2120aee38f83d1c7abdb Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Sun, 17 Jul 2022 15:10:56 +0200 Subject: [PATCH 2/4] llvm: correctly lower c-abi for Wasm target When lowering the return type for Wasm if the calling convention is `C`, it now correctly lower it according to what clang does as specified in: https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md This makes use of the same logic as the Wasm backend, ensuring the generated code does not diverge in function signatures. When passing arguments accross the C-ABI for the Wasm target, we want slightly different behavior than x86_64. For instance: a struct with multiple fields must always be passed by reference, even if its ABI size fits in a single integer. However, we do pass larger integers such as 128bit by value, which LLVM will correctly lower to use double arguments instead. --- src/arch/wasm/abi.zig | 55 +++++++++++++++++++++---------------------- src/codegen/llvm.zig | 31 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/arch/wasm/abi.zig b/src/arch/wasm/abi.zig index 77ba695a90..f86d82d70e 100644 --- a/src/arch/wasm/abi.zig +++ b/src/arch/wasm/abi.zig @@ -23,8 +23,6 @@ pub fn classifyType(ty: Type, target: Target) [2]Class { if (!ty.hasRuntimeBitsIgnoreComptime()) return none; switch (ty.zigTypeTag()) { .Struct => { - // When the (maybe) scalar type exceeds max 'direct' integer size - if (ty.abiSize(target) > 8) return memory; // When the struct type is non-scalar if (ty.structFieldCount() > 1) return memory; // When the struct's alignment is non-natural @@ -34,56 +32,57 @@ pub fn classifyType(ty: Type, target: Target) [2]Class { return memory; } } - if (field.ty.isInt() or field.ty.isAnyFloat()) { - return direct; - } return classifyType(field.ty, target); }, .Int, .Enum, .ErrorSet, .Vector => { const int_bits = ty.intInfo(target).bits; if (int_bits <= 64) return direct; - if (int_bits > 64 and int_bits <= 128) return .{ .direct, .direct }; + if (int_bits <= 128) return .{ .direct, .direct }; return memory; }, .Float => { const float_bits = ty.floatBits(target); if (float_bits <= 64) return direct; - if (float_bits > 64 and float_bits <= 128) return .{ .direct, .direct }; + if (float_bits <= 128) return .{ .direct, .direct }; return memory; }, .Bool => return direct, .Array => return memory, - .ErrorUnion => { - const has_tag = ty.errorUnionSet().hasRuntimeBitsIgnoreComptime(); - const has_pl = ty.errorUnionPayload().hasRuntimeBitsIgnoreComptime(); - if (!has_pl) return direct; - if (!has_tag) { - return classifyType(ty.errorUnionPayload(), target); - } - return memory; - }, + // .ErrorUnion => { + // const has_tag = ty.errorUnionSet().hasRuntimeBitsIgnoreComptime(); + // const has_pl = ty.errorUnionPayload().hasRuntimeBitsIgnoreComptime(); + // if (!has_pl) return direct; + // if (!has_tag) { + // return classifyType(ty.errorUnionPayload(), target); + // } + // return memory; + // }, .Optional => { - if (ty.isPtrLikeOptional()) return direct; - var buf: Type.Payload.ElemType = undefined; - const pl_has_bits = ty.optionalChild(&buf).hasRuntimeBitsIgnoreComptime(); - if (!pl_has_bits) return direct; - return memory; + std.debug.assert(ty.isPtrLikeOptional()); + return direct; + // var buf: Type.Payload.ElemType = undefined; + // const pl_has_bits = ty.optionalChild(&buf).hasRuntimeBitsIgnoreComptime(); + // if (!pl_has_bits) return direct; + // return memory; }, .Pointer => { - // Slices act like struct and will be passed by reference - if (ty.isSlice()) return memory; + // // Slices act like struct and will be passed by reference + // if (ty.isSlice()) return memory; return direct; }, .Union => { const layout = ty.unionGetLayout(target); - if (layout.payload_size == 0 and layout.tag_size != 0) { - return classifyType(ty.unionTagTypeSafety().?, target); - } + std.debug.assert(layout.tag_size == 0); + // if (layout.payload_size == 0 and layout.tag_size != 0) { + // return classifyType(ty.unionTagType().?, target); + // } if (ty.unionFields().count() > 1) return memory; return classifyType(ty.unionFields().values()[0].ty, target); }, - .AnyFrame, .Frame => return direct, - + // .AnyFrame, .Frame => return direct, + .ErrorUnion, + .Frame, + .AnyFrame, .NoReturn, .Void, .Type, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index c80ef3adc0..8dbb9cae31 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -22,6 +22,7 @@ const Type = @import("../type.zig").Type; const LazySrcLoc = Module.LazySrcLoc; const CType = @import("../type.zig").CType; const x86_64_abi = @import("../arch/x86_64/abi.zig"); +const wasm_c_abi = @import("../arch/wasm/abi.zig"); const Error = error{ OutOfMemory, CodegenFail }; @@ -9093,6 +9094,10 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory, else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, }, + .wasm32 => { + const classes = wasm_c_abi.classifyType(fn_info.return_type, target); + return classes[0] == .indirect; + }, else => return false, // TODO investigate C ABI for other architectures }, else => return false, @@ -9197,6 +9202,20 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm. return dg.context.structType(&llvm_types_buffer, llvm_types_index, .False); }, }, + .wasm32 => { + if (is_scalar) { + return dg.lowerType(fn_info.return_type); + } + const classes = wasm_c_abi.classifyType(fn_info.return_type, target); + if (classes[0] == .indirect or classes[0] == .none) { + return dg.context.voidType(); + } + + assert(classes[0] == .direct and classes[1] == .none); + const scalar_type = wasm_c_abi.scalarType(fn_info.return_type, target); + const abi_size = scalar_type.abiSize(target); + return dg.context.intType(@intCast(c_uint, abi_size * 8)); + }, // TODO investigate C ABI for other architectures else => return dg.lowerType(fn_info.return_type), } @@ -9372,6 +9391,18 @@ const ParamTypeIterator = struct { return .multiple_llvm_ints; }, }, + .wasm32 => { + it.zig_index += 1; + it.llvm_index += 1; + if (is_scalar) { + return .byval; + } + const classes = wasm_c_abi.classifyType(ty, it.target); + if (classes[0] == .indirect) { + return .byref; + } + return .abi_sized_int; + }, // TODO investigate C ABI for other architectures else => { it.zig_index += 1; From 6b4d4c70fd8b8696eb1220438b89f769a3c3a908 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Tue, 19 Jul 2022 22:01:32 +0200 Subject: [PATCH 3/4] wasm: Only allow lowering C-ABI compatible types --- src/arch/wasm/abi.zig | 20 +------------------- src/codegen/llvm.zig | 5 +---- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/src/arch/wasm/abi.zig b/src/arch/wasm/abi.zig index f86d82d70e..b1e99f4f92 100644 --- a/src/arch/wasm/abi.zig +++ b/src/arch/wasm/abi.zig @@ -48,38 +48,20 @@ pub fn classifyType(ty: Type, target: Target) [2]Class { }, .Bool => return direct, .Array => return memory, - // .ErrorUnion => { - // const has_tag = ty.errorUnionSet().hasRuntimeBitsIgnoreComptime(); - // const has_pl = ty.errorUnionPayload().hasRuntimeBitsIgnoreComptime(); - // if (!has_pl) return direct; - // if (!has_tag) { - // return classifyType(ty.errorUnionPayload(), target); - // } - // return memory; - // }, .Optional => { std.debug.assert(ty.isPtrLikeOptional()); return direct; - // var buf: Type.Payload.ElemType = undefined; - // const pl_has_bits = ty.optionalChild(&buf).hasRuntimeBitsIgnoreComptime(); - // if (!pl_has_bits) return direct; - // return memory; }, .Pointer => { - // // Slices act like struct and will be passed by reference - // if (ty.isSlice()) return memory; + std.debug.assert(!ty.isSlice()); return direct; }, .Union => { const layout = ty.unionGetLayout(target); std.debug.assert(layout.tag_size == 0); - // if (layout.payload_size == 0 and layout.tag_size != 0) { - // return classifyType(ty.unionTagType().?, target); - // } if (ty.unionFields().count() > 1) return memory; return classifyType(ty.unionFields().values()[0].ty, target); }, - // .AnyFrame, .Frame => return direct, .ErrorUnion, .Frame, .AnyFrame, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 8dbb9cae31..b4bf8449ce 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -9094,10 +9094,7 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory, else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, }, - .wasm32 => { - const classes = wasm_c_abi.classifyType(fn_info.return_type, target); - return classes[0] == .indirect; - }, + .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect, else => return false, // TODO investigate C ABI for other architectures }, else => return false, From a7417f783953c0f39e929a690926336e5c495f30 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Wed, 20 Jul 2022 21:51:18 +0200 Subject: [PATCH 4/4] wasm: Enable C-ABI tests for self-hosted compiler --- build.zig | 15 +++++++++++++- test/stage1/c_abi/build_wasm.zig | 24 ++++++++++++++++++++++ test/standalone.zig | 6 ++++++ test/tests.zig | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 test/stage1/c_abi/build_wasm.zig diff --git a/build.zig b/build.zig index 9cfa57223c..4f60f26756 100644 --- a/build.zig +++ b/build.zig @@ -484,7 +484,20 @@ pub fn build(b: *Builder) !void { )); toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes)); - toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, enable_macos_sdk, target, omit_stage2)); + toolchain_step.dependOn(tests.addStandaloneTests( + b, + test_filter, + modes, + skip_non_native, + enable_macos_sdk, + target, + omit_stage2, + b.enable_darling, + b.enable_qemu, + b.enable_rosetta, + b.enable_wasmtime, + b.enable_wine, + )); toolchain_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk, omit_stage2)); toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes)); toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes)); diff --git a/test/stage1/c_abi/build_wasm.zig b/test/stage1/c_abi/build_wasm.zig new file mode 100644 index 0000000000..b288650855 --- /dev/null +++ b/test/stage1/c_abi/build_wasm.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const rel_opts = b.standardReleaseOptions(); + const target: std.zig.CrossTarget = .{ .cpu_arch = .wasm32, .os_tag = .wasi }; + b.use_stage1 = false; + + const c_obj = b.addObject("cfuncs", null); + c_obj.addCSourceFile("cfuncs.c", &[_][]const u8{"-std=c99"}); + c_obj.setBuildMode(rel_opts); + c_obj.linkSystemLibrary("c"); + c_obj.setTarget(target); + + const main = b.addTest("main.zig"); + main.setBuildMode(rel_opts); + main.addObject(c_obj); + main.setTarget(target); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&main.step); + + b.default_step.dependOn(test_step); +} diff --git a/test/standalone.zig b/test/standalone.zig index a53a151130..f3a6f73423 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -41,6 +41,12 @@ pub fn addCases(cases: *tests.StandaloneContext) void { if (builtin.cpu.arch == .x86_64) { cases.addBuildFile("test/stage1/c_abi/build.zig", .{}); } + // C ABI tests only pass for the Wasm target when using stage2 + cases.addBuildFile("test/stage1/c_abi/build_wasm.zig", .{ + .requires_stage2 = true, + .use_emulation = true, + }); + cases.addBuildFile("test/standalone/c_compiler/build.zig", .{ .build_modes = true, .cross_targets = true, diff --git a/test/tests.zig b/test/tests.zig index e70da1d070..dcc891f878 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -463,6 +463,11 @@ pub fn addStandaloneTests( enable_macos_sdk: bool, target: std.zig.CrossTarget, omit_stage2: bool, + enable_darling: bool, + enable_qemu: bool, + enable_rosetta: bool, + enable_wasmtime: bool, + enable_wine: bool, ) *build.Step { const cases = b.allocator.create(StandaloneContext) catch unreachable; cases.* = StandaloneContext{ @@ -475,6 +480,11 @@ pub fn addStandaloneTests( .enable_macos_sdk = enable_macos_sdk, .target = target, .omit_stage2 = omit_stage2, + .enable_darling = enable_darling, + .enable_qemu = enable_qemu, + .enable_rosetta = enable_rosetta, + .enable_wasmtime = enable_wasmtime, + .enable_wine = enable_wine, }; standalone.addCases(cases); @@ -962,6 +972,11 @@ pub const StandaloneContext = struct { enable_macos_sdk: bool, target: std.zig.CrossTarget, omit_stage2: bool, + enable_darling: bool = false, + enable_qemu: bool = false, + enable_rosetta: bool = false, + enable_wasmtime: bool = false, + enable_wine: bool = false, pub fn addC(self: *StandaloneContext, root_src: []const u8) void { self.addAllArgs(root_src, true); @@ -976,6 +991,7 @@ pub const StandaloneContext = struct { cross_targets: bool = false, requires_macos_sdk: bool = false, requires_stage2: bool = false, + use_emulation: bool = false, }) void { const b = self.b; @@ -1007,6 +1023,24 @@ pub const StandaloneContext = struct { zig_args.append(target_arg) catch unreachable; } + if (features.use_emulation) { + if (self.enable_darling) { + zig_args.append("-fdarling") catch unreachable; + } + if (self.enable_qemu) { + zig_args.append("-fqemu") catch unreachable; + } + if (self.enable_rosetta) { + zig_args.append("-frosetta") catch unreachable; + } + if (self.enable_wasmtime) { + zig_args.append("-fwasmtime") catch unreachable; + } + if (self.enable_wine) { + zig_args.append("-fwine") catch unreachable; + } + } + const modes = if (features.build_modes) self.modes else &[1]Mode{.Debug}; for (modes) |mode| { const arg = switch (mode) {