Skip to content

Commit 52fe5ad

Browse files
committed
More precise constant globals
1 parent 321ead8 commit 52fe5ad

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

compiler/lib-wasm/code_generation.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ val function_body :
207207

208208
val variable_type : Code.Var.t -> Wasm_ast.value_type option t
209209

210+
val expression_type : Wasm_ast.expression -> Wasm_ast.value_type option t
211+
210212
val array_placeholder : Code.Var.t -> expression
211213

212214
val default_value :

compiler/lib-wasm/gc_target.ml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ module Value = struct
640640
let int_asr = Arith.( asr )
641641
end
642642

643+
let store_in_global ?(name = "const") c =
644+
let name = Code.Var.fresh_n name in
645+
let* typ = expression_type c in
646+
let* () =
647+
register_global name { mut = false; typ = Option.value ~default:Type.value typ } c
648+
in
649+
return (W.GlobalGet name)
650+
643651
module Memory = struct
644652
let wasm_cast ty e =
645653
let* e = e in
@@ -888,7 +896,9 @@ module Memory = struct
888896
in
889897
let* ty = Type.int32_type in
890898
let* e = e in
891-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
899+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
900+
let* b = is_small_constant e in
901+
if b then store_in_global e' else return e'
892902

893903
let box_int32 e = make_int32 ~kind:`Int32 e
894904

@@ -906,7 +916,9 @@ module Memory = struct
906916
in
907917
let* ty = Type.int64_type in
908918
let* e = e in
909-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
919+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
920+
let* b = is_small_constant e in
921+
if b then store_in_global e' else return e'
910922

911923
let box_int64 e = make_int64 e
912924

@@ -926,11 +938,6 @@ module Constant = struct
926938
strings are encoded as a sequence of bytes in the wasm module. *)
927939
let string_length_threshold = 64
928940

929-
let store_in_global ?(name = "const") c =
930-
let name = Code.Var.fresh_n name in
931-
let* () = register_global name { mut = false; typ = Type.value } c in
932-
return (W.GlobalGet name)
933-
934941
let byte_string s =
935942
let b = Buffer.create (String.length s) in
936943
String.iter s ~f:(function
@@ -1066,13 +1073,15 @@ module Constant = struct
10661073
if b then return c else store_in_global c
10671074
| Const_named name -> store_in_global ~name c
10681075
| Mutated ->
1076+
let* typ = Type.string_type in
10691077
let name = Code.Var.fresh_n "const" in
1078+
let* placeholder = array_placeholder typ in
10701079
let* () =
10711080
register_global
10721081
~constant:true
10731082
name
1074-
{ mut = true; typ = Type.value }
1075-
(W.RefI31 (Const (I32 0l)))
1083+
{ mut = true; typ = Ref { nullable = false; typ = Type typ } }
1084+
placeholder
10761085
in
10771086
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10781087
return (W.GlobalGet name))

0 commit comments

Comments
 (0)