[llvm-branch-commits] [clang] [llvm] [OpenMP] Use device scope for atomic constructs on the device (PR #240426)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 9 07:52:09 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Robert Imschweiler (ro-i)

<details>
<summary>Changes</summary>

OpenMP 6.0, 17.8.5 specifies that "The memscope clause determines the binding thread set for an atomic region. If the memscope clause is not present, the behavior is as if the memscope clause appeared on the construct with the device scope-specifier."

Thread the sync scopes through codegen amd also add a release note because this is changing existing behavior.
Existing behavior was overly cautios for OpenMP 5.2, too, though. The atomic construct never specified the option of having system-level scope. This only came with the memscope clause and it's "all" scope-specifier.

Claude assisted with this patch.

---

Patch is 33.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/240426.diff


9 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+3) 
- (modified) clang/lib/CodeGen/CGAtomic.cpp (+16-11) 
- (modified) clang/lib/CodeGen/CGStmtOpenMP.cpp (+24-9) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+6-3) 
- (modified) clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp (+8-8) 
- (modified) clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp (+2-2) 
- (added) clang/test/OpenMP/target_atomic_device_scope.cpp (+130) 
- (modified) llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h (+5-2) 
- (modified) llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp (+11-10) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 603b3096259cb5..e6d1de482fcd54 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1546,6 +1546,9 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
 
 ### OpenMP Support
 
+- `atomic` constructs in target device code now use device scope instead of
+  system scope, matching the OpenMP 6.0 default when no `memscope` clause is
+  given.
 - Fixed an OpenMP `requires` directive read from a PCH or module losing its effect on
   semantic checks, which caused spurious `reverse_offload` errors.
 - Added the OpenMP 6.1 `#pragma omp flatten` loop transformation and the
diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp
index cd7b278eab15d9..d10f381e71648b 100644
--- a/clang/lib/CodeGen/CGAtomic.cpp
+++ b/clang/lib/CodeGen/CGAtomic.cpp
@@ -38,10 +38,13 @@ namespace {
     bool UseLibcall;
     LValue LVal;
     CGBitFieldInfo BFI;
+    llvm::SyncScope::ID Scope;
+
   public:
-    AtomicInfo(CodeGenFunction &CGF, LValue &lvalue)
+    AtomicInfo(CodeGenFunction &CGF, LValue &lvalue,
+               llvm::SyncScope::ID Scope = llvm::SyncScope::System)
         : CGF(CGF), AtomicSizeInBits(0), ValueSizeInBits(0),
-          EvaluationKind(TEK_Scalar), UseLibcall(true) {
+          EvaluationKind(TEK_Scalar), UseLibcall(true), Scope(Scope) {
       assert(!lvalue.isGlobalReg());
       ASTContext &C = CGF.getContext();
       if (lvalue.isSimple()) {
@@ -1631,7 +1634,7 @@ llvm::Value *AtomicInfo::EmitAtomicLoadOp(llvm::AtomicOrdering AO,
   if (shouldCastToInt(Addr.getElementType(), CmpXchg))
     Addr = castToAtomicIntPointer(Addr);
   llvm::LoadInst *Load = CGF.Builder.CreateLoad(Addr, "atomic-load");
-  Load->setAtomic(AO);
+  Load->setAtomic(AO, Scope);
   CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Load);
 
   // Other decoration.
@@ -1705,8 +1708,9 @@ RValue AtomicInfo::EmitAtomicLoad(AggValueSlot ResultSlot, SourceLocation Loc,
 /// we produce is an r-value of the atomic *value* type.
 RValue CodeGenFunction::EmitAtomicLoad(LValue src, SourceLocation loc,
                                        llvm::AtomicOrdering AO, bool IsVolatile,
-                                       AggValueSlot resultSlot) {
-  AtomicInfo Atomics(*this, src);
+                                       AggValueSlot resultSlot,
+                                       llvm::SyncScope::ID SSID) {
+  AtomicInfo Atomics(*this, src, SSID);
   return Atomics.EmitAtomicLoad(resultSlot, loc, /*AsValue=*/true, AO,
                                 IsVolatile);
 }
@@ -1796,7 +1800,7 @@ std::pair<llvm::Value *, llvm::Value *> AtomicInfo::EmitAtomicCompareExchangeOp(
   // Do the atomic store.
   Address Addr = getAtomicAddressAsAtomicIntPointer();
   auto *Inst = CGF.Builder.CreateAtomicCmpXchg(Addr, ExpectedVal, DesiredVal,
-                                               Success, Failure);
+                                               Success, Failure, Scope);
   // Other decoration.
   Inst->setVolatile(LVal.isVolatileQualified());
   Inst->setWeak(IsWeak);
@@ -2102,14 +2106,14 @@ void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue lvalue,
 /// storage for any padding that was necessary.
 void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
                                       llvm::AtomicOrdering AO, bool IsVolatile,
-                                      bool isInit) {
+                                      bool isInit, llvm::SyncScope::ID SSID) {
   // If this is an aggregate r-value, it should agree in type except
   // maybe for address-space qualification.
   assert(!rvalue.isAggregate() ||
          rvalue.getAggregateAddress().getElementType() ==
              dest.getAddress().getElementType());
 
-  AtomicInfo atomics(*this, dest);
+  AtomicInfo atomics(*this, dest, SSID);
   LValue LVal = atomics.getAtomicLValue();
 
   // If this is an initialization, just put the value there normally.
@@ -2157,7 +2161,7 @@ void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
       AO = llvm::AtomicOrdering::Release;
     // Initializations don't need to be atomic.
     if (!isInit) {
-      store->setAtomic(AO);
+      store->setAtomic(AO, SSID);
       getTargetHooks().setTargetAtomicMetadata(*this, *store);
     }
 
@@ -2212,8 +2216,9 @@ llvm::FenceInst *CodeGenFunction::emitAtomicFence(llvm::AtomicOrdering Order,
 
 void CodeGenFunction::EmitAtomicUpdate(
     LValue LVal, llvm::AtomicOrdering AO,
-    const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile) {
-  AtomicInfo Atomics(*this, LVal);
+    const llvm::function_ref<RValue(RValue)> &UpdateOp, bool IsVolatile,
+    llvm::SyncScope::ID SSID) {
+  AtomicInfo Atomics(*this, LVal, SSID);
   Atomics.EmitAtomicUpdate(AO, UpdateOp, IsVolatile);
 }
 
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 6b77bf53a29317..c940dee27f0e53 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -6926,12 +6926,23 @@ convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
   return ComplexVal;
 }
 
+/// Without a memscope clause, the binding thread set of an atomic region is
+/// all threads on the device.
+static llvm::SyncScope::ID getOMPAtomicSyncScope(CodeGenFunction &CGF,
+                                                 llvm::AtomicOrdering AO) {
+  if (!CGF.getLangOpts().OpenMPIsTargetDevice)
+    return llvm::SyncScope::System;
+  return CGF.getTargetHooks().getLLVMSyncScopeID(
+      CGF.getLangOpts(), SyncScope::DeviceScope, AO, CGF.getLLVMContext());
+}
+
 static void emitSimpleAtomicStore(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
                                   LValue LVal, RValue RVal) {
   if (LVal.isGlobalReg())
     CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
   else
-    CGF.EmitAtomicStore(RVal, LVal, AO, LVal.isVolatile(), /*isInit=*/false);
+    CGF.EmitAtomicStore(RVal, LVal, AO, LVal.isVolatile(), /*isInit=*/false,
+                        getOMPAtomicSyncScope(CGF, AO));
 }
 
 static RValue emitSimpleAtomicLoad(CodeGenFunction &CGF,
@@ -6939,9 +6950,11 @@ static RValue emitSimpleAtomicLoad(CodeGenFunction &CGF,
                                    SourceLocation Loc) {
   if (LVal.isGlobalReg())
     return CGF.EmitLoadOfLValue(LVal, Loc);
-  return CGF.EmitAtomicLoad(
-      LVal, Loc, llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO),
-      LVal.isVolatile());
+  llvm::AtomicOrdering LoadAO =
+      llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO);
+  return CGF.EmitAtomicLoad(LVal, Loc, LoadAO, LVal.isVolatile(),
+                            AggValueSlot::ignored(),
+                            getOMPAtomicSyncScope(CGF, LoadAO));
 }
 
 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
@@ -7133,8 +7146,8 @@ static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
       UpdateVal = CGF.Builder.CreateCast(llvm::Instruction::CastOps::UIToFP, IC,
                                          X.getAddress().getElementType());
   }
-  llvm::AtomicRMWInst *Res =
-      CGF.emitAtomicRMWInst(RMWOp, X.getAddress(), UpdateVal, AO);
+  llvm::AtomicRMWInst *Res = CGF.emitAtomicRMWInst(
+      RMWOp, X.getAddress(), UpdateVal, AO, getOMPAtomicSyncScope(CGF, AO));
   return std::make_pair(true, RValue::get(Res));
 }
 
@@ -7156,7 +7169,8 @@ std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
       EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
     } else {
       // Perform compare-and-swap procedure.
-      EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
+      EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified(),
+                       getOMPAtomicSyncScope(*this, AO));
     }
   }
   return Res;
@@ -7409,18 +7423,19 @@ static void emitOMPAtomicCompareExpr(
   bool FineGrainedMemory =
       AtomicOpts.getOption(AtomicOptionKind::FineGrainedMemory);
   bool RemoteMemory = AtomicOpts.getOption(AtomicOptionKind::RemoteMemory);
+  llvm::SyncScope::ID SSID = getOMPAtomicSyncScope(CGF, AO);
   if (FailAO == llvm::AtomicOrdering::NotAtomic) {
     // fail clause was not mentioned on the
     // "#pragma omp atomic compare" construct.
     CGF.Builder.restoreIP(OMPBuilder.createAtomicCompare(
         CGF.Builder, XOpVal, VOpVal, ROpVal, EVal, DVal, AO, Op, IsXBinopExpr,
         IsPostfixUpdate, IsFailOnly, /*IsWeak=*/false, IgnoreDenormalMode,
-        FineGrainedMemory, RemoteMemory));
+        FineGrainedMemory, RemoteMemory, SSID));
   } else
     CGF.Builder.restoreIP(OMPBuilder.createAtomicCompare(
         CGF.Builder, XOpVal, VOpVal, ROpVal, EVal, DVal, AO, Op, IsXBinopExpr,
         IsPostfixUpdate, IsFailOnly, FailAO, /*IsWeak=*/false,
-        IgnoreDenormalMode, FineGrainedMemory, RemoteMemory));
+        IgnoreDenormalMode, FineGrainedMemory, RemoteMemory, SSID));
 }
 
 static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index e6ecf3bca3e94c..0152fd44327010 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -4440,12 +4440,14 @@ class CodeGenFunction : public CodeGenTypeCache {
 
   RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
                         llvm::AtomicOrdering AO, bool IsVolatile = false,
-                        AggValueSlot slot = AggValueSlot::ignored());
+                        AggValueSlot slot = AggValueSlot::ignored(),
+                        llvm::SyncScope::ID SSID = llvm::SyncScope::System);
 
   void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
 
   void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO,
-                       bool IsVolatile, bool isInit);
+                       bool IsVolatile, bool isInit,
+                       llvm::SyncScope::ID SSID = llvm::SyncScope::System);
 
   std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange(
       LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
@@ -4471,7 +4473,8 @@ class CodeGenFunction : public CodeGenTypeCache {
 
   void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO,
                         const llvm::function_ref<RValue(RValue)> &UpdateOp,
-                        bool IsVolatile);
+                        bool IsVolatile,
+                        llvm::SyncScope::ID SSID = llvm::SyncScope::System);
 
   /// EmitToMemory - Change a scalar value from its value
   /// representation to its in-memory representation.
diff --git a/clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp b/clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp
index f1771252a4d58c..59f59419c48fe8 100644
--- a/clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp
+++ b/clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp
@@ -11,7 +11,7 @@ double dv, dx;
 // DEFAULT-SAME: ) #[[ATTR0:[0-9]+]] {
 // DEFAULT-NEXT:  [[ENTRY:.*:]]
 // DEFAULT-NEXT:    [[TMP0:%.*]] = load float, ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
-// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] monotonic, align 4, !amdgpu.no.fine.grained.memory [[META4:![0-9]+]], !amdgpu.no.remote.memory [[META4]]
+// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] syncscope("agent") monotonic, align 4, !amdgpu.no.fine.grained.memory [[META4:![0-9]+]], !amdgpu.no.remote.memory [[META4]]
 // DEFAULT-NEXT:    [[ADD:%.*]] = fadd float [[TMP1]], [[TMP0]]
 // DEFAULT-NEXT:    store float [[ADD]], ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
 // DEFAULT-NEXT:    ret void
@@ -20,7 +20,7 @@ double dv, dx;
 // UNSAFE-FP-ATOMICS-SAME: ) #[[ATTR0:[0-9]+]] {
 // UNSAFE-FP-ATOMICS-NEXT:  [[ENTRY:.*:]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[TMP0:%.*]] = load float, ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
-// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] monotonic, align 4, !atomic.ignore.denormal.mode [[META4:![0-9]+]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] syncscope("agent") monotonic, align 4, !atomic.ignore.denormal.mode [[META4:![0-9]+]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[ADD:%.*]] = fadd float [[TMP1]], [[TMP0]]
 // UNSAFE-FP-ATOMICS-NEXT:    store float [[ADD]], ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
 // UNSAFE-FP-ATOMICS-NEXT:    ret void
@@ -34,7 +34,7 @@ void atomic_fadd_f32() {
 // DEFAULT-SAME: ) #[[ATTR0]] {
 // DEFAULT-NEXT:  [[ENTRY:.*:]]
 // DEFAULT-NEXT:    [[TMP0:%.*]] = load double, ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
-// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] syncscope("agent") monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // DEFAULT-NEXT:    [[ADD:%.*]] = fadd double [[TMP1]], [[TMP0]]
 // DEFAULT-NEXT:    store double [[ADD]], ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
 // DEFAULT-NEXT:    ret void
@@ -43,7 +43,7 @@ void atomic_fadd_f32() {
 // UNSAFE-FP-ATOMICS-SAME: ) #[[ATTR0]] {
 // UNSAFE-FP-ATOMICS-NEXT:  [[ENTRY:.*:]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[TMP0:%.*]] = load double, ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
-// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fadd ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] syncscope("agent") monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[ADD:%.*]] = fadd double [[TMP1]], [[TMP0]]
 // UNSAFE-FP-ATOMICS-NEXT:    store double [[ADD]], ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
 // UNSAFE-FP-ATOMICS-NEXT:    ret void
@@ -57,14 +57,14 @@ void atomic_fadd_f64() {
 // DEFAULT-SAME: ) #[[ATTR0]] {
 // DEFAULT-NEXT:  [[ENTRY:.*:]]
 // DEFAULT-NEXT:    [[TMP0:%.*]] = load double, ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
-// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fmin ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fmin ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] syncscope("agent") monotonic, align 8, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // DEFAULT-NEXT:    ret void
 //
 // UNSAFE-FP-ATOMICS-LABEL: define hidden void @_Z23atomic_compare_fmin_f64v(
 // UNSAFE-FP-ATOMICS-SAME: ) #[[ATTR0]] {
 // UNSAFE-FP-ATOMICS-NEXT:  [[ENTRY:.*:]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[TMP0:%.*]] = load double, ptr addrspacecast (ptr addrspace(1) @dv to ptr), align 8
-// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fmin ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] monotonic, align 8, !atomic.ignore.denormal.mode [[META4]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fmin ptr addrspacecast (ptr addrspace(1) @dx to ptr), double [[TMP0]] syncscope("agent") monotonic, align 8, !atomic.ignore.denormal.mode [[META4]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // UNSAFE-FP-ATOMICS-NEXT:    ret void
 //
 void atomic_compare_fmin_f64() {
@@ -77,14 +77,14 @@ void atomic_compare_fmin_f64() {
 // DEFAULT-SAME: ) #[[ATTR0]] {
 // DEFAULT-NEXT:  [[ENTRY:.*:]]
 // DEFAULT-NEXT:    [[TMP0:%.*]] = load float, ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
-// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fmax ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] monotonic, align 4, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// DEFAULT-NEXT:    [[TMP1:%.*]] = atomicrmw fmax ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] syncscope("agent") monotonic, align 4, !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // DEFAULT-NEXT:    ret void
 //
 // UNSAFE-FP-ATOMICS-LABEL: define hidden void @_Z23atomic_compare_fmax_f32v(
 // UNSAFE-FP-ATOMICS-SAME: ) #[[ATTR0]] {
 // UNSAFE-FP-ATOMICS-NEXT:  [[ENTRY:.*:]]
 // UNSAFE-FP-ATOMICS-NEXT:    [[TMP0:%.*]] = load float, ptr addrspacecast (ptr addrspace(1) @fv to ptr), align 4
-// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fmax ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] monotonic, align 4, !atomic.ignore.denormal.mode [[META4]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
+// UNSAFE-FP-ATOMICS-NEXT:    [[TMP1:%.*]] = atomicrmw fmax ptr addrspacecast (ptr addrspace(1) @fx to ptr), float [[TMP0]] syncscope("agent") monotonic, align 4, !atomic.ignore.denormal.mode [[META4]], !amdgpu.no.fine.grained.memory [[META4]], !amdgpu.no.remote.memory [[META4]]
 // UNSAFE-FP-ATOMICS-NEXT:    ret void
 //
 void atomic_compare_fmax_f32() {
diff --git a/clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp b/clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
index da833bd2f94552..39f98bed8a6392 100644
--- a/clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
+++ b/clang/test/OpenMP/nvptx_nested_parallel_codegen.cpp
@@ -123,7 +123,7 @@ int main() {
 // CHECK1-NEXT:    [[C_ADDR:%.*]] = alloca ptr, align 8
 // CHECK1-NEXT:    store ptr [[C]], ptr [[C_ADDR]], align 8
 // CHECK1-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[C_ADDR]], align 8
-// CHECK1-NEXT:    [[TMP1:%.*]] = atomicrmw add ptr [[TMP0]], i32 1 monotonic, align 4
+// CHECK1-NEXT:    [[TMP1:%.*]] = atomicrmw add ptr [[TMP0]], i32 1 syncscope("device") monotonic, align 4
 // CHECK1-NEXT:    ret void
 //
 //
@@ -235,7 +235,7 @@ int main() {
 // CHECK2-NEXT:    [[C_ADDR:%.*]] = alloca ptr, align 4
 // CHECK2-NEXT:    store ptr [[C]], ptr [[C_ADDR]], align 4
 // CHECK2-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[C_ADDR]], align 4
-// CHECK2-NEXT:    [[TMP1:%.*]] = atomicrmw add ptr [[TMP0]], i32 1 monotonic, align 4
+// CHECK2-NEXT:    [[TMP1:%.*]] = atomicrmw add ptr [[TMP0]], i32 1 syncscope("device") monotonic, align 4
 // CHECK2-NEXT:    ret void
 //
 //
diff --git a/clang/test/OpenMP/target_atomic_device_scope.cpp b/clang/test/OpenMP/target_atomic_device_scope.cpp
new file mode 100644
index 00000000000000..b16c5c447f7756
--- /dev/null
+++ b/clang/test/OpenMP/target_atomic_device_scope.cpp
@@ -0,0 +1,130 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck -check-prefix=HOST %s
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-target-device -o - | FileCheck -check-prefix=AMDGPU %s
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple nvptx64-nvidia-cuda -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-target-device -o - | FileCheck -check-prefix=NVPTX %s
+
+#pragma omp declare target
+
+int iv, ix;
+float fv, fx;
+double dv, dx;
+
+// HOST-LABEL: define dso_local void @_Z10atomic_opsv(
+// HOST-SAME: ) #[[ATTR0:[0-9]+]] {
+// HOST-NEXT:  [[ENTRY:.*]]:
+// HOST-NEXT:    [[ATOMIC_TEMP:%.*]] = alloca float, align 4
+// HOST-NEXT:    [[ATOMIC_LOAD:%.*]] = load atomic i32, ptr @ix monotonic, align 4
+// HOST-NEXT:    store i32 [[ATOMIC_LOAD]], ptr @iv, align 4
+// HOST-NEXT:    [[TMP0:%.*]] = load i32, ptr @iv, align 4
+// HOST-NEXT:    store atomic i32 [[TMP0]], ptr @ix monotonic, align 4
+// HOST-NEXT:    [[TMP1:%.*]] = load i32, ptr @iv, ali...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/240426


More information about the llvm-branch-commits mailing list