Skip to content

Commit

Permalink
Merge pull request #199 from 0xPolygonMiden/greenhat/wasm-memory-grow…
Browse files Browse the repository at this point in the history
…-workaround

[2/x] feat: add workaround for wasm `memory.grow` op translation
  • Loading branch information
greenhat authored Jun 6, 2024
2 parents f2cb2b7 + 53b752d commit 4fda020
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 298 deletions.
18 changes: 16 additions & 2 deletions codegen/masm/src/codegen/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ impl<'b, 'f: 'b> BlockEmitter<'b, 'f> {
// handled separately within the specific handlers for those instructions
let args = self.function.f.dfg.inst_args(inst_info.inst);
self.schedule_operands(args, inst_info.plain_arguments()).unwrap_or_else(|err| {
panic!("failed to schedule operands for {}: {err:?}", inst_info.inst)
panic!(
"failed to schedule operands: {:?} \n for inst:\n {}\n {:?}\n with error: {err:?}",
args,
inst_info.inst,
self.function.f.dfg.inst(inst_info.inst),
)
});

match self.function.f.dfg.inst(inst_info.inst) {
Expand Down Expand Up @@ -1000,7 +1005,16 @@ impl<'b, 'f: 'b> BlockEmitter<'b, 'f> {
// No loops involved
(None, None) => {
assert!(is_first_visit);
assert_eq!(self.controlling_loop, None);
assert_eq!(
self.controlling_loop,
None,
"unexpected controlling loop: {:?}, parent: {:?}",
self.function.loops.loop_header(self.controlling_loop.unwrap()),
self.function
.loops
.loop_parent(self.controlling_loop.unwrap())
.map(|l| self.function.loops.loop_header(l))
);
None
}
// Entering a top-level loop, set the controlling loop
Expand Down
10 changes: 8 additions & 2 deletions frontend-wasm/src/code_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ pub fn translate_operator(
}
/******************************* Memory management *********************************/
Operator::MemoryGrow { .. } => {
let arg = state.pop1_casted(U32, builder, span);
state.push1(builder.ins().mem_grow(arg, span));
// let arg = state.pop1_casted(U32, builder, span);
// state.push1(builder.ins().mem_grow(arg, span));
// TODO: temporary workaround until we have this instruction
// properly implemented in codegen Wasm `memory.grow` instruction is
// expected to return -1 if the memory cannot grow and the old mem
// page count if it can grow.
// Return total Miden memory size
state.push1(builder.ins().i32(mem_total_pages(), span));
}
Operator::MemorySize { .. } => {
// Return total Miden memory size
Expand Down
3 changes: 1 addition & 2 deletions frontend-wasm/src/code_translator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ fn memory_grow() {
"#,
expect![[r#"
(let (v0 i32) (const.i32 1))
(let (v1 u32) (cast v0))
(let (v2 i32) (memory.grow v1))
(let (v1 i32) (const.i32 1048575))
"#]],
)
}
Expand Down
69 changes: 34 additions & 35 deletions tests/integration/expected/abi_transform_tx_kernel_get_inputs.hir
Original file line number Diff line number Diff line change
Expand Up @@ -167,50 +167,49 @@
(let (v20 u32) (cast v18))
(let (v21 u32) (shr.wrapping v19 v20))
(let (v22 i32) (cast v21))
(let (v23 u32) (cast v22))
(let (v24 i32) (memory.grow v23))
(let (v25 i32) (const.i32 -1))
(let (v26 i1) (neq v24 v25))
(let (v27 i32) (cast v26))
(let (v28 i1) (neq v27 0))
(condbr v28 (block 3) (block 4)))
(let (v23 i32) (const.i32 1048575))
(let (v24 i32) (const.i32 -1))
(let (v25 i1) (neq v23 v24))
(let (v26 i32) (cast v25))
(let (v27 i1) (neq v26 0))
(condbr v27 (block 3) (block 4)))

(block 1
(ret))

(block 2 (param v45 i32) (param v46 i32) (param v50 i32)
(let (v47 u32) (cast v45))
(let (v48 u32) (add.checked v47 4))
(let (v49 (ptr i32)) (inttoptr v48))
(store v49 v46)
(let (v51 u32) (cast v45))
(let (v52 (ptr i32)) (inttoptr v51))
(store v52 v50)
(block 2 (param v44 i32) (param v45 i32) (param v49 i32)
(let (v46 u32) (cast v44))
(let (v47 u32) (add.checked v46 4))
(let (v48 (ptr i32)) (inttoptr v47))
(store v48 v45)
(let (v50 u32) (cast v44))
(let (v51 (ptr i32)) (inttoptr v50))
(store v51 v49)
(br (block 1)))

(block 3
(let (v31 i32) (const.i32 16))
(let (v32 i32) (shl.wrapping v24 v31))
(let (v33 i64) (const.i64 0))
(let (v34 u32) (cast v32))
(let (v35 u32) (add.checked v34 4))
(let (v36 (ptr i64)) (inttoptr v35))
(store v36 v33)
(let (v37 i32) (const.i32 -65536))
(let (v38 i32) (band v17 v37))
(let (v39 i32) (add.wrapping v32 v38))
(let (v40 i32) (const.i32 2))
(let (v41 i32) (bor v39 v40))
(let (v42 u32) (cast v32))
(let (v43 (ptr i32)) (inttoptr v42))
(store v43 v41)
(let (v44 i32) (const.i32 0))
(br (block 2 v0 v32 v44)))
(let (v30 i32) (const.i32 16))
(let (v31 i32) (shl.wrapping v23 v30))
(let (v32 i64) (const.i64 0))
(let (v33 u32) (cast v31))
(let (v34 u32) (add.checked v33 4))
(let (v35 (ptr i64)) (inttoptr v34))
(store v35 v32)
(let (v36 i32) (const.i32 -65536))
(let (v37 i32) (band v17 v36))
(let (v38 i32) (add.wrapping v31 v37))
(let (v39 i32) (const.i32 2))
(let (v40 i32) (bor v38 v39))
(let (v41 u32) (cast v31))
(let (v42 (ptr i32)) (inttoptr v41))
(store v42 v40)
(let (v43 i32) (const.i32 0))
(br (block 2 v0 v31 v43)))

(block 4
(let (v29 i32) (const.i32 1))
(let (v30 i32) (const.i32 0))
(br (block 2 v0 v30 v29)))
(let (v28 i32) (const.i32 1))
(let (v29 i32) (const.i32 0))
(br (block 2 v0 v29 v28)))
)

(func (export #wee_alloc::alloc_first_fit)
Expand Down
109 changes: 54 additions & 55 deletions tests/integration/expected/components/add_wasm_component.hir
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@
(block 1 (param v3 i32)
(ret v3))

(block 2 (param v87 i32) (param v91 i32)
(let (v88 i32) (const.i32 16))
(let (v89 i32) (add.wrapping v87 v88))
(let (v90 (ptr i32)) (global.symbol #__stack_pointer))
(store v90 v89)
(br (block 1 v91)))
(block 2 (param v86 i32) (param v90 i32)
(let (v87 i32) (const.i32 16))
(let (v88 i32) (add.wrapping v86 v87))
(let (v89 (ptr i32)) (global.symbol #__stack_pointer))
(store v89 v88)
(br (block 1 v90)))

(block 3
(let (v10 u32) (cast v0))
Expand All @@ -520,15 +520,15 @@
(block 4
(br (block 2 v7 v1)))

(block 5 (param v79 i32) (param v80 i32) (param v92 i32)
(let (v81 u32) (cast v80))
(let (v82 u32) (add.checked v81 12))
(let (v83 (ptr i32)) (inttoptr v82))
(let (v84 i32) (load v83))
(let (v85 u32) (cast v79))
(let (v86 (ptr i32)) (inttoptr v85))
(store v86 v84)
(br (block 2 v80 v92)))
(block 5 (param v78 i32) (param v79 i32) (param v91 i32)
(let (v80 u32) (cast v79))
(let (v81 u32) (add.checked v80 12))
(let (v82 (ptr i32)) (inttoptr v81))
(let (v83 i32) (load v82))
(let (v84 u32) (cast v78))
(let (v85 (ptr i32)) (inttoptr v84))
(store v85 v83)
(br (block 2 v79 v91)))

(block 6
(let (v27 i32) (const.i32 -4))
Expand All @@ -550,50 +550,49 @@
(let (v43 u32) (cast v41))
(let (v44 u32) (shr.wrapping v42 v43))
(let (v45 i32) (cast v44))
(let (v46 u32) (cast v45))
(let (v47 i32) (memory.grow v46))
(let (v48 i32) (const.i32 -1))
(let (v49 i1) (neq v47 v48))
(let (v50 i32) (cast v49))
(let (v51 i1) (neq v50 0))
(condbr v51 (block 7) (block 8)))
(let (v46 i32) (const.i32 1048575))
(let (v47 i32) (const.i32 -1))
(let (v48 i1) (neq v46 v47))
(let (v49 i32) (cast v48))
(let (v50 i1) (neq v49 0))
(condbr v50 (block 7) (block 8)))

(block 7
(let (v53 i32) (const.i32 16))
(let (v54 i32) (shl.wrapping v47 v53))
(let (v55 i32) (const.i32 0))
(let (v56 u32) (cast v54))
(let (v57 u32) (add.checked v56 4))
(let (v58 (ptr i32)) (inttoptr v57))
(store v58 v55)
(let (v59 u32) (cast v7))
(let (v60 u32) (add.checked v59 12))
(let (v61 (ptr i32)) (inttoptr v60))
(let (v62 i32) (load v61))
(let (v63 u32) (cast v54))
(let (v64 u32) (add.checked v63 8))
(let (v65 (ptr i32)) (inttoptr v64))
(store v65 v62)
(let (v66 i32) (const.i32 -65536))
(let (v67 i32) (band v40 v66))
(let (v68 i32) (add.wrapping v54 v67))
(let (v69 i32) (const.i32 2))
(let (v70 i32) (bor v68 v69))
(let (v71 u32) (cast v54))
(let (v72 (ptr i32)) (inttoptr v71))
(store v72 v70)
(let (v73 u32) (cast v7))
(let (v74 u32) (add.checked v73 12))
(let (v75 (ptr i32)) (inttoptr v74))
(store v75 v54)
(let (v76 i32) (const.i32 12))
(let (v77 i32) (add.wrapping v7 v76))
(let (v78 i32) (call #wee_alloc::alloc_first_fit v22 v1 v77))
(br (block 5 v0 v7 v78)))
(let (v52 i32) (const.i32 16))
(let (v53 i32) (shl.wrapping v46 v52))
(let (v54 i32) (const.i32 0))
(let (v55 u32) (cast v53))
(let (v56 u32) (add.checked v55 4))
(let (v57 (ptr i32)) (inttoptr v56))
(store v57 v54)
(let (v58 u32) (cast v7))
(let (v59 u32) (add.checked v58 12))
(let (v60 (ptr i32)) (inttoptr v59))
(let (v61 i32) (load v60))
(let (v62 u32) (cast v53))
(let (v63 u32) (add.checked v62 8))
(let (v64 (ptr i32)) (inttoptr v63))
(store v64 v61)
(let (v65 i32) (const.i32 -65536))
(let (v66 i32) (band v40 v65))
(let (v67 i32) (add.wrapping v53 v66))
(let (v68 i32) (const.i32 2))
(let (v69 i32) (bor v67 v68))
(let (v70 u32) (cast v53))
(let (v71 (ptr i32)) (inttoptr v70))
(store v71 v69)
(let (v72 u32) (cast v7))
(let (v73 u32) (add.checked v72 12))
(let (v74 (ptr i32)) (inttoptr v73))
(store v74 v53)
(let (v75 i32) (const.i32 12))
(let (v76 i32) (add.wrapping v7 v75))
(let (v77 i32) (call #wee_alloc::alloc_first_fit v22 v1 v76))
(br (block 5 v0 v7 v77)))

(block 8
(let (v52 i32) (const.i32 0))
(br (block 5 v0 v7 v52)))
(let (v51 i32) (const.i32 0))
(br (block 5 v0 v7 v51)))
)

(func (export #<wee_alloc::WeeAlloc as core::alloc::global::GlobalAlloc>::dealloc)
Expand Down
Loading

0 comments on commit 4fda020

Please sign in to comment.