Skip to content

Commit a074d06

Browse files
authored
inference: mark flag for effect-free :calls during abstractinterpret (#47689)
So that they can be deleted during the first `compact!`-ion. This allows us to delete an inlineable and effect-free, but unused call. This is essentially an alternative of #47305, but doesn't introduce a problem like #47374.
1 parent 181328c commit a074d06

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,13 @@ function abstract_eval_statement_expr(interp::AbstractInterpreter, e::Expr, vtyp
22502250
merge_effects!(interp, sv, effects)
22512251
if isa(sv, InferenceState)
22522252
sv.stmt_info[sv.currpc] = info
2253+
# mark this call statement as DCE-elgible
2254+
# TODO better to do this in a single pass based on the `info` object at the end of abstractinterpret?
2255+
if is_removable_if_unused(effects)
2256+
add_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2257+
else
2258+
sub_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2259+
end
22532260
end
22542261
end
22552262
t = rt
@@ -2362,6 +2369,14 @@ function abstract_eval_statement_expr(interp::AbstractInterpreter, e::Expr, vtyp
23622369
(;rt, effects) = abstract_eval_foreigncall(interp, e, vtypes, sv, mi)
23632370
t = rt
23642371
merge_effects!(interp, sv, effects)
2372+
if isa(sv, InferenceState)
2373+
# mark this call statement as DCE-elgible
2374+
if is_removable_if_unused(effects)
2375+
add_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2376+
else
2377+
sub_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2378+
end
2379+
end
23652380
elseif ehead === :cfunction
23662381
effects = EFFECTS_UNKNOWN
23672382
merge_effects!(interp, sv, effects)

base/compiler/inferencestate.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ function print_callstack(sv::InferenceState)
532532
end
533533

534534
get_curr_ssaflag(sv::InferenceState) = sv.src.ssaflags[sv.currpc]
535+
add_curr_ssaflag!(sv::InferenceState, flag::UInt8) = sv.src.ssaflags[sv.currpc] |= flag
536+
sub_curr_ssaflag!(sv::InferenceState, flag::UInt8) = sv.src.ssaflags[sv.currpc] &= ~flag
535537

536538
function narguments(sv::InferenceState)
537539
def = sv.linfo.def

test/compiler/inline.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,7 @@ Base.setindex!(s::SafeRef, x) = setfield!(s, 1, x)
10771077
noninlined_dce_new(s)
10781078
nothing
10791079
end
1080-
# should be resolved once we merge https://github.com/JuliaLang/julia/pull/43923
1081-
@test_broken fully_eliminated((Union{Symbol,String},)) do s
1080+
@test fully_eliminated((Union{Symbol,String},)) do s
10821081
noninlined_dce_new(s)
10831082
nothing
10841083
end
@@ -1820,6 +1819,26 @@ let ir = Base.code_ircode(big_tuple_test1, Tuple{})[1][1]
18201819
@test length(ir.stmts) == 1
18211820
end
18221821

1822+
# inlineable but removable call should be eligible for DCE
1823+
Base.@assume_effects :removable @inline function inlineable_effect_free(a::Float64)
1824+
a == Inf && return zero(a)
1825+
return sin(a) + cos(a)
1826+
end
1827+
@test fully_eliminated((Float64,)) do a
1828+
b = inlineable_effect_free(a)
1829+
c = inlineable_effect_free(b)
1830+
nothing
1831+
end
1832+
1833+
# https://github.com/JuliaLang/julia/issues/47374
1834+
function f47374(x)
1835+
[f47374(i, x) for i in 1:1]
1836+
end
1837+
function f47374(i::Int, x)
1838+
return 1.0
1839+
end
1840+
@test f47374(rand(1)) == Float64[1.0]
1841+
18231842
# compiler should recognize effectful :static_parameter
18241843
# https://github.com/JuliaLang/julia/issues/45490
18251844
issue45490_1(x::Union{T, Nothing}, y::Union{T, Nothing}) where {T} = T

0 commit comments

Comments
 (0)