diff --git a/compiler/src/codegen/compcore.re b/compiler/src/codegen/compcore.re index 183799258..9a7f3977e 100644 --- a/compiler/src/codegen/compcore.re +++ b/compiler/src/codegen/compcore.re @@ -441,30 +441,37 @@ let compile_bind = switch (action) { | BindGet => Expression.Global_get.make(wasm_mod, slot, typ) | BindSet({value, initial}) => - Expression.Global_set.make( - wasm_mod, - slot, - if (initial) { - value; - } else { - Expression.Tuple_extract.make( - wasm_mod, - Expression.Tuple_make.make( + if (initial && Expression.get_kind(value) == Expression.Const) { + // TODO(#2336): Only optimize unexported globals or when use-start-section is enabled + Global.remove_global(wasm_mod, slot); + ignore @@ Global.add_global(wasm_mod, slot, typ, true, value); + Expression.Nop.make(wasm_mod); + } else { + Expression.Global_set.make( + wasm_mod, + slot, + if (initial) { + value; + } else { + Expression.Tuple_extract.make( wasm_mod, - [ - appropriate_incref(wasm_mod, env, value, b), - appropriate_decref( - wasm_mod, - env, - Expression.Global_get.make(wasm_mod, slot, typ), - b, - ), - ], - ), - 0, - ); - }, - ) + Expression.Tuple_make.make( + wasm_mod, + [ + appropriate_incref(wasm_mod, env, value, b), + appropriate_decref( + wasm_mod, + env, + Expression.Global_get.make(wasm_mod, slot, typ), + b, + ), + ], + ), + 0, + ); + }, + ); + } | BindTee({value}) => Expression.Block.make( wasm_mod, diff --git a/compiler/test/suites/basic_functionality.re b/compiler/test/suites/basic_functionality.re index f737ce0eb..135d37d09 100644 --- a/compiler/test/suites/basic_functionality.re +++ b/compiler/test/suites/basic_functionality.re @@ -391,6 +391,6 @@ describe("basic functionality", ({test, testSkip}) => { ~config_fn=smallestFileConfig, "smallest_grain_program", "", - 3091, + 3007, ); });