Skip to content

Commit de9d3f2

Browse files
committed
Nullable globals + more precise constant globals
1 parent 015b228 commit de9d3f2

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

compiler/lib-wasm/code_generation.ml

+14-6
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,13 @@ and expression_type (e : W.expression) st =
536536
| GlobalGet x ->
537537
( (try
538538
let typ = (Var.Map.find x st.context.constant_globals).typ in
539-
if Poly.equal typ st.context.value_type then None else Some typ
539+
if Poly.equal typ st.context.value_type
540+
then None
541+
else
542+
Some
543+
(match typ with
544+
| Ref { typ; nullable = true } -> Ref { typ; nullable = false }
545+
| _ -> typ)
540546
with Not_found -> None)
541547
, st )
542548
| Seq (_, e') -> expression_type e' st
@@ -637,10 +643,13 @@ let rec store ?(always = false) ?typ x e =
637643
match typ with
638644
| Some typ -> return typ
639645
| None -> (
640-
let* typ = expression_type e in
641-
match typ with
642-
| None -> value_type
643-
| Some typ -> return typ)
646+
if always
647+
then value_type
648+
else
649+
let* typ = expression_type e in
650+
match typ with
651+
| None -> value_type
652+
| Some typ -> return typ)
644653
in
645654
let* default, typ', cast = default_value typ in
646655
let* () =
@@ -652,7 +661,6 @@ let rec store ?(always = false) ?typ x e =
652661
in
653662
register_global ~constant:true x { mut = true; typ = typ' } default
654663
in
655-
let* () = register_constant x (W.GlobalGet x) in
656664
instr (GlobalSet (x, e))
657665
else
658666
let* typ =

compiler/lib-wasm/code_generation.mli

+2
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

+14-7
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ module Value = struct
605605
let int_asr = binop Arith.( asr )
606606
end
607607

608+
let store_in_global ?(name = "const") c =
609+
let name = Code.Var.fresh_n name in
610+
let* typ = expression_type c in
611+
let* () =
612+
register_global name { mut = false; typ = Option.value ~default:Type.value typ } c
613+
in
614+
return (W.GlobalGet name)
615+
608616
module Memory = struct
609617
let wasm_cast ty e =
610618
let* e = e in
@@ -864,7 +872,9 @@ module Memory = struct
864872
in
865873
let* ty = Type.int32_type in
866874
let* e = e in
867-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
875+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
876+
let* b = is_small_constant e in
877+
if b then store_in_global e' else return e'
868878

869879
let box_int32 e = make_int32 ~kind:`Int32 e
870880

@@ -882,7 +892,9 @@ module Memory = struct
882892
in
883893
let* ty = Type.int64_type in
884894
let* e = e in
885-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
895+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
896+
let* b = is_small_constant e in
897+
if b then store_in_global e' else return e'
886898

887899
let box_int64 e = make_int64 e
888900

@@ -902,11 +914,6 @@ module Constant = struct
902914
strings are encoded as a sequence of bytes in the wasm module. *)
903915
let string_length_threshold = 64
904916

905-
let store_in_global ?(name = "const") c =
906-
let name = Code.Var.fresh_n name in
907-
let* () = register_global name { mut = false; typ = Type.value } c in
908-
return (W.GlobalGet name)
909-
910917
let str_js_utf8 s =
911918
let b = Buffer.create (String.length s) in
912919
String.iter s ~f:(function

0 commit comments

Comments
 (0)