@@ -90,7 +90,8 @@ function alias_elimination(sys)
90
90
end
91
91
Main. _a[] = ag, invag
92
92
processed = falses (nvars)
93
- iag = InducedAliasGraph (ag, invag, var_to_diff, processed)
93
+ # iag = InducedAliasGraph(ag, invag, var_to_diff, processed)
94
+ iag = InducedAliasGraph (ag, invag, var_to_diff)
94
95
relative_level = BitDict (nvars)
95
96
newag = AliasGraph (nvars)
96
97
for (v, dv) in enumerate (var_to_diff)
@@ -101,10 +102,14 @@ function alias_elimination(sys)
101
102
# variabels.
102
103
# Note that `rootlv` is non-positive
103
104
r, rootlv = walk_to_root! (relative_level, iag, v)
105
+ fill! (iag. visited, false )
104
106
let
105
107
sv = fullvars[v]
106
108
root = fullvars[r]
107
- @warn " " sv=> root level= rootlv
109
+ @info " Found root $r " sv=> root level= rootlv
110
+ for vv in relative_level
111
+ @show fullvars[vv[1 ]]
112
+ end
108
113
end
109
114
level_to_var = Int[]
110
115
extreme_var (var_to_diff, r, nothing , Val (false ), callback = Base. Fix1 (push!, level_to_var))
@@ -116,11 +121,16 @@ function alias_elimination(sys)
116
121
# FIXME : only alias variables in the reachable set
117
122
if level + 1 <= length (level_to_var)
118
123
# TODO : make sure the coefficient is 1
119
- newag[v] = 1 => level_to_var[level + 1 ]
124
+ av = level_to_var[level + 1 ]
125
+ if v != av # if the level_to_var isn't from the root branch
126
+ newag[v] = 1 => av
127
+ # @info "create alias" fullvars[v] => fullvars[level_to_var[level + 1]]
128
+ end
120
129
else
121
130
@assert length (level_to_var) == level
122
131
push! (level_to_var, v)
123
132
end
133
+ processed[v] = true
124
134
current_level[] += 1
125
135
end
126
136
end
@@ -139,14 +149,30 @@ function alias_elimination(sys)
139
149
end
140
150
end
141
151
end
152
+ #=
153
+ for (v, (c, a)) in ag
154
+ va = iszero(a) ? a : fullvars[a]
155
+ @warn "old alias" fullvars[v] => (c, va)
156
+ end
157
+ for (v, (c, a)) in newag
158
+ va = iszero(a) ? a : fullvars[a]
159
+ @warn "new alias" fullvars[v] => (c, va)
160
+ end
161
+ =#
162
+ println (" ================" )
142
163
newkeys = keys (newag)
143
164
for (v, (c, a)) in ag
144
165
(v in newkeys || a in newkeys) && continue
145
- newag[v] = c => a
166
+ if iszero (c)
167
+ newag[v] = c
168
+ else
169
+ newag[v] = c => a
170
+ end
146
171
end
147
172
ag = newag
148
173
for (v, (c, a)) in ag
149
- @warn " new alias" fullvars[v] => (c, fullvars[a])
174
+ va = iszero (a) ? a : fullvars[a]
175
+ @warn " new alias" fullvars[v] => (c, va)
150
176
end
151
177
152
178
subs = Dict ()
@@ -179,14 +205,15 @@ function alias_elimination(sys)
179
205
newstates = []
180
206
for j in eachindex (fullvars)
181
207
if j in keys (ag)
208
+ #=
182
209
_, var = ag[j]
183
210
iszero(var) && continue
184
211
# Put back equations for alias eliminated dervars
185
212
if isdervar(state.structure, var)
186
213
has_higher_order = false
187
214
v = var
188
215
while (v = var_to_diff[v]) !== nothing
189
- if ! (v in keys (ag))
216
+ if !(v::Int in keys(ag))
190
217
has_higher_order = true
191
218
break
192
219
end
@@ -197,6 +224,7 @@ function alias_elimination(sys)
197
224
diff_to_var[j] === nothing && push!(newstates, rhs)
198
225
end
199
226
end
227
+ =#
200
228
else
201
229
diff_to_var[j] === nothing && push! (newstates, fullvars[j])
202
230
end
405
433
406
434
Graphs. neighbors (iag:: InducedAliasGraph , v:: Integer ) = IAGNeighbors (iag, v)
407
435
436
+ struct RootedAliasTree
437
+ iag:: InducedAliasGraph
438
+ root:: Int
439
+ end
440
+
441
+ AbstractTrees. childtype (:: Type{<:RootedAliasTree} ) = Union{RootedAliasTree, Int}
442
+ AbstractTrees. children (rat:: RootedAliasTree ) = RootedAliasChildren (rat)
443
+ AbstractTrees. nodevalue (rat:: RootedAliasTree ) = rat. root
444
+ AbstractTrees. shouldprintkeys (rat:: RootedAliasTree ) = false
445
+ has_fast_reverse (:: Type{<:AbstractSimpleTreeIter{<:RootedAliasTree}} ) = false
446
+
447
+ struct RootedAliasChildren
448
+ t:: RootedAliasTree
449
+ end
450
+
451
+ function Base. iterate (c:: RootedAliasChildren , s = nothing )
452
+ rat = c. t
453
+ @unpack iag, root = rat
454
+ @unpack ag, invag, var_to_diff, visited = iag
455
+ (root = var_to_diff[root]) === nothing && return nothing
456
+ root:: Int
457
+ if s === nothing
458
+ stage = 1
459
+ it = iterate (neighbors (invag, root))
460
+ s = (stage, it)
461
+ end
462
+ (stage, it) = s
463
+ if stage == 1 # root
464
+ stage += 1
465
+ return root, (stage, it)
466
+ elseif stage == 2 # ag
467
+ stage += 1
468
+ cv = get (ag, root, nothing )
469
+ if cv != = nothing
470
+ return RootedAliasTree (iag, cv[2 ]), (stage, it)
471
+ end
472
+ end
473
+ # invag (stage 3)
474
+ it === nothing && return nothing
475
+ e, ns = it
476
+ return RootedAliasTree (iag, e), (stage, iterate (invag, ns))
477
+ end
478
+
408
479
count_nonzeros (a:: AbstractArray ) = count (! iszero, a)
409
480
410
481
# N.B.: Ordinarily sparse vectors allow zero stored elements.
0 commit comments