Skip to content

Commit 5d9d890

Browse files
authored
[NVPTX] Add more clear error message for using invalid syncscope (#165737)
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.
1 parent ecaaebf commit 5d9d890

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ bool NVPTXDAGToDAGISel::tryFence(SDNode *N) {
18361836
return true;
18371837
}
18381838

1839-
NVPTXScopes::NVPTXScopes(LLVMContext &C) {
1839+
NVPTXScopes::NVPTXScopes(LLVMContext &C) : Context(&C) {
18401840
Scopes[C.getOrInsertSyncScopeID("singlethread")] = NVPTX::Scope::Thread;
18411841
Scopes[C.getOrInsertSyncScopeID("")] = NVPTX::Scope::System;
18421842
Scopes[C.getOrInsertSyncScopeID("block")] = NVPTX::Scope::Block;
@@ -1851,11 +1851,21 @@ NVPTX::Scope NVPTXScopes::operator[](SyncScope::ID ID) const {
18511851

18521852
auto S = Scopes.find(ID);
18531853
if (S == Scopes.end()) {
1854-
// TODO:
1855-
// - Add API to LLVMContext to get the name of a single scope.
1856-
// - Use that API here to print an error containing the name
1857-
// of this Unknown ID.
1858-
report_fatal_error(formatv("Could not find scope ID={}.", int(ID)));
1854+
auto scopeName = Context->getSyncScopeName(ID);
1855+
assert(scopeName.has_value() && "Scope name must exist.");
1856+
1857+
// Build list of supported syncscopes programmatically
1858+
SmallVector<StringRef> supportedScopes;
1859+
for (const auto &Entry : Scopes) {
1860+
if (auto name = Context->getSyncScopeName(Entry.first))
1861+
supportedScopes.push_back(name->empty() ? "<empty string>" : *name);
1862+
}
1863+
1864+
reportFatalUsageError(
1865+
formatv("NVPTX backend does not support syncscope \"{0}\" (ID={1}).\n"
1866+
"Supported syncscopes are: {2}.",
1867+
scopeName.value(), int(ID),
1868+
make_range(supportedScopes.begin(), supportedScopes.end())));
18591869
}
18601870
return S->second;
18611871
}

llvm/lib/Target/NVPTX/NVPTXISelDAGToDAG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct NVPTXScopes {
3535

3636
private:
3737
SmallMapVector<SyncScope::ID, NVPTX::Scope, 8> Scopes{};
38+
LLVMContext *Context = nullptr;
3839
};
3940

4041
class LLVM_LIBRARY_VISIBILITY NVPTXDAGToDAGISel : public SelectionDAGISel {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: not llc -mcpu=sm_100a -mtriple=nvptx64 -mattr=+ptx86 %s 2>&1 | FileCheck %s
2+
3+
; Test that we get a clear error message when using an unsupported syncscope.
4+
5+
; CHECK: NVPTX backend does not support syncscope "agent"
6+
; CHECK: Supported syncscopes are: singlethread, <empty string>, block, cluster, device
7+
define i32 @cmpxchg_unsupported_syncscope_agent(ptr %addr, i32 %cmp, i32 %new) {
8+
%result = cmpxchg ptr %addr, i32 %cmp, i32 %new syncscope("agent") monotonic monotonic
9+
%value = extractvalue { i32, i1 } %result, 0
10+
ret i32 %value
11+
}

0 commit comments

Comments
 (0)