Skip to content

Commit 0988db0

Browse files
committed
More precise constant globals
1 parent 0bae92d commit 0988db0

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

203203
val variable_type : Code.Var.t -> Wasm_ast.value_type option t
204204

205+
val expression_type : Wasm_ast.expression -> Wasm_ast.value_type option t
206+
205207
val array_placeholder : Code.Var.t -> expression
206208

207209
val default_value :

compiler/lib-wasm/gc_target.ml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,14 @@ module Value = struct
629629
let int_asr = Arith.( asr )
630630
end
631631

632+
let store_in_global ?(name = "const") c =
633+
let name = Code.Var.fresh_n name in
634+
let* typ = expression_type c in
635+
let* () =
636+
register_global name { mut = false; typ = Option.value ~default:Type.value typ } c
637+
in
638+
return (W.GlobalGet name)
639+
632640
module Memory = struct
633641
let wasm_cast ty e =
634642
let* e = e in
@@ -885,7 +893,9 @@ module Memory = struct
885893
in
886894
let* ty = Type.int32_type in
887895
let* e = e in
888-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
896+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
897+
let* b = is_small_constant e in
898+
if b then store_in_global e' else return e'
889899

890900
let box_int32 e = make_int32 ~kind:`Int32 e
891901

@@ -903,7 +913,9 @@ module Memory = struct
903913
in
904914
let* ty = Type.int64_type in
905915
let* e = e in
906-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
916+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
917+
let* b = is_small_constant e in
918+
if b then store_in_global e' else return e'
907919

908920
let box_int64 e = make_int64 e
909921

@@ -923,11 +935,6 @@ module Constant = struct
923935
strings are encoded as a sequence of bytes in the wasm module. *)
924936
let string_length_threshold = 64
925937

926-
let store_in_global ?(name = "const") c =
927-
let name = Code.Var.fresh_n name in
928-
let* () = register_global name { mut = false; typ = Type.value } c in
929-
return (W.GlobalGet name)
930-
931938
let byte_string s =
932939
let b = Buffer.create (String.length s) in
933940
String.iter s ~f:(function
@@ -1060,13 +1067,15 @@ module Constant = struct
10601067
if b then return c else store_in_global c
10611068
| Const_named name -> store_in_global ~name c
10621069
| Mutated ->
1070+
let* typ = Type.string_type in
10631071
let name = Code.Var.fresh_n "const" in
1072+
let* placeholder = array_placeholder typ in
10641073
let* () =
10651074
register_global
10661075
~constant:true
10671076
name
1068-
{ mut = true; typ = Type.value }
1069-
(W.RefI31 (Const (I32 0l)))
1077+
{ mut = true; typ = Ref { nullable = false; typ = Type typ } }
1078+
placeholder
10701079
in
10711080
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10721081
return (W.GlobalGet name))

0 commit comments

Comments
 (0)