Skip to content

Commit c913bff

Browse files
authored
Add make tracer via immutable constructor helper (#950)
1 parent 75ffa36 commit c913bff

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/Tracing.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,81 @@ function make_tracer(
932932
end
933933
append_path(@nospecialize(path), i) = (path..., i)
934934

935+
function make_tracer_via_immutable_constructor(
936+
seen,
937+
@nospecialize(prev),
938+
@nospecialize(path),
939+
mode;
940+
@nospecialize(track_numbers::Type = Union{}),
941+
@nospecialize(sharding = Sharding.NoSharding()),
942+
@nospecialize(runtime = nothing),
943+
kwargs...,
944+
)
945+
RT = Core.Typeof(prev)
946+
if haskey(seen, prev)
947+
if mode == TracedToTypes
948+
id = seen[prev]
949+
push!(path, id)
950+
return nothing
951+
elseif mode != NoStopTracedTrack && haskey(seen, prev)
952+
return seen[prev]
953+
end
954+
elseif mode == TracedToTypes
955+
push!(path, RT)
956+
seen[prev] = VisitedObject(length(seen) + 1)
957+
end
958+
TT = traced_type(RT, Val(mode), track_numbers, sharding, runtime)
959+
@assert !Base.isabstracttype(RT)
960+
@assert Base.isconcretetype(RT)
961+
nf = fieldcount(RT)
962+
963+
@assert !ismutabletype(TT)
964+
965+
if nf == 0
966+
if mode == TracedToTypes
967+
push!(path, prev)
968+
return nothing
969+
end
970+
return prev
971+
end
972+
973+
flds = Vector{Any}(undef, nf)
974+
changed = false
975+
for i in 1:nf
976+
if isdefined(prev, i)
977+
newpath = mode == TracedToTypes ? path : append_path(path, i)
978+
xi = Base.getfield(prev, i)
979+
xi2 = make_tracer(
980+
seen,
981+
xi,
982+
newpath,
983+
mode;
984+
track_numbers,
985+
sharding=Base.getproperty(sharding, i),
986+
runtime,
987+
kwargs...,
988+
)
989+
if xi !== xi2
990+
changed = true
991+
end
992+
flds[i] = xi2
993+
else
994+
nf = i - 1 # rest of tail must be undefined values
995+
break
996+
end
997+
end
998+
if mode == TracedToTypes
999+
return nothing
1000+
end
1001+
if !changed
1002+
seen[prev] = prev
1003+
return prev
1004+
end
1005+
y = TT(flds...)
1006+
seen[prev] = y
1007+
return y
1008+
end
1009+
9351010
function make_tracer(
9361011
seen,
9371012
@nospecialize(prev),

0 commit comments

Comments
 (0)