Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): Cleaner wasm output for low-level wasm types #1158

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -504,30 +504,31 @@ let cleanup_local_slot_instructions = (wasm_mod, env: codegen_env, argtypes) =>
);
flatten(List.append(arg_instrs, stack_instrs));
};
let appropriate_incref = (wasm_mod, env, arg, b) =>
let should_refcount = b =>
switch (b) {
| MArgBind(_, Types.HeapAllocated)
| MLocalBind(_, Types.HeapAllocated)
| MSwapBind(_, Types.HeapAllocated)
| MClosureBind(_)
| MGlobalBind(_, Types.HeapAllocated) => call_incref(wasm_mod, env, arg)
| MGlobalBind(_, Types.HeapAllocated) => true
| MArgBind(_)
| MLocalBind(_)
| MSwapBind(_)
| MGlobalBind(_) => arg
| MGlobalBind(_) => false
};

let appropriate_incref = (wasm_mod, env, arg, b) =>
if (should_refcount(b)) {
call_incref(wasm_mod, env, arg);
} else {
arg;
};

let appropriate_decref = (wasm_mod, env, arg, b) =>
switch (b) {
| MArgBind(_, Types.HeapAllocated)
| MLocalBind(_, Types.HeapAllocated)
| MSwapBind(_, Types.HeapAllocated)
| MClosureBind(_)
| MGlobalBind(_, Types.HeapAllocated) => call_decref(wasm_mod, env, arg)
| MArgBind(_)
| MLocalBind(_)
| MSwapBind(_)
| MGlobalBind(_) => arg
if (should_refcount(b)) {
call_decref(wasm_mod, env, arg);
} else {
arg;
};

let compile_bind =
Expand Down Expand Up @@ -564,40 +565,48 @@ let compile_bind =
Expression.Local_set.make(
wasm_mod,
slot,
Expression.Tuple_extract.make(
wasm_mod,
Expression.Tuple_make.make(
if (should_refcount(b)) {
Expression.Tuple_extract.make(
wasm_mod,
[
arg,
appropriate_decref(
env,
Expression.Local_get.make(wasm_mod, slot, typ),
),
],
),
0,
),
Expression.Tuple_make.make(
wasm_mod,
[
arg,
appropriate_decref(
env,
Expression.Local_get.make(wasm_mod, slot, typ),
),
],
),
0,
);
} else {
arg;
},
);
};
let tee_slot = (slot, typ, arg) => {
Expression.Local_tee.make(
wasm_mod,
slot,
Expression.Tuple_extract.make(
wasm_mod,
Expression.Tuple_make.make(
if (should_refcount(b)) {
Expression.Tuple_extract.make(
wasm_mod,
[
arg,
appropriate_decref(
env,
Expression.Local_get.make(wasm_mod, slot, typ),
),
],
),
0,
),
Expression.Tuple_make.make(
wasm_mod,
[
arg,
appropriate_decref(
env,
Expression.Local_get.make(wasm_mod, slot, typ),
),
],
),
0,
);
} else {
arg;
},
typ,
);
};
Expand Down
62 changes: 18 additions & 44 deletions compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ arrays › array_access
(func $_gmain (; has Stack IR ;) (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(i32.store
(local.tee $0
(tuple.extract 0
(tuple.make
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
(i32.const 0)
)
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
)
(i32.const 4)
Expand Down Expand Up @@ -67,17 +61,14 @@ arrays › array_access
)
(if
(i32.lt_s
(i32.const 0)
(local.tee $0
(i32.const 0)
)
(i32.sub
(i32.const 0)
(i32.load offset=4
(local.tee $2
(tuple.extract 0
(tuple.make
(global.get $global_0)
(i32.const 0)
)
)
(local.tee $1
(global.get $global_0)
)
)
)
Expand All @@ -95,9 +86,9 @@ arrays › array_access
(if
(i32.le_s
(i32.load offset=4
(local.get $2)
(local.get $1)
)
(local.get $1)
(local.get $0)
)
(block
(drop
Expand All @@ -109,33 +100,16 @@ arrays › array_access
(unreachable)
)
)
(tuple.extract 0
(tuple.make
(call $wimport_GRAIN$MODULE$runtime/gc_incRef
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$incRef)
(i32.load offset=8
(i32.add
(i32.shl
(if (result i32)
(i32.lt_u
(local.get $1)
(i32.const 0)
)
(i32.add
(local.get $1)
(i32.load offset=4
(local.get $2)
)
)
(local.get $1)
)
(i32.const 2)
)
(local.get $2)
)
(call $wimport_GRAIN$MODULE$runtime/gc_incRef
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$incRef)
(i32.load offset=8
(i32.add
(i32.shl
(local.get $0)
(i32.const 2)
)
(local.get $1)
)
(local.get $0)
)
)
)
Expand Down
18 changes: 4 additions & 14 deletions compiler/test/__snapshots__/arrays.24453e6e.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ arrays › array1_trailing
(local $0 i32)
(i32.store
(local.tee $0
(tuple.extract 0
(tuple.make
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
(i32.const 0)
)
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
)
(i32.const 4)
Expand All @@ -44,12 +39,7 @@ arrays › array1_trailing
(local.get $0)
(i32.const 7)
)
(tuple.extract 0
(tuple.make
(local.get $0)
(local.get $0)
)
)
(local.get $0)
)
(func $_start (; has Stack IR ;)
(drop
Expand Down
64 changes: 24 additions & 40 deletions compiler/test/__snapshots__/arrays.28fcc534.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ arrays › array_access4
(func $_gmain (; has Stack IR ;) (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(i32.store
(local.tee $0
(tuple.extract 0
(tuple.make
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
(i32.const 0)
)
(call $wimport_GRAIN$MODULE$runtime/gc_malloc
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$malloc)
(i32.const 20)
)
)
(i32.const 4)
Expand Down Expand Up @@ -67,19 +61,14 @@ arrays › array_access4
)
(if
(i32.lt_s
(local.tee $1
(local.tee $0
(i32.const -2)
)
(i32.sub
(i32.const 0)
(i32.load offset=4
(local.tee $2
(tuple.extract 0
(tuple.make
(global.get $global_0)
(i32.const 0)
)
)
(local.tee $1
(global.get $global_0)
)
)
)
Expand All @@ -97,9 +86,9 @@ arrays › array_access4
(if
(i32.le_s
(i32.load offset=4
(local.get $2)
(local.get $1)
)
(local.get $1)
(local.get $0)
)
(block
(drop
Expand All @@ -111,33 +100,28 @@ arrays › array_access4
(unreachable)
)
)
(tuple.extract 0
(tuple.make
(call $wimport_GRAIN$MODULE$runtime/gc_incRef
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$incRef)
(i32.load offset=8
(i32.add
(i32.shl
(if (result i32)
(i32.lt_s
(local.get $1)
(i32.const 0)
)
(i32.add
(local.get $1)
(i32.load offset=4
(local.get $2)
)
)
(call $wimport_GRAIN$MODULE$runtime/gc_incRef
(global.get $wimport_GRAIN$MODULE$runtime/gc_GRAIN$EXPORT$incRef)
(i32.load offset=8
(i32.add
(i32.shl
(if (result i32)
(i32.lt_s
(local.get $0)
(i32.const 0)
)
(i32.add
(local.get $0)
(i32.load offset=4
(local.get $1)
)
(i32.const 2)
)
(local.get $2)
(local.get $0)
)
(i32.const 2)
)
(local.get $1)
)
(local.get $0)
)
)
)
Expand Down
Loading