Skip to content

Commit 8d14082

Browse files
committed
More precise constant globals
1 parent 702eb1c commit 8d14082

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
@@ -200,6 +200,8 @@ val function_body :
200200

201201
val variable_type : Code.Var.t -> Wasm_ast.value_type option t
202202

203+
val expression_type : Wasm_ast.expression -> Wasm_ast.value_type option t
204+
203205
val array_placeholder : Code.Var.t -> expression
204206

205207
val default_value :

compiler/lib-wasm/gc_target.ml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,14 @@ module Value = struct
600600
let int_asr = Arith.( asr )
601601
end
602602

603+
let store_in_global ?(name = "const") c =
604+
let name = Code.Var.fresh_n name in
605+
let* typ = expression_type c in
606+
let* () =
607+
register_global name { mut = false; typ = Option.value ~default:Type.value typ } c
608+
in
609+
return (W.GlobalGet name)
610+
603611
module Memory = struct
604612
let wasm_cast ty e =
605613
let* e = e in
@@ -856,7 +864,9 @@ module Memory = struct
856864
in
857865
let* ty = Type.int32_type in
858866
let* e = e in
859-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
867+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
868+
let* b = is_small_constant e in
869+
if b then store_in_global e' else return e'
860870

861871
let box_int32 e = make_int32 ~kind:`Int32 e
862872

@@ -874,7 +884,9 @@ module Memory = struct
874884
in
875885
let* ty = Type.int64_type in
876886
let* e = e in
877-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
887+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
888+
let* b = is_small_constant e in
889+
if b then store_in_global e' else return e'
878890

879891
let box_int64 e = make_int64 e
880892

@@ -894,11 +906,6 @@ module Constant = struct
894906
strings are encoded as a sequence of bytes in the wasm module. *)
895907
let string_length_threshold = 64
896908

897-
let store_in_global ?(name = "const") c =
898-
let name = Code.Var.fresh_n name in
899-
let* () = register_global name { mut = false; typ = Type.value } c in
900-
return (W.GlobalGet name)
901-
902909
let byte_string s =
903910
let b = Buffer.create (String.length s) in
904911
String.iter s ~f:(function
@@ -1031,13 +1038,15 @@ module Constant = struct
10311038
if b then return c else store_in_global c
10321039
| Const_named name -> store_in_global ~name c
10331040
| Mutated ->
1041+
let* typ = Type.string_type in
10341042
let name = Code.Var.fresh_n "const" in
1043+
let* placeholder = array_placeholder typ in
10351044
let* () =
10361045
register_global
10371046
~constant:true
10381047
name
1039-
{ mut = true; typ = Type.value }
1040-
(W.RefI31 (Const (I32 0l)))
1048+
{ mut = true; typ = Ref { nullable = false; typ = Type typ } }
1049+
placeholder
10411050
in
10421051
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10431052
return (W.GlobalGet name))

0 commit comments

Comments
 (0)