Skip to content

Commit 9caf18b

Browse files
[Backport to 9] Implement support for SPV_KHR_shader_clock (#2026) (#2329)
Link to the spec: https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/SPV_KHR_shader_clock.asciidoc Co-authored-by: jgstarIntel <joey.genfi@intel.com>
1 parent b60ddd9 commit 9caf18b

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

include/LLVMSPIRVExtensions.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EXT(SPV_KHR_float_controls)
1010
EXT(SPV_KHR_expect_assume)
1111
EXT(SPV_KHR_integer_dot_product)
1212
EXT(SPV_KHR_linkonce_odr)
13+
EXT(SPV_KHR_shader_clock)
1314
EXT(SPV_INTEL_subgroups)
1415
EXT(SPV_INTEL_media_block_io)
1516
EXT(SPV_INTEL_device_side_avc_motion_estimation)

lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,5 +3159,41 @@ class SPIRVSplitBarrierINTELBase : public SPIRVInstTemplateBase {
31593159
_SPIRV_OP(ControlBarrierArriveINTEL, false, 4)
31603160
_SPIRV_OP(ControlBarrierWaitINTEL, false, 4)
31613161
#undef _SPIRV_OP
3162+
template <Op OC> class SPIRVReadClockKHRInstBase : public SPIRVUnaryInst<OC> {
3163+
protected:
3164+
SPIRVCapVec getRequiredCapability() const override {
3165+
return getVec(CapabilityShaderClockKHR);
3166+
}
3167+
3168+
SPIRVExtSet getRequiredExtensions() const override {
3169+
return getSet(ExtensionID::SPV_KHR_shader_clock);
3170+
}
3171+
3172+
void validate() const override {
3173+
SPIRVUnaryInst<OC>::validate();
3174+
3175+
SPIRVType *ResCompTy = this->getType();
3176+
SPIRVWord ResCompCount = 1;
3177+
if (ResCompTy->isTypeVector()) {
3178+
ResCompCount = ResCompTy->getVectorComponentCount();
3179+
ResCompTy = ResCompTy->getVectorComponentType();
3180+
}
3181+
auto InstName = OpCodeNameMap::map(OC);
3182+
SPIRVErrorLog &SPVErrLog = this->getModule()->getErrorLog();
3183+
3184+
// check for either 64 bit int type or two element vector of 32 bit int
3185+
// types.
3186+
SPVErrLog.checkError(
3187+
ResCompTy->isTypeInt(64) ||
3188+
(ResCompCount == 2 && ResCompTy->isTypeInt(32)),
3189+
SPIRVEC_InvalidInstruction,
3190+
InstName + "\nResult value must be a scalar of integer"
3191+
" 64-bit type or two element vector of 32-bit type\n");
3192+
}
3193+
};
3194+
#define _SPIRV_OP(x, ...) typedef SPIRVReadClockKHRInstBase<Op##x> SPIRV##x;
3195+
_SPIRV_OP(ReadClockKHR)
3196+
#undef _SPIRV_OP
3197+
31623198
} // namespace SPIRV
31633199
#endif // SPIRV_LIBSPIRV_SPIRVINSTRUCTION_H

lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
524524
add(CapabilitySignedZeroInfNanPreserve, "SignedZeroInfNanPreserve");
525525
add(CapabilityRoundingModeRTE, "RoundingModeRTE");
526526
add(CapabilityRoundingModeRTZ, "RoundingModeRTZ");
527+
add(CapabilityShaderClockKHR, "ShaderClockKHR");
527528
add(CapabilityAtomicFloat32AddEXT, "AtomicFloat32AddEXT");
528529
add(CapabilityAtomicFloat64AddEXT, "AtomicFloat64AddEXT");
529530
add(CapabilityAtomicFloat32MinMaxEXT, "AtomicFloat32MinMaxEXT");

lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ _SPIRV_OP(SUDotKHR, 4452)
334334
_SPIRV_OP(SDotAccSatKHR, 4453)
335335
_SPIRV_OP(UDotAccSatKHR, 4454)
336336
_SPIRV_OP(SUDotAccSatKHR, 4455)
337+
_SPIRV_OP(ReadClockKHR, 5056)
337338
_SPIRV_OP(SubgroupShuffleINTEL, 5571)
338339
_SPIRV_OP(SubgroupShuffleDownINTEL, 5572)
339340
_SPIRV_OP(SubgroupShuffleUpINTEL, 5573)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_shader_clock
3+
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
4+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
5+
; RUN: llvm-spirv %t.spv -o %t.rev.bc -r --spirv-target-env=SPV-IR
6+
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
7+
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
8+
9+
; RUN: not llvm-spirv %t.bc 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
10+
; CHECK-ERROR: RequiresExtension: Feature requires the following SPIR-V extension:
11+
; CHECK-ERROR-NEXT: SPV_KHR_shader_clock
12+
13+
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"
14+
target triple = "spir64-unknown-unknown"
15+
16+
; CHECK-SPIRV: Capability ShaderClockKHR
17+
; CHECK-SPIRV: Extension "SPV_KHR_shader_clock"
18+
; CHECK-SPIRV: TypeInt [[#I32Ty:]] 32
19+
; CHECK-SPIRV: TypeInt [[#I64Ty:]] 64
20+
; CHECK-SPIRV: TypeVector [[#I32v2Ty:]] [[#I32Ty]] 2
21+
22+
; CHECK-SPIRV: FunctionParameter [[#I32Ty]] [[I32ValId:.*]]
23+
24+
; CHECK-SPIRV: ReadClockKHR [[#I32v2Ty]] [[#Res1:]] [[I32ValId]]
25+
; CHECK-SPIRV: ReadClockKHR [[#I64Ty]] [[#Res2:]] [[I32ValId]]
26+
27+
; CHECK-LLVM: call spir_func <2 x i32> @_Z20__spirv_ReadClockKHR
28+
; CHECK-LLVM: call spir_func i64 @_Z20__spirv_ReadClockKHR
29+
30+
define spir_func void @_Z7read_types(i32 %a) {
31+
%1 = tail call spir_func <2 x i32> @_Z20__spirv_ReadClockKHRIDv2_jET_j(i32 %a)
32+
%2 = tail call spir_func i64 @_Z20__spirv_ReadClockKHRImET_j(i32 %a)
33+
ret void
34+
}
35+
36+
declare spir_func <2 x i32> @_Z20__spirv_ReadClockKHRIDv2_jET_j(i32)
37+
38+
declare spir_func i64 @_Z20__spirv_ReadClockKHRImET_j(i32)
39+
40+
!opencl.spir.version = !{!0}
41+
!spirv.Source = !{!1}
42+
!llvm.ident = !{!2}
43+
44+
!0 = !{i32 1, i32 2}
45+
!1 = !{i32 4, i32 100000}
46+
!2 = !{!"clang version 16.0.0"}

0 commit comments

Comments
 (0)