Skip to content

Commit 7676c07

Browse files
authored
Fix CSE equations on params (oxcaml#2628)
1 parent 156deae commit 7676c07

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

middle_end/flambda2/simplify/common_subexpression_elimination.ml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,33 @@ end
105105

106106
let cse_with_eligible_lhs ~typing_env_at_fork ~cse_at_each_use ~params prev_cse
107107
(extra_bindings : EPA.t) extra_equations =
108-
let params = List.map params ~f:Bound_parameter.name |> Name.Set.of_list in
108+
let params_set =
109+
List.map params ~f:Bound_parameter.name |> Name.Set.of_list
110+
in
111+
let params = List.map params ~f:Bound_parameter.simple in
109112
let is_param simple =
110113
Simple.pattern_match simple
111-
~name:(fun name ~coercion:_ -> Name.Set.mem name params)
114+
~name:(fun name ~coercion:_ -> Name.Set.mem name params_set)
112115
~const:(fun _ -> false)
113116
in
114117
List.fold_left cse_at_each_use ~init:EP.Map.empty
115118
~f:(fun eligible (env_at_use, id, cse) ->
116119
let find_new_name =
120+
let find_param simple params =
121+
List.find_opt
122+
~f:(fun param ->
123+
match
124+
TE.get_canonical_simple_exn env_at_use param
125+
~min_name_mode:NM.normal
126+
~name_mode_of_existing_simple:NM.normal
127+
with
128+
| exception Not_found -> false
129+
| arg -> Simple.equal arg simple)
130+
params
131+
in
117132
match (extra_bindings : EPA.t) with
118-
| Empty -> fun _arg -> None
119-
| Non_empty { extra_args; extra_params } ->
133+
| Empty -> fun arg -> find_param arg params
134+
| Non_empty { extra_args; extra_params } -> (
120135
let extra_args = RI.Map.find id extra_args in
121136
let rec find_name simple params args =
122137
match args, params with
@@ -137,7 +152,10 @@ let cse_with_eligible_lhs ~typing_env_at_fork ~cse_at_each_use ~params prev_cse
137152
find_name simple params args)
138153
in
139154
fun arg ->
140-
find_name arg (Bound_parameters.to_list extra_params) extra_args
155+
match find_param arg params with
156+
| None ->
157+
find_name arg (Bound_parameters.to_list extra_params) extra_args
158+
| Some _ as r -> r)
141159
in
142160
EP.Map.fold
143161
(fun prim bound_to eligible ->

0 commit comments

Comments
 (0)