From 2c2bc9c8df3d8205caf4e49f0fcf7496b3d71444 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Wed, 1 Nov 2023 19:43:03 +0100 Subject: [PATCH 1/2] wasm: fix bitcasting to -and from arrays Arrays are currently always passed by reference, this means that we always keep the value in linear memory and never load it to Wasm's stack. Scalar values however do get lowered to Wasm's stack. This means when bitcasting from an array to a scalar value, we must load the memory of the array as such scalar type. To bitcast a scalar type to an array, we allocate a new temporary in the linear data segment, and then store the scalar value there. --- src/arch/wasm/CodeGen.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 9da4d3003b..21ddd91120 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -3814,6 +3814,16 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const bitcast_result = try func.bitcast(wanted_ty, given_ty, operand); break :result try bitcast_result.toLocal(func, wanted_ty); } + const mod = func.bin_file.base.options.module.?; + if (isByRef(given_ty, mod) and !isByRef(wanted_ty, mod)) { + const loaded_memory = try func.load(operand, wanted_ty, 0); + break :result try loaded_memory.toLocal(func, wanted_ty); + } + if (!isByRef(given_ty, mod) and isByRef(wanted_ty, mod)) { + const stack_memory = try func.allocStack(wanted_ty); + try func.store(stack_memory, operand, given_ty, 0); + break :result stack_memory; + } break :result func.reuseOperand(ty_op.operand, operand); }; func.finishAir(inst, result, &.{ty_op.operand}); From db1825e93159f753207f255ad6709074fece6dc5 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Wed, 1 Nov 2023 19:45:22 +0100 Subject: [PATCH 2/2] wasm: re-enable regressed tests --- lib/test_runner.zig | 4 +--- test/behavior/bugs/1851.zig | 1 - test/behavior/struct.zig | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/test_runner.zig b/lib/test_runner.zig index 7ed29ebfb1..18608d298a 100644 --- a/lib/test_runner.zig +++ b/lib/test_runner.zig @@ -13,9 +13,7 @@ var cmdline_buffer: [4096]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer); pub fn main() void { - if (builtin.zig_backend == .stage2_aarch64 or - builtin.zig_backend == .stage2_wasm) - { + if (builtin.zig_backend == .stage2_aarch64) { return mainSimple() catch @panic("test failure"); } diff --git a/test/behavior/bugs/1851.zig b/test/behavior/bugs/1851.zig index 787c8cbd86..a61e17e8df 100644 --- a/test/behavior/bugs/1851.zig +++ b/test/behavior/bugs/1851.zig @@ -7,7 +7,6 @@ test "allocation and looping over 3-byte integer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_llvm and builtin.os.tag == .macos) { return error.SkipZigTest; // TODO diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index b46164a1b5..e07b10054c 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1351,7 +1351,6 @@ test "under-aligned struct field" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; const U = extern union { fd: i32,