Skip to content

Commit fab177e

Browse files
committed
Distinguish float field accesses in the Code IR
1 parent f96b7a0 commit fab177e

File tree

13 files changed

+79
-79
lines changed

13 files changed

+79
-79
lines changed

compiler/lib/code.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,18 @@ type mutability =
459459
| Immutable
460460
| Maybe_mutable
461461

462+
type field_type =
463+
| Non_float
464+
| Float
465+
462466
type expr =
463467
| Apply of
464468
{ f : Var.t
465469
; args : Var.t list
466470
; exact : bool
467471
}
468472
| Block of int * Var.t array * array_or_not * mutability
469-
| Field of Var.t * int
473+
| Field of Var.t * int * field_type
470474
| Closure of Var.t list * cont
471475
| Constant of constant
472476
| Prim of prim * prim_arg list
@@ -475,7 +479,7 @@ type expr =
475479
type instr =
476480
| Let of Var.t * expr
477481
| Assign of Var.t * Var.t
478-
| Set_field of Var.t * int * Var.t
482+
| Set_field of Var.t * int * field_type * Var.t
479483
| Offset_ref of Var.t * int
480484
| Array_set of Var.t * Var.t * Var.t
481485

@@ -619,7 +623,8 @@ module Print = struct
619623
Format.fprintf f "; %d = %a" i Var.print a.(i)
620624
done;
621625
Format.fprintf f "}"
622-
| Field (x, i) -> Format.fprintf f "%a[%d]" Var.print x i
626+
| Field (x, i, Non_float) -> Format.fprintf f "%a[%d]" Var.print x i
627+
| Field (x, i, Float) -> Format.fprintf f "FLOAT{%a[%d]}" Var.print x i
623628
| Closure (l, c) -> Format.fprintf f "fun(%a){%a}" var_list l cont c
624629
| Constant c -> Format.fprintf f "CONST{%a}" constant c
625630
| Prim (p, l) -> prim f p l
@@ -629,7 +634,10 @@ module Print = struct
629634
match i with
630635
| Let (x, e) -> Format.fprintf f "%a = %a" Var.print x expr e
631636
| Assign (x, y) -> Format.fprintf f "(assign) %a = %a" Var.print x Var.print y
632-
| Set_field (x, i, y) -> Format.fprintf f "%a[%d] = %a" Var.print x i Var.print y
637+
| Set_field (x, i, Non_float, y) ->
638+
Format.fprintf f "%a[%d] = %a" Var.print x i Var.print y
639+
| Set_field (x, i, Float, y) ->
640+
Format.fprintf f "FLOAT{%a[%d]} = %a" Var.print x i Var.print y
633641
| Offset_ref (x, i) -> Format.fprintf f "%a[0] += %d" Var.print x i
634642
| Array_set (x, y, z) ->
635643
Format.fprintf f "%a[%a] = %a" Var.print x Var.print y Var.print z
@@ -903,7 +911,7 @@ let invariant { blocks; start; _ } =
903911
let check_expr = function
904912
| Apply _ -> ()
905913
| Block (_, _, _, _) -> ()
906-
| Field (_, _) -> ()
914+
| Field (_, _, _) -> ()
907915
| Closure (l, cont) ->
908916
List.iter l ~f:define;
909917
check_cont cont
@@ -917,7 +925,7 @@ let invariant { blocks; start; _ } =
917925
define x;
918926
check_expr e
919927
| Assign _ -> ()
920-
| Set_field (_, _i, _) -> ()
928+
| Set_field (_, _i, _, _) -> ()
921929
| Offset_ref (_x, _i) -> ()
922930
| Array_set (_x, _y, _z) -> ()
923931
in

compiler/lib/code.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,18 @@ type mutability =
218218
| Immutable
219219
| Maybe_mutable
220220

221+
type field_type =
222+
| Non_float
223+
| Float
224+
221225
type expr =
222226
| Apply of
223227
{ f : Var.t
224228
; args : Var.t list
225229
; exact : bool (* if true, then # of arguments = # of parameters *)
226230
}
227231
| Block of int * Var.t array * array_or_not * mutability
228-
| Field of Var.t * int
232+
| Field of Var.t * int * field_type
229233
| Closure of Var.t list * cont
230234
| Constant of constant
231235
| Prim of prim * prim_arg list
@@ -234,7 +238,7 @@ type expr =
234238
type instr =
235239
| Let of Var.t * expr
236240
| Assign of Var.t * Var.t
237-
| Set_field of Var.t * int * Var.t
241+
| Set_field of Var.t * int * field_type * Var.t
238242
| Offset_ref of Var.t * int
239243
| Array_set of Var.t * Var.t * Var.t
240244

compiler/lib/deadcode.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ and mark_expr st e =
7171
mark_var st f;
7272
List.iter args ~f:(fun x -> mark_var st x)
7373
| Block (_, a, _, _) -> Array.iter a ~f:(fun x -> mark_var st x)
74-
| Field (x, _) -> mark_var st x
74+
| Field (x, _, _) -> mark_var st x
7575
| Closure (_, (pc, _)) -> mark_reachable st pc
7676
| Special _ -> ()
7777
| Prim (_, l) ->
@@ -91,7 +91,7 @@ and mark_reachable st pc =
9191
match i with
9292
| Let (_, e) -> if not (pure_expr st.pure_funs e) then mark_expr st e
9393
| Assign _ -> ()
94-
| Set_field (x, _, y) -> (
94+
| Set_field (x, _, _, y) -> (
9595
match st.defs.(Var.idx x) with
9696
| [ Expr (Block _) ] when st.live.(Var.idx x) = 0 ->
9797
(* We will keep this instruction only if x is live *)
@@ -124,7 +124,7 @@ and mark_reachable st pc =
124124
let live_instr st i =
125125
match i with
126126
| Let (x, e) -> st.live.(Var.idx x) > 0 || not (pure_expr st.pure_funs e)
127-
| Assign (x, _) | Set_field (x, _, _) -> st.live.(Var.idx x) > 0
127+
| Assign (x, _) | Set_field (x, _, _, _) -> st.live.(Var.idx x) > 0
128128
| Offset_ref _ | Array_set _ -> true
129129

130130
let rec filter_args st pl al =
@@ -201,7 +201,7 @@ let f ({ blocks; _ } as p : Code.program) =
201201
match i with
202202
| Let (x, e) -> add_def defs x (Expr e)
203203
| Assign (x, y) -> add_def defs x (Var y)
204-
| Set_field (_, _, _) | Array_set (_, _, _) | Offset_ref (_, _) -> ());
204+
| Set_field (_, _, _, _) | Array_set (_, _, _) | Offset_ref (_, _) -> ());
205205
match fst block.branch with
206206
| Return _ | Raise _ | Stop -> ()
207207
| Branch cont -> add_cont_dep blocks defs cont

compiler/lib/duplicate.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let expr s e =
2727
| Apply { f; args; exact } ->
2828
Apply { f = s f; args = List.map args ~f:(fun x -> s x); exact }
2929
| Block (n, a, k, mut) -> Block (n, Array.map a ~f:(fun x -> s x), k, mut)
30-
| Field (x, n) -> Field (s x, n)
30+
| Field (x, n, field_type) -> Field (s x, n, field_type)
3131
| Closure _ -> failwith "Inlining/Duplicating closure is currenly not supported"
3232
| Special x -> Special x
3333
| Prim (p, l) ->
@@ -41,7 +41,7 @@ let instr s i =
4141
match i with
4242
| Let (x, e) -> Let (s x, expr s e)
4343
| Assign (x, y) -> Assign (s x, s y)
44-
| Set_field (x, n, y) -> Set_field (s x, n, s y)
44+
| Set_field (x, n, typ, y) -> Set_field (s x, n, typ, s y)
4545
| Offset_ref (x, n) -> Offset_ref (s x, n)
4646
| Array_set (x, y, z) -> Array_set (s x, s y, s z)
4747

compiler/lib/eval.ml

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

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

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

62-
let float_unop l f =
62+
let float_unop (l : constant list) (f : float -> float) : constant option =
6363
match l with
6464
| [ Float i ] -> Some (Float (f i))
6565
| [ Int i ] -> Some (Float (f (Int32.to_float i)))
@@ -433,10 +433,11 @@ let rec do_not_raise pc visited blocks =
433433
let b = Addr.Map.find pc blocks in
434434
List.iter b.body ~f:(fun (i, _loc) ->
435435
match i with
436-
| Array_set (_, _, _) | Offset_ref (_, _) | Set_field (_, _, _) | Assign _ -> ()
436+
| Array_set (_, _, _) | Offset_ref (_, _) | Set_field (_, _, _, _) | Assign _ ->
437+
()
437438
| Let (_, e) -> (
438439
match e with
439-
| Block (_, _, _, _) | Field (_, _) | Constant _ | Closure _ -> ()
440+
| Block (_, _, _, _) | Field (_, _, _) | Constant _ | Closure _ -> ()
440441
| Apply _ -> raise May_raise
441442
| Special _ -> ()
442443
| 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
@@ -104,7 +104,7 @@ let expr_deps blocks vars deps defs x e =
104104
List.iter l ~f:(fun x -> add_param_def vars defs x);
105105
cont_deps blocks vars deps defs cont
106106
| Block (_, a, _, _) -> Array.iter a ~f:(fun y -> add_dep deps x y)
107-
| Field (y, _) -> add_dep deps x y
107+
| Field (y, _, _) -> add_dep deps x y
108108

109109
let program_deps { blocks; _ } =
110110
let nv = Var.count () in
@@ -148,7 +148,7 @@ let propagate1 deps defs st x =
148148
match e with
149149
| Constant _ | Apply _ | Prim _ | Special _ | Closure _ | Block _ ->
150150
Var.Set.singleton x
151-
| Field (y, n) ->
151+
| Field (y, n, _) ->
152152
var_set_lift
153153
(fun z ->
154154
match defs.(Var.idx z) with
@@ -254,7 +254,7 @@ let program_escape defs known_origins { blocks; _ } =
254254
match i with
255255
| Let (x, e) -> expr_escape st x e
256256
| Assign _ -> ()
257-
| Set_field (x, _, y) | Array_set (x, _, y) ->
257+
| Set_field (x, _, _, y) | Array_set (x, _, y) ->
258258
Var.Set.iter
259259
(fun y -> Var.ISet.add possibly_mutable y)
260260
(Var.Tbl.get known_origins x);
@@ -278,7 +278,7 @@ let propagate2 ?(skip_param = false) defs known_origins possibly_mutable st x =
278278
| Expr e -> (
279279
match e with
280280
| Constant _ | Closure _ | Apply _ | Prim _ | Block _ | Special _ -> false
281-
| Field (y, n) ->
281+
| Field (y, n, _) ->
282282
Var.Tbl.get st y
283283
|| Var.Set.exists
284284
(fun z ->
@@ -401,7 +401,7 @@ let the_native_string_of info x =
401401
(*XXX Maybe we could iterate? *)
402402
let direct_approx (info : Info.t) x =
403403
match info.info_defs.(Var.idx x) with
404-
| Expr (Field (y, n)) ->
404+
| Expr (Field (y, n, _)) ->
405405
get_approx
406406
info
407407
(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
@@ -1070,7 +1070,7 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
10701070
| NotArray | Unknown -> Mlvalue.Block.make ~tag ~args:contents
10711071
in
10721072
(x, prop, queue), []
1073-
| Field (x, n) ->
1073+
| Field (x, n, _) ->
10741074
let (px, cx), queue = access_queue queue x in
10751075
(Mlvalue.Block.field cx n, or_p px mutable_p, queue), []
10761076
| Closure (args, ((pc, _) as cont)) ->
@@ -1378,7 +1378,7 @@ and translate_instr ctx expr_queue instr =
13781378
expr_queue
13791379
prop
13801380
(instrs @ [ J.variable_declaration [ J.V x, (ce, loc) ], loc ]))
1381-
| Set_field (x, n, y) ->
1381+
| Set_field (x, n, _, y) ->
13821382
let loc = source_location_ctx ctx pc in
13831383
let (_px, cx), expr_queue = access_queue expr_queue x in
13841384
let (_py, cy), expr_queue = access_queue expr_queue y in

compiler/lib/global_deadcode.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ let definitions prog =
6969
match i with
7070
| Let (x, e) -> set_def x (Expr e)
7171
| Assign (x, _) -> set_def x Param
72-
| Set_field (_, _, _) | Offset_ref (_, _) | Array_set (_, _, _) -> ())
72+
| Set_field (_, _, _, _) | Offset_ref (_, _) | Array_set (_, _, _) -> ())
7373
block.body)
7474
prog.blocks;
7575
defs
@@ -128,7 +128,7 @@ let usages prog (global_info : Global_flow.info) :
128128
~f:(fun a -> if variable_may_escape a global_info then add_use Compute x a)
129129
args
130130
| Block (_, vars, _, _) -> Array.iter ~f:(add_use Compute x) vars
131-
| Field (z, _) -> add_use Compute x z
131+
| Field (z, _, _) -> add_use Compute x z
132132
| Constant _ -> ()
133133
| Special _ -> ()
134134
| Closure (_, cont) -> add_cont_deps cont
@@ -149,7 +149,7 @@ let usages prog (global_info : Global_flow.info) :
149149
| Let (x, e) -> add_expr_uses x e
150150
(* For assignment, propagate liveness from new to old variable like a block parameter *)
151151
| Assign (x, y) -> add_use Propagate x y
152-
| Set_field (_, _, _) | Offset_ref (_, _) | Array_set (_, _, _) -> ())
152+
| Set_field (_, _, _, _) | Offset_ref (_, _) | Array_set (_, _, _) -> ())
153153
block.body;
154154
(* Add uses from block branch *)
155155
match fst block.branch with
@@ -175,7 +175,7 @@ let expr_vars e =
175175
List.fold_left ~f:(fun acc x -> Var.Set.add x acc) ~init:vars args
176176
| Block (_, params, _, _) ->
177177
Array.fold_left ~f:(fun acc x -> Var.Set.add x acc) ~init:vars params
178-
| Field (z, _) -> Var.Set.add z vars
178+
| Field (z, _, _) -> Var.Set.add z vars
179179
| Prim (_, args) ->
180180
List.fold_left
181181
~f:(fun acc v ->
@@ -225,14 +225,14 @@ let liveness prog pure_funs (global_info : Global_flow.info) =
225225
~f:(fun x -> if variable_may_escape x global_info then add_top x)
226226
args
227227
| Block (_, _, _, _)
228-
| Field (_, _)
228+
| Field (_, _, _)
229229
| Closure (_, _)
230230
| Constant _
231231
| Prim (_, _)
232232
| Special _ ->
233233
let vars = expr_vars e in
234234
Var.Set.iter add_top vars)
235-
| Set_field (x, i, y) ->
235+
| Set_field (x, i, _, y) ->
236236
add_live_field x i;
237237
add_top y
238238
| Array_set (x, y, z) ->
@@ -294,12 +294,12 @@ let propagate uses defs live_vars live_table x =
294294
if Var.equal v x && IntSet.mem i fields then found := true)
295295
vars;
296296
if !found then Top else Dead
297-
| Expr (Field (_, i)) -> Live (IntSet.singleton i)
297+
| Expr (Field (_, i, _)) -> Live (IntSet.singleton i)
298298
| _ -> Top)
299299
(* If y is top and y is a field access, x depends only on that field *)
300300
| Top -> (
301301
match Var.Tbl.get defs y with
302-
| Expr (Field (_, i)) -> Live (IntSet.singleton i)
302+
| Expr (Field (_, i, _)) -> Live (IntSet.singleton i)
303303
| _ -> Top))
304304
(* If x is used as an argument for parameter y, then contribution is liveness of y *)
305305
| Propagate -> Var.Tbl.get live_table y
@@ -358,8 +358,9 @@ let zero prog sentinal live_table =
358358
| Apply ap ->
359359
let args = List.map ~f:zero_var ap.args in
360360
Let (x, Apply { ap with args })
361-
| Field (_, _) | Closure (_, _) | Constant _ | Prim (_, _) | Special _ -> instr)
362-
| Assign (_, _) | Set_field (_, _, _) | Offset_ref (_, _) | Array_set (_, _, _) ->
361+
| Field (_, _, _) | Closure (_, _) | Constant _ | Prim (_, _) | Special _ -> instr
362+
)
363+
| Assign (_, _) | Set_field (_, _, _, _) | Offset_ref (_, _) | Array_set (_, _, _) ->
363364
instr
364365
in
365366
let zero_block block =

compiler/lib/global_flow.ml

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

236236
let program_deps st { blocks; _ } =
237237
Addr.Map.iter
@@ -242,7 +242,7 @@ let program_deps st { blocks; _ } =
242242
add_expr_def st x e;
243243
expr_deps blocks st x e
244244
| Assign (x, y) -> add_assign_def st x y
245-
| Set_field (x, _, y) | Array_set (x, _, y) ->
245+
| Set_field (x, _, _, y) | Array_set (x, _, y) ->
246246
possibly_mutable st x;
247247
do_escape st Escape y
248248
| Offset_ref _ -> ());
@@ -275,7 +275,7 @@ let program_deps st { blocks; _ } =
275275
List.iter
276276
~f:(fun (i, _) ->
277277
match i with
278-
| Let (y, Field (x', _)) when Var.equal b x' ->
278+
| Let (y, Field (x', _, _)) when Var.equal b x' ->
279279
Hashtbl.add st.known_cases y tags
280280
| _ -> ())
281281
block.body)
@@ -403,7 +403,7 @@ let propagate st ~update approx x =
403403
(* A constant cannot contain a function *)
404404
Domain.bot
405405
| Closure _ | Block _ -> Domain.singleton x
406-
| Field (y, n) -> (
406+
| Field (y, n, _) -> (
407407
match Var.Tbl.get approx y with
408408
| Values { known; others } ->
409409
let tags =

0 commit comments

Comments
 (0)