Skip to content

Commit 459452d

Browse files
committed
feat(compiler): Optimize constant global initializations
1 parent 1c5478e commit 459452d

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

compiler/src/codegen/compcore.re

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -441,30 +441,38 @@ let compile_bind =
441441
switch (action) {
442442
| BindGet => Expression.Global_get.make(wasm_mod, slot, typ)
443443
| BindSet({value, initial}) =>
444-
Expression.Global_set.make(
445-
wasm_mod,
446-
slot,
447-
if (initial) {
448-
value;
449-
} else {
450-
Expression.Tuple_extract.make(
451-
wasm_mod,
452-
Expression.Tuple_make.make(
444+
if (initial && Expression.get_kind(value) == Expression.Const) {
445+
// TODO(#2336): Only optimize unexported globals or when use-start-section is enabled
446+
447+
Global.remove_global(wasm_mod, slot);
448+
ignore @@ Global.add_global(wasm_mod, slot, typ, true, value);
449+
Expression.Nop.make(wasm_mod);
450+
} else {
451+
Expression.Global_set.make(
452+
wasm_mod,
453+
slot,
454+
if (initial) {
455+
value;
456+
} else {
457+
Expression.Tuple_extract.make(
453458
wasm_mod,
454-
[
455-
appropriate_incref(wasm_mod, env, value, b),
456-
appropriate_decref(
457-
wasm_mod,
458-
env,
459-
Expression.Global_get.make(wasm_mod, slot, typ),
460-
b,
461-
),
462-
],
463-
),
464-
0,
465-
);
466-
},
467-
)
459+
Expression.Tuple_make.make(
460+
wasm_mod,
461+
[
462+
appropriate_incref(wasm_mod, env, value, b),
463+
appropriate_decref(
464+
wasm_mod,
465+
env,
466+
Expression.Global_get.make(wasm_mod, slot, typ),
467+
b,
468+
),
469+
],
470+
),
471+
0,
472+
);
473+
},
474+
);
475+
}
468476
| BindTee({value}) =>
469477
Expression.Block.make(
470478
wasm_mod,

compiler/test/suites/basic_functionality.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,6 @@ describe("basic functionality", ({test, testSkip}) => {
391391
~config_fn=smallestFileConfig,
392392
"smallest_grain_program",
393393
"",
394-
3091,
394+
3007,
395395
);
396396
});

0 commit comments

Comments
 (0)