Skip to content

Commit 5dbc219

Browse files
extract the dummy writing pattern as a function
1 parent 2c6d5f6 commit 5dbc219

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/main/scala/wasm/StagedMiniWasm.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ trait StagedWasmEvaluator extends SAIOps {
2828
// a cache storing the compiled code for each function, to reduce re-compilation
2929
val compileCache = new HashMap[Int, Rep[(Cont[Unit]) => Unit]]
3030

31+
def funHere[A:Manifest,B:Manifest](f: Rep[A] => Rep[B], dummy: Rep[Unit] = "dummy".reflectCtrlWith[Unit]()): Rep[A => B] = {
32+
// to avoid LMS lifting a function, we create a dummy node and read it inside function
33+
fun((x: Rep[A]) => {
34+
"dummy-op".reflectCtrlWith[Unit](dummy)
35+
f(x)
36+
})
37+
}
38+
3139
// NOTE: We don't support Ans type polymorphism yet
3240
def eval(insts: List[Instr],
3341
kont: Rep[Cont[Unit]],
@@ -88,37 +96,31 @@ trait StagedWasmEvaluator extends SAIOps {
8896
// no need to modify the stack when entering a block
8997
// the type system guarantees that we will never take more than the input size from the stack
9098
val funcTy = ty.funcType
91-
val dummy = "dummy".reflectCtrlWith[Unit]()
9299
// TODO: somehow the type of exitSize in residual program is nothing
93-
def restK: Rep[Cont[Unit]] = fun((_: Rep[Unit]) => {
100+
def restK: Rep[Cont[Unit]] = funHere((_: Rep[Unit]) => {
94101
info(s"Exiting the block, stackSize =", Stack.size)
95-
"dummy-op".reflectCtrlWith[Unit](dummy)
96102
eval(rest, kont, trail)
97103
})
98104
eval(inner, restK, restK :: trail)
99105
case Loop(ty, inner) =>
100106
val funcTy = ty.funcType
101107
val exitSize = Stack.size - funcTy.inps.size + funcTy.out.size
102-
val dummy = "dummy".reflectCtrlWith[Unit]()
103-
def restK = fun((_: Rep[Unit]) => {
104-
"dummy-op".reflectCtrlWith[Unit](dummy)
108+
def restK = funHere((_: Rep[Unit]) => {
105109
info(s"Exiting the loop, stackSize =", Stack.size)
106110
eval(rest, kont, trail)
107111
})
108-
def loop : Rep[Unit => Unit] = fun((_u: Rep[Unit]) => {
109-
"dummy-op".reflectCtrlWith[Unit](dummy)
112+
val dummy = "dummy".reflectCtrlWith[Unit]()
113+
def loop : Rep[Unit => Unit] = funHere((_u: Rep[Unit]) => {
110114
info(s"Entered the loop, stackSize =", Stack.size)
111115
eval(inner, restK, loop :: trail)
112-
})
116+
}, dummy) // <-- if we don't pass this dummy argument, lots of code will be generated
113117
loop(())
114118
case If(ty, thn, els) =>
115119
val funcTy = ty.funcType
116120
val exitSize = Stack.size - funcTy.inps.size + funcTy.out.size
117121
val cond = Stack.pop()
118-
val dummy = "dummy".reflectCtrlWith[Unit]()
119122
// TODO: can we avoid code duplication here?
120-
def restK = fun((_: Rep[Unit]) => {
121-
"dummy-op".reflectCtrlWith[Unit](dummy)
123+
def restK = funHere((_: Rep[Unit]) => {
122124
info(s"Exiting the if, stackSize =", Stack.size)
123125
eval(rest, kont, trail)
124126
})

0 commit comments

Comments
 (0)