Commit 95845ba2ac

Veikka Tuominen <git@vexu.eu>
2024-05-20 13:38:59
llvm: fix `@wasmMemory{Size,Grow}` for wasm64
Closes #19942
1 parent 4a09703
Changed files (5)
doc/langref.html.in
@@ -9040,7 +9040,7 @@ test "integer cast panic" {
       {#header_close#}
 
       {#header_open|@wasmMemorySize#}
-      <pre>{#syntax#}@wasmMemorySize(index: u32) u32{#endsyntax#}</pre>
+      <pre>{#syntax#}@wasmMemorySize(index: u32) usize{#endsyntax#}</pre>
       <p>
       This function returns the size of the Wasm memory identified by {#syntax#}index{#endsyntax#} as
       an unsigned value in units of Wasm pages. Note that each Wasm page is 64KB in size.
@@ -9054,7 +9054,7 @@ test "integer cast panic" {
       {#header_close#}
 
       {#header_open|@wasmMemoryGrow#}
-      <pre>{#syntax#}@wasmMemoryGrow(index: u32, delta: u32) i32{#endsyntax#}</pre>
+      <pre>{#syntax#}@wasmMemoryGrow(index: u32, delta: usize) isize{#endsyntax#}</pre>
       <p>
       This function increases the size of the Wasm memory identified by {#syntax#}index{#endsyntax#} by
       {#syntax#}delta{#endsyntax#} in units of unsigned number of Wasm pages. Note that each Wasm page
lib/std/zig/AstGen.zig
@@ -9461,7 +9461,7 @@ fn builtinCall(
         },
         .wasm_memory_grow => {
             const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
-            const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[1]);
+            const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, params[1]);
             const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{
                 .node = gz.nodeIndexToRelative(node),
                 .lhs = index_arg,
src/codegen/llvm.zig
@@ -7499,7 +7499,8 @@ pub const FuncGen = struct {
         const o = self.dg.object;
         const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
         const index = pl_op.payload;
-        return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{.i32}, &.{
+        const llvm_usize = try o.lowerType(Type.usize);
+        return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{
             try o.builder.intValue(.i32, index),
         }, "");
     }
@@ -7508,7 +7509,8 @@ pub const FuncGen = struct {
         const o = self.dg.object;
         const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
         const index = pl_op.payload;
-        return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{.i32}, &.{
+        const llvm_isize = try o.lowerType(Type.isize);
+        return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{
             try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),
         }, "");
     }
src/Air.zig
@@ -782,13 +782,13 @@ pub const Inst = struct {
         field_parent_ptr,
 
         /// Implements @wasmMemorySize builtin.
-        /// Result type is always `u32`,
+        /// Result type is always `usize`,
         /// Uses the `pl_op` field, payload represents the index of the target memory.
         /// The operand is unused and always set to `Ref.none`.
         wasm_memory_size,
 
         /// Implements @wasmMemoryGrow builtin.
-        /// Result type is always `i32`,
+        /// Result type is always `isize`,
         /// Uses the `pl_op` field, payload represents the index of the target memory.
         wasm_memory_grow,
 
@@ -1471,8 +1471,8 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)
         .save_err_return_trace_index,
         => return Type.usize,
 
-        .wasm_memory_grow => return Type.i32,
-        .wasm_memory_size => return Type.u32,
+        .wasm_memory_grow => return Type.isize,
+        .wasm_memory_size => return Type.usize,
 
         .int_from_bool => return Type.u1,
 
src/Sema.zig
@@ -26238,7 +26238,7 @@ fn zirWasmMemoryGrow(
     const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{
         .needed_comptime_reason = "wasm memory size index must be comptime-known",
     }));
-    const delta = try sema.coerce(block, Type.u32, try sema.resolveInst(extra.rhs), delta_src);
+    const delta = try sema.coerce(block, Type.usize, try sema.resolveInst(extra.rhs), delta_src);
 
     try sema.requireRuntimeBlock(block, builtin_src, null);
     return block.addInst(.{