Skip to content

Commit 86375a2

Browse files
committed
[SPIRV] Add support for SPV_KHR_bit_instructions
Draft version of patch to add support for SPV_KHR_bit_instructions. Created revision so we can discuss how to proceed here. SPV_KHR_bit_instructions (http://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/SPV_KHR_bit_instructions.html), adds support for instructions that we already support through the Shader capability but without requiring the shader cap. However we currently have no way to disable the shader cap afaiu. On the other hand, SPV_KHR_bit_instructions was one of the extensions @mpaszkowski listed that we need to implement. So what does it mean at the moment the need to implement this? Is it just the ability to issue the extension and capabilities instructions without issuing the Shader capability? If so, then I will need to add here a way to disable the Shader capability. What do you think? Differential Revision: https://reviews.llvm.org/D156661
1 parent 85ec68d commit 86375a2

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "SPIRVModuleAnalysis.h"
18+
#include "MCTargetDesc/SPIRVBaseInfo.h"
19+
#include "MCTargetDesc/SPIRVMCTargetDesc.h"
1820
#include "SPIRV.h"
1921
#include "SPIRVSubtarget.h"
2022
#include "SPIRVTargetMachine.h"
@@ -523,6 +525,12 @@ void SPIRV::RequirementHandler::addAvailableCaps(const CapabilityList &ToAdd) {
523525
SPIRV::OperandCategory::CapabilityOperand, Cap));
524526
}
525527

528+
void SPIRV::RequirementHandler::removeCapabilityIf(const Capability::Capability ToRemove,
529+
const Capability::Capability IfPresent) {
530+
if (AvailableCaps.contains(IfPresent))
531+
AvailableCaps.erase(ToRemove);
532+
}
533+
526534
namespace llvm {
527535
namespace SPIRV {
528536
void RequirementHandler::initAvailableCapabilities(const SPIRVSubtarget &ST) {
@@ -734,6 +742,11 @@ void addInstrRequirements(const MachineInstr &MI,
734742
break;
735743
}
736744
case SPIRV::OpBitReverse:
745+
case SPIRV::OpBitFieldInsert:
746+
case SPIRV::OpBitFieldSExtract:
747+
case SPIRV::OpBitFieldUExtract:
748+
Reqs.addExtension(SPIRV::Extension::SPV_KHR_bit_instructions);
749+
break;
737750
case SPIRV::OpTypeRuntimeArray:
738751
Reqs.addCapability(SPIRV::Capability::Shader);
739752
break;
@@ -887,6 +900,12 @@ void addInstrRequirements(const MachineInstr &MI,
887900
default:
888901
break;
889902
}
903+
904+
// If we require capability Shader, then we can remove the requirement for
905+
// the BitInstructions capability, since Shader is a superset capability
906+
// of BitInstructions.
907+
Reqs.removeCapabilityIf(SPIRV::Capability::Shader,
908+
SPIRV::Capability::BitInstructions);
890909
}
891910

892911
static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ struct RequirementHandler {
113113
bool isCapabilityAvailable(Capability::Capability Cap) const {
114114
return AvailableCaps.contains(Cap);
115115
}
116+
117+
// Remove capability ToRemove, but only if IfPresent is present.
118+
void removeCapabilityIf(const Capability::Capability ToRemove,
119+
const Capability::Capability IfPresent);
116120
};
117121

118122
using InstrList = SmallVector<MachineInstr *>;

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "SPIRVSubtarget.h"
14+
#include "MCTargetDesc/SPIRVBaseInfo.h"
1415
#include "SPIRV.h"
1516
#include "SPIRVGlobalRegistry.h"
1617
#include "SPIRVLegalizerInfo.h"

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ defm PhysicalStorageBufferAddressesEXT : CapabilityOperand<5347, 0, 0, [], [Shad
450450
defm CooperativeMatrixNV : CapabilityOperand<5357, 0, 0, [], [Shader]>;
451451
defm ArbitraryPrecisionIntegersINTEL : CapabilityOperand<5844, 0, 0, [SPV_INTEL_arbitrary_precision_integers], [Int8, Int16]>;
452452
defm OptNoneINTEL : CapabilityOperand<6094, 0, 0, [SPV_INTEL_optnone], []>;
453+
defm BitInstructions : CapabilityOperand<6025, 0, 0, [SPV_KHR_bit_instructions], []>;
453454

454455
//===----------------------------------------------------------------------===//
455456
// Multiclass used to define SourceLanguage enum values and at the same time

llvm/test/CodeGen/SPIRV/transcoding/OpBitReverse_i32.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
22

3+
; CHECK-SPIRV: OpCapability BitInstructions
4+
; CHECK-SPIRV-NEXT: OpExtension "SPV_KHR_bit_instructions"
5+
36
; CHECK-SPIRV: %[[#int:]] = OpTypeInt 32
47
; CHECK-SPIRV: OpBitReverse %[[#int]]
58

0 commit comments

Comments
 (0)