Skip to content

Commit caf9f29

Browse files
committed
More precise constant globals
1 parent 95d06ea commit caf9f29

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
@@ -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
@@ -906,7 +914,9 @@ module Memory = struct
906914
in
907915
let* ty = Type.int32_type in
908916
let* e = e in
909-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
917+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
918+
let* b = is_small_constant e in
919+
if b then store_in_global e' else return e'
910920

911921
let box_int32 e = make_int32 ~kind:`Int32 e
912922

@@ -924,7 +934,9 @@ module Memory = struct
924934
in
925935
let* ty = Type.int64_type in
926936
let* e = e in
927-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
937+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
938+
let* b = is_small_constant e in
939+
if b then store_in_global e' else return e'
928940

929941
let box_int64 e = make_int64 e
930942

@@ -944,11 +956,6 @@ module Constant = struct
944956
strings are encoded as a sequence of bytes in the wasm module. *)
945957
let string_length_threshold = 64
946958

947-
let store_in_global ?(name = "const") c =
948-
let name = Code.Var.fresh_n name in
949-
let* () = register_global name { mut = false; typ = Type.value } c in
950-
return (W.GlobalGet name)
951-
952959
let byte_string s =
953960
let b = Buffer.create (String.length s) in
954961
String.iter s ~f:(function
@@ -1081,13 +1088,15 @@ module Constant = struct
10811088
if b then return c else store_in_global c
10821089
| Const_named name -> store_in_global ~name c
10831090
| Mutated ->
1091+
let* typ = Type.string_type in
10841092
let name = Code.Var.fresh_n "const" in
1093+
let* placeholder = array_placeholder typ in
10851094
let* () =
10861095
register_global
10871096
~constant:true
10881097
name
1089-
{ mut = true; typ = Type.value }
1090-
(W.RefI31 (Const (I32 0l)))
1098+
{ mut = true; typ = Ref { nullable = false; typ = Type typ } }
1099+
placeholder
10911100
in
10921101
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10931102
return (W.GlobalGet name))

0 commit comments

Comments
 (0)