Skip to content

Commit

Permalink
[Backport to 9] Implement support for SPV_KHR_shader_clock (#2026) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KorovinVlad and jgstarIntel authored Feb 2, 2024
1 parent b60ddd9 commit 9caf18b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ EXT(SPV_KHR_float_controls)
EXT(SPV_KHR_expect_assume)
EXT(SPV_KHR_integer_dot_product)
EXT(SPV_KHR_linkonce_odr)
EXT(SPV_KHR_shader_clock)
EXT(SPV_INTEL_subgroups)
EXT(SPV_INTEL_media_block_io)
EXT(SPV_INTEL_device_side_avc_motion_estimation)
Expand Down
36 changes: 36 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3159,5 +3159,41 @@ class SPIRVSplitBarrierINTELBase : public SPIRVInstTemplateBase {
_SPIRV_OP(ControlBarrierArriveINTEL, false, 4)
_SPIRV_OP(ControlBarrierWaitINTEL, false, 4)
#undef _SPIRV_OP
template <Op OC> class SPIRVReadClockKHRInstBase : public SPIRVUnaryInst<OC> {
protected:
SPIRVCapVec getRequiredCapability() const override {
return getVec(CapabilityShaderClockKHR);
}

SPIRVExtSet getRequiredExtensions() const override {
return getSet(ExtensionID::SPV_KHR_shader_clock);
}

void validate() const override {
SPIRVUnaryInst<OC>::validate();

SPIRVType *ResCompTy = this->getType();
SPIRVWord ResCompCount = 1;
if (ResCompTy->isTypeVector()) {
ResCompCount = ResCompTy->getVectorComponentCount();
ResCompTy = ResCompTy->getVectorComponentType();
}
auto InstName = OpCodeNameMap::map(OC);
SPIRVErrorLog &SPVErrLog = this->getModule()->getErrorLog();

// check for either 64 bit int type or two element vector of 32 bit int
// types.
SPVErrLog.checkError(
ResCompTy->isTypeInt(64) ||
(ResCompCount == 2 && ResCompTy->isTypeInt(32)),
SPIRVEC_InvalidInstruction,
InstName + "\nResult value must be a scalar of integer"
" 64-bit type or two element vector of 32-bit type\n");
}
};
#define _SPIRV_OP(x, ...) typedef SPIRVReadClockKHRInstBase<Op##x> SPIRV##x;
_SPIRV_OP(ReadClockKHR)
#undef _SPIRV_OP

} // namespace SPIRV
#endif // SPIRV_LIBSPIRV_SPIRVINSTRUCTION_H
1 change: 1 addition & 0 deletions lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
add(CapabilitySignedZeroInfNanPreserve, "SignedZeroInfNanPreserve");
add(CapabilityRoundingModeRTE, "RoundingModeRTE");
add(CapabilityRoundingModeRTZ, "RoundingModeRTZ");
add(CapabilityShaderClockKHR, "ShaderClockKHR");
add(CapabilityAtomicFloat32AddEXT, "AtomicFloat32AddEXT");
add(CapabilityAtomicFloat64AddEXT, "AtomicFloat64AddEXT");
add(CapabilityAtomicFloat32MinMaxEXT, "AtomicFloat32MinMaxEXT");
Expand Down
1 change: 1 addition & 0 deletions lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ _SPIRV_OP(SUDotKHR, 4452)
_SPIRV_OP(SDotAccSatKHR, 4453)
_SPIRV_OP(UDotAccSatKHR, 4454)
_SPIRV_OP(SUDotAccSatKHR, 4455)
_SPIRV_OP(ReadClockKHR, 5056)
_SPIRV_OP(SubgroupShuffleINTEL, 5571)
_SPIRV_OP(SubgroupShuffleDownINTEL, 5572)
_SPIRV_OP(SubgroupShuffleUpINTEL, 5573)
Expand Down
46 changes: 46 additions & 0 deletions test/extensions/KHR/SPV_KHR_shader_clock/shader_clock.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_shader_clock
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
; RUN: llvm-spirv %t.spv -o %t.rev.bc -r --spirv-target-env=SPV-IR
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

; RUN: not llvm-spirv %t.bc 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
; CHECK-ERROR: RequiresExtension: Feature requires the following SPIR-V extension:
; CHECK-ERROR-NEXT: SPV_KHR_shader_clock

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

; CHECK-SPIRV: Capability ShaderClockKHR
; CHECK-SPIRV: Extension "SPV_KHR_shader_clock"
; CHECK-SPIRV: TypeInt [[#I32Ty:]] 32
; CHECK-SPIRV: TypeInt [[#I64Ty:]] 64
; CHECK-SPIRV: TypeVector [[#I32v2Ty:]] [[#I32Ty]] 2

; CHECK-SPIRV: FunctionParameter [[#I32Ty]] [[I32ValId:.*]]

; CHECK-SPIRV: ReadClockKHR [[#I32v2Ty]] [[#Res1:]] [[I32ValId]]
; CHECK-SPIRV: ReadClockKHR [[#I64Ty]] [[#Res2:]] [[I32ValId]]

; CHECK-LLVM: call spir_func <2 x i32> @_Z20__spirv_ReadClockKHR
; CHECK-LLVM: call spir_func i64 @_Z20__spirv_ReadClockKHR

define spir_func void @_Z7read_types(i32 %a) {
%1 = tail call spir_func <2 x i32> @_Z20__spirv_ReadClockKHRIDv2_jET_j(i32 %a)
%2 = tail call spir_func i64 @_Z20__spirv_ReadClockKHRImET_j(i32 %a)
ret void
}

declare spir_func <2 x i32> @_Z20__spirv_ReadClockKHRIDv2_jET_j(i32)

declare spir_func i64 @_Z20__spirv_ReadClockKHRImET_j(i32)

!opencl.spir.version = !{!0}
!spirv.Source = !{!1}
!llvm.ident = !{!2}

!0 = !{i32 1, i32 2}
!1 = !{i32 4, i32 100000}
!2 = !{!"clang version 16.0.0"}

0 comments on commit 9caf18b

Please sign in to comment.