Skip to content

Commit 7f18f76

Browse files
authored
inference: enhance memoryop tfunc robustness (#52185)
Make them able to handle potential `Vararg` argument. Also adds plenty of tests.
1 parent 4689850 commit 7f18f76

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

base/compiler/tfuncs.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ end
19921992
add_tfunc(memoryref_isassigned, 3, 3, memoryref_isassigned_tfunc, 20)
19931993

19941994
@nospecs function memoryref_tfunc(𝕃::AbstractLattice, mem)
1995-
a = widenconst(mem)
1995+
a = widenconst(unwrapva(mem))
19961996
if !has_free_typevars(a)
19971997
unw = unwrap_unionall(a)
19981998
if isa(unw, DataType) && unw.name === GenericMemory.body.body.body.name
@@ -2006,7 +2006,10 @@ add_tfunc(memoryref_isassigned, 3, 3, memoryref_isassigned_tfunc, 20)
20062006
return GenericMemoryRef
20072007
end
20082008
@nospecs function memoryref_tfunc(𝕃::AbstractLattice, ref, idx)
2009-
memoryref_tfunc(𝕃, ref, idx, Const(true))
2009+
if isvarargtype(idx)
2010+
idx = unwrapva(idx)
2011+
end
2012+
return memoryref_tfunc(𝕃, ref, idx, Const(true))
20102013
end
20112014
@nospecs function memoryref_tfunc(𝕃::AbstractLattice, ref, idx, boundscheck)
20122015
memoryref_builtin_common_errorcheck(ref, Const(:not_atomic), boundscheck) || return Bottom
@@ -2021,12 +2024,10 @@ add_tfunc(memoryref, 1, 3, memoryref_tfunc, 1)
20212024
end
20222025
add_tfunc(memoryrefoffset, 1, 1, memoryrefoffset_tfunc, 5)
20232026

2024-
2025-
20262027
@nospecs function memoryref_builtin_common_errorcheck(mem, order, boundscheck)
20272028
hasintersect(widenconst(mem), GenericMemoryRef) || return false
20282029
hasintersect(widenconst(order), Symbol) || return false
2029-
hasintersect(widenconst(boundscheck), Bool) || return false
2030+
hasintersect(widenconst(unwrapva(boundscheck)), Bool) || return false
20302031
return true
20312032
end
20322033

test/compiler/inference.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,15 +1580,21 @@ end
15801580
f_typeof_tfunc(x) = typeof(x)
15811581
@test Base.return_types(f_typeof_tfunc, (Union{<:T, Int} where T<:Complex,)) == Any[Union{Type{Int}, Type{Complex{T}} where T<:Real}]
15821582

1583-
# memoryref_tfunc, memoryrefget_tfunc, memoryrefset!_tfunc
1583+
# memoryref_tfunc, memoryrefget_tfunc, memoryrefset!_tfunc, memoryref_isassigned, memoryrefoffset_tfunc
15841584
let memoryref_tfunc(@nospecialize xs...) = Core.Compiler.memoryref_tfunc(Core.Compiler.fallback_lattice, xs...)
15851585
memoryrefget_tfunc(@nospecialize xs...) = Core.Compiler.memoryrefget_tfunc(Core.Compiler.fallback_lattice, xs...)
1586+
memoryref_isassigned_tfunc(@nospecialize xs...) = Core.Compiler.memoryref_isassigned_tfunc(Core.Compiler.fallback_lattice, xs...)
15861587
memoryrefset!_tfunc(@nospecialize xs...) = Core.Compiler.memoryrefset!_tfunc(Core.Compiler.fallback_lattice, xs...)
1588+
memoryrefoffset_tfunc(@nospecialize xs...) = Core.Compiler.memoryrefoffset_tfunc(Core.Compiler.fallback_lattice, xs...)
1589+
interp = Core.Compiler.NativeInterpreter()
1590+
builtin_tfunction(@nospecialize xs...) = Core.Compiler.builtin_tfunction(interp, xs..., nothing)
15871591
@test memoryref_tfunc(Memory{Int}) == MemoryRef{Int}
15881592
@test memoryref_tfunc(Memory{Integer}) == MemoryRef{Integer}
15891593
@test memoryref_tfunc(MemoryRef{Int}, Int) == MemoryRef{Int}
1594+
@test memoryref_tfunc(MemoryRef{Int}, Vararg{Int}) == MemoryRef{Int}
15901595
@test memoryref_tfunc(MemoryRef{Int}, Int, Symbol) == Union{}
15911596
@test memoryref_tfunc(MemoryRef{Int}, Int, Bool) == MemoryRef{Int}
1597+
@test memoryref_tfunc(MemoryRef{Int}, Int, Vararg{Bool}) == MemoryRef{Int}
15921598
@test memoryref_tfunc(Memory{Int}, Int) == Union{}
15931599
@test memoryref_tfunc(Any, Any, Any) == Any # also probably could be GenericMemoryRef
15941600
@test memoryref_tfunc(Any, Any) == Any # also probably could be GenericMemoryRef
@@ -1603,6 +1609,20 @@ let memoryref_tfunc(@nospecialize xs...) = Core.Compiler.memoryref_tfunc(Core.Co
16031609
@test memoryrefget_tfunc(MemoryRef{Int}, String, Bool) === Union{}
16041610
@test memoryrefget_tfunc(MemoryRef{Int}, Symbol, String) === Union{}
16051611
@test memoryrefget_tfunc(Any, Any, Any) === Any
1612+
@test builtin_tfunction(Core.memoryrefget, Any[MemoryRef{Int}, Vararg{Any}]) == Int
1613+
@test builtin_tfunction(Core.memoryrefget, Any[MemoryRef{Int}, Symbol, Bool, Vararg{Bool}]) == Int
1614+
@test memoryref_isassigned_tfunc(MemoryRef{Any}, Symbol, Bool) === Bool
1615+
@test memoryref_isassigned_tfunc(MemoryRef{Any}, Any, Any) === Bool
1616+
@test memoryref_isassigned_tfunc(MemoryRef{<:Integer}, Symbol, Bool) === Bool
1617+
@test memoryref_isassigned_tfunc(GenericMemoryRef, Symbol, Bool) === Bool
1618+
@test memoryref_isassigned_tfunc(GenericMemoryRef{:not_atomic}, Symbol, Bool) === Bool
1619+
@test memoryref_isassigned_tfunc(Vector{Int}, Symbol, Bool) === Union{}
1620+
@test memoryref_isassigned_tfunc(String, Symbol, Bool) === Union{}
1621+
@test memoryref_isassigned_tfunc(MemoryRef{Int}, String, Bool) === Union{}
1622+
@test memoryref_isassigned_tfunc(MemoryRef{Int}, Symbol, String) === Union{}
1623+
@test memoryref_isassigned_tfunc(Any, Any, Any) === Bool
1624+
@test builtin_tfunction(Core.memoryref_isassigned, Any[MemoryRef{Int}, Vararg{Any}]) == Bool
1625+
@test builtin_tfunction(Core.memoryref_isassigned, Any[MemoryRef{Int}, Symbol, Bool, Vararg{Bool}]) == Bool
16061626
@test memoryrefset!_tfunc(MemoryRef{Int}, Int, Symbol, Bool) === MemoryRef{Int}
16071627
let ua = MemoryRef{<:Integer}
16081628
@test memoryrefset!_tfunc(ua, Int, Symbol, Bool) === ua
@@ -1617,6 +1637,15 @@ let memoryref_tfunc(@nospecialize xs...) = Core.Compiler.memoryref_tfunc(Core.Co
16171637
@test memoryrefset!_tfunc(GenericMemoryRef{:not_atomic}, Any, Any, Any) === GenericMemoryRef{:not_atomic}
16181638
@test memoryrefset!_tfunc(GenericMemoryRef, Any, Any, Any) === GenericMemoryRef
16191639
@test memoryrefset!_tfunc(Any, Any, Any, Any) === Any # also probably could be GenericMemoryRef
1640+
@test builtin_tfunction(Core.memoryrefset!, Any[MemoryRef{Int}, Vararg{Any}]) == MemoryRef{Int}
1641+
@test builtin_tfunction(Core.memoryrefset!, Any[MemoryRef{Int}, Vararg{Symbol}]) == Union{}
1642+
@test builtin_tfunction(Core.memoryrefset!, Any[MemoryRef{Int}, Any, Symbol, Vararg{Bool}]) == MemoryRef{Int}
1643+
@test builtin_tfunction(Core.memoryrefset!, Any[MemoryRef{Int}, Any, Symbol, Bool, Vararg{Any}]) == MemoryRef{Int}
1644+
@test memoryrefoffset_tfunc(MemoryRef) == memoryrefoffset_tfunc(GenericMemoryRef) == Int
1645+
@test memoryrefoffset_tfunc(Memory) == memoryrefoffset_tfunc(GenericMemory) == Union{}
1646+
@test builtin_tfunction(Core.memoryrefoffset, Any[Vararg{MemoryRef}]) == Int
1647+
@test builtin_tfunction(Core.memoryrefoffset, Any[Vararg{Any}]) == Int
1648+
@test builtin_tfunction(Core.memoryrefoffset, Any[Vararg{Memory}]) == Union{}
16201649
end
16211650

16221651
let tuple_tfunc(@nospecialize xs...) =

0 commit comments

Comments
 (0)