Skip to content

Commit 7a9a018

Browse files
committed
inference: mark flag for effect-free :calls during optimization
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 113efb6 commit 7a9a018

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
@@ -239,6 +239,12 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
239239
# and avoid keeping track of a more complex result type.
240240
rettype = Any
241241
end
242+
# mark this call statement as DCE-elgible
243+
if is_removable_if_unused(all_effects)
244+
add_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
245+
else
246+
sub_curr_ssaflag!(sv, IR_FLAG_EFFECT_FREE)
247+
end
242248
add_call_backedges!(interp, rettype, all_effects, edges, matches, atype, sv)
243249
if !isempty(sv.pclimitations) # remove self, if present
244250
delete!(sv.pclimitations, sv)

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)