Skip to content

Commit 1045bd8

Browse files
authored
[Compiler] begin new approach to verify --trim output (#57530)
Reimplement all of the trim verification support in Julia as a compiler analysis pass. Move all this verification code into the Compiler julia code, where we have much better utilities for pretty printing and better observability for analysis.
1 parent 05b4722 commit 1045bd8

File tree

16 files changed

+480
-355
lines changed

16 files changed

+480
-355
lines changed

Compiler/src/Compiler.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,19 @@ macro __SOURCE_FILE__()
189189
return QuoteNode(__source__.file::Symbol)
190190
end
191191

192-
module IRShow end
192+
module IRShow end # relies on string and IO operations defined in Base
193+
baremodule TrimVerifier end # relies on IRShow, so define this afterwards
194+
193195
function load_irshow!()
194196
if isdefined(Base, :end_base_include)
195197
# This code path is exclusively for Revise, which may want to re-run this
196198
# after bootstrap.
197-
include(IRShow, Base.joinpath(Base.dirname(Base.String(@__SOURCE_FILE__)), "ssair/show.jl"))
199+
Compilerdir = Base.dirname(Base.String(@__SOURCE_FILE__))
200+
include(IRShow, Base.joinpath(Compilerdir, "ssair/show.jl"))
201+
include(TrimVerifier, Base.joinpath(Compilerdir, "verifytrim.jl"))
198202
else
199203
include(IRShow, "ssair/show.jl")
204+
include(TrimVerifier, "verifytrim.jl")
200205
end
201206
end
202207
if !isdefined(Base, :end_base_include)

Compiler/src/bootstrap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function bootstrap!()
7171
end
7272
end
7373
end
74-
codeinfos = typeinf_ext_toplevel(methods, [world], false)
74+
codeinfos = typeinf_ext_toplevel(methods, [world], TRIM_NO)
7575
for i = 1:2:length(codeinfos)
7676
ci = codeinfos[i]::CodeInstance
7777
src = codeinfos[i + 1]::CodeInfo

Compiler/src/typeinfer.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,12 @@ function typeinf_ext_toplevel(mi::MethodInstance, world::UInt, source_mode::UInt
12901290
end
12911291

12921292
# This is a bridge for the C code calling `jl_typeinf_func()` on set of Method matches
1293-
function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::Bool)
1293+
# The trim_mode can be any of:
1294+
const TRIM_NO = 0
1295+
const TRIM_SAFE = 1
1296+
const TRIM_UNSAFE = 2
1297+
const TRIM_UNSAFE_WARN = 3
1298+
function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_mode::Int)
12941299
inspected = IdSet{CodeInstance}()
12951300
tocompile = Vector{CodeInstance}()
12961301
codeinfos = []
@@ -1338,7 +1343,7 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::
13381343
src = codeinfo_for_const(interp, mi, callee.rettype_const)
13391344
elseif haskey(interp.codegen, callee)
13401345
src = interp.codegen[callee]
1341-
elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && !trim
1346+
elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && trim_mode == TRIM_NO
13421347
src = retrieve_code_info(mi, get_inference_world(interp))
13431348
else
13441349
# TODO: typeinf_code could return something with different edges/ages/owner/abi (needing an update to callee), which we don't handle here
@@ -1353,15 +1358,19 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::
13531358
end
13541359
push!(codeinfos, callee)
13551360
push!(codeinfos, src)
1356-
elseif trim
1357-
println("warning: failed to get code for ", mi)
13581361
end
13591362
end
13601363
latest = false
13611364
end
1365+
if trim_mode != TRIM_NO && trim_mode != TRIM_UNSAFE
1366+
verify_typeinf_trim(codeinfos, trim_mode == TRIM_UNSAFE_WARN)
1367+
end
13621368
return codeinfos
13631369
end
13641370

1371+
verify_typeinf_trim(io::IO, codeinfos::Vector{Any}, onlywarn::Bool) = (msg = "--trim verifier not defined"; onlywarn ? println(io, msg) : error(msg))
1372+
verify_typeinf_trim(codeinfos::Vector{Any}, onlywarn::Bool) = invokelatest(verify_typeinf_trim, stdout, codeinfos, onlywarn)
1373+
13651374
function return_type(@nospecialize(f), t::DataType) # this method has a special tfunc
13661375
world = tls_world_age()
13671376
args = Any[_return_type, NativeInterpreter(world), Tuple{Core.Typeof(f), t.parameters...}]

0 commit comments

Comments
 (0)