Skip to content

Commit bea7b6f

Browse files
authored
follow up the inlining unmatched type param PR (#46484)
This commit follows up #45062: - eliminate closure capturing, and improve type stability a bit - refactor the test structure so that they are more aligned with the other parts of tests
1 parent 19f44b6 commit bea7b6f

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

base/compiler/ssair/passes.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ function is_getfield_captures(@nospecialize(def), compact::IncrementalCompact)
352352
end
353353

354354
struct LiftedValue
355-
x
356-
LiftedValue(@nospecialize x) = new(x)
355+
val
356+
LiftedValue(@nospecialize val) = new(val)
357357
end
358358
const LiftedLeaves = IdDict{Any, Union{Nothing,LiftedValue}}
359359

@@ -578,7 +578,7 @@ function lift_comparison_leaves!(@specialize(tfunc),
578578
visited_phinodes, cmp, lifting_cache, Bool,
579579
lifted_leaves::LiftedLeaves, val, nothing)::LiftedValue
580580

581-
compact[idx] = lifted_val.x
581+
compact[idx] = lifted_val.val
582582
end
583583

584584
struct LiftedPhi
@@ -626,7 +626,7 @@ function perform_lifting!(compact::IncrementalCompact,
626626
end
627627
end
628628

629-
the_leaf_val = isa(the_leaf, LiftedValue) ? the_leaf.x : nothing
629+
the_leaf_val = isa(the_leaf, LiftedValue) ? the_leaf.val : nothing
630630
if !isa(the_leaf_val, SSAValue)
631631
all_same = false
632632
end
@@ -690,7 +690,7 @@ function perform_lifting!(compact::IncrementalCompact,
690690
resize!(new_node.values, length(new_node.values)+1)
691691
continue
692692
end
693-
val = lifted_val.x
693+
val = lifted_val.val
694694
if isa(val, AnySSAValue)
695695
callback = (@nospecialize(pi), @nospecialize(idx)) -> true
696696
val = simple_walk(compact, val, callback)
@@ -750,18 +750,18 @@ function lift_svec_ref!(compact::IncrementalCompact, idx::Int, stmt::Expr)
750750
elseif is_known_call(def, Core._compute_sparams, compact)
751751
res = _lift_svec_ref(def, compact)
752752
if res !== nothing
753-
compact[idx] = res
753+
compact[idx] = res.val
754754
end
755755
return
756756
end
757757
end
758758
end
759759

760-
function _lift_svec_ref(def::Expr, compact::IncrementalCompact)
761-
# TODO: We could do the whole lifing machinery here, but really all
762-
# we want to do is clean this up when it got inserted by inlining,
763-
# which always targets simple `svec` call or `_compute_sparams`,
764-
# so this specialized lifting would be enough
760+
# TODO: We could do the whole lifing machinery here, but really all
761+
# we want to do is clean this up when it got inserted by inlining,
762+
# which always targets simple `svec` call or `_compute_sparams`,
763+
# so this specialized lifting would be enough
764+
@inline function _lift_svec_ref(def::Expr, compact::IncrementalCompact)
765765
m = argextype(def.args[2], compact)
766766
isa(m, Const) || return nothing
767767
m = m.val
@@ -776,9 +776,13 @@ function _lift_svec_ref(def::Expr, compact::IncrementalCompact)
776776
sig.name === Tuple.name || return nothing
777777
length(sig.parameters) >= 1 || return nothing
778778

779-
i = findfirst(j->has_typevar(sig.parameters[j], tvar), 1:length(sig.parameters))
779+
i = let sig=sig
780+
findfirst(j->has_typevar(sig.parameters[j], tvar), 1:length(sig.parameters))
781+
end
780782
i === nothing && return nothing
781-
_any(j->has_typevar(sig.parameters[j], tvar), i+1:length(sig.parameters)) && return nothing
783+
let sig=sig
784+
any(j->has_typevar(sig.parameters[j], tvar), i+1:length(sig.parameters))
785+
end && return nothing
782786

783787
arg = sig.parameters[i]
784788
isa(arg, DataType) || return nothing
@@ -808,7 +812,7 @@ function _lift_svec_ref(def::Expr, compact::IncrementalCompact)
808812
length(applyTbody.parameters) == length(arg.parameters) == 1 || return nothing
809813
applyTbody.parameters[1] === applyTvar || return nothing
810814
arg.parameters[1] === tvar || return nothing
811-
return argdef.args[3]
815+
return LiftedValue(argdef.args[3])
812816
end
813817

814818
# NOTE we use `IdSet{Int}` instead of `BitSet` for in these passes since they work on IR after inlining,
@@ -1017,7 +1021,7 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing, InliningState} = nothin
10171021
@assert val !== nothing
10181022
end
10191023

1020-
compact[idx] = val === nothing ? nothing : val.x
1024+
compact[idx] = val === nothing ? nothing : val.val
10211025
end
10221026

10231027
non_dce_finish!(compact)

test/compiler/inline.jl

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,26 +1518,29 @@ function oc_capture_oc(z)
15181518
end
15191519
@test fully_eliminated(oc_capture_oc, (Int,))
15201520

1521+
@eval struct OldVal{T}
1522+
x::T
1523+
(OV::Type{OldVal{T}})() where T = $(Expr(:new, :OV))
1524+
end
1525+
with_unmatched_typeparam1(x::OldVal{i}) where {i} = i
1526+
with_unmatched_typeparam2() = [ Base.donotdelete(OldVal{i}()) for i in 1:10000 ]
1527+
function with_unmatched_typeparam3()
1528+
f(x::OldVal{i}) where {i} = i
1529+
r = 0
1530+
for i = 1:10000
1531+
r += f(OldVal{i}())
1532+
end
1533+
return r
1534+
end
1535+
15211536
@testset "Inlining with unmatched type parameters" begin
1522-
@eval struct OldVal{T}
1523-
x::T
1524-
(OV::Type{OldVal{T}})() where T = $(Expr(:new, :OV))
1525-
end
1526-
let f(x) = OldVal{x}()
1527-
g() = [ Base.donotdelete(OldVal{i}()) for i in 1:10000 ]
1528-
h() = begin
1529-
f(x::OldVal{i}) where {i} = i
1530-
r = 0
1531-
for i = 1:10000
1532-
r += f(OldVal{i}())
1533-
end
1534-
return r
1535-
end
1536-
srcs = (code_typed1(f, (Any,)),
1537-
code_typed1(g),
1538-
code_typed1(h))
1539-
for src in srcs
1540-
@test !any(@nospecialize(x) -> isexpr(x, :call) && length(x.args) == 1, src.code)
1541-
end
1537+
let src = code_typed1(with_unmatched_typeparam1, (Any,))
1538+
@test !any(@nospecialize(x) -> isexpr(x, :call) && length(x.args) == 1, src.code)
1539+
end
1540+
let src = code_typed1(with_unmatched_typeparam2)
1541+
@test !any(@nospecialize(x) -> isexpr(x, :call) && length(x.args) == 1, src.code)
1542+
end
1543+
let src = code_typed1(with_unmatched_typeparam3)
1544+
@test !any(@nospecialize(x) -> isexpr(x, :call) && length(x.args) == 1, src.code)
15421545
end
15431546
end

0 commit comments

Comments
 (0)