@@ -121,25 +121,33 @@ function computed_highest_diff_variables(structure, ag::Union{AliasGraph, Nothin
121
121
if ag != = nothing && haskey (ag, var)
122
122
(_, stem) = ag[var]
123
123
stem == 0 && continue
124
- # Ascend the stem
125
- while isempty (𝑑neighbors (graph, var))
126
- var′ = invview (var_to_diff)[var]
127
- var′ === nothing && break
128
- stem′ = invview (var_to_diff)[stem]
129
- avar′ = haskey (ag, var′) ? ag[var′][2 ] : nothing
130
- if avar′ == stem || var′ == stem
131
- # If we have a self-loop in the stem, we could have the
132
- # var′ also alias to the original stem. In that case, the
133
- # derivative of the stem is highest differentiated, because of the loop
124
+ # If we have a self-loop in the stem, we could have the
125
+ # var′ also alias to the original stem. In that case, the
126
+ # derivative of the stem is highest differentiated, because of the loop
127
+ loop_found = false
128
+ var′ = invview (var_to_diff)[var]
129
+ while var′ != = nothing
130
+ if var′ == stem || (haskey (ag, var′) && ag[var′][2 ] == stem)
134
131
dstem = var_to_diff[stem]
135
132
@assert dstem != = nothing
136
133
varwhitelist[dstem] = true
134
+ loop_found = true
137
135
break
138
136
end
137
+ var′ = invview (var_to_diff)[var′]
138
+ end
139
+ loop_found && continue
140
+ # Ascend the stem
141
+ while isempty (𝑑neighbors (graph, var))
142
+ var′ = invview (var_to_diff)[var]
143
+ var′ === nothing && break
144
+ loop_found = false
145
+ cvar = var′
139
146
# Invariant from alias elimination: Stem is chosen to have
140
147
# the highest differentiation order.
148
+ stem′ = invview (var_to_diff)[stem]
141
149
@assert stem′ != = nothing
142
- if avar′ != stem′
150
+ if ! haskey (ag, var′) || (ag[var′][ 2 ] != stem′)
143
151
varwhitelist[stem] = true
144
152
break
145
153
end
0 commit comments