Skip to content

Commit 46edf96

Browse files
authored
Add a testsuite for inference caching (#567)
1 parent cb736ee commit 46edf96

7 files changed

+203
-2
lines changed

src/jlgen.jl

+11
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,14 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
749749

750750
return llvm_mod, compiled
751751
end
752+
753+
function CC.typeinf(interp::GPUInterpreter, frame::CC.InferenceState)
754+
if CC.__measure_typeinf__[]
755+
CC.Timings.enter_new_timer(frame)
756+
v = CC._typeinf(interp, frame)
757+
CC.Timings.exit_current_timer(frame)
758+
return v
759+
else
760+
return CC._typeinf(interp, frame)
761+
end
762+
end

test/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
44
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
5+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
56
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
67
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
78
SPIRV_LLVM_Translator_unified_jll = "85f0d8ed-5b39-5caa-b1ae-7472de402361"

test/native_tests.jl

+59
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,65 @@ end
535535
["jl_invoke", "apply_iterate",
536536
"inttoptr", "apply_type"]) broken=VERSION>=v"1.11.0-DEV.392"
537537
end
538+
end # testitem
539+
540+
@testitem "native precompile" setup=[Precompile,] begin
541+
542+
using Test
543+
precompile_test_harness("Inference caching") do load_path
544+
# Write out the Native test setup as a micro package
545+
create_standalone(load_path, "NativeCompiler", "native_testsetup.jl")
546+
547+
write(joinpath(load_path, "InferenceCaching.jl"), :(module InferenceCaching
548+
import NativeCompiler
549+
import GPUCompiler
550+
using PrecompileTools
551+
552+
function kernel()
553+
return
554+
end
555+
556+
let
557+
job, _ = NativeCompiler.create_job(kernel, ())
558+
GPUCompiler.code_typed(job)
559+
end
560+
561+
# identity is foreign
562+
@setup_workload begin
563+
job, _ = NativeCompiler.create_job(identity, (Int,))
564+
@compile_workload begin
565+
GPUCompiler.code_typed(job)
566+
end
567+
end
568+
end) |> string)
569+
570+
Base.compilecache(Base.PkgId("InferenceCaching"))
571+
@eval let
572+
import NativeCompiler
573+
574+
# Check that no cached entry is present
575+
identity_mi = GPUCompiler.methodinstance(typeof(identity), Tuple{Int})
576+
577+
token = let
578+
job, _ = NativeCompiler.create_job(identity, (Int,))
579+
GPUCompiler.ci_cache_token(job)
580+
end
581+
ci = isdefined(identity_mi, :cache) ? identity_mi.cache : nothing
582+
while ci !== nothing
583+
@test ci.owner !== token
584+
ci = isdefined(ci, :next) ? ci.next : nothing
585+
end
586+
587+
using InferenceCaching
588+
589+
# Check that kernel survived
590+
kernel_mi = GPUCompiler.methodinstance(typeof(InferenceCaching.kernel), Tuple{})
591+
@test check_presence(kernel_mi, token)
592+
593+
# check that identity survived
594+
@test check_presence(identity_mi, token)
595+
end
596+
end
538597

539598
############################################################################################
540599

test/precompile_testsetup.jl

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
@testsetup module Precompile
2+
3+
using Test
4+
using ReTestItems
5+
6+
export precompile_test_harness, check_presence, create_standalone
7+
8+
function precompile_test_harness(@nospecialize(f), testset::String)
9+
@testset "$testset" begin
10+
precompile_test_harness(f, true)
11+
end
12+
end
13+
function precompile_test_harness(@nospecialize(f), separate::Bool)
14+
load_path = mktempdir()
15+
load_cache_path = separate ? mktempdir() : load_path
16+
try
17+
pushfirst!(LOAD_PATH, load_path)
18+
pushfirst!(DEPOT_PATH, load_cache_path)
19+
f(load_path)
20+
finally
21+
try
22+
rm(load_path, force=true, recursive=true)
23+
catch err
24+
@show err
25+
end
26+
if separate
27+
try
28+
rm(load_cache_path, force=true, recursive=true)
29+
catch err
30+
@show err
31+
end
32+
end
33+
filter!(()(load_path), LOAD_PATH)
34+
separate && filter!(()(load_cache_path), DEPOT_PATH)
35+
end
36+
nothing
37+
end
38+
39+
function check_presence(mi, token)
40+
found = false
41+
ci = isdefined(mi, :cache) ? mi.cache : nothing
42+
while ci !== nothing
43+
if ci.owner === token && ci.max_world == typemax(UInt)
44+
found = true
45+
break
46+
end
47+
ci = isdefined(ci, :next) ? ci.next : nothing
48+
end
49+
return found
50+
end
51+
52+
function create_standalone(load_path, name::String, file)
53+
cp(joinpath(@__DIR__, "runtime.jl"), joinpath(load_path, "runtime.jl"), force=true)
54+
55+
TS = include(file)
56+
code = TS.code
57+
if code.head == :begin
58+
code.head = :block
59+
end
60+
@assert code.head == :block
61+
code = Expr(:module, true, Symbol(name), code)
62+
63+
# Write out the test setup as a micro package
64+
write(joinpath(load_path, "$name.jl"), string(code))
65+
Base.compilecache(Base.PkgId(name))
66+
end
67+
68+
end # testsetup

test/ptx_tests.jl

+56
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,63 @@ end
321321
end
322322

323323
end
324+
end # testitem
324325

326+
@testitem "PTX precompile" setup=[Precompile,] begin
327+
precompile_test_harness("Inference caching") do load_path
328+
# Write out the PTX test setup as a micro package
329+
create_standalone(load_path, "PTXCompiler", "ptx_testsetup.jl")
330+
331+
write(joinpath(load_path, "InferenceCaching.jl"), :(module InferenceCaching
332+
import PTXCompiler
333+
import GPUCompiler
334+
using PrecompileTools
335+
336+
function kernel()
337+
return
338+
end
339+
340+
let
341+
job, _ = PTXCompiler.create_job(kernel, ())
342+
GPUCompiler.code_typed(job)
343+
end
344+
345+
# identity is foreign
346+
@setup_workload begin
347+
job, _ = PTXCompiler.create_job(identity, (Int,))
348+
@compile_workload begin
349+
GPUCompiler.code_typed(job)
350+
end
351+
end
352+
end) |> string)
353+
354+
Base.compilecache(Base.PkgId("InferenceCaching"))
355+
@eval let
356+
import PTXCompiler
357+
358+
# Check that no cached entry is present
359+
identity_mi = GPUCompiler.methodinstance(typeof(identity), Tuple{Int})
360+
361+
token = let
362+
job, _ = PTXCompiler.create_job(identity, (Int,))
363+
GPUCompiler.ci_cache_token(job)
364+
end
365+
ci = isdefined(identity_mi, :cache) ? identity_mi.cache : nothing
366+
while ci !== nothing
367+
@test ci.owner !== token
368+
ci = isdefined(ci, :next) ? ci.next : nothing
369+
end
370+
371+
using InferenceCaching
372+
373+
# Check that kernel survived
374+
kernel_mi = GPUCompiler.methodinstance(typeof(InferenceCaching.kernel), Tuple{})
375+
@test check_presence(kernel_mi, token)
376+
377+
# check that identity survived
378+
@test check_presence(identity_mi, token)
379+
end
380+
end
325381

326382
############################################################################################
327383

test/ptx_testsetup.jl

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ include("runtime.jl")
99
struct CompilerParams <: AbstractCompilerParams end
1010

1111
PTXCompilerJob = CompilerJob{PTXCompilerTarget,CompilerParams}
12-
GPUCompiler.runtime_module(::PTXCompilerJob) = TestRuntime
1312

1413
struct PTXKernelState
1514
data::Int64

test/runtests.jl

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ runtests(GPUCompiler; nworkers=min(Sys.CPU_THREADS,4), nworker_threads=1,
2222
end
2323
end
2424

25-
if ti.name in ["PTX", "GCN"] && Sys.isapple() && VERSION >= v"1.10-"
25+
if ti.name in ["PTX", "GCN", "PTX precompile"] && Sys.isapple() && VERSION >= v"1.10-"
2626
# support for AMDGPU and NVTX on macOS has been removed from Julia's LLVM build
2727
return false
2828
end
2929

30+
3031
if ti.name in ["SPIRV"] && !(SPIRV_LLVM_Translator_unified_jll.is_available() && SPIRV_Tools_jll.is_available())
3132
# SPIRV needs it's tools to be available
3233
return false
3334
end
35+
36+
if ti.name in ["PTX precompile", "native precompile"] && VERSION < v"1.11-"
37+
# precompile needs v1.11
38+
return false
39+
end
40+
3441
true
3542
end

0 commit comments

Comments
 (0)