Skip to content

Commit 0b432e7

Browse files
committed
Fix bad interaction of deadcode elimination and unboxed float records
When one of the fields is not used, we fill it with the float value 0. rather than using the generic placeholder `(ref.i31 (i32.const 0))`, which does not make sense.
1 parent 886b6b2 commit 0b432e7

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ let generate_prelude ~out_file =
146146
| Some p -> p
147147
| None -> assert false
148148
in
149-
let Driver.{ program; variable_uses; in_cps; _ } = Driver.optimize ~profile code in
149+
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
150+
Driver.optimize ~profile code
151+
in
150152
let context = Wa_generate.start () in
151153
let debug = Parse_bytecode.Debug.create ~include_cmis:false false in
152154
let _ =
@@ -155,6 +157,7 @@ let generate_prelude ~out_file =
155157
~unit_name:(Some "prelude")
156158
~live_vars:variable_uses
157159
~in_cps
160+
~deadcode_sentinal
158161
~debug
159162
program
160163
in
@@ -305,11 +308,20 @@ let run
305308
| None, Some p -> p
306309
| None, None -> assert false
307310
in
308-
let Driver.{ program; variable_uses; in_cps; _ } = Driver.optimize ~profile code in
311+
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
312+
Driver.optimize ~profile code
313+
in
309314
let context = Wa_generate.start () in
310315
let debug = one.debug in
311316
let toplevel_name, generated_js =
312-
Wa_generate.f ~context ~unit_name ~live_vars:variable_uses ~in_cps ~debug program
317+
Wa_generate.f
318+
~context
319+
~unit_name
320+
~live_vars:variable_uses
321+
~in_cps
322+
~deadcode_sentinal
323+
~debug
324+
program
313325
in
314326
if standalone then Wa_generate.add_start_function ~context toplevel_name;
315327
Wa_generate.output ch ~context ~debug;

compiler/lib/wasm/wa_curry.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,13 @@ module Make (Target : Wa_target_sig.S) = struct
257257
(fun ~typ closure ->
258258
let* l = expression_list load l in
259259
call ?typ ~cps:true ~arity closure l)
260-
(let* args = Memory.allocate ~tag:0 (List.map ~f:(fun x -> `Var x) (List.tl l)) in
260+
(let* args =
261+
(* We don't need the deadcode sentinal when the tag is 0 *)
262+
Memory.allocate
263+
~tag:0
264+
~deadcode_sentinal:(Code.Var.fresh ())
265+
(List.map ~f:(fun x -> `Var x) (List.tl l))
266+
in
261267
let* make_iterator =
262268
register_import ~name:"caml_apply_continuation" (Fun (func_type 0))
263269
in

compiler/lib/wasm/wa_gc_target.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,16 +656,18 @@ module Memory = struct
656656
let* ty = Type.float_type in
657657
wasm_struct_get ty (wasm_cast ty e) 0
658658

659-
let allocate ~tag l =
659+
let allocate ~tag ~deadcode_sentinal l =
660660
if tag = 254
661661
then
662662
let* l =
663663
expression_list
664664
(fun v ->
665-
unbox_float
666-
(match v with
667-
| `Var y -> load y
668-
| `Expr e -> return e))
665+
match v with
666+
| `Var y ->
667+
if Code.Var.equal y deadcode_sentinal
668+
then return (W.Const (F64 0.))
669+
else unbox_float (load y)
670+
| `Expr e -> unbox_float (return e))
669671
l
670672
in
671673
let* ty = Type.float_array_type in

compiler/lib/wasm/wa_generate.ml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Generate (Target : Wa_target_sig.S) = struct
3232
type ctx =
3333
{ live : int array
3434
; in_cps : Effects.in_cps
35+
; deadcode_sentinal : Var.t
3536
; blocks : block Addr.Map.t
3637
; closures : Wa_closure_conversion.closure Var.Map.t
3738
; global_context : Wa_code_generation.context
@@ -209,7 +210,10 @@ module Generate (Target : Wa_target_sig.S) = struct
209210
let* closure = load f in
210211
return (W.Call (apply, args @ [ closure ]))
211212
| Block (tag, a, _, _) ->
212-
Memory.allocate ~tag (List.map ~f:(fun x -> `Var x) (Array.to_list a))
213+
Memory.allocate
214+
~deadcode_sentinal:ctx.deadcode_sentinal
215+
~tag
216+
(List.map ~f:(fun x -> `Var x) (Array.to_list a))
213217
| Field (x, n, Non_float) -> Memory.field (load x) n
214218
| Field (x, n, Float) ->
215219
Memory.float_array_get
@@ -633,7 +637,7 @@ module Generate (Target : Wa_target_sig.S) = struct
633637
l
634638
~init:(return [])
635639
in
636-
Memory.allocate ~tag:0 l
640+
Memory.allocate ~tag:0 ~deadcode_sentinal:ctx.deadcode_sentinal l
637641
| Extern name, l -> (
638642
let name = Primitive.resolve name in
639643
try
@@ -1088,14 +1092,22 @@ module Generate (Target : Wa_target_sig.S) = struct
10881092
~should_export
10891093
~warn_on_unhandled_effect
10901094
*)
1095+
~deadcode_sentinal
10911096
~debug =
10921097
global_context.unit_name <- unit_name;
10931098
let p, closures = Wa_closure_conversion.f p in
10941099
(*
10951100
Code.Print.program (fun _ _ -> "") p;
10961101
*)
10971102
let ctx =
1098-
{ live = live_vars; in_cps; blocks = p.blocks; closures; global_context; debug }
1103+
{ live = live_vars
1104+
; in_cps
1105+
; deadcode_sentinal
1106+
; blocks = p.blocks
1107+
; closures
1108+
; global_context
1109+
; debug
1110+
}
10991111
in
11001112
let toplevel_name = Var.fresh_n "toplevel" in
11011113
let functions =
@@ -1198,10 +1210,10 @@ let fix_switch_branches p =
11981210

11991211
let start () = make_context ~value_type:Wa_gc_target.Value.value
12001212

1201-
let f ~context ~unit_name p ~live_vars ~in_cps ~debug =
1213+
let f ~context ~unit_name p ~live_vars ~in_cps ~deadcode_sentinal ~debug =
12021214
let p = if Config.Flag.effects () then fix_switch_branches p else p in
12031215
let module G = Generate (Wa_gc_target) in
1204-
G.f ~context ~unit_name ~live_vars ~in_cps ~debug p
1216+
G.f ~context ~unit_name ~live_vars ~in_cps ~deadcode_sentinal ~debug p
12051217

12061218
let add_start_function =
12071219
let module G = Generate (Wa_gc_target) in

compiler/lib/wasm/wa_generate.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ val f :
2626
-> Code.program
2727
-> live_vars:int array
2828
-> in_cps:Effects.in_cps
29+
-> deadcode_sentinal:Code.Var.t
2930
-> debug:Parse_bytecode.Debug.t
3031
-> Wa_ast.var * (string list * (string * Javascript.expression) list)
3132

compiler/lib/wasm/wa_target_sig.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module type S = sig
2121

2222
module Memory : sig
2323
val allocate :
24-
tag:int -> [ `Expr of Wa_ast.expression | `Var of Wa_ast.var ] list -> expression
24+
tag:int
25+
-> deadcode_sentinal:Code.Var.t
26+
-> [ `Expr of Wa_ast.expression | `Var of Wa_ast.var ] list
27+
-> expression
2528

2629
val load_function_pointer :
2730
cps:bool

0 commit comments

Comments
 (0)