Skip to content

Commit 51f4632

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 3f23c45 commit 51f4632

File tree

6 files changed

+91
-14
lines changed

6 files changed

+91
-14
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
4848
# aren't any in the throw block either to enable other optimizations.
4949
add_remark!(interp, sv, "Skipped call in throw block")
5050
nonoverlayed = false
51-
if isoverlayed(method_table(interp)) && sv.ipo_effects.nonoverlayed
51+
if isoverlayed(method_table(interp)) && is_nonoverlayed(sv.ipo_effects)
5252
# as we may want to concrete-evaluate this frame in cases when there are
5353
# no overlayed calls, try an additional effort now to check if this call
5454
# isn't overlayed rather than just handling it conservatively
@@ -712,7 +712,7 @@ function concrete_eval_eligible(interp::AbstractInterpreter,
712712
@nospecialize(f), result::MethodCallResult, arginfo::ArgInfo, sv::InferenceState)
713713
# disable concrete-evaluation since this function call is tainted by some overlayed
714714
# method and currently there is no direct way to execute overlayed methods
715-
isoverlayed(method_table(interp)) && !result.edge_effects.nonoverlayed && return false
715+
isoverlayed(method_table(interp)) && !is_nonoverlayed(result.edge_effects) && return false
716716
return f !== nothing &&
717717
result.edge !== nothing &&
718718
is_total_or_error(result.edge_effects) &&

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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,25 @@ function Effects(e::Effects = EFFECTS_UNKNOWN′;
7979
inbounds_taints_consistency)
8080
end
8181

82+
is_consistent(effects::Effects) = effects.consistent === ALWAYS_TRUE
83+
is_effect_free(effects::Effects) = effects.effect_free === ALWAYS_TRUE
84+
is_nothrow(effects::Effects) = effects.nothrow === ALWAYS_TRUE
85+
is_terminates(effects::Effects) = effects.terminates === ALWAYS_TRUE
86+
is_nonoverlayed(effects::Effects) = effects.nonoverlayed
87+
8288
is_total_or_error(effects::Effects) =
83-
effects.consistent === ALWAYS_TRUE &&
84-
effects.effect_free === ALWAYS_TRUE &&
85-
effects.terminates === ALWAYS_TRUE
89+
is_consistent(effects) &&
90+
is_effect_free(effects) &&
91+
is_terminates(effects)
8692

8793
is_total(effects::Effects) =
8894
is_total_or_error(effects) &&
89-
effects.nothrow === ALWAYS_TRUE
95+
is_nothrow(effects)
9096

9197
is_removable_if_unused(effects::Effects) =
92-
effects.effect_free === ALWAYS_TRUE &&
93-
effects.terminates === ALWAYS_TRUE &&
94-
effects.nothrow === ALWAYS_TRUE
98+
is_effect_free(effects) &&
99+
is_terminates(effects) &&
100+
is_nothrow(effects)
95101

96102
function encode_effects(e::Effects)
97103
return (e.consistent.state << 0) |

base/reflection.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,35 @@ function return_types(@nospecialize(f), @nospecialize(types=default_tt(f));
13101310
return rt
13111311
end
13121312

1313+
function infer_effects(@nospecialize(f), @nospecialize(types=default_tt(f));
1314+
world = get_world_counter(),
1315+
interp = Core.Compiler.NativeInterpreter(world))
1316+
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
1317+
types = to_tuple_type(types)
1318+
if isa(f, Core.Builtin)
1319+
args = Any[types.parameters...]
1320+
rt = Core.Compiler.builtin_tfunction(interp, f, args, nothing)
1321+
return Core.Compiler.builtin_effects(f, args, rt)
1322+
else
1323+
effects = Core.Compiler.EFFECTS_TOTAL
1324+
matches = _methods(f, types, -1, world)::Vector
1325+
if isempty(matches)
1326+
# although this call is known to throw MethodError (thus `nothrow=ALWAYS_TRUE`),
1327+
# still mark it `TRISTATE_UNKNOWN` just in order to be consistent with a result
1328+
# derived by the effect analysis, which can't prove guaranteed throwness at this moment
1329+
return Core.Compiler.Effects(effects; nothrow=Core.Compiler.TRISTATE_UNKNOWN)
1330+
end
1331+
for match in matches
1332+
match = match::Core.MethodMatch
1333+
frame = Core.Compiler.typeinf_frame(interp,
1334+
match.method, match.spec_types, match.sparams, #=run_optimizer=#false)
1335+
frame === nothing && return Core.Compiler.Effects()
1336+
effects = Core.Compiler.tristate_merge(effects, frame.ipo_effects)
1337+
end
1338+
return effects
1339+
end
1340+
end
1341+
13131342
"""
13141343
print_statement_costs(io::IO, f, types)
13151344

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,35 @@ 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+
function f_no_methods end
980+
981+
@testset "infer_effects" begin
982+
@test Base.infer_effects(issue41694, (Int,)) |> Core.Compiler.is_terminates
983+
@test Base.infer_effects((Int,)) do x
984+
issue41694(x)
985+
end |> Core.Compiler.is_terminates
986+
@test Base.infer_effects(issue41694) |> Core.Compiler.is_terminates # use `default_tt`
987+
let effects = Base.infer_effects(maybe_effectful, (Any,)) # union split
988+
@test !Core.Compiler.is_consistent(effects)
989+
@test !Core.Compiler.is_effect_free(effects)
990+
@test !Core.Compiler.is_nothrow(effects)
991+
@test !Core.Compiler.is_terminates(effects)
992+
@test !Core.Compiler.is_nonoverlayed(effects)
993+
end
994+
@test Base.infer_effects(f_no_methods) |> !Core.Compiler.is_nothrow
995+
# builtins
996+
@test Base.infer_effects(typeof, (Any,)) |> Core.Compiler.is_total
997+
@test Base.infer_effects(===, (Any,Any)) |> Core.Compiler.is_total
998+
end

0 commit comments

Comments
 (0)