Skip to content

Commit f7aa97b

Browse files
committed
inference: mark flag for effect-free :calls during abstractinterpret
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 d0a211a commit f7aa97b

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,12 @@ function abstract_eval_statement_expr(interp::AbstractInterpreter, e::Expr, vtyp
22432243
merge_effects!(interp, sv, effects)
22442244
if isa(sv, InferenceState)
22452245
sv.stmt_info[sv.currpc] = info
2246+
# mark this call statement as DCE-elgible
2247+
if is_removable_if_unused(effects)
2248+
add_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2249+
else
2250+
sub_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
2251+
end
22462252
end
22472253
end
22482254
t = rt

base/compiler/inferencestate.jl

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

539539
get_curr_ssaflag(sv::InferenceState) = sv.src.ssaflags[sv.currpc]
540+
add_curr_ssaflag!(sv::InferenceState, flag::UInt8) = sv.src.ssaflags[sv.currpc] |= flag
541+
sub_curr_ssaflag!(sv::InferenceState, flag::UInt8) = sv.src.ssaflags[sv.currpc] &= ~flag
540542

541543
function narguments(sv::InferenceState)
542544
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
@@ -1817,6 +1816,26 @@ let ir = Base.code_ircode(big_tuple_test1, Tuple{})[1][1]
18171816
@test length(ir.stmts) == 1
18181817
end
18191818

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

0 commit comments

Comments
 (0)