@@ -9,42 +9,44 @@ const NamedVar = Union{Symbol,GlobalRef}
9
9
struct Links
10
10
ssas:: Vector{Int}
11
11
slots:: Vector{Int}
12
- names:: Vector{NamedVar }
12
+ names:: Vector{GlobalRef }
13
13
end
14
- Links () = Links (Int[], Int[], NamedVar [])
14
+ Links () = Links (Int[], Int[], GlobalRef [])
15
15
16
16
function Base. show (io:: IO , l:: Links )
17
17
print (io, " ssas: " , showempty (l. ssas),
18
18
" , slots: " , showempty (l. slots),
19
19
" , names: " )
20
- print (IOContext (io, :typeinfo => Vector{NamedVar }), showempty (l. names))
20
+ print (IOContext (io, :typeinfo => Vector{GlobalRef }), showempty (l. names))
21
21
print (io, ' ;' )
22
22
end
23
23
24
24
struct CodeLinks
25
+ thismod:: Module
25
26
ssapreds:: Vector{Links}
26
27
ssasuccs:: Vector{Links}
27
28
slotpreds:: Vector{Links}
28
29
slotsuccs:: Vector{Links}
29
30
slotassigns:: Vector{Vector{Int}}
30
- namepreds:: Dict{NamedVar ,Links}
31
- namesuccs:: Dict{NamedVar ,Links}
32
- nameassigns:: Dict{NamedVar ,Vector{Int}}
31
+ namepreds:: Dict{GlobalRef ,Links}
32
+ namesuccs:: Dict{GlobalRef ,Links}
33
+ nameassigns:: Dict{GlobalRef ,Vector{Int}}
33
34
end
34
- function CodeLinks (nlines:: Int , nslots:: Int )
35
+ function CodeLinks (thismod :: Module , nlines:: Int , nslots:: Int )
35
36
makelinks (n) = [Links () for _ = 1 : n]
36
37
37
- return CodeLinks (makelinks (nlines),
38
+ return CodeLinks (thismod,
39
+ makelinks (nlines),
38
40
makelinks (nlines),
39
41
makelinks (nslots),
40
42
makelinks (nslots),
41
43
[Int[] for _ = 1 : nslots],
42
- Dict {NamedVar ,Links} (),
43
- Dict {NamedVar ,Links} (),
44
- Dict {NamedVar ,Vector{Int}} ())
44
+ Dict {GlobalRef ,Links} (),
45
+ Dict {GlobalRef ,Links} (),
46
+ Dict {GlobalRef ,Vector{Int}} ())
45
47
end
46
- function CodeLinks (src:: CodeInfo )
47
- cl = CodeLinks (length (src. code), length (src. slotnames))
48
+ function CodeLinks (thismod :: Module , src:: CodeInfo )
49
+ cl = CodeLinks (thismod, length (src. code), length (src. slotnames))
48
50
direct_links! (cl, src)
49
51
end
50
52
@@ -203,20 +205,23 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
203
205
end
204
206
end
205
207
206
- P = Pair{Union{SSAValue,SlotNumber,NamedVar },Links}
208
+ P = Pair{Union{SSAValue,SlotNumber,GlobalRef },Links}
207
209
208
210
for (i, stmt) in enumerate (src. code)
209
211
if isexpr (stmt, :thunk ) && isa (stmt. args[1 ], CodeInfo)
210
- icl = CodeLinks (stmt. args[1 ])
212
+ icl = CodeLinks (cl . thismod, stmt. args[1 ])
211
213
add_inner! (cl, icl, i)
212
214
continue
213
215
elseif isa (stmt, Expr) && stmt. head ∈ trackedheads
214
216
if stmt. head === :method && length (stmt. args) === 3 && isa (stmt. args[3 ], CodeInfo)
215
- icl = CodeLinks (stmt. args[3 ])
217
+ icl = CodeLinks (cl . thismod, stmt. args[3 ])
216
218
add_inner! (cl, icl, i)
217
219
end
218
220
name = stmt. args[1 ]
219
- if isa (name, Symbol)
221
+ if isa (name, NamedVar)
222
+ if isa (name, Symbol)
223
+ name = GlobalRef (cl. thismod, name)
224
+ end
220
225
assign = get (cl. nameassigns, name, nothing )
221
226
if assign === nothing
222
227
cl. nameassigns[name] = assign = Int[]
@@ -228,6 +233,10 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
228
233
end
229
234
target = P (name, targetstore)
230
235
add_links! (target, stmt, cl)
236
+ elseif name in (nothing , false )
237
+ else
238
+ @show stmt
239
+ error (" name " , typeof (name), " not recognized" )
231
240
end
232
241
rhs = stmt
233
242
target = P (SSAValue (i), cl. ssapreds[i])
@@ -263,9 +272,9 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
263
272
return cl
264
273
end
265
274
266
- function add_links! (target:: Pair{Union{SSAValue,SlotNumber,NamedVar },Links} , @nospecialize (stmt), cl:: CodeLinks )
275
+ function add_links! (target:: Pair{Union{SSAValue,SlotNumber,GlobalRef },Links} , @nospecialize (stmt), cl:: CodeLinks )
267
276
_targetid, targetstore = target
268
- targetid = _targetid:: Union{SSAValue,SlotNumber,NamedVar }
277
+ targetid = _targetid:: Union{SSAValue,SlotNumber,GlobalRef }
269
278
# Adds bidirectional edges
270
279
if @isssa (stmt)
271
280
stmt = stmt:: AnySSAValue
@@ -275,7 +284,10 @@ function add_links!(target::Pair{Union{SSAValue,SlotNumber,NamedVar},Links}, @no
275
284
stmt = stmt:: AnySlotNumber
276
285
push! (targetstore, SlotNumber (stmt. id))
277
286
push! (cl. slotsuccs[stmt. id], targetid)
278
- elseif isa (stmt, Symbol) || isa (stmt, GlobalRef) # NamedVar
287
+ elseif isa (stmt, NamedVar) # NamedVar
288
+ if isa (stmt, Symbol)
289
+ stmt = GlobalRef (cl. thismod, stmt)
290
+ end
279
291
push! (targetstore, stmt)
280
292
namestore = get (cl. namesuccs, stmt, nothing )
281
293
if namestore === nothing
355
367
struct CodeEdges
356
368
preds:: Vector{Vector{Int}}
357
369
succs:: Vector{Vector{Int}}
358
- byname:: Dict{NamedVar ,Variable}
370
+ byname:: Dict{GlobalRef ,Variable}
359
371
end
360
372
CodeEdges (n:: Integer ) = CodeEdges ([Int[] for i = 1 : n], [Int[] for i = 1 : n], Dict {Union{GlobalRef,Symbol},Variable} ())
361
373
@@ -385,8 +397,8 @@ Analyze `src` and determine the chain of dependencies.
385
397
- `edges.byname[v]` returns information about the predecessors, successors, and assignment statements
386
398
for an object `v::$NamedVar `.
387
399
"""
388
- function CodeEdges (src:: CodeInfo )
389
- cl = CodeLinks (src)
400
+ function CodeEdges (mod :: Module , src:: CodeInfo )
401
+ cl = CodeLinks (mod, src)
390
402
CodeEdges (src, cl)
391
403
end
392
404
@@ -546,7 +558,7 @@ function terminal_preds(i::Int, edges::CodeEdges)
546
558
end
547
559
548
560
"""
549
- isrequired = lines_required(obj::$NamedVar , src::CodeInfo, edges::CodeEdges)
561
+ isrequired = lines_required(obj::GlobalRef , src::CodeInfo, edges::CodeEdges)
550
562
isrequired = lines_required(idx::Int, src::CodeInfo, edges::CodeEdges)
551
563
552
564
Determine which lines might need to be executed to evaluate `obj` or the statement indexed by `idx`.
@@ -556,9 +568,9 @@ will end up skipping a subset of such statements, perhaps while repeating others
556
568
557
569
See also [`lines_required!`](@ref) and [`selective_eval!`](@ref).
558
570
"""
559
- function lines_required (obj:: NamedVar , src:: CodeInfo , edges:: CodeEdges ; kwargs... )
571
+ function lines_required (obj:: GlobalRef , src:: CodeInfo , edges:: CodeEdges ; kwargs... )
560
572
isrequired = falses (length (edges. preds))
561
- objs = Set {NamedVar } ([obj])
573
+ objs = Set {GlobalRef } ([obj])
562
574
return lines_required! (isrequired, objs, src, edges; kwargs... )
563
575
end
564
576
@@ -583,7 +595,7 @@ For example, use `norequire = LoweredCodeUtils.exclude_named_typedefs(src, edges
583
595
extracting method signatures and not evaluating new definitions.
584
596
"""
585
597
function lines_required! (isrequired:: AbstractVector{Bool} , src:: CodeInfo , edges:: CodeEdges ; kwargs... )
586
- objs = Set {NamedVar } ()
598
+ objs = Set {GlobalRef } ()
587
599
return lines_required! (isrequired, objs, src, edges; kwargs... )
588
600
end
589
601
@@ -643,7 +655,7 @@ function lines_required!(isrequired::AbstractVector{Bool}, objs, src::CodeInfo,
643
655
end
644
656
645
657
function add_requests! (isrequired, objs, edges:: CodeEdges , norequire)
646
- objsnew = Set {NamedVar } ()
658
+ objsnew = Set {GlobalRef } ()
647
659
for obj in objs
648
660
add_obj! (isrequired, objsnew, obj, edges, norequire)
649
661
end
0 commit comments