Skip to content

Commit f79daef

Browse files
authored
Fix topological iterator in control_flow_graph.ml (#2371)
1 parent eac07ff commit f79daef

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

middle_end/flambda2/simplify/flow/control_flow_graph.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,17 @@ let fixpoint t ~init ~f =
122122
| G.Has_loop conts ->
123123
let q = Queue.create () in
124124
List.iter (fun k -> Queue.add k q) conts;
125-
let q_s = ref (Continuation.Set.of_list conts) in
125+
(* We enforce the invariant that [q] contains no duplicates, and that
126+
[!q_s] precisely contains the continuations in [conts] that are not
127+
in [q]. Besides ensuring we don't add the same element twice to
128+
[q], this enforces as well that only continuations in the current
129+
strongly-connected component, that is, the continuations in
130+
[conts], can ever be in [q]. *)
131+
let q_s = ref Continuation.Set.empty in
126132
let cur = ref res in
127133
while not (Queue.is_empty q) do
128134
let callee = Queue.pop q in
129-
q_s := Continuation.Set.remove callee !q_s;
135+
q_s := Continuation.Set.add callee !q_s;
130136
let callee_set = Continuation.Map.find callee !cur in
131137
let callers =
132138
match Continuation.Map.find callee t.callers with
@@ -144,10 +150,10 @@ let fixpoint t ~init ~f =
144150
if not (Variable.Set.equal caller_set caller_new_set)
145151
then (
146152
cur := Continuation.Map.add caller caller_new_set !cur;
147-
if not (Continuation.Set.mem caller !q_s)
153+
if Continuation.Set.mem caller !q_s
148154
then (
149155
Queue.add caller q;
150-
q_s := Continuation.Set.add caller !q_s)))
156+
q_s := Continuation.Set.remove caller !q_s)))
151157
callers
152158
done;
153159
!cur)

0 commit comments

Comments
 (0)