Skip to content

Commit

Permalink
Use smallest possible alignment
Browse files Browse the repository at this point in the history
Co-Authored-By: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
  • Loading branch information
daxpedda and Liamolucko committed Jun 6, 2023
1 parent 36fcc41 commit 8a88582
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
6 changes: 2 additions & 4 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,12 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
let malloc = js.cx.export_name_of(*malloc);
let i = js.tmp();
let align = std::cmp::max(kind.size(), 4);
js.prelude(&format!(
"const ptr{i} = {f}({0}, wasm.{malloc}, {align});",
val,
i = i,
f = func,
malloc = malloc,
align = align,
));
js.prelude(&format!("const len{} = WASM_VECTOR_LEN;", i));
js.push(format!("ptr{}", i));
Expand Down Expand Up @@ -928,7 +926,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
let malloc = js.cx.export_name_of(*malloc);
let val = js.pop();
js.prelude(&format!(
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc}, 4);",
"var ptr{i} = isLikeNone({0}) ? 0 : {f}({0}, wasm.{malloc});",
val,
i = i,
f = func,
Expand All @@ -946,7 +944,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
let malloc = js.cx.export_name_of(*malloc);
let i = js.tmp();
js.prelude(&format!(
"var ptr{i} = {f}({val}, wasm.{malloc}, 4);",
"var ptr{i} = {f}({val}, wasm.{malloc});",
val = val,
i = i,
f = func,
Expand Down
10 changes: 5 additions & 5 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,14 +1259,14 @@ impl<'a> Context<'a> {
"\
if (realloc === undefined) {{
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 4) >>> 0;
const ptr = malloc(buf.length, 1) >>> 0;
{mem}().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}}
let len = arg.length;
let ptr = malloc(len, 4) >>> 0;
let ptr = malloc(len, 1) >>> 0;
const mem = {mem}();
Expand Down Expand Up @@ -1294,7 +1294,7 @@ impl<'a> Context<'a> {
if (offset !== 0) {{
arg = arg.slice(offset);
}}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 4) >>> 0;
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = {mem}().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
{debug_end}
Expand Down Expand Up @@ -1415,8 +1415,8 @@ impl<'a> Context<'a> {
self.expose_wasm_vector_len();
self.global(&format!(
"
function {}(arg, malloc, align) {{
const ptr = malloc(arg.length * {size}, align) >>> 0;
function {}(arg, malloc) {{
const ptr = malloc(arg.length * {size}, {size}) >>> 0;
{}().set(arg, ptr / {size});
WASM_VECTOR_LEN = arg.length;
return ptr;
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 4) >>> 0;
const ptr = malloc(buf.length, 1) >>> 0;
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 4) >>> 0;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8Memory0();

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

Expand Down
6 changes: 3 additions & 3 deletions crates/threads-xform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn inject_start(
// local = malloc(stack.size, align) [aka base]
with_temp_stack(body, memory, stack, |body| {
body.i32_const(stack.size as i32)
.i32_const(4)
.i32_const(16)
.call(malloc)
.local_tee(local);
});
Expand Down Expand Up @@ -427,14 +427,14 @@ fn inject_destroy(
// we're destroying somebody else's stack, so we can use our own
body.local_get(stack_alloc)
.i32_const(stack.size as i32)
.i32_const(4)
.i32_const(16)
.call(free);
},
|body| {
with_temp_stack(body, memory, stack, |body| {
body.global_get(stack.alloc)
.i32_const(stack.size as i32)
.i32_const(4)
.i32_const(16)
.call(free);
});

Expand Down

0 comments on commit 8a88582

Please sign in to comment.