Skip to content

Commit 1c732b7

Browse files
authored
Index lifted cont params by variables instead of custom indexes (#3303)
Index lifted cont params y variables instead of custom indexes This should have no observable effect, but will simplify the continuation specialization work, particularly when paired with the replay histories feature, so that lifted cont params can be correctly tracked between a first pass and subsequent passes during continuation specialization.
1 parent 159c81b commit 1c732b7

File tree

2 files changed

+18
-45
lines changed

2 files changed

+18
-45
lines changed

middle_end/flambda2/simplify/lifted_cont_params.ml

+17-42
Original file line numberDiff line numberDiff line change
@@ -13,82 +13,57 @@
1313
(* *)
1414
(**************************************************************************)
1515

16-
module Id : sig
17-
type t
18-
19-
val fresh : unit -> t
20-
21-
val print : Format.formatter -> t -> unit
22-
23-
module Map : Container_types.Map with type key = t
24-
end = struct
25-
type t = int
26-
27-
let print = Numbers.Int.print
28-
29-
let fresh =
30-
let r = ref 0 in
31-
fun () ->
32-
incr r;
33-
!r
34-
35-
module Tree = Patricia_tree.Make (struct
36-
let print = print
37-
end)
38-
39-
module Map = Tree.Map
40-
end
16+
module BP = Bound_parameter
4117

4218
type t =
4319
{ len : int;
44-
new_params_indexed : Bound_parameter.t Id.Map.t
20+
new_params_indexed : BP.t BP.Map.t
4521
}
4622

4723
let print ppf { len = _; new_params_indexed } =
4824
Format.fprintf ppf "@[<hov 1>(@[<hov 1>(new_params_indexed@ %a)@])@]"
49-
(Id.Map.print Bound_parameter.print)
50-
new_params_indexed
25+
(BP.Map.print BP.print) new_params_indexed
5126

52-
let empty = { len = 0; new_params_indexed = Id.Map.empty }
27+
let empty = { len = 0; new_params_indexed = BP.Map.empty }
5328

5429
let is_empty { len; new_params_indexed = _ } = len = 0
5530

5631
let length { len; new_params_indexed = _ } = len
5732

5833
let new_param t bound_param =
59-
(* create a fresh var/bound_param to index the new parameter *)
60-
let id = Id.fresh () in
61-
let new_params_indexed = Id.Map.add id bound_param t.new_params_indexed in
34+
let new_params_indexed =
35+
BP.Map.add bound_param bound_param t.new_params_indexed
36+
in
6237
{ len = t.len + 1; new_params_indexed }
6338

6439
let rename t =
65-
let bindings = Id.Map.bindings t.new_params_indexed in
40+
let bindings = BP.Map.bindings t.new_params_indexed in
6641
let keys, bound_param_list = List.split bindings in
6742
let bound_params = Bound_parameters.create bound_param_list in
6843
let new_bound_params = Bound_parameters.rename bound_params in
6944
let renaming =
7045
Bound_parameters.renaming bound_params ~guaranteed_fresh:new_bound_params
7146
in
7247
let new_params_indexed =
73-
Id.Map.of_list
48+
BP.Map.of_list
7449
(List.combine keys (Bound_parameters.to_list new_bound_params))
7550
in
7651
{ t with new_params_indexed }, renaming
7752

7853
let fold_aux ~init ~f { len = _; new_params_indexed } =
79-
Id.Map.fold f new_params_indexed init
54+
BP.Map.fold f new_params_indexed init
8055

8156
let fold ~init ~f t = fold_aux ~init ~f:(fun _ param acc -> f param acc) t
8257

83-
let rec find_arg id = function
58+
let rec find_arg bp = function
8459
| [] ->
8560
Misc.fatal_errorf
8661
"Missing lifted param id: %a not found in lifted_cont_params stack"
87-
Id.print id
62+
BP.print bp
8863
| { len = _; new_params_indexed } :: r -> (
89-
match Id.Map.find_opt id new_params_indexed with
90-
| Some param -> Bound_parameter.simple param
91-
| None -> find_arg id r)
64+
match BP.Map.find_opt bp new_params_indexed with
65+
| Some param -> BP.simple param
66+
| None -> find_arg bp r)
9267

9368
(* NOTE about the order of the returned args/params for the {args} and
9469
{bound_parameters} functions: The exact order does not matter as long as both
@@ -99,8 +74,8 @@ let rec find_arg id = function
9974
order of the bindings in the Map, but that's fine since it is the case for
10075
both functions. *)
10176
let args ~callee_lifted_params ~caller_stack_lifted_params =
102-
fold_aux callee_lifted_params ~init:[] ~f:(fun id _callee_param acc ->
103-
find_arg id caller_stack_lifted_params :: acc)
77+
fold_aux callee_lifted_params ~init:[] ~f:(fun bp_key _callee_param acc ->
78+
find_arg bp_key caller_stack_lifted_params :: acc)
10479

10580
let bound_parameters t =
10681
Bound_parameters.create

middle_end/flambda2/simplify/lifted_cont_params.mli

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
mapping for each continuation. Then, for a given call site, for each new
3333
parameter, we can lookup the original variable that introduced it, and then
3434
look into the stack of parent continuations to see which one first defines
35-
a new (lifting) param that maps back to the same original variable. To avoid
36-
confusing the original variables and the renamed parameters, we instead use
37-
unique ids (which are in fact integers). *)
35+
a new (lifting) param that maps back to the same original variable. *)
3836

3937
(** This type represents all of the new params for one lifted continuation.
4038
These added parameters are internally indexed by a unique identifier: when an individual

0 commit comments

Comments
 (0)