Skip to content

Commit 151a17d

Browse files
committed
[SPIR-V] Upd SPV_KHR_shader_clock extension
Add __spirv_ReadClockKHR support
1 parent cbfa5c8 commit 151a17d

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
@@ -1443,18 +1443,25 @@ static bool generateKernelClockInst(const SPIRV::IncomingCall *Call,
14431443

14441444
Register ResultReg = Call->ReturnRegister;
14451445

1446-
// Deduce the `Scope` operand from the builtin function name.
1447-
SPIRV::Scope::Scope ScopeArg =
1448-
StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
1449-
.EndsWith("device", SPIRV::Scope::Scope::Device)
1450-
.EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
1451-
.EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
1452-
Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
1453-
1454-
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1455-
.addDef(ResultReg)
1456-
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1457-
.addUse(ScopeReg);
1446+
if (Builtin->Name == "__spirv_ReadClockKHR") {
1447+
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1448+
.addDef(ResultReg)
1449+
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1450+
.addUse(Call->Arguments[0]);
1451+
} else {
1452+
// Deduce the `Scope` operand from the builtin function name.
1453+
SPIRV::Scope::Scope ScopeArg =
1454+
StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
1455+
.EndsWith("device", SPIRV::Scope::Scope::Device)
1456+
.EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
1457+
.EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
1458+
Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
1459+
1460+
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
1461+
.addDef(ResultReg)
1462+
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
1463+
.addUse(ScopeReg);
1464+
}
14581465

14591466
return true;
14601467
}

llvm/lib/Target/SPIRV/SPIRVBuiltins.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ defm : DemangledNativeBuiltin<"clock_read_sub_group", OpenCL_std, KernelClock, 0
11451145
defm : DemangledNativeBuiltin<"clock_read_hilo_device", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
11461146
defm : DemangledNativeBuiltin<"clock_read_hilo_work_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
11471147
defm : DemangledNativeBuiltin<"clock_read_hilo_sub_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
1148+
defm : DemangledNativeBuiltin<"__spirv_ReadClockKHR", OpenCL_std, KernelClock, 1, 1, OpReadClockKHR>;
11481149

11491150
//===----------------------------------------------------------------------===//
11501151
// 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)