Skip to content

Commit a0d1302

Browse files
authored
PTX: Use LLVM 22 back-end from JLL. (#801)
1 parent 5e4c84f commit a0d1302

5 files changed

Lines changed: 66 additions & 3 deletions

File tree

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GPUCompiler"
22
uuid = "61eb1bfa-7361-4325-ad38-22787b887f55"
3-
version = "1.17.0"
3+
version = "1.18.0"
44
authors = ["Tim Besard <tim.besard@gmail.com>"]
55

66
[workspace]
@@ -22,6 +22,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
2222

2323
[weakdeps]
2424
LLVMDowngrader_jll = "f52de702-fb25-5922-94ba-81dd59b07444"
25+
NVPTX_LLVM_Backend_jll = "ef6e0fe3-e6ef-59c0-bde6-4989574699e0"
2526

2627
[compat]
2728
ExprTools = "0.1"
@@ -30,6 +31,7 @@ LLVM = "9.8.1"
3031
LLVMDowngrader_jll = "0.7"
3132
Libdl = "1"
3233
Logging = "1"
34+
NVPTX_LLVM_Backend_jll = "22"
3335
PrecompileTools = "1"
3436
Preferences = "1"
3537
Scratch = "1"

src/ptx.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# implementation of the GPUCompiler interfaces for generating PTX code
22

3+
const NVPTX_LLVM_Backend_jll =
4+
LazyModule("NVPTX_LLVM_Backend_jll",
5+
UUID("ef6e0fe3-e6ef-59c0-bde6-4989574699e0"))
6+
7+
38
## target
49

510
export PTXCompilerTarget
@@ -341,6 +346,44 @@ function finish_ir!(@nospecialize(job::CompilerJob{PTXCompilerTarget}),
341346
return entry
342347
end
343348

349+
@unlocked function mcgen(@nospecialize(job::CompilerJob{PTXCompilerTarget}),
350+
mod::LLVM.Module, format=LLVM.API.LLVMAssemblyFile)
351+
if !isavailable(NVPTX_LLVM_Backend_jll) || !NVPTX_LLVM_Backend_jll.is_available()
352+
error("NVPTX LLVM back-end not loaded; cannot compile to PTX.")
353+
end
354+
355+
target = job.config.target
356+
filetype = if format == LLVM.API.LLVMAssemblyFile
357+
"asm"
358+
elseif format == LLVM.API.LLVMObjectFile
359+
"obj"
360+
else
361+
error("Unsupported PTX output format $format")
362+
end
363+
364+
input = tempname(cleanup=false) * ".bc"
365+
output = tempname(cleanup=false) * (filetype == "asm" ? ".ptx" : ".cubin")
366+
write(input, mod)
367+
368+
cmd = `$(NVPTX_LLVM_Backend_jll.llc()) $input
369+
-mtriple=$(llvm_triple(target))
370+
-mcpu=$(cpu_name(target))
371+
-mattr=+ptx$(target.ptx.major)$(target.ptx.minor)
372+
-filetype=$filetype
373+
-o $output`
374+
try
375+
run(cmd)
376+
catch
377+
error("""Failed to compile to PTX with external llc.
378+
If you think this is a bug, please file an issue and attach $(input).""")
379+
end
380+
381+
code = filetype == "asm" ? read(output, String) : String(read(output))
382+
rm(input)
383+
rm(output)
384+
return code
385+
end
386+
344387
function llvm_debug_info(@nospecialize(job::CompilerJob{PTXCompilerTarget}))
345388
# allow overriding the debug info from CUDA.jl
346389
if job.config.target.debuginfo

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
77
LLVMDowngrader_jll = "f52de702-fb25-5922-94ba-81dd59b07444"
88
LLVM_jll = "86de99a1-58d6-5da7-8064-bd56ce2e322c"
99
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
10+
NVPTX_LLVM_Backend_jll = "ef6e0fe3-e6ef-59c0-bde6-4989574699e0"
1011
ParallelTestRunner = "d3525ed8-44d0-4b2c-a655-542cee43accc"
1112
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1213
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

test/ptx.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ if :NVPTX in LLVM.backends()
139139
@test @filecheck begin
140140
@check_label ".visible .func {{(julia|j)_parent[0-9_]*}}"
141141
@check "call.uni"
142-
@check_same cond=(LLVM.version() >= v"21") "{{(julia|j)_child_}}"
143-
@check_next cond=(LLVM.version() < v"21") "{{(julia|j)_child_}}"
142+
@check_same "{{(julia|j)_child_}}"
144143
PTX.code_native(mod.parent, Tuple{Int64})
145144
end
146145
end

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using ParallelTestRunner
22
import GPUCompiler, LLVM
3+
using GPUCompiler, LLVM
4+
using SPIRV_LLVM_Backend_jll, SPIRV_LLVM_Translator_jll, SPIRV_Tools_jll
5+
using NVPTX_LLVM_Backend_jll
36

47
const init_code = quote
58
using GPUCompiler, LLVM
69
using SPIRV_LLVM_Backend_jll, SPIRV_LLVM_Translator_jll, SPIRV_Tools_jll
710
using LLVMDowngrader_jll
11+
using NVPTX_LLVM_Backend_jll
812

913
# include all helpers
1014
include(joinpath(@__DIR__, "helpers", "runtime.jl"))
@@ -29,12 +33,26 @@ if filter_tests!(testsuite, args)
2933
end
3034

3135
if LLVM.is_asserts()
36+
@warn "LLVM with assertions; skipping GCN tests"
3237
delete!(testsuite, "gcn")
3338
end
3439
if VERSION < v"1.11"
40+
@warn "Julia 1.11+ required for precompile tests; skipping"
3541
delete!(testsuite, "ptx/precompile")
3642
delete!(testsuite, "native/precompile")
3743
end
44+
if !SPIRV_LLVM_Backend_jll.is_available() || !SPIRV_LLVM_Translator_jll.is_available() || !SPIRV_Tools_jll.is_available()
45+
@warn "SPIRV back-end not available; skipping SPIRV tests"
46+
for key in collect(keys(testsuite))
47+
startswith(key, "spirv") && delete!(testsuite, key)
48+
end
49+
end
50+
if !NVPTX_LLVM_Backend_jll.is_available()
51+
@warn "NVPTX back-end not available; skipping PTX tests"
52+
for key in collect(keys(testsuite))
53+
startswith(key, "ptx") && delete!(testsuite, key)
54+
end
55+
end
3856
end
3957

4058
runtests(GPUCompiler, args; testsuite, init_code)

0 commit comments

Comments
 (0)