Skip to content

Commit 2753ef9

Browse files
committed
remove :boundscheck argument from Core._svec_ref (#50561)
`Core._svec_ref` has accepted `boundscheck`-value as the first argument since it was added in #45062. Nonetheless, `Core._svec_ref` simply calls `jl_svec_ref` in either the interpreter or the codegen, and thus the `boundscheck` value isn't utilized in any optimizations. Rather, even worse, this `boundscheck`-argument negatively influences the effect analysis (xref #50167 for details) and has caused type inference regressions as reported in #50544. For these reasons, this commit simply eliminates the `boundscheck` argument from `Core._svec_ref`. Consequently, `getindex(::SimpleVector, ::Int)` is now being concrete-eval eligible. closes #50544
1 parent d6eaa7c commit 2753ef9

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

base/compiler/ssair/inlining.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ end
18171817

18181818
function insert_spval!(insert_node!::Inserter, spvals_ssa::SSAValue, spidx::Int, do_isdefined::Bool)
18191819
ret = insert_node!(
1820-
effect_free_and_nothrow(NewInstruction(Expr(:call, Core._svec_ref, false, spvals_ssa, spidx), Any)))
1820+
effect_free_and_nothrow(NewInstruction(Expr(:call, Core._svec_ref, spvals_ssa, spidx), Any)))
18211821
tcheck_not = nothing
18221822
if do_isdefined
18231823
tcheck = insert_node!(

base/compiler/ssair/passes.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,10 @@ function perform_lifting!(compact::IncrementalCompact,
810810
end
811811

812812
function lift_svec_ref!(compact::IncrementalCompact, idx::Int, stmt::Expr)
813-
length(stmt.args) != 4 && return
813+
length(stmt.args) != 3 && return
814814

815-
vec = stmt.args[3]
816-
val = stmt.args[4]
815+
vec = stmt.args[2]
816+
val = stmt.args[3]
817817
valT = argextype(val, compact)
818818
(isa(valT, Const) && isa(valT.val, Int)) || return
819819
valI = valT.val::Int

base/essentials.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ end
741741

742742
# SimpleVector
743743

744-
@eval getindex(v::SimpleVector, i::Int) = (@_foldable_meta; Core._svec_ref($(Expr(:boundscheck)), v, i))
744+
getindex(v::SimpleVector, i::Int) = (@_foldable_meta; Core._svec_ref(v, i))
745745
function length(v::SimpleVector)
746746
@_total_meta
747747
t = @_gc_preserve_begin v

src/builtins.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -1655,11 +1655,9 @@ JL_CALLABLE(jl_f__compute_sparams)
16551655

16561656
JL_CALLABLE(jl_f__svec_ref)
16571657
{
1658-
JL_NARGS(_svec_ref, 3, 3);
1659-
jl_value_t *b = args[0];
1660-
jl_svec_t *s = (jl_svec_t*)args[1];
1661-
jl_value_t *i = (jl_value_t*)args[2];
1662-
JL_TYPECHK(_svec_ref, bool, b);
1658+
JL_NARGS(_svec_ref, 2, 2);
1659+
jl_svec_t *s = (jl_svec_t*)args[0];
1660+
jl_value_t *i = (jl_value_t*)args[1];
16631661
JL_TYPECHK(_svec_ref, simplevector, (jl_value_t*)s);
16641662
JL_TYPECHK(_svec_ref, long, i);
16651663
size_t len = jl_svec_len(s);

test/compiler/inference.jl

+16
Original file line numberDiff line numberDiff line change
@@ -5014,3 +5014,19 @@ let 𝕃 = Core.Compiler.SimpleInferenceLattice.instance
50145014
map(T -> Issue49785{S,T}, (a = S,))
50155015
end isa Vector
50165016
end
5017+
5018+
# `getindex(::SimpleVector, ::Int)` shuold be concrete-evaluated
5019+
@eval Base.return_types() do
5020+
$(Core.svec(1,Int,nothing))[2]
5021+
end |> only == Type{Int}
5022+
# https://github.com/JuliaLang/julia/issues/50544
5023+
struct Issue50544{T<:Tuple}
5024+
t::T
5025+
end
5026+
Base.@propagate_inbounds f_issue50544(x, i, ii...) = f_issue50544(f_issue50544(x, i), ii...)
5027+
Base.@propagate_inbounds f_issue50544(::Type{Issue50544{T}}, i) where T = T.parameters[i]
5028+
g_issue50544(T...) = Issue50544{Tuple{T...}}
5029+
h_issue50544(x::T) where T = g_issue50544(f_issue50544(T, 1), f_issue50544(T, 2, 1))
5030+
let x = Issue50544((1, Issue50544((2.0, 'x'))))
5031+
@test only(Base.return_types(h_issue50544, (typeof(x),))) == Type{Issue50544{Tuple{Int,Float64}}}
5032+
end

test/core.jl

+4
Original file line numberDiff line numberDiff line change
@@ -8040,3 +8040,7 @@ bar50293(@nospecialize(u)) = (Base.issingletontype(u.a), baz50293(u.a))
80408040
let u = Union{Type{Union{}}, Type{Any}}, ab = bar50293(u)
80418041
@test ab[1] == ab[2] == false
80428042
end
8043+
8044+
# `SimpleVector`-operations should be concrete-eval eligible
8045+
@test Core.Compiler.is_foldable(Base.infer_effects(length, (Core.SimpleVector,)))
8046+
@test Core.Compiler.is_foldable(Base.infer_effects(getindex, (Core.SimpleVector,Int)))

0 commit comments

Comments
 (0)