Skip to content

Commit db19e7c

Browse files
committed
Distinguish float field accesses in the Code IR
1 parent 702b674 commit db19e7c

File tree

11 files changed

+65
-66
lines changed

11 files changed

+65
-66
lines changed

compiler/lib/code.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,18 @@ type mutability =
377377
| Immutable
378378
| Maybe_mutable
379379

380+
type field_type =
381+
| Non_float
382+
| Float
383+
380384
type expr =
381385
| Apply of
382386
{ f : Var.t
383387
; args : Var.t list
384388
; exact : bool
385389
}
386390
| Block of int * Var.t array * array_or_not * mutability
387-
| Field of Var.t * int
391+
| Field of Var.t * int * field_type
388392
| Closure of Var.t list * cont
389393
| Constant of constant
390394
| Prim of prim * prim_arg list
@@ -393,7 +397,7 @@ type expr =
393397
type instr =
394398
| Let of Var.t * expr
395399
| Assign of Var.t * Var.t
396-
| Set_field of Var.t * int * Var.t
400+
| Set_field of Var.t * int * field_type * Var.t
397401
| Offset_ref of Var.t * int
398402
| Array_set of Var.t * Var.t * Var.t
399403

@@ -537,7 +541,8 @@ module Print = struct
537541
Format.fprintf f "; %d = %a" i Var.print a.(i)
538542
done;
539543
Format.fprintf f "}"
540-
| Field (x, i) -> Format.fprintf f "%a[%d]" Var.print x i
544+
| Field (x, i, Non_float) -> Format.fprintf f "%a[%d]" Var.print x i
545+
| Field (x, i, Float) -> Format.fprintf f "FLOAT{%a[%d]}" Var.print x i
541546
| Closure (l, c) -> Format.fprintf f "fun(%a){%a}" var_list l cont c
542547
| Constant c -> Format.fprintf f "CONST{%a}" constant c
543548
| Prim (p, l) -> prim f p l
@@ -547,7 +552,10 @@ module Print = struct
547552
match i with
548553
| Let (x, e) -> Format.fprintf f "%a = %a" Var.print x expr e
549554
| Assign (x, y) -> Format.fprintf f "(assign) %a = %a" Var.print x Var.print y
550-
| Set_field (x, i, y) -> Format.fprintf f "%a[%d] = %a" Var.print x i Var.print y
555+
| Set_field (x, i, Non_float, y) ->
556+
Format.fprintf f "%a[%d] = %a" Var.print x i Var.print y
557+
| Set_field (x, i, Float, y) ->
558+
Format.fprintf f "FLOAT{%a[%d]} = %a" Var.print x i Var.print y
551559
| Offset_ref (x, i) -> Format.fprintf f "%a[0] += %d" Var.print x i
552560
| Array_set (x, y, z) ->
553561
Format.fprintf f "%a[%a] = %a" Var.print x Var.print y Var.print z
@@ -821,7 +829,7 @@ let invariant { blocks; start; _ } =
821829
let check_expr = function
822830
| Apply _ -> ()
823831
| Block (_, _, _, _) -> ()
824-
| Field (_, _) -> ()
832+
| Field (_, _, _) -> ()
825833
| Closure (l, cont) ->
826834
List.iter l ~f:define;
827835
check_cont cont
@@ -835,7 +843,7 @@ let invariant { blocks; start; _ } =
835843
define x;
836844
check_expr e
837845
| Assign _ -> ()
838-
| Set_field (_, _i, _) -> ()
846+
| Set_field (_, _i, _, _) -> ()
839847
| Offset_ref (_x, _i) -> ()
840848
| Array_set (_x, _y, _z) -> ()
841849
in

compiler/lib/code.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,18 @@ type mutability =
191191
| Immutable
192192
| Maybe_mutable
193193

194+
type field_type =
195+
| Non_float
196+
| Float
197+
194198
type expr =
195199
| Apply of
196200
{ f : Var.t
197201
; args : Var.t list
198202
; exact : bool (* if true, then # of arguments = # of parameters *)
199203
}
200204
| Block of int * Var.t array * array_or_not * mutability
201-
| Field of Var.t * int
205+
| Field of Var.t * int * field_type
202206
| Closure of Var.t list * cont
203207
| Constant of constant
204208
| Prim of prim * prim_arg list
@@ -207,7 +211,7 @@ type expr =
207211
type instr =
208212
| Let of Var.t * expr
209213
| Assign of Var.t * Var.t
210-
| Set_field of Var.t * int * Var.t
214+
| Set_field of Var.t * int * field_type * Var.t
211215
| Offset_ref of Var.t * int
212216
| Array_set of Var.t * Var.t * Var.t
213217

compiler/lib/deadcode.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ and mark_expr st e =
6262
mark_var st f;
6363
List.iter args ~f:(fun x -> mark_var st x)
6464
| Block (_, a, _, _) -> Array.iter a ~f:(fun x -> mark_var st x)
65-
| Field (x, _) -> mark_var st x
65+
| Field (x, _, _) -> mark_var st x
6666
| Closure (_, (pc, _)) -> mark_reachable st pc
6767
| Special _ -> ()
6868
| Prim (_, l) ->
@@ -82,7 +82,7 @@ and mark_reachable st pc =
8282
match i with
8383
| Let (_, e) -> if not (pure_expr st.pure_funs e) then mark_expr st e
8484
| Assign _ -> ()
85-
| Set_field (x, _, y) ->
85+
| Set_field (x, _, _, y) ->
8686
mark_var st x;
8787
mark_var st y
8888
| Array_set (x, y, z) ->
@@ -190,7 +190,7 @@ let f ({ blocks; _ } as p : Code.program) =
190190
match i with
191191
| Let (x, e) -> add_def defs x (Expr e)
192192
| Assign (x, y) -> add_def defs x (Var y)
193-
| Set_field (_, _, _) | Array_set (_, _, _) | Offset_ref (_, _) -> ());
193+
| Set_field (_, _, _, _) | Array_set (_, _, _) | Offset_ref (_, _) -> ());
194194
match fst block.branch with
195195
| Return _ | Raise _ | Stop -> ()
196196
| Branch cont -> add_cont_dep blocks defs cont

compiler/lib/eval.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let shift l w t f =
4242
Some (Int (w (f (t i) (Int32.to_int j land 0x1f))))
4343
| _ -> None
4444

45-
let float_binop_aux l f =
45+
let float_binop_aux (l : constant list) (f : float -> float -> 'a) : 'a option =
4646
let args =
4747
match l with
4848
| [ Float i; Float j ] -> Some (i, j)
@@ -55,12 +55,12 @@ let float_binop_aux l f =
5555
| None -> None
5656
| Some (i, j) -> Some (f i j)
5757

58-
let float_binop l f =
58+
let float_binop (l : constant list) (f : float -> float -> float) : constant option =
5959
match float_binop_aux l f with
6060
| Some x -> Some (Float x)
6161
| None -> None
6262

63-
let float_unop l f =
63+
let float_unop (l : constant list) (f : float -> float) : constant option =
6464
match l with
6565
| [ Float i ] -> Some (Float (f i))
6666
| [ Int i ] -> Some (Float (f (Int32.to_float i)))
@@ -426,10 +426,11 @@ let rec do_not_raise pc visited blocks =
426426
let b = Addr.Map.find pc blocks in
427427
List.iter b.body ~f:(fun (i, _loc) ->
428428
match i with
429-
| Array_set (_, _, _) | Offset_ref (_, _) | Set_field (_, _, _) | Assign _ -> ()
429+
| Array_set (_, _, _) | Offset_ref (_, _) | Set_field (_, _, _, _) | Assign _ ->
430+
()
430431
| Let (_, e) -> (
431432
match e with
432-
| Block (_, _, _, _) | Field (_, _) | Constant _ | Closure _ -> ()
433+
| Block (_, _, _, _) | Field (_, _, _) | Constant _ | Closure _ -> ()
433434
| Apply _ -> raise May_raise
434435
| Special _ -> ()
435436
| Prim (Extern name, _) when Primitive.is_pure name -> ()

compiler/lib/flow.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let expr_deps blocks vars deps defs x e =
9494
List.iter l ~f:(fun x -> add_param_def vars defs x);
9595
cont_deps blocks vars deps defs cont
9696
| Block (_, a, _, _) -> Array.iter a ~f:(fun y -> add_dep deps x y)
97-
| Field (y, _) -> add_dep deps x y
97+
| Field (y, _, _) -> add_dep deps x y
9898

9999
let program_deps { blocks; _ } =
100100
let nv = Var.count () in
@@ -138,7 +138,7 @@ let propagate1 deps defs st x =
138138
match e with
139139
| Constant _ | Apply _ | Prim _ | Special _ | Closure _ | Block _ ->
140140
Var.Set.singleton x
141-
| Field (y, n) ->
141+
| Field (y, n, _) ->
142142
var_set_lift
143143
(fun z ->
144144
match defs.(Var.idx z) with
@@ -244,7 +244,7 @@ let program_escape defs known_origins { blocks; _ } =
244244
match i with
245245
| Let (x, e) -> expr_escape st x e
246246
| Assign _ -> ()
247-
| Set_field (x, _, y) | Array_set (x, _, y) ->
247+
| Set_field (x, _, _, y) | Array_set (x, _, y) ->
248248
Var.Set.iter
249249
(fun y -> Var.ISet.add possibly_mutable y)
250250
(Var.Tbl.get known_origins x);
@@ -268,7 +268,7 @@ let propagate2 ?(skip_param = false) defs known_origins possibly_mutable st x =
268268
| Expr e -> (
269269
match e with
270270
| Constant _ | Closure _ | Apply _ | Prim _ | Block _ | Special _ -> false
271-
| Field (y, n) ->
271+
| Field (y, n, _) ->
272272
Var.Tbl.get st y
273273
|| Var.Set.exists
274274
(fun z ->
@@ -360,7 +360,7 @@ let the_native_string_of info x =
360360
(*XXX Maybe we could iterate? *)
361361
let direct_approx info x =
362362
match info.info_defs.(Var.idx x) with
363-
| Expr (Field (y, n)) ->
363+
| Expr (Field (y, n, _)) ->
364364
get_approx
365365
info
366366
(fun z ->

compiler/lib/freevars.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let iter_expr_free_vars f e =
3434
f x;
3535
List.iter ~f args
3636
| Block (_, a, _, _) -> Array.iter ~f a
37-
| Field (x, _) -> f x
37+
| Field (x, _, _) -> f x
3838
| Closure _ -> ()
3939
| Special _ -> ()
4040
| Prim (_, l) ->
@@ -46,7 +46,7 @@ let iter_expr_free_vars f e =
4646
let iter_instr_free_vars f i =
4747
match i with
4848
| Let (_, e) -> iter_expr_free_vars f e
49-
| Set_field (x, _, y) ->
49+
| Set_field (x, _, _, y) ->
5050
f x;
5151
f y
5252
| Offset_ref (x, _) -> f x

compiler/lib/generate.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
12401240
| NotArray | Unknown -> Mlvalue.Block.make ~tag ~args:contents
12411241
in
12421242
(x, prop, queue), []
1243-
| Field (x, n) ->
1243+
| Field (x, n, _) ->
12441244
let (px, cx), queue = access_queue queue x in
12451245
(Mlvalue.Block.field cx n, or_p px mutable_p, queue), []
12461246
| Closure (args, ((pc, _) as cont)) ->
@@ -1532,7 +1532,7 @@ and translate_instr ctx expr_queue instr =
15321532
expr_queue
15331533
prop
15341534
(instrs @ [ J.variable_declaration [ J.V x, (ce, loc) ], loc ]))
1535-
| Set_field (x, n, y) ->
1535+
| Set_field (x, n, _, y) ->
15361536
let loc = source_location_ctx ctx pc in
15371537
let (_px, cx), expr_queue = access_queue expr_queue x in
15381538
let (_py, cy), expr_queue = access_queue expr_queue y in

compiler/lib/global_flow.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ let expr_deps blocks st x e =
230230
| Closure (l, cont) ->
231231
List.iter l ~f:(fun x -> add_param_def st x);
232232
cont_deps blocks st cont
233-
| Field (y, _) -> add_dep st x y
233+
| Field (y, _, _) -> add_dep st x y
234234

235235
let program_deps st { blocks; _ } =
236236
Addr.Map.iter
@@ -241,7 +241,7 @@ let program_deps st { blocks; _ } =
241241
add_expr_def st x e;
242242
expr_deps blocks st x e
243243
| Assign (x, y) -> add_assign_def st x y
244-
| Set_field (x, _, y) | Array_set (x, _, y) ->
244+
| Set_field (x, _, _, y) | Array_set (x, _, y) ->
245245
possibly_mutable st x;
246246
do_escape st Escape y
247247
| Offset_ref _ -> ());
@@ -274,7 +274,7 @@ let program_deps st { blocks; _ } =
274274
List.iter
275275
~f:(fun (i, _) ->
276276
match i with
277-
| Let (y, Field (x', _)) when Var.equal b x' ->
277+
| Let (y, Field (x', _, _)) when Var.equal b x' ->
278278
Hashtbl.add st.known_cases y tags
279279
| _ -> ())
280280
block.body)
@@ -401,7 +401,7 @@ let propagate st ~update approx x =
401401
(* A constant cannot contain a function *)
402402
Domain.bot
403403
| Closure _ | Block _ -> Domain.singleton x
404-
| Field (y, n) -> (
404+
| Field (y, n, _) -> (
405405
match Var.Tbl.get approx y with
406406
| Values { known; others } ->
407407
let tags =

0 commit comments

Comments
 (0)