Skip to content

Commit 919d293

Browse files
authored
[clang][SPIR-V] Use the SPIR-V backend by default (#129545)
The SPIR-V backend is now a supported backend, and we believe it is ready to be used by default in Clang over the SPIR-V translator. Some IR generated by Clang today, such as those requiring SPIR-V target address spaces, cannot be compiled by the translator for reasons in this [RFC](https://discourse.llvm.org/t/rfc-the-spir-v-backend-should-change-its-address-space-mappings/82640), so we expect even more programs to work as well. Enable it by default, but keep some of the code as it is still called by the HIP toolchain directly. --------- Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
1 parent a619f31 commit 919d293

File tree

5 files changed

+24
-72
lines changed

5 files changed

+24
-72
lines changed

clang/docs/UsersManual.rst

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,25 +4681,7 @@ Clang supports generation of SPIR-V conformant to `the OpenCL Environment
46814681
Specification
46824682
<https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html>`_.
46834683

4684-
To generate SPIR-V binaries, Clang uses the external ``llvm-spirv`` tool from the
4685-
`SPIRV-LLVM-Translator repo
4686-
<https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_.
4687-
4688-
Prior to the generation of SPIR-V binary with Clang, ``llvm-spirv``
4689-
should be built or installed. Please refer to `the following instructions
4690-
<https://github.com/KhronosGroup/SPIRV-LLVM-Translator#build-instructions>`_
4691-
for more details. Clang will look for ``llvm-spirv-<LLVM-major-version>`` and
4692-
``llvm-spirv`` executables, in this order, in the ``PATH`` environment variable.
4693-
Clang uses ``llvm-spirv`` with `the widely adopted assembly syntax package
4694-
<https://github.com/KhronosGroup/SPIRV-LLVM-Translator/#build-with-spirv-tools>`_.
4695-
4696-
`The versioning
4697-
<https://github.com/KhronosGroup/SPIRV-LLVM-Translator/releases>`_ of
4698-
``llvm-spirv`` is aligned with Clang major releases. The same applies to the
4699-
main development branch. It is therefore important to ensure the ``llvm-spirv``
4700-
version is in alignment with the Clang version. For troubleshooting purposes
4701-
``llvm-spirv`` can be `tested in isolation
4702-
<https://github.com/KhronosGroup/SPIRV-LLVM-Translator#test-instructions>`_.
4684+
To generate SPIR-V binaries, Clang uses the in-tree LLVM SPIR-V backend.
47034685

47044686
Example usage for OpenCL kernel compilation:
47054687

@@ -4717,18 +4699,6 @@ Converting to SPIR-V produced with the optimization levels other than `-O0` is
47174699
currently available as an experimental feature and it is not guaranteed to work
47184700
in all cases.
47194701

4720-
Clang also supports integrated generation of SPIR-V without use of ``llvm-spirv``
4721-
tool as an experimental feature when ``-fintegrated-objemitter`` flag is passed in
4722-
the command line.
4723-
4724-
.. code-block:: console
4725-
4726-
$ clang --target=spirv32 -fintegrated-objemitter -c test.cl
4727-
4728-
Note that only very basic functionality is supported at this point and therefore
4729-
it is not suitable for arbitrary use cases. This feature is only enabled when clang
4730-
build is configured with ``-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=SPIRV`` option.
4731-
47324702
Linking is done using ``spirv-link`` from `the SPIRV-Tools project
47334703
<https://github.com/KhronosGroup/SPIRV-Tools#linker>`_. Similar to other external
47344704
linkers, Clang will expect ``spirv-link`` to be installed separately and to be

clang/lib/Driver/ToolChains/SPIRV.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ void SPIRV::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
9393
constructAssembleCommand(C, *this, JA, Output, Inputs[0], {});
9494
}
9595

96-
clang::driver::Tool *SPIRVToolChain::getTranslator() const {
97-
if (!Translator)
98-
Translator = std::make_unique<SPIRV::Translator>(*this);
99-
return Translator.get();
100-
}
101-
10296
clang::driver::Tool *SPIRVToolChain::getAssembler() const {
10397
if (!Assembler)
10498
Assembler = std::make_unique<SPIRV::Assembler>(*this);
@@ -114,8 +108,6 @@ clang::driver::Tool *SPIRVToolChain::getTool(Action::ActionClass AC) const {
114108
switch (AC) {
115109
default:
116110
break;
117-
case Action::BackendJobClass:
118-
return SPIRVToolChain::getTranslator();
119111
case Action::AssembleJobClass:
120112
return SPIRVToolChain::getAssembler();
121113
}

clang/lib/Driver/ToolChains/SPIRV.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class LLVM_LIBRARY_VISIBILITY Assembler final : public Tool {
6969
namespace toolchains {
7070

7171
class LLVM_LIBRARY_VISIBILITY SPIRVToolChain : public ToolChain {
72-
mutable std::unique_ptr<Tool> Translator;
7372
mutable std::unique_ptr<Tool> Assembler;
7473

7574
public:
@@ -78,7 +77,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRVToolChain : public ToolChain {
7877

7978
bool useIntegratedAs() const override { return true; }
8079

81-
bool IsIntegratedBackendDefault() const override { return false; }
80+
bool IsIntegratedBackendDefault() const override { return true; }
8281
bool IsNonIntegratedBackendSupported() const override { return true; }
8382
bool IsMathErrnoDefault() const override { return false; }
8483
bool isCrossCompiling() const override { return true; }
@@ -97,7 +96,6 @@ class LLVM_LIBRARY_VISIBILITY SPIRVToolChain : public ToolChain {
9796
Tool *buildLinker() const override;
9897

9998
private:
100-
clang::driver::Tool *getTranslator() const;
10199
clang::driver::Tool *getAssembler() const;
102100

103101
bool NativeLLVMSupport;

clang/test/Driver/spirv-openmp-toolchain.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
// verify the tools invocations
66
// CHECK: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-emit-llvm-bc"{{.*}}"-x" "c"
7-
// CHECK: "-cc1" "-triple" "spirv64-intel" "-aux-triple" "x86_64-unknown-linux-gnu"
8-
// CHECK: llvm-spirv{{.*}}
7+
// CHECK: "-cc1" "-triple" "spirv64-intel" "-aux-triple" "x86_64-unknown-linux-gnu"{{.*}} "-o" "{{.*}}.o"
98
// CHECK: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-emit-obj"
109
// CHECK: clang-linker-wrapper{{.*}} "-o" "a.out"
1110

@@ -32,8 +31,7 @@
3231
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-bindings -fopenmp=libomp -fopenmp-targets=spirv64-intel -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-BINDINGS
3332

3433
// CHECK-BINDINGS: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HOST_BC:.+]]"
35-
// CHECK-BINDINGS: "spirv64-intel" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[DEVICE_TEMP_BC:.+]]"
36-
// CHECK-BINDINGS: "spirv64-intel" - "SPIR-V::Translator", inputs: ["[[DEVICE_TEMP_BC]]"], output: "[[DEVICE_SPV:.+]]"
34+
// CHECK-BINDINGS: "spirv64-intel" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[DEVICE_SPV:.+]]"
3735
// CHECK-BINDINGS: "x86_64-unknown-linux-gnu" - "Offload::Packager", inputs: ["[[DEVICE_SPV]]"], output: "[[DEVICE_IMAGE:.+]]"
3836
// CHECK-BINDINGS: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HOST_BC]]", "[[DEVICE_IMAGE]]"], output: "[[HOST_OBJ:.+]]"
3937
// CHECK-BINDINGS: "x86_64-unknown-linux-gnu" - "Offload::Linker", inputs: ["[[HOST_OBJ]]"], output: "a.out"
@@ -43,8 +41,8 @@
4341
// CHECK-BINDINGS-TEMPS: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HOST_PP:.+]]"
4442
// CHECK-BINDINGS-TEMPS: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HOST_PP]]"], output: "[[HOST_BC:.+]]"
4543
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "clang", inputs: ["[[INPUT]]"], output: "[[DEVICE_PP:.+]]"
46-
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "clang", inputs: ["[[DEVICE_PP]]", "[[HOST_BC]]"], output: "[[DEVICE_TEMP_BC:.+]]"
47-
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "SPIR-V::Translator", inputs: ["[[DEVICE_TEMP_BC]]"], output: "[[DEVICE_ASM:.+]]"
44+
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "clang", inputs: ["[[DEVICE_PP]]", "[[HOST_BC]]"], output: "[[DEVICE_BC:.+]]"
45+
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "clang", inputs: ["[[DEVICE_BC]]"], output: "[[DEVICE_ASM:.+]]"
4846
// CHECK-BINDINGS-TEMPS: "spirv64-intel" - "SPIR-V::Assembler", inputs: ["[[DEVICE_ASM]]"], output: "[[DEVICE_SPV:.+]]"
4947
// CHECK-BINDINGS-TEMPS: "x86_64-unknown-linux-gnu" - "Offload::Packager", inputs: ["[[DEVICE_SPV]]"], output: "[[DEVICE_IMAGE:.+]]"
5048
// CHECK-BINDINGS-TEMPS: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HOST_BC]]", "[[DEVICE_IMAGE]]"], output: "[[HOST_ASM:.+]]"

clang/test/Driver/spirv-toolchain.cl

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
// RUN: %clang -### --target=spirv64 -x c -c %s 2>&1 | FileCheck --check-prefix=SPV64 %s
77

88
// SPV64: "-cc1" "-triple" "spirv64"
9-
// SPV64-SAME: "-o" [[BC:".*bc"]]
10-
// SPV64: {{llvm-spirv.*"}} [[BC]] "-o" {{".*o"}}
9+
// SPV64-SAME: "-o" {{".*o"}}
1110

1211
// RUN: %clang -### --target=spirv32 -x cl -c %s 2>&1 | FileCheck --check-prefix=SPV32 %s
1312
// RUN: %clang -### --target=spirv32 %s 2>&1 | FileCheck --check-prefix=SPV32 %s
@@ -16,8 +15,7 @@
1615
// RUN: %clang -### --target=spirv32 -x c -c %s 2>&1 | FileCheck --check-prefix=SPV32 %s
1716

1817
// SPV32: "-cc1" "-triple" "spirv32"
19-
// SPV32-SAME: "-o" [[BC:".*bc"]]
20-
// SPV32: {{llvm-spirv.*"}} [[BC]] "-o" {{".*o"}}
18+
// SPV32-SAME: "-o" {{".*o"}}
2119

2220
//-----------------------------------------------------------------------------
2321
// Check Assembly emission.
@@ -27,17 +25,15 @@
2725
// RUN: %clang -### --target=spirv64 -x c -S %s 2>&1 | FileCheck --check-prefix=SPT64 %s
2826

2927
// SPT64: "-cc1" "-triple" "spirv64"
30-
// SPT64-SAME: "-o" [[BC:".*bc"]]
31-
// SPT64: {{llvm-spirv.*"}} [[BC]] "--spirv-tools-dis" "-o" {{".*s"}}
28+
// SPT64-SAME: "-o" {{".*s"}}
3229

3330
// RUN: %clang -### --target=spirv32 -x cl -S %s 2>&1 | FileCheck --check-prefix=SPT32 %s
3431
// RUN: %clang -### --target=spirv32 -x ir -S %s 2>&1 | FileCheck --check-prefix=SPT32 %s
3532
// RUN: %clang -### --target=spirv32 -x clcpp -c %s 2>&1 | FileCheck --check-prefix=SPV32 %s
3633
// RUN: %clang -### --target=spirv32 -x c -S %s 2>&1 | FileCheck --check-prefix=SPT32 %s
3734

3835
// SPT32: "-cc1" "-triple" "spirv32"
39-
// SPT32-SAME: "-o" [[BC:".*bc"]]
40-
// SPT32: {{llvm-spirv.*"}} [[BC]] "--spirv-tools-dis" "-o" {{".*s"}}
36+
// SPT32-SAME: "-o" {{".*s"}}
4137

4238
//-----------------------------------------------------------------------------
4339
// Check assembly input -> object output
@@ -55,50 +51,48 @@
5551
// TMP: "-cc1" "-triple" "spirv64"
5652
// TMP-SAME: "-o" [[BC:".*bc"]]
5753
// TMP-SAME: [[I]]
58-
// TMP: {{llvm-spirv.*"}} [[BC]] "--spirv-tools-dis" "-o" [[S:".*s"]]
54+
// TMP: "-cc1"
55+
// TMP-SAME: "-o" [[S:".*s"]]
56+
// TMP-SAME: [[BC]]
5957
// TMP: {{spirv-as.*"}} [[S]] "-o" {{".*o"}}
6058

6159
//-----------------------------------------------------------------------------
6260
// Check linking when multiple input files are passed.
6361
// RUN: %clang -### -target spirv64 %s %s 2>&1 | FileCheck --check-prefix=SPLINK %s
6462

6563
// SPLINK: "-cc1" "-triple" "spirv64"
66-
// SPLINK-SAME: "-o" [[BC:".*bc"]]
67-
// SPLINK: {{llvm-spirv.*"}} [[BC]] "-o" [[SPV1:".*o"]]
64+
// SPLINK-SAME: "-o" [[SPV1:".*o"]]
6865
// SPLINK: "-cc1" "-triple" "spirv64"
69-
// SPLINK-SAME: "-o" [[BC:".*bc"]]
70-
// SPLINK: {{llvm-spirv.*"}} [[BC]] "-o" [[SPV2:".*o"]]
66+
// SPLINK-SAME: "-o" [[SPV2:".*o"]]
7167
// SPLINK: {{spirv-link.*"}} [[SPV1]] [[SPV2]] "-o" "a.out"
7268

7369
//-----------------------------------------------------------------------------
7470
// Check bindings when linking when multiple input files are passed.
7571
// RUN: %clang -### -target spirv64 -ccc-print-bindings %s %s 2>&1 | FileCheck --check-prefix=SPLINK-BINDINGS %s
7672

77-
// SPLINK-BINDINGS: "clang", inputs: [[[CL:".*cl"]]], output: [[BC1:".*bc"]]
78-
// SPLINK-BINDINGS: "SPIR-V::Translator", inputs: [[[BC1]]], output: [[OBJ1:".*o"]]
79-
// SPLINK-BINDINGS: "clang", inputs: [[[CL]]], output: [[BC2:".*bc"]]
80-
// SPLINK-BINDINGS: "SPIR-V::Translator", inputs: [[[BC2]]], output: [[OBJ2:".*o"]]
73+
// SPLINK-BINDINGS: "clang", inputs: [[[CL:".*cl"]]], output: [[OBJ1:".*o"]]
74+
// SPLINK-BINDINGS: "clang", inputs: [[[CL]]], output: [[OBJ2:".*o"]]
8175
// SPLINK-BINDINGS: "SPIR-V::Linker", inputs: [[[OBJ1]], [[OBJ2]]], output: "a.out"
8276

8377
//-----------------------------------------------------------------------------
8478
// Check external vs internal object emission.
8579
// RUN: %clang -### --target=spirv64 -fno-integrated-objemitter %s 2>&1 | FileCheck --check-prefix=XTOR %s
8680
// RUN: %clang -### --target=spirv64 -fintegrated-objemitter %s 2>&1 | FileCheck --check-prefix=BACKEND %s
8781

88-
// XTOR: {{llvm-spirv.*"}}
89-
// BACKEND-NOT: {{llvm-spirv.*"}}
82+
// XTOR-NOT: "llvm-spirv.*"
83+
// BACKEND-NOT: "llvm-spirv.*"
9084

9185
//-----------------------------------------------------------------------------
92-
// Check llvm-spirv-<LLVM_VERSION_MAJOR> is used if it is found in PATH.
86+
// Check spirv-as-<LLVM_VERSION_MAJOR> is used if it is found in PATH.
9387
//
9488
// This test uses the PATH environment variable; on Windows, we may need to retain
9589
// the original path for the built Clang binary to be able to execute (as it is
9690
// used for locating dependent DLLs). Therefore, skip this test on system-windows.
9791
//
9892
// RUN: mkdir -p %t/versioned
99-
// RUN: touch %t/versioned/llvm-spirv-%llvm-version-major \
100-
// RUN: && chmod +x %t/versioned/llvm-spirv-%llvm-version-major
101-
// RUN: %if !system-windows %{ env "PATH=%t/versioned" %clang -### --target=spirv64 -x cl -c %s 2>&1 \
93+
// RUN: touch %t/versioned/spirv-as-%llvm-version-major \
94+
// RUN: && chmod +x %t/versioned/spirv-as-%llvm-version-major
95+
// RUN: %if !system-windows %{ env "PATH=%t/versioned" %clang -### --target=spirv64 -x cl -c --save-temps %s 2>&1 \
10296
// RUN: | FileCheck -DVERSION=%llvm-version-major --check-prefix=VERSIONED %s %}
10397

104-
// VERSIONED: {{.*}}llvm-spirv-[[VERSION]]
98+
// VERSIONED: {{.*}}spirv-as-[[VERSION]]

0 commit comments

Comments
 (0)