Skip to content

Commit 85acfa2

Browse files
svenvhsys-ce-bb
authored andcommitted
Never emit CapabilityShader for bitreverse (#2287)
This translator currently only supports the OpenCL/compute "flavour" of SPIR-V, but for the `llvm.bitreverse` intrinsic it could produce SPIR-V modules declaring the `Shader` Capability. When translating `llvm.bitreverse` without having the `SPV_KHR_bit_instructions` extension enabled, avoid producing SPIR-V modules declaring the `Shader` Capability and report an error instead. Original commit: KhronosGroup/SPIRV-LLVM-Translator@17a5d2d
1 parent e80e8ea commit 85acfa2

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,8 +3901,12 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
39013901
return nullptr;
39023902
}
39033903
case Intrinsic::bitreverse: {
3904-
if (!BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bit_instructions)) {
3905-
BM->addCapability(CapabilityShader);
3904+
if (!BM->getErrorLog().checkError(
3905+
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bit_instructions),
3906+
SPIRVEC_InvalidFunctionCall, II,
3907+
"Translation of llvm.bitreverse intrinsic requires "
3908+
"SPV_KHR_bit_instructions extension.")) {
3909+
return nullptr;
39063910
}
39073911
SPIRVType *Ty = transType(II->getType());
39083912
SPIRVValue *Op = transValue(II->getArgOperand(0), BB);

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,10 +3007,7 @@ _SPIRV_OP(SUDotAccSatKHR, true, 6, true, 3)
30073007
class SPIRVBitOp : public SPIRVInstTemplateBase {
30083008
public:
30093009
SPIRVCapVec getRequiredCapability() const override {
3010-
if (Module->isAllowedToUseExtension(ExtensionID::SPV_KHR_bit_instructions))
3011-
return getVec(CapabilityBitInstructions);
3012-
3013-
return getVec(CapabilityShader);
3010+
return getVec(CapabilityBitInstructions);
30143011
}
30153012

30163013
std::optional<ExtensionID> getRequiredExtension() const override {

llvm-spirv/test/extensions/KHR/SPV_KHR_bit_instructions/capabilities.ll

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
; RUN: llvm-as %s -o %t.bc
22

3-
; RUN: llvm-spirv %t.bc -spirv-text --spirv-ext=-SPV_KHR_bit_instructions -o %t.txt
4-
; RUN: FileCheck < %t.txt %s --check-prefixes=CHECK-COMMON,CHECK-WITHOUT-EXT
3+
; RUN: not llvm-spirv %t.bc -spirv-text --spirv-ext=-SPV_KHR_bit_instructions -o - 2>&1 | FileCheck %s --check-prefixes=CHECK-WITHOUT-EXT
54

65
; RUN: llvm-spirv %t.bc -spirv-text --spirv-ext=+SPV_KHR_bit_instructions -o %t.txt
7-
; RUN: FileCheck < %t.txt %s --check-prefixes=CHECK-COMMON,CHECK-WITH-EXT
6+
; RUN: FileCheck < %t.txt %s --check-prefixes=CHECK-WITH-EXT
87

9-
; CHECK-WITHOUT-EXT: Capability Shader
8+
; CHECK-WITHOUT-EXT: Unexpected llvm intrinsic:
9+
; CHECK-WITHOUT-EXT: Translation of llvm.bitreverse intrinsic requires SPV_KHR_bit_instructions extension.
1010

11-
; CHECK-WITH-EXT-NOT: Capability Shader
1211
; CHECK-WITH-EXT: Capability BitInstructions
13-
; CHECK-WITH-EXT-NOT: Capability Shader
1412
; CHECK-WITH-EXT: Extension "SPV_KHR_bit_instructions"
1513

16-
; CHECK-COMMON: 4 BitReverse
14+
; CHECK-WITH-EXT: 4 BitReverse
1715

1816
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
1917
target triple = "spir-unknown-unknown"

llvm-spirv/test/transcoding/OpBitReverse_i32.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3-
; RUN: llvm-spirv %t.bc -o %t.spv
2+
; RUN: llvm-spirv --spirv-ext=+SPV_KHR_bit_instructions -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
; RUN: llvm-spirv --spirv-ext=+SPV_KHR_bit_instructions %t.bc -o %t.spv
44
; RUN: spirv-val %t.spv
55
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM
66

llvm-spirv/test/transcoding/OpBitReverse_v2i16.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3-
; RUN: llvm-spirv %t.bc -o %t.spv
2+
; RUN: llvm-spirv --spirv-ext=+SPV_KHR_bit_instructions -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
; RUN: llvm-spirv --spirv-ext=+SPV_KHR_bit_instructions %t.bc -o %t.spv
44
; RUN: spirv-val %t.spv
55
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM
66

0 commit comments

Comments
 (0)