Skip to content

Commit 033a712

Browse files
daxpeddaLiamolucko
andcommitted
Use smallest possible alignment
Co-Authored-By: Liam Murphy <[email protected]>
1 parent 36fcc41 commit 033a712

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

crates/cli-support/src/js/binding.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,12 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
817817
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
818818
let malloc = js.cx.export_name_of(*malloc);
819819
let i = js.tmp();
820-
let align = std::cmp::max(kind.size(), 4);
821820
js.prelude(&format!(
822-
"const ptr{i} = {f}({0}, wasm.{malloc}, {align});",
821+
"const ptr{i} = {f}({0}, wasm.{malloc});",
823822
val,
824823
i = i,
825824
f = func,
826825
malloc = malloc,
827-
align = align,
828826
));
829827
js.prelude(&format!("const len{} = WASM_VECTOR_LEN;", i));
830828
js.push(format!("ptr{}", i));
@@ -928,7 +926,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
928926
let malloc = js.cx.export_name_of(*malloc);
929927
let val = js.pop();
930928
js.prelude(&format!(
931-
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc}, 4);",
929+
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc});",
932930
val,
933931
i = i,
934932
f = func,
@@ -946,7 +944,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
946944
let malloc = js.cx.export_name_of(*malloc);
947945
let i = js.tmp();
948946
js.prelude(&format!(
949-
"var ptr{i} = {f}({val}, wasm.{malloc}, 4);",
947+
"var ptr{i} = {f}({val}, wasm.{malloc});",
950948
val = val,
951949
i = i,
952950
f = func,

crates/cli-support/src/js/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1259,14 +1259,14 @@ impl<'a> Context<'a> {
12591259
"\
12601260
if (realloc === undefined) {{
12611261
const buf = cachedTextEncoder.encode(arg);
1262-
const ptr = malloc(buf.length, 4) >>> 0;
1262+
const ptr = malloc(buf.length, 1) >>> 0;
12631263
{mem}().subarray(ptr, ptr + buf.length).set(buf);
12641264
WASM_VECTOR_LEN = buf.length;
12651265
return ptr;
12661266
}}
12671267
12681268
let len = arg.length;
1269-
let ptr = malloc(len, 4) >>> 0;
1269+
let ptr = malloc(len, 1) >>> 0;
12701270
12711271
const mem = {mem}();
12721272
@@ -1294,7 +1294,7 @@ impl<'a> Context<'a> {
12941294
if (offset !== 0) {{
12951295
arg = arg.slice(offset);
12961296
}}
1297-
ptr = realloc(ptr, len, len = offset + arg.length * 3, 4) >>> 0;
1297+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
12981298
const view = {mem}().subarray(ptr + offset, ptr + len);
12991299
const ret = encodeString(arg, view);
13001300
{debug_end}
@@ -1415,8 +1415,8 @@ impl<'a> Context<'a> {
14151415
self.expose_wasm_vector_len();
14161416
self.global(&format!(
14171417
"
1418-
function {}(arg, malloc, align) {{
1419-
const ptr = malloc(arg.length * {size}, align) >>> 0;
1418+
function {}(arg, malloc) {{
1419+
const ptr = malloc(arg.length * {size}, {size}) >>> 0;
14201420
{}().set(arg, ptr / {size});
14211421
WASM_VECTOR_LEN = arg.length;
14221422
return ptr;

crates/cli/tests/reference/string-arg.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ function passStringToWasm0(arg, malloc, realloc) {
4747

4848
if (realloc === undefined) {
4949
const buf = cachedTextEncoder.encode(arg);
50-
const ptr = malloc(buf.length, 4) >>> 0;
50+
const ptr = malloc(buf.length, 1) >>> 0;
5151
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
5252
WASM_VECTOR_LEN = buf.length;
5353
return ptr;
5454
}
5555

5656
let len = arg.length;
57-
let ptr = malloc(len, 4) >>> 0;
57+
let ptr = malloc(len, 1) >>> 0;
5858

5959
const mem = getUint8Memory0();
6060

@@ -70,7 +70,7 @@ function passStringToWasm0(arg, malloc, realloc) {
7070
if (offset !== 0) {
7171
arg = arg.slice(offset);
7272
}
73-
ptr = realloc(ptr, len, len = offset + arg.length * 3, 4) >>> 0;
73+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
7474
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
7575
const ret = encodeString(arg, view);
7676

crates/threads-xform/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fn inject_start(
347347
// local = malloc(stack.size, align) [aka base]
348348
with_temp_stack(body, memory, stack, |body| {
349349
body.i32_const(stack.size as i32)
350-
.i32_const(4)
350+
.i32_const(16)
351351
.call(malloc)
352352
.local_tee(local);
353353
});
@@ -427,14 +427,14 @@ fn inject_destroy(
427427
// we're destroying somebody else's stack, so we can use our own
428428
body.local_get(stack_alloc)
429429
.i32_const(stack.size as i32)
430-
.i32_const(4)
430+
.i32_const(16)
431431
.call(free);
432432
},
433433
|body| {
434434
with_temp_stack(body, memory, stack, |body| {
435435
body.global_get(stack.alloc)
436436
.i32_const(stack.size as i32)
437-
.i32_const(4)
437+
.i32_const(16)
438438
.call(free);
439439
});
440440

0 commit comments

Comments
 (0)