Skip to content

Commit 92396aa

Browse files
vtjnashKristofferC
authored andcommitted
[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. (cherry picked from commit 1045bd8)
1 parent d04631d commit 92396aa

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
@@ -188,14 +188,19 @@ macro __SOURCE_FILE__()
188188
return QuoteNode(__source__.file::Symbol)
189189
end
190190

191-
module IRShow end
191+
module IRShow end # relies on string and IO operations defined in Base
192+
baremodule TrimVerifier end # relies on IRShow, so define this afterwards
193+
192194
function load_irshow!()
193195
if isdefined(Base, :end_base_include)
194196
# This code path is exclusively for Revise, which may want to re-run this
195197
# after bootstrap.
196-
include(IRShow, Base.joinpath(Base.dirname(Base.String(@__SOURCE_FILE__)), "ssair/show.jl"))
198+
Compilerdir = Base.dirname(Base.String(@__SOURCE_FILE__))
199+
include(IRShow, Base.joinpath(Compilerdir, "ssair/show.jl"))
200+
include(TrimVerifier, Base.joinpath(Compilerdir, "verifytrim.jl"))
197201
else
198202
include(IRShow, "ssair/show.jl")
203+
include(TrimVerifier, "verifytrim.jl")
199204
end
200205
end
201206
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
@@ -1273,7 +1273,12 @@ function typeinf_ext_toplevel(mi::MethodInstance, world::UInt, source_mode::UInt
12731273
end
12741274

12751275
# This is a bridge for the C code calling `jl_typeinf_func()` on set of Method matches
1276-
function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::Bool)
1276+
# The trim_mode can be any of:
1277+
const TRIM_NO = 0
1278+
const TRIM_SAFE = 1
1279+
const TRIM_UNSAFE = 2
1280+
const TRIM_UNSAFE_WARN = 3
1281+
function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_mode::Int)
12771282
inspected = IdSet{CodeInstance}()
12781283
tocompile = Vector{CodeInstance}()
12791284
codeinfos = []
@@ -1321,7 +1326,7 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::
13211326
src = codeinfo_for_const(interp, mi, callee.rettype_const)
13221327
elseif haskey(interp.codegen, callee)
13231328
src = interp.codegen[callee]
1324-
elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && !trim
1329+
elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && trim_mode == TRIM_NO
13251330
src = retrieve_code_info(mi, get_inference_world(interp))
13261331
else
13271332
# TODO: typeinf_code could return something with different edges/ages/owner/abi (needing an update to callee), which we don't handle here
@@ -1336,15 +1341,19 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::
13361341
end
13371342
push!(codeinfos, callee)
13381343
push!(codeinfos, src)
1339-
elseif trim
1340-
println("warning: failed to get code for ", mi)
13411344
end
13421345
end
13431346
latest = false
13441347
end
1348+
if trim_mode != TRIM_NO && trim_mode != TRIM_UNSAFE
1349+
verify_typeinf_trim(codeinfos, trim_mode == TRIM_UNSAFE_WARN)
1350+
end
13451351
return codeinfos
13461352
end
13471353

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

0 commit comments

Comments
 (0)