Skip to content

Commit 99c40f6

Browse files
[SPIR-V] Introduce a command line option to support compatibility with Khronos SPIRV Translator (#86101)
SPIRV-LLVM-Translator project (https://github.com/KhronosGroup/SPIRV-LLVM-Translator) from Khronos Group is a tool and a library for bi-directional translation between SPIR-V and LLVM IR. In its backward translation from SPIR-V to LLVM IR SPIRV-LLVM-Translator isn't necessarily able to cover the same SPIR-V patterns/instructions set that SPIRV Backend produces, even if we target the same SPIR-V version in both SPIRV-LLVM-Translator and SPIRV Backend projects. To improve interoperability and ability to apply SPIRV Backend output in different products this PR introduces a notion of a mode of SPIR-V output that is compatible with a subset of SPIR-V supported by SPIRV-LLVM-Translator. This includes a new command line option that doesn't influence default behavior of SPIRV Backend and one test case that demonstrates how this command line option may be used to get a practical benefit of producing that one of two possible and similar output options that can be understood by SPIRV-LLVM-Translator.
1 parent 75e528f commit 99c40f6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ using namespace llvm;
2727
#define GET_SUBTARGETINFO_CTOR
2828
#include "SPIRVGenSubtargetInfo.inc"
2929

30+
static cl::opt<bool>
31+
SPVTranslatorCompat("translator-compatibility-mode",
32+
cl::desc("SPIR-V Translator compatibility mode"),
33+
cl::Optional, cl::init(false));
34+
3035
cl::list<SPIRV::Extension::Extension> Extensions(
3136
"spirv-extensions", cl::desc("SPIR-V extensions"), cl::ZeroOrMore,
3237
cl::Hidden,
@@ -157,8 +162,9 @@ bool SPIRVSubtarget::isAtLeastOpenCLVer(uint32_t VerToCompareTo) const {
157162
}
158163

159164
// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
165+
// In SPIR-V Translator compatibility mode this feature is not available.
160166
bool SPIRVSubtarget::canDirectlyComparePointers() const {
161-
return isAtLeastVer(SPIRVVersion, 14);
167+
return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, 14);
162168
}
163169

164170
void SPIRVSubtarget::initAvailableExtensions() {

llvm/test/CodeGen/SPIRV/instructions/ptrcmp.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s --translator-compatibility-mode -o - | FileCheck %s --check-prefix=CHECK-COMPAT
5+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s --translator-compatibility-mode -o - -filetype=obj | spirv-val %}
26

37
; CHECK-DAG: OpName [[EQ:%.*]] "test_eq"
48
; CHECK-DAG: OpName [[NE:%.*]] "test_ne"
9+
; CHECK-COMPAT-DAG: OpName [[EQ:%.*]] "test_eq"
10+
; CHECK-COMPAT-DAG: OpName [[NE:%.*]] "test_ne"
511
; CHECK-DAG: OpName [[ULT:%.*]] "test_ult"
612
; CHECK-DAG: OpName [[SLT:%.*]] "test_slt"
713
; CHECK-DAG: OpName [[ULE:%.*]] "test_ule"
@@ -19,6 +25,9 @@
1925
; CHECK-NEXT: [[R:%.*]] = OpPtrEqual {{%.+}} [[A]] [[B]]
2026
; CHECK-NEXT: OpReturnValue [[R]]
2127
; CHECK-NEXT: OpFunctionEnd
28+
; CHECK-COMPAT: [[EQ]] = OpFunction
29+
; CHECK-COMPAT-NOT: OpPtrEqual
30+
; CHECK-COMPAT: OpFunctionEnd
2231
define i1 @test_eq(i16* %a, i16* %b) {
2332
%r = icmp eq i16* %a, %b
2433
ret i1 %r
@@ -31,6 +40,9 @@ define i1 @test_eq(i16* %a, i16* %b) {
3140
; CHECK-NEXT: [[R:%.*]] = OpPtrNotEqual {{%.+}} [[A]] [[B]]
3241
; CHECK-NEXT: OpReturnValue [[R]]
3342
; CHECK-NEXT: OpFunctionEnd
43+
; CHECK-COMPAT: [[NE]] = OpFunction
44+
; CHECK-COMPAT-NOT: OpPtrNotEqual
45+
; CHECK-COMPAT: OpFunctionEnd
3446
define i1 @test_ne(i16* %a, i16* %b) {
3547
%r = icmp ne i16* %a, %b
3648
ret i1 %r

0 commit comments

Comments
 (0)