Skip to content

Commit 02722c2

Browse files
KornevNikitaLukacma
authored andcommitted
[SPIRV] Upd SPV_KHR_shader_clock extension (llvm#160147)
Add __spirv_ReadClockKHR support
1 parent 11e5ef1 commit 02722c2

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,18 +1499,25 @@ static bool generateKernelClockInst(const SPIRV::IncomingCall *Call,
14991499

15001500
Register ResultReg = Call->ReturnRegister;
15011501

1502-
// Deduce the `Scope` operand from the builtin function name.
1503-
SPIRV::Scope::Scope ScopeArg =
1504-
StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
1505-
.EndsWith("device", SPIRV::Scope::Scope::Device)
1506-
.EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
1507-
.EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
1508-
Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
1509-
1510-
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1511-
.addDef(ResultReg)
1512-
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1513-
.addUse(ScopeReg);
1502+
if (Builtin->Name == "__spirv_ReadClockKHR") {
1503+
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1504+
.addDef(ResultReg)
1505+
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1506+
.addUse(Call->Arguments[0]);
1507+
} else {
1508+
// Deduce the `Scope` operand from the builtin function name.
1509+
SPIRV::Scope::Scope ScopeArg =
1510+
StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
1511+
.EndsWith("device", SPIRV::Scope::Scope::Device)
1512+
.EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
1513+
.EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
1514+
Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
1515+
1516+
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1517+
.addDef(ResultReg)
1518+
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1519+
.addUse(ScopeReg);
1520+
}
15141521

15151522
return true;
15161523
}

llvm/lib/Target/SPIRV/SPIRVBuiltins.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ defm : DemangledNativeBuiltin<"clock_read_sub_group", OpenCL_std, KernelClock, 0
11741174
defm : DemangledNativeBuiltin<"clock_read_hilo_device", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
11751175
defm : DemangledNativeBuiltin<"clock_read_hilo_work_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
11761176
defm : DemangledNativeBuiltin<"clock_read_hilo_sub_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
1177+
defm : DemangledNativeBuiltin<"__spirv_ReadClockKHR", OpenCL_std, KernelClock, 1, 1, OpReadClockKHR>;
11771178

11781179
//===----------------------------------------------------------------------===//
11791180
// Class defining an atomic instruction on floating-point numbers.

llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
; CHECK: OpReadClockKHR [[v2uint]] [[uint_1]]
2121
; CHECK: OpReadClockKHR [[v2uint]] [[uint_2]]
2222
; CHECK: OpReadClockKHR [[v2uint]] [[uint_3]]
23+
; CHECK: OpReadClockKHR [[ulong]] [[uint_1]]
24+
; CHECK: OpReadClockKHR [[ulong]] [[uint_2]]
25+
; CHECK: OpReadClockKHR [[ulong]] [[uint_3]]
2326

2427
define dso_local spir_kernel void @test_clocks(ptr addrspace(1) nocapture noundef writeonly align 8 %out64, ptr addrspace(1) nocapture noundef writeonly align 8 %outv2) {
2528
entry:
@@ -39,6 +42,9 @@ entry:
3942
%call9 = tail call spir_func <2 x i32> @_Z25clock_read_hilo_sub_groupv()
4043
%arrayidx10 = getelementptr inbounds i8, ptr addrspace(1) %outv2, i32 16
4144
store <2 x i32> %call9, ptr addrspace(1) %arrayidx10, align 8
45+
%call10 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 1)
46+
%call11 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 2)
47+
%call12 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 3)
4248
ret void
4349
}
4450

@@ -59,3 +65,6 @@ declare spir_func <2 x i32> @_Z26clock_read_hilo_work_groupv() local_unnamed_add
5965

6066
; Function Attrs: convergent nounwind
6167
declare spir_func <2 x i32> @_Z25clock_read_hilo_sub_groupv() local_unnamed_addr
68+
69+
; Function Attrs: nounwind
70+
declare spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32)

0 commit comments

Comments
 (0)