[clang] 486370c - [AMDGPU][Clang] refactor addrspace and scope checks [NFC] (#199175)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 28 00:17:06 PDT 2026
Author: Sameer Sahasrabuddhe
Date: 2026-05-28T07:17:00Z
New Revision: 486370c33fbd75247ef608a2ebc04ddaefd2ab46
URL: https://github.com/llvm/llvm-project/commit/486370c33fbd75247ef608a2ebc04ddaefd2ab46
DIFF: https://github.com/llvm/llvm-project/commit/486370c33fbd75247ef608a2ebc04ddaefd2ab46.diff
LOG: [AMDGPU][Clang] refactor addrspace and scope checks [NFC] (#199175)
Assisted-By: Claude Opus 4.6
Added:
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaAMDGPU.cpp
clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-cooperative-atomics.cl
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e330ea03d0544..bcd6f29fe2369 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -14236,7 +14236,7 @@ def note_amdgcn_unguarded_builtin_silence
: Note<"enclose %0 in a __builtin_amdgcn_is_invocable check to silence "
"this warning">;
-def err_amdgcn_coop_atomic_invalid_as : Error<"cooperative atomic requires a global or generic pointer">;
+def err_amdgcn_global_or_flat_pointer_required : Error<"builtin requires a global or generic pointer">;
def err_amdgcn_dmask_has_too_many_bits_set
: Error<"dmask argument cannot have more bits set than there are elements "
diff --git a/clang/lib/Sema/SemaAMDGPU.cpp b/clang/lib/Sema/SemaAMDGPU.cpp
index c7420f0559a51..60f74fd15226f 100644
--- a/clang/lib/Sema/SemaAMDGPU.cpp
+++ b/clang/lib/Sema/SemaAMDGPU.cpp
@@ -452,19 +452,38 @@ bool SemaAMDGPU::checkAtomicOrderingCABIArg(Expr *E, bool MayLoad,
return false;
}
-bool SemaAMDGPU::checkCoopAtomicFunctionCall(CallExpr *TheCall, bool IsStore) {
- bool Fail = false;
-
- // First argument is a global or generic pointer.
+// Check that the first argument to TheCall is a global or generic pointer.
+static bool checkGlobalOrFlatPointerArg(SemaAMDGPU &S, CallExpr *TheCall) {
Expr *PtrArg = TheCall->getArg(0);
QualType PtrTy = PtrArg->getType()->getPointeeType();
- unsigned AS = getASTContext().getTargetAddressSpace(PtrTy.getAddressSpace());
+ unsigned AS =
+ S.getASTContext().getTargetAddressSpace(PtrTy.getAddressSpace());
if (AS != llvm::AMDGPUAS::FLAT_ADDRESS &&
AS != llvm::AMDGPUAS::GLOBAL_ADDRESS) {
- Fail = true;
- Diag(TheCall->getBeginLoc(), diag::err_amdgcn_coop_atomic_invalid_as)
- << PtrArg->getSourceRange();
+ return S.Diag(TheCall->getBeginLoc(),
+ diag::err_amdgcn_global_or_flat_pointer_required)
+ << PtrArg->getSourceRange();
+ }
+ return false;
+}
+
+static bool checkScopeAsInt(SemaAMDGPU &S, Expr *Scope) {
+ if (Scope->isValueDependent())
+ return false;
+ auto ScopeModel = AtomicScopeModel::create(AtomicScopeModelKind::Generic);
+ if (std::optional<llvm::APSInt> Result =
+ Scope->getIntegerConstantExpr(S.SemaRef.Context)) {
+ if (!ScopeModel->isValid(Result->getZExtValue())) {
+ return S.Diag(Scope->getBeginLoc(),
+ diag::err_atomic_op_has_invalid_sync_scope)
+ << Scope->getSourceRange();
+ }
}
+ return false;
+}
+
+bool SemaAMDGPU::checkCoopAtomicFunctionCall(CallExpr *TheCall, bool IsStore) {
+ bool Fail = checkGlobalOrFlatPointerArg(*this, TheCall);
Expr *AO = TheCall->getArg(IsStore ? 2 : 1);
Expr *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
@@ -488,27 +507,15 @@ bool SemaAMDGPU::checkCoopAtomicFunctionCall(CallExpr *TheCall, bool IsStore) {
}
bool SemaAMDGPU::checkAtomicMonitorLoad(CallExpr *TheCall) {
- bool Fail = false;
-
Expr *AO = TheCall->getArg(1);
Expr *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
if (AO->isValueDependent() || Scope->isValueDependent())
return false;
- Fail |= checkAtomicOrderingCABIArg(TheCall->getArg(1), /*MayLoad=*/true,
- /*MayStore=*/false);
-
- auto ScopeModel = AtomicScopeModel::create(AtomicScopeModelKind::Generic);
- if (std::optional<llvm::APSInt> Result =
- Scope->getIntegerConstantExpr(SemaRef.Context)) {
- if (!ScopeModel->isValid(Result->getZExtValue())) {
- Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_sync_scope)
- << Scope->getSourceRange();
- Fail = true;
- }
- }
-
+ bool Fail = checkAtomicOrderingCABIArg(AO, /*MayLoad=*/true,
+ /*MayStore=*/false);
+ Fail |= checkScopeAsInt(*this, Scope);
return Fail;
}
diff --git a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-cooperative-atomics.cl b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-cooperative-atomics.cl
index 8f02e6775d37a..a440a1c040270 100644
--- a/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-cooperative-atomics.cl
+++ b/clang/test/SemaOpenCL/builtins-amdgcn-error-gfx1250-cooperative-atomics.cl
@@ -48,7 +48,7 @@ v4i test_amdgcn_cooperative_atomic_load_8x16B_acq_rel(global v4i* gaddr)
void test_amdgcn_cooperative_atomic_store_32x4B__sharedptr(local int* addr, int val)
{
- __builtin_amdgcn_cooperative_atomic_store_32x4B(addr, val, __ATOMIC_RELAXED, ""); // expected-error {{cooperative atomic requires a global or generic pointer}}
+ __builtin_amdgcn_cooperative_atomic_store_32x4B(addr, val, __ATOMIC_RELAXED, ""); // expected-error {{builtin requires a global or generic pointer}}
}
void test_amdgcn_cooperative_atomic_store_32x4B__ordering_not_imm(local int* addr, int ord, int val)
More information about the cfe-commits
mailing list