44# https://github.com/KhronosGroup/LLVM-SPIRV-Backend/blob/master/llvm/docs/SPIR-V-Backend.rst
55# https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/master/docs/SPIRVRepresentationInLLVM.rst
66
7- const SPIRV_LLVM_Translator_unified_jll = LazyModule (" SPIRV_LLVM_Translator_unified_jll" , UUID (" 85f0d8ed-5b39-5caa-b1ae-7472de402361" ))
8- const SPIRV_Tools_jll = LazyModule (" SPIRV_Tools_jll" , UUID (" 6ac6d60f-d740-5983-97d7-a4482c0689f4" ))
7+ const SPIRV_LLVM_Backend_jll =
8+ LazyModule (" SPIRV_LLVM_Backend_jll" ,
9+ UUID (" 4376b9bf-cff8-51b6-bb48-39421dff0d0c" ))
10+ const SPIRV_LLVM_Translator_unified_jll =
11+ LazyModule (" SPIRV_LLVM_Translator_unified_jll" ,
12+ UUID (" 85f0d8ed-5b39-5caa-b1ae-7472de402361" ))
13+ const SPIRV_Tools_jll =
14+ LazyModule (" SPIRV_Tools_jll" ,
15+ UUID (" 6ac6d60f-d740-5983-97d7-a4482c0689f4" ))
916
1017
1118# # target
1219
1320export SPIRVCompilerTarget
1421
1522Base. @kwdef struct SPIRVCompilerTarget <: AbstractCompilerTarget
23+ version:: Union{Nothing,VersionNumber} = nothing
1624 extensions:: Vector{String} = []
1725 supports_fp16:: Bool = true
1826 supports_fp64:: Bool = true
27+
28+ backend:: Symbol = isavailable (SPIRV_LLVM_Backend_jll) ? :llvm : :khronos
29+ # XXX : these don't really belong in the _target_ struct
30+ validate:: Bool = false
31+ optimize:: Bool = false
1932end
2033
21- llvm_triple (:: SPIRVCompilerTarget ) = Int=== Int64 ? " spir64-unknown-unknown" : " spirv-unknown-unknown"
34+ function llvm_triple (target:: SPIRVCompilerTarget )
35+ if target. backend == :llvm
36+ architecture = Int=== Int64 ? " spirv64" : " spirv32" # could also be "spirv" for logical addressing
37+ subarchitecture = target. version === nothing ? " " : " v$(target. version. major) .$(target. version. minor) "
38+ vendor = " unknown" # could also be AMD
39+ os = " unknown"
40+ environment = " unknown"
41+ return " $architecture$subarchitecture -$vendor -$os -$environment "
42+ elseif target. backend == :khronos
43+ return Int=== Int64 ? " spir64-unknown-unknown" : " spirv-unknown-unknown"
44+ end
45+ end
2246
2347# SPIRV is not supported by our LLVM builds, so we can't get a target machine
2448llvm_machine (:: SPIRVCompilerTarget ) = nothing
@@ -32,7 +56,8 @@ llvm_datalayout(::SPIRVCompilerTarget) = Int===Int64 ?
3256
3357# TODO : encode debug build or not in the compiler job
3458# https://github.com/JuliaGPU/CUDAnative.jl/issues/368
35- runtime_slug (job:: CompilerJob{SPIRVCompilerTarget} ) = " spirv"
59+ runtime_slug (job:: CompilerJob{SPIRVCompilerTarget} ) =
60+ " spirv-" * String (job. config. target. backend)
3661
3762function finish_module! (job:: CompilerJob{SPIRVCompilerTarget} , mod:: LLVM.Module , entry:: LLVM.Function )
3863 # update calling convention
90115 # (SPIRV-LLVM-Translator#1140)
91116 rm_freeze! (mod)
92117
93-
94118 # translate to SPIR-V
95119 input = tempname (cleanup= false ) * " .bc"
96120 translated = tempname (cleanup= false ) * " .spv"
97- options = ` --spirv-debug-info-version=ocl-100`
98- if ! isempty (job. config. target. extensions)
99- str = join (map (ext-> " +$ext " , job. config. target. extensions), " ," )
100- options = ` $options --spirv-ext=$str `
101- end
102121 write (input, mod)
103- let cmd = ` $(SPIRV_LLVM_Translator_unified_jll. llvm_spirv ()) $options -o $translated $input `
104- proc = run (ignorestatus (cmd))
105- if ! success (proc)
106- error (""" Failed to translate LLVM code to SPIR-V.
107- If you think this is a bug, please file an issue and attach $(input) .""" )
122+ if job. config. target. backend === :llvm
123+ cmd = ` $(SPIRV_LLVM_Backend_jll. llc ()) $input -filetype=obj -o $translated `
124+
125+ if ! isempty (job. config. target. extensions)
126+ str = join (map (ext-> " +$ext " , job. config. target. extensions), " ," )
127+ cmd = ` $(cmd) -spirv-ext=$str `
128+ end
129+ elseif job. config. target. backend === :khronos
130+ cmd = ` $(SPIRV_LLVM_Translator_unified_jll. llvm_spirv ()) -o $translated $input --spirv-debug-info-version=ocl-100`
131+
132+ if ! isempty (job. config. target. extensions)
133+ str = join (map (ext-> " +$ext " , job. config. target. extensions), " ," )
134+ cmd = ` $(cmd) --spirv-ext=$str `
135+ end
136+
137+ if job. config. target. version != = nothing
138+ cmd = ` $(cmd) --spirv-max-version=$(job. config. target. version. major) .$(job. config. target. version. minor) `
108139 end
109140 end
141+ proc = run (ignorestatus (cmd))
142+ if ! success (proc)
143+ error (""" Failed to translate LLVM code to SPIR-V.
144+ If you think this is a bug, please file an issue and attach $(input) .""" )
145+ end
110146
111147 # validate
112- # XXX : parameterize this on the `validate` driver argument
113- # XXX : our code currently doesn't pass the validator
114- # if Base.JLOptions().debug_level >= 2
115- # cmd = `$(SPIRV_Tools_jll.spirv_val()) $translated`
116- # proc = run(ignorestatus(cmd))
117- # if !success(proc)
118- # error("""Failed to validate generated SPIR-V.
119- # If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
120- # end
121- # end
148+ if job. config. target. validate
149+ cmd = ` $(SPIRV_Tools_jll. spirv_val ()) $translated `
150+ proc = run (ignorestatus (cmd))
151+ if ! success (proc)
152+ error (""" Failed to validate generated SPIR-V.
153+ If you think this is a bug, please file an issue and attach $(input) and $(translated) .""" )
154+ end
155+ end
122156
123157 # optimize
124- # XXX : parameterize this on the `optimize` driver argument
125- # XXX : the optimizer segfaults on some of our code
126158 optimized = tempname (cleanup= false ) * " .spv"
127- # let cmd = `$(SPIRV_Tools_jll.spirv_opt()) -O --skip-validation $translated -o $optimized`
128- # proc = run(ignorestatus(cmd))
129- # if !success(proc)
130- # error("""Failed to optimize generated SPIR-V.
131- # If you think this is a bug, please file an issue and attach $(input) and $(translated).""")
132- # end
133- # end
159+ if job. config. target. optimize
160+ cmd = ` $(SPIRV_Tools_jll. spirv_opt ()) -O --skip-validation $translated -o $optimized `
161+ proc = run (ignorestatus (cmd))
162+ if ! success (proc)
163+ error (""" Failed to optimize generated SPIR-V.
164+ If you think this is a bug, please file an issue and attach $(input) and $(translated) .""" )
165+ end
166+ else
167+ cp (translated, optimized)
168+ end
134169
135170 output = if format == LLVM. API. LLVMObjectFile
136171 read (translated)
141176
142177 rm (input)
143178 rm (translated)
144- # rm(optimized)
179+ rm (optimized)
145180
146181 return output
147182end
0 commit comments