From 33b42b3f3350e2f61d7613d5481a390350ddfb20 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 14 Feb 2024 19:23:11 +0900 Subject: [PATCH] minor refactoring on the `CustomAbstractInterpreterCaching` test case By using `@newinterp`. --- test/compiler/newinterp.jl | 4 +- test/precompile.jl | 100 +++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/test/compiler/newinterp.jl b/test/compiler/newinterp.jl index 1157a52ebbcbe..74a2b29cabc28 100644 --- a/test/compiler/newinterp.jl +++ b/test/compiler/newinterp.jl @@ -9,7 +9,7 @@ Defines new `NewInterpreter <: AbstractInterpreter` whose cache is separated from the native code cache, satisfying the minimum interface requirements. """ macro newinterp(InterpName) - InterpCacheName = QuoteNode(Symbol(string(InterpName, "Cache"))) + cache_token = QuoteNode(gensym(string(InterpName, "Cache"))) InterpName = esc(InterpName) C = Core CC = Core.Compiler @@ -32,6 +32,6 @@ macro newinterp(InterpName) $CC.OptimizationParams(interp::$InterpName) = interp.opt_params $CC.get_inference_world(interp::$InterpName) = interp.world $CC.get_inference_cache(interp::$InterpName) = interp.inf_cache - $CC.cache_owner(::$InterpName) = $InterpCacheName + $CC.cache_owner(::$InterpName) = $cache_token end end diff --git a/test/precompile.jl b/test/precompile.jl index 129f909dcf6e2..c4cceced8235b 100644 --- a/test/precompile.jl +++ b/test/precompile.jl @@ -1712,67 +1712,71 @@ precompile_test_harness("issue #46296") do load_path (@eval (using CodeInstancePrecompile)) end -precompile_test_harness("AbstractInterpreter caching") do load_path - write(joinpath(load_path, "SimpleModule.jl"), - """ - module SimpleModule +let newinterp_path = abspath("compiler/newinterp.jl") + precompile_test_harness("AbstractInterpreter caching") do load_path + write(joinpath(load_path, "SimpleModule.jl"), :(module SimpleModule basic_callee(x) = x basic_caller(x) = basic_callee(x) - end - """) - write(joinpath(load_path, "CustomAbstractInterpreterCaching.jl"), - """ - module CustomAbstractInterpreterCaching + end) |> string) + + write(joinpath(load_path, "CustomAbstractInterpreterCaching.jl"), :(module CustomAbstractInterpreterCaching import SimpleModule: basic_caller, basic_callee + module Custom const CC = Core.Compiler - - struct InvalidationTesterToken end - - struct InvalidationTester <: CC.AbstractInterpreter - world::UInt - inf_params::CC.InferenceParams - opt_params::CC.OptimizationParams - inf_cache::Vector{CC.InferenceResult} - function InvalidationTester(; - world::UInt = Base.get_world_counter(), - inf_params::CC.InferenceParams = CC.InferenceParams(), - opt_params::CC.OptimizationParams = CC.OptimizationParams(), - inf_cache::Vector{CC.InferenceResult} = CC.InferenceResult[]) - return new(world, inf_params, opt_params, inf_cache) - end - end - - CC.InferenceParams(interp::InvalidationTester) = interp.inf_params - CC.OptimizationParams(interp::InvalidationTester) = interp.opt_params - CC.get_inference_world(interp::InvalidationTester) = interp.world - CC.get_inference_cache(interp::InvalidationTester) = interp.inf_cache - CC.cache_owner(::InvalidationTester) = InvalidationTesterToken() + include("$($newinterp_path)") + @newinterp PrecompileInterpreter end Base.return_types((Float64,)) do x basic_caller(x) end - Base.return_types((Float64,); interp=Custom.InvalidationTester()) do x + Base.return_types((Float64,); interp=Custom.PrecompileInterpreter()) do x basic_caller(x) end + Base.return_types((Vector{Float64},)) do x + sum(x) + end + Base.return_types((Vector{Float64},); interp=Custom.PrecompileInterpreter()) do x + sum(x) + end + end) |> string) + Base.compilecache(Base.PkgId("CustomAbstractInterpreterCaching")) + @eval let + using CustomAbstractInterpreterCaching + cache_owner = Core.Compiler.cache_owner( + CustomAbstractInterpreterCaching.Custom.PrecompileInterpreter()) + let m = only(methods(CustomAbstractInterpreterCaching.basic_callee)) + mi = only(Base.specializations(m)) + ci = mi.cache + @test isdefined(ci, :next) + @test ci.owner === nothing + @test ci.max_world == typemax(UInt) + ci = ci.next + @test !isdefined(ci, :next) + @test ci.owner === cache_owner + @test ci.max_world == typemax(UInt) + end + let m = only(methods(sum, (Vector{Float64},))) + found = false + for mi in Base.specializations(m) + if mi isa Core.MethodInstance && mi.specTypes == Tuple{typeof(sum),Vector{Float64}} + ci = mi.cache + @test isdefined(ci, :next) + @test ci.owner === cache_owner + @test ci.max_world == typemax(UInt) + ci = ci.next + @test !isdefined(ci, :next) + @test ci.owner === nothing + @test ci.max_world == typemax(UInt) + found = true + break + end + end + @test found + end end - """) - Base.compilecache(Base.PkgId("CustomAbstractInterpreterCaching")) - (@eval begin - using CustomAbstractInterpreterCaching - let m = only(methods(CustomAbstractInterpreterCaching.basic_callee)) - mi = only(Base.specializations(m)) - ci = mi.cache - @test isdefined(ci, :next) - @test ci.owner === nothing - @test ci.max_world == typemax(UInt) - ci = ci.next - @test !isdefined(ci, :next) - @test ci.owner === CustomAbstractInterpreterCaching.Custom.InvalidationTesterToken() - @test ci.max_world == typemax(UInt) - end - end) + end end precompile_test_harness("Recursive types") do load_path