Skip to content

Commit 464b43f

Browse files
committed
Add new table: input types seen in the tests
stats-intypes: fix module provenance of a type Before we recorded module of the method, not module of the type.
1 parent fa0e7eb commit 464b43f

File tree

7 files changed

+91
-11
lines changed

7 files changed

+91
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
\.~*
2+
scratch

Stability/src/CSVize.jl

+28-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,25 @@ struct ModuleStatsPerInstanceRecord
2929
line :: Int
3030
end
3131

32-
# Convert stats to vectors of records
32+
struct ModuleStatsInTypeRecord
33+
modl :: String
34+
tyname :: String
35+
occurs :: Int
36+
end
37+
38+
#
39+
# Convert stats dicitonaries to vectors of records
40+
#
3341
modstats_table(ms :: ModuleStats, errio = stdout :: IO) ::
34-
Tuple{Vector{ModuleStatsPerMethodRecord}, Vector{ModuleStatsPerInstanceRecord}} = begin
42+
Tuple{
43+
Vector{ModuleStatsPerMethodRecord},
44+
Vector{ModuleStatsPerInstanceRecord},
45+
Vector{ModuleStatsInTypeRecord}} = begin
46+
3547
resmeth = []
3648
resmi = []
49+
resty = []
50+
3751
m2rettype = Dict{Method, Set{String}}()
3852
for (mi,cfgst) in ms.mistats
3953
try
@@ -84,5 +98,16 @@ modstats_table(ms :: ModuleStats, errio = stdout :: IO) ::
8498
throw(err)
8599
end
86100
end
87-
(resmeth,resmi)
101+
for (ty,tystat) in ms.tystats
102+
try
103+
modl = "$(tystat.modl)"
104+
tyname = "$(ty)"
105+
push!(resty,
106+
ModuleStatsInTypeRecord(modl, tyname, tystat.occurs))
107+
catch err
108+
println(errio, "ERROR: modstats_table: ty-loop: $err");
109+
throw(err)
110+
end
111+
end
112+
(resmeth,resmi,resty)
88113
end

Stability/src/Stability.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using Pkg
99
using CSV
1010

1111
export is_concrete_type, is_grounded_call, all_mis_of_module,
12-
MIStats, MethodStats, ModuleStats,
12+
MIStats, MethodStats, InTypeStats, ModuleStats,
1313
module_stats, modstats_summary, modstats_table,
1414
package_stats, cfg_stats,
1515
show_comma_sep
@@ -52,6 +52,7 @@ include("Utils.jl")
5252
# * stability-errors.out
5353
# * stability-stats-per-method.csv
5454
# * stability-stats-per-instance.csv
55+
# * stability-stats-intypes.csv
5556
# * $pakg-version.txt (version stamp for future reference)
5657
#
5758
# Setting a package version:
@@ -114,6 +115,7 @@ package_stats(pakg :: String, ver = nothing) = begin
114115
#
115116
txtToCsv(work_dir, "stability-stats-per-method")
116117
txtToCsv(work_dir, "stability-stats-per-instance")
118+
txtToCsv(work_dir, "stability-stats-intypes")
117119
@myinfo pkgtag "Results successfully converted to CSV. The package is DONE!"
118120
end
119121

Stability/src/Stats.jl

+40-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ is_return(::Any) = false
4949

5050
# -------------------------------------------
5151
#
52-
#
5352
# Statistics gathered per method.
5453
#
5554
# -------------------------------------------
@@ -87,6 +86,23 @@ import Base.(+)
8786
show_comma_sep(xs::Vector) = join(xs, ",")
8887

8988

89+
# --------------------------------------------------------
90+
#
91+
# Statistics for all types occured during instantiations.
92+
#
93+
# --------------------------------------------------------
94+
95+
# Note on "mutable": stats are only mutable during their calculation.
96+
@kwdef mutable struct InTypeStats
97+
modl :: String
98+
occurs :: Int
99+
end
100+
101+
InTypeStats(modl :: Module) = InTypeStats("$modl", 0)
102+
InTypeStats(modl :: String) = InTypeStats(modl, 0)
103+
104+
@deriveEq(InTypeStats)
105+
90106
# -------------------------------------------
91107
#
92108
# Statistics gathered per module.
@@ -97,9 +113,15 @@ show_comma_sep(xs::Vector) = join(xs, ",")
97113
modl :: Module
98114
mestats :: Dict{Method, MethodStats}
99115
mistats :: Dict{MethodInstance, MIStats}
116+
tystats :: Dict{Any, InTypeStats}
100117
end
101118

102-
ModuleStats(modl :: Module) = ModuleStats(modl, Dict{Method, MethodStats}(), Dict{Method, MIStats}())
119+
ModuleStats(modl :: Module) = ModuleStats(
120+
modl,
121+
Dict{Method, MethodStats}(),
122+
Dict{Method, MIStats}(),
123+
Dict{Any, InTypeStats}()
124+
)
103125

104126
@deriveEq(ModuleStats)
105127

@@ -133,17 +155,20 @@ module_stats(modl :: Module, errio :: IO = stderr) = begin
133155
res = ModuleStats(modl)
134156
mis = all_mis_of_module(modl)
135157
for mi in mis
158+
159+
# Special cases: @generated, blocklisted
136160
if isdefined(mi.def, :generator) # can't handle @generated functions, Issue #11
137161
@debug "GENERATED $(mi)"
138162
continue
139163
end
140-
141164
is_blocklisted(modl, mi.def.module) && (@debug "alien: $mi.def defined in $mi.def.module"; continue)
142165

166+
# Lookup method stats object for this `mi`
143167
fs = get!(res.mestats, mi.def,
144168
fstats_default(mi.def.nospecialize,
145169
occursin("Vararg","$(mi.def.sig)")))
146170
try
171+
# Get code of the `mi` for later analysis
147172
call = reconstruct_func_call(mi)
148173
if call === nothing # this mi is a constructor call - skip
149174
delete!(res.mestats, mi.def)
@@ -152,7 +177,7 @@ module_stats(modl :: Module, errio :: IO = stderr) = begin
152177

153178
fs.occurs += 1
154179

155-
# handle stability/groundedness
180+
# Check stability/groundedness of the code
156181
mi_st = false
157182
mi_gd = false
158183
(code,rettype) = run_type_inference(call...);
@@ -165,8 +190,18 @@ module_stats(modl :: Module, errio :: IO = stderr) = begin
165190
end
166191
end
167192

168-
# handle instance CFG stats
193+
# Handle instance CFG stats
169194
res.mistats[mi] = MIStats(mi_st, mi_gd, cfg_stats(code)..., rettype, call[2] #= input types =#)
195+
196+
# Collect intypes
197+
intypes = Base.unwrap_unionall(mi.specTypes).types[2:end]
198+
for ty in intypes
199+
tymodl = moduleChainOfType(ty)
200+
tystat = get!(res.tystats,
201+
ty,
202+
InTypeStats(tymodl))
203+
tystat.occurs += 1
204+
end
170205
catch err
171206
fs.fail += 1
172207
print(errio, "ERROR: ");

Stability/src/Utils.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,19 @@ end
4646

4747
txtToCsv(work_dir :: String, basename :: String) = begin
4848
resf = joinpath(work_dir, "$basename.txt")
49-
isfile(resf) || (@error "Stability analysis failed to produce output $resf"; return)
49+
isfile(resf) || (throw(ErrorException("Stability analysis failed to produce output $resf")))
5050
st =
5151
eval(Meta.parse(
5252
open(f-> read(f,String), resf,"r")))
5353
CSV.write(joinpath(work_dir, "$basename.csv"), st)
5454
end
55+
56+
moduleChainOfType(@nospecialize(ty)) :: String = begin
57+
mod=parentmodule(ty)
58+
res="$mod"
59+
while parentmodule(ty) != mod
60+
mod = parentmodule(mod)
61+
res = "$mod." * res
62+
end
63+
res
64+
end

Stability/src/pkg-test-override.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function Pkg.Operations.gen_test_code(testfile::String;
4949
open(out -> println(out, pakg * "," * show_comma_sep(summary)), joinpath(wdir, "stability-summary.out"), "w")
5050
5151
@info "[Stability] [Package: " * pakg * "] Constructing a table from the stats..."
52-
(methst, mist) = modstats_table(ms)
52+
(methst, mist, tyt) = modstats_table(ms)
53+
5354
@info "[Stability] [Package: " * pakg * "] Table size (per method): " * string(length(methst))
5455
outf = joinpath(wdir, "stability-stats-per-method.txt")
5556
@info "[Stability] [Package: " * pakg * "] About to store per method results to: " * outf
@@ -59,6 +60,11 @@ function Pkg.Operations.gen_test_code(testfile::String;
5960
outf = joinpath(wdir, "stability-stats-per-instance.txt")
6061
@info "[Stability] [Package: " * pakg * "] About to store per instance results to: " * outf
6162
open(f-> println(f,mist), outf,"w")
63+
64+
@info "[Stability] [Package: " * pakg * "] Table size (types): " * string(length(tyt))
65+
outf = joinpath(wdir, "stability-stats-intypes.txt")
66+
@info "[Stability] [Package: " * pakg * "] About to store intypes to: " * outf
67+
open(f-> println(f,tyt), outf,"w")
6268
end
6369
@info "[Stability] [Package: " * pakg * "] Finish testing + analysis"
6470
#### End

Stability/test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ finst=fmeth.specializations[1]
2222
ModuleStats(M,
2323
Dict(fmeth=>MethodStats(; occurs=1, stable=1, grounded=1, nospec=0, vararg=0, fail=0)),
2424
Dict(finst=>MIStats(; st=1, gd=1, gt=0, rt=0, rettype=Int64, intypes=Core.svec(Int64))),
25+
Dict(Int64=>InTypeStats("Core", 1)),
2526
)
2627
end

0 commit comments

Comments
 (0)