Skip to content

Commit

Permalink
Don't pad bounds check in array accumulator
Browse files Browse the repository at this point in the history
  • Loading branch information
samestep committed Nov 9, 2023
1 parent d99f86d commit 2bf9385
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,21 +1083,22 @@ pub fn compile<'a, O: Eq + Hash, T: Refs<'a, Opaque = O>>(f: Node<'a, O, T>) ->
// accumulator pointer for calls to the zero function for elements if this array
// stores composite values
let mut zero = Function::new([(2, ValType::I32)]);
let mut total = aligned(size * n, 8);
let bound = size * n; // use this instead of padded `total` for bounds checking
let mut total = aligned(bound, 8);
// same as zero, the local is a pointer to the end of the accumulator array, used
// for bounds checking
let mut add = Function::new([(1, ValType::I32)]);

if n > 0 {
zero.instruction(&Instruction::LocalGet(0));
zero.instruction(&Instruction::I32Const(total.try_into().unwrap()));
zero.instruction(&Instruction::I32Const(bound.try_into().unwrap()));
zero.instruction(&Instruction::I32Add);
zero.instruction(&Instruction::LocalTee(2));
zero.instruction(&Instruction::LocalSet(3));
zero.instruction(&Instruction::Loop(BlockType::Empty));

add.instruction(&Instruction::LocalGet(0));
add.instruction(&Instruction::I32Const(total.try_into().unwrap()));
add.instruction(&Instruction::I32Const(bound.try_into().unwrap()));
add.instruction(&Instruction::I32Add);
add.instruction(&Instruction::LocalSet(2));
add.instruction(&Instruction::Loop(BlockType::Empty));
Expand Down

0 comments on commit 2bf9385

Please sign in to comment.