Skip to content

Conversation

@KornevNikita
Copy link
Contributor

Add __spirv_ReadClockKHR support

Add __spirv_ReadClockKHR support
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2025

@llvm/pr-subscribers-backend-spir-v

Author: Nikita Kornev (KornevNikita)

Changes

Add __spirv_ReadClockKHR support


Full diff: https://github.com/llvm/llvm-project/pull/160147.diff

3 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp (+19-12)
  • (modified) llvm/lib/Target/SPIRV/SPIRVBuiltins.td (+1)
  • (modified) llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll (+9)
diff --git a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
index 2abd9d36f7606..8bedd8c860ab2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
@@ -1443,18 +1443,25 @@ static bool generateKernelClockInst(const SPIRV::IncomingCall *Call,
 
   Register ResultReg = Call->ReturnRegister;
 
-  // Deduce the `Scope` operand from the builtin function name.
-  SPIRV::Scope::Scope ScopeArg =
-      StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
-          .EndsWith("device", SPIRV::Scope::Scope::Device)
-          .EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
-          .EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
-  Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
-
-  MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
-      .addDef(ResultReg)
-      .addUse(GR->getSPIRVTypeID(Call->ReturnType))
-      .addUse(ScopeReg);
+  if (Builtin->Name == "__spirv_ReadClockKHR") {
+    MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
+        .addDef(ResultReg)
+        .addUse(GR->getSPIRVTypeID(Call->ReturnType))
+        .addUse(Call->Arguments[0]);
+  } else {
+    // Deduce the `Scope` operand from the builtin function name.
+    SPIRV::Scope::Scope ScopeArg =
+        StringSwitch<SPIRV::Scope::Scope>(Builtin->Name)
+            .EndsWith("device", SPIRV::Scope::Scope::Device)
+            .EndsWith("work_group", SPIRV::Scope::Scope::Workgroup)
+            .EndsWith("sub_group", SPIRV::Scope::Scope::Subgroup);
+    Register ScopeReg = buildConstantIntReg32(ScopeArg, MIRBuilder, GR);
+
+    MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
+        .addDef(ResultReg)
+        .addUse(GR->getSPIRVTypeID(Call->ReturnType))
+        .addUse(ScopeReg);
+  }
 
   return true;
 }
diff --git a/llvm/lib/Target/SPIRV/SPIRVBuiltins.td b/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
index d08560bb6565a..02f15bed396a1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
+++ b/llvm/lib/Target/SPIRV/SPIRVBuiltins.td
@@ -1145,6 +1145,7 @@ defm : DemangledNativeBuiltin<"clock_read_sub_group", OpenCL_std, KernelClock, 0
 defm : DemangledNativeBuiltin<"clock_read_hilo_device", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
 defm : DemangledNativeBuiltin<"clock_read_hilo_work_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
 defm : DemangledNativeBuiltin<"clock_read_hilo_sub_group", OpenCL_std, KernelClock, 0, 0, OpReadClockKHR>;
+defm : DemangledNativeBuiltin<"__spirv_ReadClockKHR", OpenCL_std, KernelClock, 1, 1, OpReadClockKHR>;
 
 //===----------------------------------------------------------------------===//
 // Class defining an atomic instruction on floating-point numbers.
diff --git a/llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll b/llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll
index bd07ba1316ece..eb4cf76c96da6 100644
--- a/llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll
+++ b/llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_shader_clock/shader_clock.ll
@@ -20,6 +20,9 @@
 ; CHECK: OpReadClockKHR [[v2uint]] [[uint_1]]
 ; CHECK: OpReadClockKHR [[v2uint]] [[uint_2]]
 ; CHECK: OpReadClockKHR [[v2uint]] [[uint_3]]
+; CHECK: OpReadClockKHR [[ulong]] [[uint_1]]
+; CHECK: OpReadClockKHR [[ulong]] [[uint_2]]
+; CHECK: OpReadClockKHR [[ulong]] [[uint_3]]
 
 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) {
 entry:
@@ -39,6 +42,9 @@ entry:
   %call9 = tail call spir_func <2 x i32> @_Z25clock_read_hilo_sub_groupv()
   %arrayidx10 = getelementptr inbounds i8, ptr addrspace(1) %outv2, i32 16
   store <2 x i32> %call9, ptr addrspace(1) %arrayidx10, align 8
+  %call10 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 1)
+  %call11 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 2)
+  %call12 = call spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32 3)
   ret void
 }
 
@@ -59,3 +65,6 @@ declare spir_func <2 x i32> @_Z26clock_read_hilo_work_groupv() local_unnamed_add
 
 ; Function Attrs: convergent nounwind
 declare spir_func <2 x i32> @_Z25clock_read_hilo_sub_groupv() local_unnamed_addr
+
+; Function Attrs: nounwind
+declare spir_func i64 @_Z27__spirv_ReadClockKHR_Rulongi(i32)

@KornevNikita KornevNikita changed the title [SPIR-V] Upd SPV_KHR_shader_clock extension [SPIRV] Upd SPV_KHR_shader_clock extension Sep 25, 2025
Comment on lines +1446 to +1451
if (Builtin->Name == "__spirv_ReadClockKHR") {
MIRBuilder.buildInstr(SPIRV::OpReadClockKHR)
.addDef(ResultReg)
.addUse(GR->getSPIRVTypeID(Call->ReturnType))
.addUse(Call->Arguments[0]);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's not needed. I'm looking at #96790 and it seems like changing
defm : DemangledNativeBuiltin<"__spirv_ReadClockKHR", OpenCL_std, KernelClock, 1, 1, OpReadClockKHR>;
to
defm : DemangledGroupBuiltinWrapper<"__spirv_ReadClockKHR", OpenCL_std, 1, 1, OpReadClockKHR>;

should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to apply this, but it doesn't work like that - llc crashes. I believe DemangledGroupBuiltinWrapper is intended for different Group operations. Looking at how other built-ins are processed, I guess the way suggested here should be correct.

@KornevNikita
Copy link
Contributor Author

@MrSidims could you please re-run the pre-commit testing

@KornevNikita
Copy link
Contributor Author

Ping

@KornevNikita
Copy link
Contributor Author

@MrSidims thanks, I guess it's ready to merge?

@MrSidims MrSidims merged commit 3f47a7b into llvm:main Oct 23, 2025
11 checks passed
@github-actions
Copy link

@KornevNikita Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Oct 23, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/33835

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-shell :: Commands/CommandScriptImmediateOutput/CommandScriptImmediateOutputFile.test (1439 of 3258)
PASS: lldb-unit :: Core/./LLDBCoreTests/23/118 (1440 of 3258)
PASS: lldb-shell :: Breakpoint/dummy-target.test (1441 of 3258)
PASS: lldb-shell :: ObjectFile/ELF/elf-memory.test (1442 of 3258)
PASS: lldb-shell :: Minidump/memory-region-from-module.yaml (1443 of 3258)
PASS: lldb-shell :: SymbolFile/DWARF/delayed-definition-die-searching.test (1444 of 3258)
PASS: lldb-shell :: REPL/Basic.test (1445 of 3258)
PASS: lldb-shell :: Register/x86-fp-write.test (1446 of 3258)
PASS: lldb-shell :: Register/x86-64-ymm-read.test (1447 of 3258)
XFAIL: lldb-shell :: Register/x86-64-zmm-write.test (1448 of 3258)
FAIL: lldb-unit :: DAP/./DAPTests/10/77 (1449 of 3258)
******************** TEST 'lldb-unit :: DAP/./DAPTests/10/77' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb/unittests/DAP/./DAPTests-lldb-unit-2462420-10-77.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=77 GTEST_SHARD_INDEX=10 /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb/unittests/DAP/./DAPTests
--

Script:
--
/home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb/unittests/DAP/./DAPTests --gtest_filter=DisconnectRequestHandlerTest.DisconnectTriggersTerminateCommands
--
/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/unittests/DAP/Handler/DisconnectTest.cpp:52: Failure
Actual function call count doesn't match EXPECT_CALL(client, Received(Output("2\n")))...
         Expected: to be called twice
           Actual: called once - unsatisfied and active


/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/unittests/DAP/Handler/DisconnectTest.cpp:52
Actual function call count doesn't match EXPECT_CALL(client, Received(Output("2\n")))...
         Expected: to be called twice
           Actual: called once - unsatisfied and active



********************
PASS: lldb-shell :: Register/x86-64-fp-write.test (1450 of 3258)
PASS: lldb-shell :: SymbolFile/DWARF/x86/debug_loc.s (1451 of 3258)
PASS: lldb-shell :: Commands/command-disassemble.s (1452 of 3258)
PASS: lldb-unit :: Core/./LLDBCoreTests/24/118 (1453 of 3258)
PASS: lldb-shell :: Register/x86-64-gp-read.test (1454 of 3258)
PASS: lldb-shell :: Register/x86-mm-xmm-write.test (1455 of 3258)
PASS: lldb-shell :: Register/x86-mm-xmm-read.test (1456 of 3258)
PASS: lldb-shell :: Expr/TestStringLiteralExpr.test (1457 of 3258)
PASS: lldb-shell :: Expr/nodefaultlib.cpp (1458 of 3258)
PASS: lldb-shell :: Commands/command-image-lookup.yaml (1459 of 3258)
PASS: lldb-shell :: Driver/TestQuiet.test (1460 of 3258)
PASS: lldb-shell :: Register/x86-64-read.test (1461 of 3258)
PASS: lldb-unit :: Core/./LLDBCoreTests/109/118 (1462 of 3258)
PASS: lldb-shell :: SymbolFile/NativePDB/lookup-by-types.cpp (1463 of 3258)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants