4
4
# https://github.com/KhronosGroup/LLVM-SPIRV-Backend/blob/master/llvm/docs/SPIR-V-Backend.rst
5
5
# https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/master/docs/SPIRVRepresentationInLLVM.rst
6
6
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" ))
9
16
10
17
11
18
# # target
12
19
13
20
export SPIRVCompilerTarget
14
21
15
22
Base. @kwdef struct SPIRVCompilerTarget <: AbstractCompilerTarget
23
+ version:: Union{Nothing,VersionNumber} = nothing
16
24
extensions:: Vector{String} = []
17
25
supports_fp16:: Bool = true
18
26
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
19
32
end
20
33
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
22
46
23
47
# SPIRV is not supported by our LLVM builds, so we can't get a target machine
24
48
llvm_machine (:: SPIRVCompilerTarget ) = nothing
@@ -32,7 +56,8 @@ llvm_datalayout(::SPIRVCompilerTarget) = Int===Int64 ?
32
56
33
57
# TODO : encode debug build or not in the compiler job
34
58
# 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)
36
61
37
62
function finish_module! (job:: CompilerJob{SPIRVCompilerTarget} , mod:: LLVM.Module , entry:: LLVM.Function )
38
63
# update calling convention
90
115
# (SPIRV-LLVM-Translator#1140)
91
116
rm_freeze! (mod)
92
117
93
-
94
118
# translate to SPIR-V
95
119
input = tempname (cleanup= false ) * " .bc"
96
120
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
102
121
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) `
108
139
end
109
140
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
110
146
111
147
# 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
122
156
123
157
# optimize
124
- # XXX : parameterize this on the `optimize` driver argument
125
- # XXX : the optimizer segfaults on some of our code
126
158
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
134
169
135
170
output = if format == LLVM. API. LLVMObjectFile
136
171
read (translated)
141
176
142
177
rm (input)
143
178
rm (translated)
144
- # rm(optimized)
179
+ rm (optimized)
145
180
146
181
return output
147
182
end
0 commit comments