Skip to content

Commit fbfec6c

Browse files
committed
effects: add reflection utility for the new effect analysis
This commit adds new reflection utility named `Base.infer_effects` that works in the same way as `Base.return_types` but returns inferred effects instead. It would be helpful to test that certain method call has an expected effects. For example, we can now remove `Base.@pure` annotation from the definition of `BroadcastStyle(a::A, b::B) where {A<:AbstractArrayStyle{M},B<:AbstractArrayStyle{N}} where {M,N}` and checks it's still eligible for concrete evaluation like this (see <#44776 (comment)> for the context): ```julia julia> import Base.Broadcast: AbstractArrayStyle, DefaultArrayStyle, Unknown julia> function BroadcastStyle(a::A, b::B) where {A<:AbstractArrayStyle{M},B<:AbstractArrayStyle{N}} where {M,N} if Base.typename(A) === Base.typename(B) return A(Val(max(M, N))) end return Unknown() end BroadcastStyle (generic function with 1 method) julia> # test that the above definition is eligible for concrete evaluation @test Base.infer_effects(BroadcastStyle, (DefaultArrayStyle{1},DefaultArrayStyle{2},)) |> Core.Compiler.is_total_or_error Test Passed ```
1 parent f86b4ef commit fbfec6c

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

base/compiler/typeinfer.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,15 +904,24 @@ end
904904

905905
# compute an inferred AST and return type
906906
function typeinf_code(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool)
907+
frame = typeinf_frame(interp, method, atype, sparams, run_optimizer)
908+
frame === nothing && return nothing, Any
909+
frame.inferred || return nothing, Any
910+
code = frame.src
911+
rt = widenconst(ignorelimited(frame.result.result))
912+
return code, rt
913+
end
914+
915+
# compute an inferred frame
916+
function typeinf_frame(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool)
907917
mi = specialize_method(method, atype, sparams)::MethodInstance
908918
ccall(:jl_typeinf_begin, Cvoid, ())
909919
result = InferenceResult(mi)
910920
frame = InferenceState(result, run_optimizer ? :global : :no, interp)
911-
frame === nothing && return (nothing, Any)
921+
frame === nothing && return nothing
912922
typeinf(interp, frame)
913923
ccall(:jl_typeinf_end, Cvoid, ())
914-
frame.inferred || return (nothing, Any)
915-
return (frame.src, widenconst(ignorelimited(result.result)))
924+
return frame
916925
end
917926

918927
# compute (and cache) an inferred AST and return type

base/compiler/types.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ function Effects(e::Effects = EFFECTS_UNKNOWN;
7878
inbounds_taints_consistency)
7979
end
8080

81+
is_consistent(effects::Effects) = effects.consistent === ALWAYS_TRUE
82+
is_effect_free(effects::Effects) = effects.effect_free === ALWAYS_TRUE
83+
is_nothrow(effects::Effects) = effects.nothrow === ALWAYS_TRUE
84+
is_terminating(effects::Effects) = effects.terminates === ALWAYS_TRUE
85+
is_overlayed(effects::Effects) = effects.overlayed
86+
8187
is_total_or_error(effects::Effects) =
82-
effects.consistent === ALWAYS_TRUE &&
83-
effects.effect_free === ALWAYS_TRUE &&
84-
effects.terminates === ALWAYS_TRUE
88+
is_consistent(effects) &&
89+
is_effect_free(effects) &&
90+
is_terminating(effects)
8591

8692
is_total(effects::Effects) =
8793
is_total_or_error(effects) &&
88-
effects.nothrow === ALWAYS_TRUE
94+
is_nothrow(effects)
8995

9096
is_removable_if_unused(effects::Effects) =
9197
effects.effect_free === ALWAYS_TRUE &&

base/reflection.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,28 @@ function return_types(@nospecialize(f), @nospecialize(types=default_tt(f));
13061306
return rt
13071307
end
13081308

1309+
function infer_effects(@nospecialize(f), @nospecialize(types=default_tt(f));
1310+
world = get_world_counter(),
1311+
interp = Core.Compiler.NativeInterpreter(world))
1312+
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
1313+
types = to_tuple_type(types)
1314+
effects = Core.Compiler.EFFECTS_TOTAL
1315+
if isa(f, Core.Builtin)
1316+
args = Any[types.parameters...]
1317+
rt = Core.Compiler.builtin_tfunction(interp, f, args, nothing)
1318+
return Core.Compiler.builtin_effects(f, args, rt)
1319+
else
1320+
for match in _methods(f, types, -1, world)::Vector
1321+
match = match::Core.MethodMatch
1322+
frame = Core.Compiler.typeinf_frame(interp,
1323+
match.method, match.spec_types, match.sparams, #=run_optimizer=#false)
1324+
frame === nothing && return Core.Compiler.Effects()
1325+
effects = Core.Compiler.tristate_merge(effects, frame.ipo_effects)
1326+
end
1327+
return effects
1328+
end
1329+
end
1330+
13091331
"""
13101332
print_statement_costs(io::IO, f, types)
13111333

test/compiler/irpasses.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,9 @@ let ci = code_typed(foo_cfg_empty, Tuple{Bool}, optimize=true)[1][1]
10361036
@test isa(ir.stmts[length(ir.stmts)][:inst], ReturnNode)
10371037
end
10381038

1039-
@test Core.Compiler.builtin_effects(getfield, Any[Complex{Int}, Symbol], Any).effect_free.state == 0x01
1040-
@test Core.Compiler.builtin_effects(getglobal, Any[Module, Symbol], Any).effect_free.state == 0x01
1039+
@test Core.Compiler.is_effect_free(Base.infer_effects(getfield, (Complex{Int}, Symbol)))
1040+
@test Core.Compiler.is_effect_free(Base.infer_effects(getglobal, (Module, Symbol)))
1041+
10411042
# Test that UseRefIterator gets SROA'd inside of new_to_regular (#44557)
10421043
# expression and new_to_regular offset are arbitrary here, we just want to see the UseRefIterator erased
10431044
let e = Expr(:call, Core.GlobalRef(Base, :arrayset), false, Core.SSAValue(4), Core.SSAValue(9), Core.SSAValue(8))

test/reflection.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,33 @@ end
964964
@eval m f4(a) = return
965965
@test Base.default_tt(m.f4) == Tuple
966966
end
967+
968+
Base.@assume_effects :terminates_locally function issue41694(x::Int)
969+
res = 1
970+
1 < x < 20 || throw("bad")
971+
while x > 1
972+
res *= x
973+
x -= 1
974+
end
975+
return res
976+
end
977+
maybe_effectful(x::Int) = 42
978+
maybe_effectful(x::Any) = unknown_operation()
979+
980+
@testset "infer_effects" begin
981+
@test Base.infer_effects(issue41694, (Int,)) |> Core.Compiler.is_terminating
982+
@test Base.infer_effects((Int,)) do x
983+
issue41694(x)
984+
end |> Core.Compiler.is_terminating
985+
@test Base.infer_effects(issue41694) |> Core.Compiler.is_terminating # use `default_tt`
986+
let effects = Base.infer_effects(maybe_effectful, (Any,)) # union split
987+
@test !Core.Compiler.is_consistent(effects)
988+
@test !Core.Compiler.is_effect_free(effects)
989+
@test !Core.Compiler.is_nothrow(effects)
990+
@test !Core.Compiler.is_terminating(effects)
991+
@test Core.Compiler.is_overlayed(effects)
992+
end
993+
# builtins
994+
@test Base.infer_effects(typeof, (Any,)) |> Core.Compiler.is_total
995+
@test Base.infer_effects(===, (Any,Any)) |> Core.Compiler.is_total
996+
end

0 commit comments

Comments
 (0)