Skip to content

Commit c7eb766

Browse files
authored
minor refactoring on the CustomAbstractInterpreterCaching test case (#53329)
By using `@newinterp`. Also ensures `cache_owner` for external abstract interpreter defined by `@newinterp` is unique always.
1 parent 61c3521 commit c7eb766

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

test/compiler/newinterp.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Defines new `NewInterpreter <: AbstractInterpreter` whose cache is separated
99
from the native code cache, satisfying the minimum interface requirements.
1010
"""
1111
macro newinterp(InterpName)
12-
InterpCacheName = QuoteNode(Symbol(string(InterpName, "Cache")))
12+
cache_token = QuoteNode(gensym(string(InterpName, "Cache")))
1313
InterpName = esc(InterpName)
1414
C = Core
1515
CC = Core.Compiler
@@ -32,6 +32,6 @@ macro newinterp(InterpName)
3232
$CC.OptimizationParams(interp::$InterpName) = interp.opt_params
3333
$CC.get_inference_world(interp::$InterpName) = interp.world
3434
$CC.get_inference_cache(interp::$InterpName) = interp.inf_cache
35-
$CC.cache_owner(::$InterpName) = $InterpCacheName
35+
$CC.cache_owner(::$InterpName) = $cache_token
3636
end
3737
end

test/precompile.jl

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,67 +1712,71 @@ precompile_test_harness("issue #46296") do load_path
17121712
(@eval (using CodeInstancePrecompile))
17131713
end
17141714

1715-
precompile_test_harness("AbstractInterpreter caching") do load_path
1716-
write(joinpath(load_path, "SimpleModule.jl"),
1717-
"""
1718-
module SimpleModule
1715+
let newinterp_path = abspath("compiler/newinterp.jl")
1716+
precompile_test_harness("AbstractInterpreter caching") do load_path
1717+
write(joinpath(load_path, "SimpleModule.jl"), :(module SimpleModule
17191718
basic_callee(x) = x
17201719
basic_caller(x) = basic_callee(x)
1721-
end
1722-
""")
1723-
write(joinpath(load_path, "CustomAbstractInterpreterCaching.jl"),
1724-
"""
1725-
module CustomAbstractInterpreterCaching
1720+
end) |> string)
1721+
1722+
write(joinpath(load_path, "CustomAbstractInterpreterCaching.jl"), :(module CustomAbstractInterpreterCaching
17261723
import SimpleModule: basic_caller, basic_callee
1724+
17271725
module Custom
17281726
const CC = Core.Compiler
1729-
1730-
struct InvalidationTesterToken end
1731-
1732-
struct InvalidationTester <: CC.AbstractInterpreter
1733-
world::UInt
1734-
inf_params::CC.InferenceParams
1735-
opt_params::CC.OptimizationParams
1736-
inf_cache::Vector{CC.InferenceResult}
1737-
function InvalidationTester(;
1738-
world::UInt = Base.get_world_counter(),
1739-
inf_params::CC.InferenceParams = CC.InferenceParams(),
1740-
opt_params::CC.OptimizationParams = CC.OptimizationParams(),
1741-
inf_cache::Vector{CC.InferenceResult} = CC.InferenceResult[])
1742-
return new(world, inf_params, opt_params, inf_cache)
1743-
end
1744-
end
1745-
1746-
CC.InferenceParams(interp::InvalidationTester) = interp.inf_params
1747-
CC.OptimizationParams(interp::InvalidationTester) = interp.opt_params
1748-
CC.get_inference_world(interp::InvalidationTester) = interp.world
1749-
CC.get_inference_cache(interp::InvalidationTester) = interp.inf_cache
1750-
CC.cache_owner(::InvalidationTester) = InvalidationTesterToken()
1727+
include("$($newinterp_path)")
1728+
@newinterp PrecompileInterpreter
17511729
end
17521730

17531731
Base.return_types((Float64,)) do x
17541732
basic_caller(x)
17551733
end
1756-
Base.return_types((Float64,); interp=Custom.InvalidationTester()) do x
1734+
Base.return_types((Float64,); interp=Custom.PrecompileInterpreter()) do x
17571735
basic_caller(x)
17581736
end
1737+
Base.return_types((Vector{Float64},)) do x
1738+
sum(x)
1739+
end
1740+
Base.return_types((Vector{Float64},); interp=Custom.PrecompileInterpreter()) do x
1741+
sum(x)
1742+
end
1743+
end) |> string)
1744+
Base.compilecache(Base.PkgId("CustomAbstractInterpreterCaching"))
1745+
@eval let
1746+
using CustomAbstractInterpreterCaching
1747+
cache_owner = Core.Compiler.cache_owner(
1748+
CustomAbstractInterpreterCaching.Custom.PrecompileInterpreter())
1749+
let m = only(methods(CustomAbstractInterpreterCaching.basic_callee))
1750+
mi = only(Base.specializations(m))
1751+
ci = mi.cache
1752+
@test isdefined(ci, :next)
1753+
@test ci.owner === nothing
1754+
@test ci.max_world == typemax(UInt)
1755+
ci = ci.next
1756+
@test !isdefined(ci, :next)
1757+
@test ci.owner === cache_owner
1758+
@test ci.max_world == typemax(UInt)
1759+
end
1760+
let m = only(methods(sum, (Vector{Float64},)))
1761+
found = false
1762+
for mi in Base.specializations(m)
1763+
if mi isa Core.MethodInstance && mi.specTypes == Tuple{typeof(sum),Vector{Float64}}
1764+
ci = mi.cache
1765+
@test isdefined(ci, :next)
1766+
@test ci.owner === cache_owner
1767+
@test ci.max_world == typemax(UInt)
1768+
ci = ci.next
1769+
@test !isdefined(ci, :next)
1770+
@test ci.owner === nothing
1771+
@test ci.max_world == typemax(UInt)
1772+
found = true
1773+
break
1774+
end
1775+
end
1776+
@test found
1777+
end
17591778
end
1760-
""")
1761-
Base.compilecache(Base.PkgId("CustomAbstractInterpreterCaching"))
1762-
(@eval begin
1763-
using CustomAbstractInterpreterCaching
1764-
let m = only(methods(CustomAbstractInterpreterCaching.basic_callee))
1765-
mi = only(Base.specializations(m))
1766-
ci = mi.cache
1767-
@test isdefined(ci, :next)
1768-
@test ci.owner === nothing
1769-
@test ci.max_world == typemax(UInt)
1770-
ci = ci.next
1771-
@test !isdefined(ci, :next)
1772-
@test ci.owner === CustomAbstractInterpreterCaching.Custom.InvalidationTesterToken()
1773-
@test ci.max_world == typemax(UInt)
1774-
end
1775-
end)
1779+
end
17761780
end
17771781

17781782
precompile_test_harness("Recursive types") do load_path

0 commit comments

Comments
 (0)