Skip to content

Commit 1aba50c

Browse files
committed
Nullable globals + more precise constant globals
1 parent 8d2a081 commit 1aba50c

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

compiler/lib-wasm/code_generation.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,13 @@ and expression_type (e : W.expression) st =
539539
| GlobalGet x ->
540540
( (try
541541
let typ = (Var.Map.find x st.context.constant_globals).typ in
542-
if Poly.equal typ st.context.value_type then None else Some typ
542+
if Poly.equal typ st.context.value_type
543+
then None
544+
else
545+
Some
546+
(match typ with
547+
| Ref { typ; nullable = true } -> Ref { typ; nullable = false }
548+
| _ -> typ)
543549
with Not_found -> None)
544550
, st )
545551
| Seq (_, e') -> expression_type e' st
@@ -640,10 +646,13 @@ let rec store ?(always = false) ?typ x e =
640646
match typ with
641647
| Some typ -> return typ
642648
| None -> (
643-
let* typ = expression_type e in
644-
match typ with
645-
| None -> value_type
646-
| Some typ -> return typ)
649+
if always
650+
then value_type
651+
else
652+
let* typ = expression_type e in
653+
match typ with
654+
| None -> value_type
655+
| Some typ -> return typ)
647656
in
648657
let* default, typ', cast = default_value typ in
649658
let* () =
@@ -655,7 +664,6 @@ let rec store ?(always = false) ?typ x e =
655664
in
656665
register_global ~constant:true x { mut = true; typ = typ' } default
657666
in
658-
let* () = register_constant x (W.GlobalGet x) in
659667
instr (GlobalSet (x, e))
660668
else
661669
let* typ =

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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ module Value = struct
607607
let int_asr = binop Arith.( asr )
608608
end
609609

610+
let store_in_global ?(name = "const") c =
611+
let name = Code.Var.fresh_n name in
612+
let* typ = expression_type c in
613+
let* () =
614+
register_global name { mut = false; typ = Option.value ~default:Type.value typ } c
615+
in
616+
return (W.GlobalGet name)
617+
610618
module Memory = struct
611619
let wasm_cast ty e =
612620
let* e = e in
@@ -866,7 +874,9 @@ module Memory = struct
866874
in
867875
let* ty = Type.int32_type in
868876
let* e = e in
869-
return (W.StructNew (ty, [ GlobalGet int32_ops; e ]))
877+
let e' = W.StructNew (ty, [ GlobalGet int32_ops; e ]) in
878+
let* b = is_small_constant e in
879+
if b then store_in_global e' else return e'
870880

871881
let box_int32 e = make_int32 ~kind:`Int32 e
872882

@@ -884,7 +894,9 @@ module Memory = struct
884894
in
885895
let* ty = Type.int64_type in
886896
let* e = e in
887-
return (W.StructNew (ty, [ GlobalGet int64_ops; e ]))
897+
let e' = W.StructNew (ty, [ GlobalGet int64_ops; e ]) in
898+
let* b = is_small_constant e in
899+
if b then store_in_global e' else return e'
888900

889901
let box_int64 e = make_int64 e
890902

@@ -904,11 +916,6 @@ module Constant = struct
904916
strings are encoded as a sequence of bytes in the wasm module. *)
905917
let string_length_threshold = 64
906918

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

0 commit comments

Comments
 (0)