Skip to content

Conversation

@smada3
Copy link
Contributor

@smada3 smada3 commented Oct 30, 2025

Using invalid syncscopes on certain NVVM intrinsics causes an obscure error to appear: (error 9: NVVM_ERROR_COMPILATION), libNVVM extra log: Could not find scope ID=5.

This is not a very helpful error. A much more useful error would be something like 'NVPTX does not support syncscope "agent"'

This would immediately make it clear that the issue is not NVPTX specific, but actually from code being fed to NVPTX. This would save users time in debugging issues related to this.

@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2025

@llvm/pr-subscribers-backend-nvptx

Author: Stefan Mada (smada3)

Changes

Using invalid syncscopes on certain NVVM intrinsics causes an obscure error to appear: (error 9: NVVM_ERROR_COMPILATION), libNVVM extra log: Could not find scope ID=5.

This is not a very helpful error. A much more useful error would be something like 'NVPTX does not support syncscope "agent"'

This would immediately make it clear that the issue is not NVPTX specific, but actually from code being fed to NVPTX. This would save users time in debugging issues related to this.


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

3 Files Affected:

  • (modified) llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp (+19-6)
  • (modified) llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h (+1)
  • (added) llvm/test/CodeGen/NVPTX/cmpxchg-unsupported-syncscope.err.ll (+11)
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp b/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp
index 7e7ee754c250d..0bcb9c89b3e8b 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp
@@ -1836,7 +1836,7 @@ bool NVPTXDAGToDAGISel::tryFence(SDNode *N) {
   return true;
 }
 
-NVPTXScopes::NVPTXScopes(LLVMContext &C) {
+NVPTXScopes::NVPTXScopes(LLVMContext &C) : Context(&C) {
   Scopes[C.getOrInsertSyncScopeID("singlethread")] = NVPTX::Scope::Thread;
   Scopes[C.getOrInsertSyncScopeID("")] = NVPTX::Scope::System;
   Scopes[C.getOrInsertSyncScopeID("block")] = NVPTX::Scope::Block;
@@ -1851,11 +1851,24 @@ NVPTX::Scope NVPTXScopes::operator[](SyncScope::ID ID) const {
 
   auto S = Scopes.find(ID);
   if (S == Scopes.end()) {
-    // TODO:
-    // - Add API to LLVMContext to get the name of a single scope.
-    // - Use that API here to print an error containing the name
-    //   of this Unknown ID.
-    report_fatal_error(formatv("Could not find scope ID={}.", int(ID)));
+    // Get the actual scope name from LLVMContext for a better error message
+    std::string scopeName = "<unknown>";
+    if (auto name = Context->getSyncScopeName(ID))
+      scopeName = name->str();
+
+    // Build list of supported syncscopes programmatically
+    std::string supportedScopes;
+    for (const auto &Entry : Scopes) {
+      if (!supportedScopes.empty())
+        supportedScopes += ", ";
+      if (auto name = Context->getSyncScopeName(Entry.first))
+        supportedScopes += name->empty() ? "<empty string>" : name->str();
+    }
+
+    reportFatalUsageError(
+        formatv("NVPTX backend does not support syncscope \"{0}\" (ID={1}).\n"
+                "Supported syncscopes are: {2}.",
+                scopeName, int(ID), supportedScopes));
   }
   return S->second;
 }
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h b/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h
index c912e709d0aa0..5fb1267224bd5 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h
+++ b/llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h
@@ -35,6 +35,7 @@ struct NVPTXScopes {
 
 private:
   SmallMapVector<SyncScope::ID, NVPTX::Scope, 8> Scopes{};
+  LLVMContext *Context = nullptr;
 };
 
 class LLVM_LIBRARY_VISIBILITY NVPTXDAGToDAGISel : public SelectionDAGISel {
diff --git a/llvm/test/CodeGen/NVPTX/cmpxchg-unsupported-syncscope.err.ll b/llvm/test/CodeGen/NVPTX/cmpxchg-unsupported-syncscope.err.ll
new file mode 100644
index 0000000000000..d3853e2fdaa88
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/cmpxchg-unsupported-syncscope.err.ll
@@ -0,0 +1,11 @@
+; RUN: not llc -mcpu=sm_100a -mtriple=nvptx64 -mattr=+ptx86 %s 2>&1 | FileCheck %s
+
+; Test that we get a clear error message when using an unsupported syncscope.
+
+; CHECK: NVPTX backend does not support syncscope "agent"
+; CHECK: Supported syncscopes are: singlethread, <empty string>, block, cluster, device
+define i32 @cmpxchg_unsupported_syncscope_agent(ptr %addr, i32 %cmp, i32 %new) {
+  %result = cmpxchg ptr %addr, i32 %cmp, i32 %new syncscope("agent") monotonic monotonic
+  %value = extractvalue { i32, i1 } %result, 0
+  ret i32 %value
+}

@smada3
Copy link
Contributor Author

smada3 commented Oct 30, 2025

cc: @durga4github @akshayrdeodhar

@akshayrdeodhar akshayrdeodhar self-requested a review October 31, 2025 18:27
Copy link
Contributor

@durga4github durga4github left a comment

Choose a reason for hiding this comment

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

The latest revision LGTM.
Please address other comments before landing.

@gonzalobg
Copy link
Contributor

Thank you Stefan for working on this. LGTM!

@smada3 smada3 enabled auto-merge (squash) November 3, 2025 21:50
@smada3 smada3 merged commit 5d9d890 into llvm:main Nov 3, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 4, 2025

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building llvm at step 6 "test-build-check-mlir-build-only-check-mlir".

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

Here is the relevant piece of the build log for the reference
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR-Unit :: IR/./MLIRIRTests/0/130 (3616 of 3627)
PASS: MLIR-Unit :: IR/./MLIRIRTests/101/130 (3617 of 3627)
PASS: MLIR-Unit :: IR/./MLIRIRTests/39/130 (3618 of 3627)
PASS: MLIR-Unit :: IR/./MLIRIRTests/38/130 (3619 of 3627)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/13/22 (3620 of 3627)
PASS: MLIR-Unit :: Pass/./MLIRPassTests/10/13 (3621 of 3627)
PASS: MLIR-Unit :: IR/./MLIRIRTests/100/130 (3622 of 3627)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/12/22 (3623 of 3627)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/11/22 (3624 of 3627)
PASS: MLIR :: mlir-reduce/dce-test.mlir (3625 of 3627)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2431.391063

rupprecht added a commit that referenced this pull request Nov 4, 2025
Omitting `-o /dev/null` may try to write output to the current dir,
which may not have write permissions on some build systems.

This fixes the test added by #165737
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.

8 participants