Skip to content

Commit c40a123

Browse files
authored
Make instruction identifiers abstract (#3635)
1 parent 09ea6b1 commit c40a123

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+348
-290
lines changed

backend/amd64/cfg_selection.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,5 @@ class selector =
345345
end
346346

347347
let fundecl ~future_funcnames f =
348-
Cfg.reset_next_instr_id ();
348+
Cfg.reset_instr_id ();
349349
(new selector)#emit_fundecl ~future_funcnames f

backend/amd64/emit.ml

-1
Original file line numberDiff line numberDiff line change
@@ -2807,4 +2807,3 @@ let end_assembly () =
28072807
(* The internal assembler does not work if reset_all is called here *)
28082808
if not !Flambda_backend_flags.internal_assembler then
28092809
reset_all ()
2810-

backend/arm64/cfg_selection.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,5 @@ class selector =
180180
end
181181

182182
let fundecl ~future_funcnames f =
183-
Cfg.reset_next_instr_id ();
183+
Cfg.reset_instr_id ();
184184
(new selector)#emit_fundecl ~future_funcnames f

backend/cfg/cfg.ml

+14-7
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ let map_first_instruction (block : basic_block) (t : 'a instr_mapper) =
218218
| None -> t.f block.terminator
219219
| Some first_instr -> t.f first_instr
220220

221-
let first_instruction_id (block : basic_block) : int =
221+
let first_instruction_id (block : basic_block) : InstructionId.t =
222222
map_first_instruction block { f = (fun instr -> instr.id) }
223223

224224
let first_instruction_stack_offset (block : basic_block) : int =
@@ -527,14 +527,11 @@ let make_instruction ~desc ?(arg = [||]) ?(res = [||]) ?(dbg = Debuginfo.none)
527527
available_across
528528
}
529529
530-
let next_instr_id = ref 0
530+
let instr_id = InstructionId.make_sequence ()
531531
532-
let reset_next_instr_id () = next_instr_id := 0
532+
let reset_instr_id () = InstructionId.reset instr_id
533533
534-
let next_instr_id () : int =
535-
let res = !next_instr_id in
536-
incr next_instr_id;
537-
res
534+
let next_instr_id () = InstructionId.get_next instr_id
538535
539536
let make_instr desc arg res dbg =
540537
{ desc;
@@ -594,3 +591,13 @@ let basic_block_contains_calls block =
594591
match[@ocaml.warning "-4"] instr.desc with
595592
| Op (Alloc _ | Poll) -> true
596593
| _ -> false)
594+
595+
let max_instr_id t =
596+
(* CR-someday xclerc for xclerc: factor out with similar function in
597+
regalloc/. *)
598+
fold_blocks t ~init:InstructionId.none ~f:(fun _label block max_id ->
599+
let max_id =
600+
DLL.fold_left block.body ~init:max_id ~f:(fun max_id instr ->
601+
InstructionId.max max_id instr.id)
602+
in
603+
InstructionId.max max_id block.terminator.id)

backend/cfg/cfg.mli

+7-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ val replace_successor_labels :
135135
vice versa. *)
136136
val can_raise_interproc : basic_block -> bool
137137

138-
val first_instruction_id : basic_block -> int
138+
val first_instruction_id : basic_block -> InstructionId.t
139139

140140
val first_instruction_stack_offset : basic_block -> int
141141

@@ -217,7 +217,7 @@ val make_instruction :
217217
?fdo:Fdo_info.t ->
218218
?live:Reg.Set.t ->
219219
stack_offset:int ->
220-
id:int ->
220+
id:InstructionId.t ->
221221
?irc_work_list:irc_work_list ->
222222
?ls_order:int ->
223223
?available_before:Reg_availability_set.t option ->
@@ -230,11 +230,14 @@ val make_instr :
230230
'a -> Reg.t array -> Reg.t array -> Debuginfo.t -> 'a instruction
231231

232232
(** These IDs are also used by [make_instr] *)
233-
val next_instr_id : unit -> int
233+
val next_instr_id : unit -> InstructionId.t
234234

235-
val reset_next_instr_id : unit -> unit
235+
val reset_instr_id : unit -> unit
236236

237237
val make_empty_block : ?label:Label.t -> terminator instruction -> basic_block
238238

239239
(** "Contains calls" in the traditional sense as used in upstream [Selectgen]. *)
240240
val basic_block_contains_calls : basic_block -> bool
241+
242+
(* [max_instr_id cfg] returns the maximum instruction identifier in [cfg]. *)
243+
val max_instr_id : t -> InstructionId.t

backend/cfg/cfg_comballoc.ml

+8-19
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ type compatible_allocations =
2121
next_cell : cell option
2222
}
2323

24-
(* [max_instr_id cfg] returns the maximum instruction identifier in [cfg]. *)
25-
let max_instr_id : Cfg.t -> int =
26-
fun cfg ->
27-
(* CR-someday xclerc for xclerc: factor out with similar function in
28-
regalloc/. *)
29-
Cfg.fold_blocks cfg ~init:Int.min_int ~f:(fun _label block max_id ->
30-
let max_id =
31-
DLL.fold_left block.body ~init:max_id ~f:(fun max_id instr ->
32-
Int.max max_id instr.Cfg.id)
33-
in
34-
Int.max max_id block.terminator.id)
35-
3624
(* [find_next_allocation cell] returns the first allocation found by iterating
3725
from [cell]. *)
3826
let rec find_next_allocation : cell option -> allocation option =
@@ -130,8 +118,8 @@ let find_compatible_allocations :
130118
- the "first" allocation is made bigger to account for all allocations;
131119
- the other allocations are replaced with a reference to the result of the
132120
previous allocation, with a different offset. *)
133-
let rec combine : max_instr_id:int ref -> cell option -> unit =
134-
fun ~max_instr_id cell ->
121+
let rec combine : instr_id:InstructionId.sequence -> cell option -> unit =
122+
fun ~instr_id cell ->
135123
let first_allocation = find_next_allocation cell in
136124
match first_allocation with
137125
| None -> ()
@@ -176,22 +164,23 @@ let rec combine : max_instr_id:int ref -> cell option -> unit =
176164
mode
177165
})
178166
};
179-
incr max_instr_id;
180167
DLL.insert_after cell
181168
{ first_allocation_instr with
182169
desc =
183170
Cfg.Op
184171
(Intop_imm (Simple_operation.Iadd, total_size_of_other_allocations));
185172
arg = [| first_allocation_res0 |];
186173
res = [| first_allocation_res0 |];
187-
id = !max_instr_id
174+
id = InstructionId.get_next instr_id
188175
});
189-
combine ~max_instr_id compatible_allocs.next_cell
176+
combine ~instr_id compatible_allocs.next_cell
190177

191178
let run : Cfg_with_layout.t -> Cfg_with_layout.t =
192179
fun cfg_with_layout ->
193180
let cfg = Cfg_with_layout.cfg cfg_with_layout in
194-
let max_instr_id = ref (max_instr_id cfg) in
181+
let instr_id =
182+
InstructionId.make_sequence ~last_used:(Cfg.max_instr_id cfg) ()
183+
in
195184
Cfg.iter_blocks cfg ~f:(fun _label block ->
196-
combine ~max_instr_id (DLL.hd_cell block.body));
185+
combine ~instr_id (DLL.hd_cell block.body));
197186
cfg_with_layout

backend/cfg/cfg_cse.ml

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[@@@ocaml.warning "+a-30-40-41-42"]
22

33
module DLL = Flambda_backend_utils.Doubly_linked_list
4-
module Instruction = Regalloc_utils.Instruction
54
module List = ListLabels
65
module Array = ArrayLabels
76

@@ -17,19 +16,19 @@ let debug = false
1716
module State : sig
1817
type t
1918

20-
val make : next_instruction_id:Instruction.id -> t
19+
val make : last_used:InstructionId.t -> t
2120

22-
val get_and_incr_instruction_id : t -> Instruction.id
21+
val get_and_incr_instruction_id : t -> InstructionId.t
2322
end = struct
2423
(* CR-soon xclerc for xclerc: factor out with the state of GI, IRC, LS. *)
25-
type t = { mutable next_instruction_id : Instruction.id }
24+
type t = { instruction_id : InstructionId.sequence }
2625

27-
let make ~next_instruction_id = { next_instruction_id }
26+
let make ~last_used =
27+
let instruction_id = InstructionId.make_sequence ~last_used () in
28+
{ instruction_id }
2829

2930
let get_and_incr_instruction_id state =
30-
let res = state.next_instruction_id in
31-
state.next_instruction_id <- succ res;
32-
res
31+
InstructionId.get_next state.instruction_id
3332
end
3433

3534
let insert_single_move :
@@ -277,9 +276,7 @@ class cse_generic =
277276
(if not (List.mem ~set:cfg.fun_codegen_options Cfg.No_CSE)
278277
then
279278
let cfg_infos = Regalloc_utils.collect_cfg_infos cfg_with_layout in
280-
let state =
281-
State.make ~next_instruction_id:(succ cfg_infos.max_instruction_id)
282-
in
279+
let state = State.make ~last_used:cfg_infos.max_instruction_id in
283280
self#cse_blocks state cfg);
284281
cfg_with_layout
285282
end

backend/cfg/cfg_dataflow.ml

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[@@@ocaml.warning "+a-4-30-40-41-42-69"]
22

33
open! Int_replace_polymorphic_compare
4-
module Instr = Numbers.Int
54
module DLL = Flambda_backend_utils.Doubly_linked_list
65

76
module type Transfer_domain_S = sig
@@ -33,7 +32,7 @@ module type Dataflow_direction_S = sig
3332
type context
3433

3534
val transfer_block :
36-
update_instr:(int -> instr_domain -> unit) ->
35+
update_instr:(InstructionId.t -> instr_domain -> unit) ->
3736
Transfer_domain.t ->
3837
Cfg.basic_block ->
3938
context ->
@@ -57,7 +56,7 @@ module type Dataflow_S = sig
5756

5857
val get_res_block : work_state -> Transfer_domain.t Label.Tbl.t
5958

60-
val get_res_instr_exn : work_state -> instr_domain Instr.Tbl.t
59+
val get_res_instr_exn : work_state -> instr_domain InstructionId.Tbl.t
6160

6261
val run : max_iteration:int -> work_state -> context -> (unit, unit) Result.t
6362
end
@@ -123,7 +122,7 @@ module Make_dataflow (D : Dataflow_direction_S) :
123122
{ cfg : Cfg.t;
124123
mutable queue : WorkSet.t;
125124
map_block : Transfer_domain.t Label.Tbl.t;
126-
map_instr : D.instr_domain Instr.Tbl.t option
125+
map_instr : D.instr_domain InstructionId.Tbl.t option
127126
}
128127

129128
type instr_domain = D.instr_domain
@@ -197,11 +196,11 @@ module Make_dataflow (D : Dataflow_direction_S) :
197196
assert (Label.Tbl.length priorities = Label.Tbl.length cfg.blocks);
198197
priorities
199198

200-
let update_instr : work_state -> int -> instr_domain -> unit =
199+
let update_instr : work_state -> InstructionId.t -> instr_domain -> unit =
201200
fun t instr_id value ->
202201
match t.map_instr with
203202
| None -> ()
204-
| Some map_instr -> Instr.Tbl.replace map_instr instr_id value
203+
| Some map_instr -> InstructionId.Tbl.replace map_instr instr_id value
205204

206205
let create :
207206
Cfg.t ->
@@ -217,7 +216,7 @@ module Make_dataflow (D : Dataflow_direction_S) :
217216
then
218217
let map_instr =
219218
(* CR-soon xclerc for xclerc: review the `16` constant. *)
220-
Instr.Tbl.create (Label.Tbl.length cfg.Cfg.blocks * 16)
219+
InstructionId.Tbl.create (Label.Tbl.length cfg.Cfg.blocks * 16)
221220
in
222221
Some map_instr
223222
else None
@@ -345,7 +344,7 @@ module Forward (D : Domain_S) (T : Forward_transfer with type domain = D.t) :
345344
type context = T.context
346345

347346
let transfer_block :
348-
update_instr:(int -> instr_domain -> unit) ->
347+
update_instr:(InstructionId.t -> instr_domain -> unit) ->
349348
Transfer_domain.t ->
350349
Cfg.basic_block ->
351350
context ->
@@ -426,8 +425,8 @@ module type Backward_S = sig
426425

427426
type _ map =
428427
| Block : domain Label.Tbl.t map
429-
| Instr : domain Instr.Tbl.t map
430-
| Both : (domain Instr.Tbl.t * domain Label.Tbl.t) map
428+
| Instr : domain InstructionId.Tbl.t map
429+
| Both : (domain InstructionId.Tbl.t * domain Label.Tbl.t) map
431430

432431
val run :
433432
Cfg.t ->
@@ -497,7 +496,7 @@ module Backward (D : Domain_S) (T : Backward_transfer with type domain = D.t) :
497496
type context = T.context
498497

499498
let transfer_block :
500-
update_instr:(int -> instr_domain -> unit) ->
499+
update_instr:(InstructionId.t -> instr_domain -> unit) ->
501500
Transfer_domain.t ->
502501
Cfg.basic_block ->
503502
context ->
@@ -532,8 +531,8 @@ module Backward (D : Domain_S) (T : Backward_transfer with type domain = D.t) :
532531

533532
type _ map =
534533
| Block : domain Label.Tbl.t map
535-
| Instr : domain Instr.Tbl.t map
536-
| Both : (domain Instr.Tbl.t * domain Label.Tbl.t) map
534+
| Instr : domain InstructionId.Tbl.t map
535+
| Both : (domain InstructionId.Tbl.t * domain Label.Tbl.t) map
537536

538537
let run :
539538
type a.

backend/cfg/cfg_dataflow.mli

+2-4
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ module type Backward_transfer = sig
8383
val exception_ : domain -> context -> (domain, error) result
8484
end
8585

86-
module Instr : Identifiable.S with type t = int
87-
8886
module Dataflow_result : sig
8987
type ('a, 'e) t =
9088
| Ok of 'a
@@ -101,8 +99,8 @@ module type Backward_S = sig
10199

102100
type _ map =
103101
| Block : domain Label.Tbl.t map
104-
| Instr : domain Instr.Tbl.t map
105-
| Both : (domain Instr.Tbl.t * domain Label.Tbl.t) map
102+
| Instr : domain InstructionId.Tbl.t map
103+
| Both : (domain InstructionId.Tbl.t * domain Label.Tbl.t) map
106104

107105
val run :
108106
Cfg.t ->

backend/cfg/cfg_deadcode.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module DLL = Flambda_backend_utils.Doubly_linked_list
66
let live_before :
77
type a. a Cfg.instruction -> Cfg_with_infos.liveness -> Reg.Set.t =
88
fun instr liveness ->
9-
match Cfg_dataflow.Instr.Tbl.find_opt liveness instr.id with
10-
| None -> fatal "no liveness information for instruction %d" instr.id
9+
match InstructionId.Tbl.find_opt liveness instr.id with
10+
| None ->
11+
fatal "no liveness information for instruction %a" InstructionId.format
12+
instr.id
1113
| Some { Cfg_liveness.before; across = _ } -> before
1214

1315
let remove_deadcode (body : Cfg.basic_instruction_list) changed liveness

backend/cfg/cfg_intf.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module S = struct
9696
mutable fdo : Fdo_info.t;
9797
mutable live : Reg.Set.t;
9898
mutable stack_offset : int;
99-
id : int;
99+
id : InstructionId.t;
100100
mutable irc_work_list : irc_work_list;
101101
mutable ls_order : int;
102102
mutable available_before : Reg_availability_set.t option;

backend/cfg/cfg_polling.ml

+8-11
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module Polls_before_prtc_transfer = struct
7171

7272
type context =
7373
{ future_funcnames : String.Set.t;
74-
optimistic_prologue_poll_instr_id : int
74+
optimistic_prologue_poll_instr_id : InstructionId.t
7575
}
7676

7777
type error = |
@@ -81,7 +81,7 @@ module Polls_before_prtc_transfer = struct
8181
fun dom instr { future_funcnames = _; optimistic_prologue_poll_instr_id } ->
8282
match instr.desc with
8383
| Op Poll ->
84-
if instr.id = optimistic_prologue_poll_instr_id
84+
if InstructionId.equal instr.id optimistic_prologue_poll_instr_id
8585
then Ok dom
8686
else Ok Always_polls
8787
| Op (Alloc _) -> Ok Always_polls
@@ -121,7 +121,7 @@ end
121121

122122
let potentially_recursive_tailcall :
123123
future_funcnames:String.Set.t ->
124-
optimistic_prologue_poll_instr_id:int ->
124+
optimistic_prologue_poll_instr_id:InstructionId.t ->
125125
Cfg.t ->
126126
Polls_before_prtc_domain.t =
127127
fun ~future_funcnames ~optimistic_prologue_poll_instr_id cfg ->
@@ -192,16 +192,13 @@ let instr_cfg_with_layout :
192192
bool =
193193
fun cfg_with_layout ~safe_map ~back_edges ->
194194
let cfg = Cfg_with_layout.cfg cfg_with_layout in
195-
let next_instruction_id =
195+
let instruction_id =
196196
lazy
197-
(let cfg_infos = Regalloc_utils.collect_cfg_infos cfg_with_layout in
198-
ref (succ cfg_infos.max_instruction_id))
197+
(let cfg = Cfg_with_layout.cfg cfg_with_layout in
198+
InstructionId.make_sequence ~last_used:(Cfg.max_instr_id cfg) ())
199199
in
200200
let next_instruction_id () =
201-
let next_ref = Lazy.force next_instruction_id in
202-
let res = !next_ref in
203-
incr next_ref;
204-
res
201+
InstructionId.get_next (Lazy.force instruction_id)
205202
in
206203
Cfg_loop_infos.EdgeSet.fold
207204
(fun { Cfg_loop_infos.Edge.src; dst } added_poll ->
@@ -368,7 +365,7 @@ let instrument_fundecl :
368365
let requires_prologue_poll :
369366
future_funcnames:Misc.Stdlib.String.Set.t ->
370367
fun_name:string ->
371-
optimistic_prologue_poll_instr_id:int ->
368+
optimistic_prologue_poll_instr_id:InstructionId.t ->
372369
Cfg.t ->
373370
bool =
374371
fun ~future_funcnames ~fun_name ~optimistic_prologue_poll_instr_id cfg ->

0 commit comments

Comments
 (0)