[llvm-branch-commits] [clang] [llvm] [Clang][OpenMP] Add the OpenMP 6.0 memscope clause (PR #241296)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 03:48:12 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang-codegen

Author: Julian Brown (jtb20)

<details>
<summary>Changes</summary>

This patch adds support for the 'memscope' clause from OpenMP 6.0 on the 'flush' and 'atomic' directives. The clause selects the threads a flush or atomic synchronizes with: those on the device (the default), those in the contention group, or those on all devices.  On 'flush', a list may be specified alongside a memscope, but not together with a memory-order clause (OpenMP 6.0, "17.8.6 flush Construct").

For the host, the scope is passed to the __kmpc_flush_explicit libomp entry point as a __MEMORY_SCOPE_* value.  For a GPU, the scope maps to a target-specific syncscope for the fence.

Assisted-By: Claude

---

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


26 Files Affected:

- (modified) clang/docs/OpenMPSupport.md (+1-1) 
- (modified) clang/docs/ReleaseNotes.md (+3-3) 
- (modified) clang/include/clang/AST/OpenMPClause.h (+82) 
- (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+5) 
- (modified) clang/include/clang/Basic/OpenMPKinds.def (+9) 
- (modified) clang/include/clang/Basic/OpenMPKinds.h (+7) 
- (modified) clang/include/clang/Sema/SemaOpenMP.h (+6) 
- (modified) clang/lib/AST/OpenMPClause.cpp (+8) 
- (modified) clang/lib/AST/StmtProfile.cpp (+2) 
- (modified) clang/lib/Basic/OpenMPKinds.cpp (+15) 
- (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+10-8) 
- (modified) clang/lib/CodeGen/CGOpenMPRuntime.h (+7-2) 
- (modified) clang/lib/CodeGen/CGStmtOpenMP.cpp (+57-32) 
- (modified) clang/lib/Parse/ParseOpenMP.cpp (+1) 
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+28-1) 
- (modified) clang/lib/Sema/TreeTransform.h (+21) 
- (modified) clang/lib/Serialization/ASTReader.cpp (+9) 
- (modified) clang/lib/Serialization/ASTWriter.cpp (+6) 
- (added) clang/test/OpenMP/flush_memscope_codegen.cpp (+101) 
- (added) clang/test/OpenMP/memscope_ast_print.cpp (+44) 
- (added) clang/test/OpenMP/memscope_messages.cpp (+26) 
- (modified) clang/tools/libclang/CIndex.cpp (+2) 
- (modified) llvm/include/llvm/Frontend/OpenMP/OMP.td (+3) 
- (modified) llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h (+7-3) 
- (modified) llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp (+8-6) 
- (modified) llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp (+31) 


``````````diff
diff --git a/clang/docs/OpenMPSupport.md b/clang/docs/OpenMPSupport.md
index 3e4311df9e4a18..218a62652cd145 100644
--- a/clang/docs/OpenMPSupport.md
+++ b/clang/docs/OpenMPSupport.md
@@ -309,7 +309,7 @@ implementation.
 | depth clause on loop fuse                                 | {none}`unclaimed`   | {none}`unclaimed`   |                                                                                                                                     |
 | workdistribute construct                                  |                     | {none}`in progress` | @skc7, @mjklemm                                                                                                                     |
 | task_iteration                                            | {none}`unclaimed`   | {none}`unclaimed`   |                                                                                                                                     |
-| memscope clause for atomic and flush                      | {none}`unclaimed`   | {none}`unclaimed`   |                                                                                                                                     |
+| memscope clause for atomic and flush                      | {part}`partial`     | {none}`unclaimed`   | Clang: flush, and the flushes implied by atomic (the atomic operation itself stays system-scope)                                    |
 | transparent clause (hull tasks)                           | {part}`partial`     | {none}`unclaimed`   | Clang parsing/sema [PR174646][PR174646]                                                                                             |
 | rule-based compound directives                            | {part}`In Progress` | {part}`In Progress` | kparzysz Testing for Fortran missing                                                                                                |
 | C23, C++23                                                | {none}`unclaimed`   |                     |                                                                                                                                     |
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index c0e602b72bd0d4..a1c2fb8ac0fab4 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1589,9 +1589,9 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
     another tile's intra-tile loop.
 
 - The `acquire`, `release` and `acq_rel` clauses of the `flush` directive now
-  take effect. On GPUs a flush is emitted as a fence of the requested ordering.
-  On the host, a flush other than a seq_cst flush needs an OpenMP runtime from
-  this release.
+  take effect, and the OpenMP 6.0 `memscope` clause is supported on `flush` and
+  `atomic`. On the host, a flush other than a seq_cst flush at device scope needs
+  an OpenMP runtime from this release.
 - Added parsing and semantic support for `dims` modifier in `num_teams`,
   `thread_limit` and `num_threads` clauses for OpenMP 6.1 or later.
 - Map-type-modifying modifiers applied to a list item with a user-defined mapper
diff --git a/clang/include/clang/AST/OpenMPClause.h b/clang/include/clang/AST/OpenMPClause.h
index 3fc2e696b1c2d0..19d3379d06c4a4 100644
--- a/clang/include/clang/AST/OpenMPClause.h
+++ b/clang/include/clang/AST/OpenMPClause.h
@@ -2400,6 +2400,88 @@ class OMPSeverityClause final : public OMPClause {
   }
 };
 
+/// This represents the 'memscope' clause in the '#pragma omp atomic' and
+/// '#pragma omp flush' directives.
+///
+/// \code
+/// #pragma omp flush release memscope(all)
+/// \endcode
+/// In this example directive '#pragma omp flush' has simple 'memscope' clause
+/// with scope specifier 'all'.
+class OMPMemscopeClause final : public OMPClause {
+  friend class OMPClauseReader;
+
+  /// Location of '('
+  SourceLocation LParenLoc;
+
+  /// A kind of the 'memscope' clause.
+  OpenMPMemscopeClauseKind Kind = OMPC_MEMSCOPE_unknown;
+
+  /// Start location of the kind in source code.
+  SourceLocation KindKwLoc;
+
+  /// Set kind of the clause.
+  ///
+  /// \param K Kind of clause.
+  void setMemscopeKind(OpenMPMemscopeClauseKind K) { Kind = K; }
+
+  /// Set clause kind location.
+  ///
+  /// \param KLoc Kind location.
+  void setMemscopeKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
+
+  /// Sets the location of '('.
+  void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
+
+public:
+  /// Build 'memscope' clause with argument \a A ('all', 'cgroup' or
+  /// 'device').
+  ///
+  /// \param A Argument of the clause ('all', 'cgroup' or 'device').
+  /// \param ALoc Starting location of the argument.
+  /// \param StartLoc Starting location of the clause.
+  /// \param LParenLoc Location of '('.
+  /// \param EndLoc Ending location of the clause.
+  OMPMemscopeClause(OpenMPMemscopeClauseKind A, SourceLocation ALoc,
+                    SourceLocation StartLoc, SourceLocation LParenLoc,
+                    SourceLocation EndLoc)
+      : OMPClause(llvm::omp::OMPC_memscope, StartLoc, EndLoc),
+        LParenLoc(LParenLoc), Kind(A), KindKwLoc(ALoc) {}
+
+  /// Build an empty clause.
+  OMPMemscopeClause()
+      : OMPClause(llvm::omp::OMPC_memscope, SourceLocation(),
+                  SourceLocation()) {}
+
+  /// Returns the location of '('.
+  SourceLocation getLParenLoc() const { return LParenLoc; }
+
+  /// Returns kind of the clause.
+  OpenMPMemscopeClauseKind getMemscopeKind() const { return Kind; }
+
+  /// Returns location of clause kind.
+  SourceLocation getMemscopeKindKwLoc() const { return KindKwLoc; }
+
+  child_range children() {
+    return child_range(child_iterator(), child_iterator());
+  }
+
+  const_child_range children() const {
+    return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+
+  child_range used_children() {
+    return child_range(child_iterator(), child_iterator());
+  }
+  const_child_range used_children() const {
+    return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+
+  static bool classof(const OMPClause *T) {
+    return T->getClauseKind() == llvm::omp::OMPC_memscope;
+  }
+};
+
 /// This represents the 'message' clause in the '#pragma omp error' and the
 /// '#pragma omp parallel' directives.
 ///
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 56aa9b772b1e75..39aa3dbc5b9816 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3677,6 +3677,11 @@ bool RecursiveASTVisitor<Derived>::VisitOMPSeverityClause(OMPSeverityClause *) {
   return true;
 }
 
+template <typename Derived>
+bool RecursiveASTVisitor<Derived>::VisitOMPMemscopeClause(OMPMemscopeClause *) {
+  return true;
+}
+
 template <typename Derived>
 bool RecursiveASTVisitor<Derived>::VisitOMPMessageClause(OMPMessageClause *C) {
   TRY_TO(TraverseStmt(C->getMessageString()));
diff --git a/clang/include/clang/Basic/OpenMPKinds.def b/clang/include/clang/Basic/OpenMPKinds.def
index 5b5222a2ab4463..a29bf3cfb474fd 100644
--- a/clang/include/clang/Basic/OpenMPKinds.def
+++ b/clang/include/clang/Basic/OpenMPKinds.def
@@ -53,6 +53,9 @@
 #ifndef OPENMP_SEVERITY_KIND
 #define OPENMP_SEVERITY_KIND(Name)
 #endif
+#ifndef OPENMP_MEMSCOPE_KIND
+#define OPENMP_MEMSCOPE_KIND(Name)
+#endif
 #ifndef OPENMP_DEFAULTMAP_MODIFIER
 #define OPENMP_DEFAULTMAP_MODIFIER(Name)
 #endif
@@ -199,6 +202,11 @@ OPENMP_AT_KIND(execution)
 OPENMP_SEVERITY_KIND(fatal)
 OPENMP_SEVERITY_KIND(warning)
 
+// Scope specifiers for 'memscope' clause.
+OPENMP_MEMSCOPE_KIND(all)
+OPENMP_MEMSCOPE_KIND(cgroup)
+OPENMP_MEMSCOPE_KIND(device)
+
 // Map types for 'map' clause.
 OPENMP_MAP_KIND(alloc)
 OPENMP_MAP_KIND(to)
@@ -330,6 +338,7 @@ OPENMP_USE_DEVICE_PTR_FALLBACK_MODIFIER(fb_preserve)
 #undef OPENMP_ATOMIC_FAIL_MODIFIER
 #undef OPENMP_AT_KIND
 #undef OPENMP_SEVERITY_KIND
+#undef OPENMP_MEMSCOPE_KIND
 #undef OPENMP_MAP_KIND
 #undef OPENMP_MAP_MODIFIER_KIND
 #undef OPENMP_MOTION_MODIFIER_KIND
diff --git a/clang/include/clang/Basic/OpenMPKinds.h b/clang/include/clang/Basic/OpenMPKinds.h
index a10d2d4bb34473..1284f0aa659cb5 100644
--- a/clang/include/clang/Basic/OpenMPKinds.h
+++ b/clang/include/clang/Basic/OpenMPKinds.h
@@ -153,6 +153,13 @@ enum OpenMPSeverityClauseKind {
   OMPC_SEVERITY_unknown
 };
 
+/// OpenMP scope specifiers for 'memscope' clause.
+enum OpenMPMemscopeClauseKind {
+#define OPENMP_MEMSCOPE_KIND(Name) OMPC_MEMSCOPE_##Name,
+#include "clang/Basic/OpenMPKinds.def"
+  OMPC_MEMSCOPE_unknown
+};
+
 /// OpenMP device type for 'device_type' clause.
 enum OpenMPDeviceType {
 #define OPENMP_DEVICE_TYPE_KIND(Name) \
diff --git a/clang/include/clang/Sema/SemaOpenMP.h b/clang/include/clang/Sema/SemaOpenMP.h
index a8a291d02722ce..4926200e987a55 100644
--- a/clang/include/clang/Sema/SemaOpenMP.h
+++ b/clang/include/clang/Sema/SemaOpenMP.h
@@ -1200,6 +1200,12 @@ class SemaOpenMP : public SemaBase {
                                        SourceLocation StartLoc,
                                        SourceLocation LParenLoc,
                                        SourceLocation EndLoc);
+  /// Called on well-formed 'memscope' clause.
+  OMPClause *ActOnOpenMPMemscopeClause(OpenMPMemscopeClauseKind Kind,
+                                       SourceLocation KindLoc,
+                                       SourceLocation StartLoc,
+                                       SourceLocation LParenLoc,
+                                       SourceLocation EndLoc);
 
   /// Called on well-formed 'message' clause.
   /// passing string for message.
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index f0102720f7d33b..89bfee5ad6c716 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -180,6 +180,7 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
   case OMPC_self_maps:
   case OMPC_at:
   case OMPC_severity:
+  case OMPC_memscope:
   case OMPC_device_type:
   case OMPC_match:
   case OMPC_nontemporal:
@@ -285,6 +286,7 @@ const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C)
   case OMPC_self_maps:
   case OMPC_at:
   case OMPC_severity:
+  case OMPC_memscope:
   case OMPC_message:
   case OMPC_device_type:
   case OMPC_match:
@@ -2173,6 +2175,12 @@ void OMPClausePrinter::VisitOMPSeverityClause(OMPSeverityClause *Node) {
      << ")";
 }
 
+void OMPClausePrinter::VisitOMPMemscopeClause(OMPMemscopeClause *Node) {
+  OS << "memscope("
+     << getOpenMPSimpleClauseTypeName(OMPC_memscope, Node->getMemscopeKind())
+     << ")";
+}
+
 void OMPClausePrinter::VisitOMPMessageClause(OMPMessageClause *Node) {
   OS << "message(";
   if (Expr *E = Node->getMessageString())
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 476610b9956c71..20a0508da38f22 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -607,6 +607,8 @@ void OMPClauseProfiler::VisitOMPAtClause(const OMPAtClause *C) {}
 
 void OMPClauseProfiler::VisitOMPSeverityClause(const OMPSeverityClause *C) {}
 
+void OMPClauseProfiler::VisitOMPMemscopeClause(const OMPMemscopeClause *C) {}
+
 void OMPClauseProfiler::VisitOMPMessageClause(const OMPMessageClause *C) {
   if (C->getMessageString())
     Profiler->VisitStmt(C->getMessageString());
diff --git a/clang/lib/Basic/OpenMPKinds.cpp b/clang/lib/Basic/OpenMPKinds.cpp
index 3d06da48b03dcf..8f19f5598826ca 100644
--- a/clang/lib/Basic/OpenMPKinds.cpp
+++ b/clang/lib/Basic/OpenMPKinds.cpp
@@ -149,6 +149,11 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
 #define OPENMP_SEVERITY_KIND(Name) .Case(#Name, OMPC_SEVERITY_##Name)
 #include "clang/Basic/OpenMPKinds.def"
         .Default(OMPC_SEVERITY_unknown);
+  case OMPC_memscope:
+    return llvm::StringSwitch<OpenMPMemscopeClauseKind>(Str)
+#define OPENMP_MEMSCOPE_KIND(Name) .Case(#Name, OMPC_MEMSCOPE_##Name)
+#include "clang/Basic/OpenMPKinds.def"
+        .Default(OMPC_MEMSCOPE_unknown);
   case OMPC_lastprivate:
     return llvm::StringSwitch<OpenMPLastprivateModifier>(Str)
 #define OPENMP_LASTPRIVATE_KIND(Name) .Case(#Name, OMPC_LASTPRIVATE_##Name)
@@ -493,6 +498,16 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
 #include "clang/Basic/OpenMPKinds.def"
     }
     llvm_unreachable("Invalid OpenMP 'severity' clause type");
+  case OMPC_memscope:
+    switch (Type) {
+    case OMPC_MEMSCOPE_unknown:
+      return "unknown";
+#define OPENMP_MEMSCOPE_KIND(Name)                                             \
+  case OMPC_MEMSCOPE_##Name:                                                   \
+    return #Name;
+#include "clang/Basic/OpenMPKinds.def"
+    }
+    llvm_unreachable("Invalid OpenMP 'memscope' clause type");
   case OMPC_lastprivate:
     switch (Type) {
     case OMPC_LASTPRIVATE_unknown:
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 9e6d3e0c5a5199..324bd0ea4ffa5d 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2952,10 +2952,11 @@ void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
 }
 
 void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
-                                SourceLocation Loc, llvm::AtomicOrdering AO) {
+                                SourceLocation Loc, llvm::AtomicOrdering AO,
+                                SyncScope Scope) {
   // The OpenMPIRBuilder emits a fence on a GPU, so GPUs always use it.
   if (CGF.CGM.getLangOpts().OpenMPIRBuilder || isGPU()) {
-    OMPBuilder.createFlush(CGF.Builder, AO);
+    OMPBuilder.createFlush(CGF.Builder, AO, getAtomicScope(Scope));
     return;
   }
   if (!CGF.HaveInsertPoint())
@@ -2966,7 +2967,8 @@ void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
   // A flush with a list is a strong flush, which is treated as seq_cst.
   if (AO == llvm::AtomicOrdering::NotAtomic)
     AO = llvm::AtomicOrdering::SequentiallyConsistent;
-  if (AO == llvm::AtomicOrdering::SequentiallyConsistent) {
+  if (AO == llvm::AtomicOrdering::SequentiallyConsistent &&
+      Scope == SyncScope::DeviceScope) {
     // Build call void __kmpc_flush(ident_t *loc)
     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
                             CGM.getModule(), OMPRTL___kmpc_flush),
@@ -2974,12 +2976,12 @@ void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
     return;
   }
   // Build call void __kmpc_flush_explicit(ident_t *loc, kmp_int32 order,
-  // kmp_int32 scope), where order is an __ATOMIC_* value and scope is the
-  // __MEMORY_SCOPE_* value for device scope.
+  // kmp_int32 scope), where order is an __ATOMIC_* value and scope is a
+  // __MEMORY_SCOPE_* value.
   llvm::Value *Args[] = {
       emitUpdateLocation(CGF, Loc),
       CGF.Builder.getInt32(static_cast<unsigned>(llvm::toCABI(AO))),
-      CGF.Builder.getInt32(static_cast<unsigned>(llvm::AtomicScope::Device))};
+      CGF.Builder.getInt32(static_cast<unsigned>(getAtomicScope(Scope)))};
   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
                           CGM.getModule(), OMPRTL___kmpc_flush_explicit),
                       Args);
@@ -13605,8 +13607,8 @@ Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate(
 
 void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF,
                                     ArrayRef<const Expr *> Vars,
-                                    SourceLocation Loc,
-                                    llvm::AtomicOrdering AO) {
+                                    SourceLocation Loc, llvm::AtomicOrdering AO,
+                                    SyncScope Scope) {
   llvm_unreachable("Not supported in SIMD-only mode");
 }
 
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h
index 2f719aafccc9cc..ae257f351aecc4 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.h
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -19,6 +19,7 @@
 #include "clang/AST/Type.h"
 #include "clang/Basic/OpenMPKinds.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SyncScope.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallPtrSet.h"
@@ -1143,8 +1144,11 @@ class CGOpenMPRuntime {
   /// \param Vars List of variables to flush.
   /// \param AO Memory ordering of the flush, or NotAtomic for a flush with a
   /// list (a strong flush).
+  /// \param Scope Memory scope of the flush, as given by the 'memscope'
+  /// clause.
   virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
-                         SourceLocation Loc, llvm::AtomicOrdering AO);
+                         SourceLocation Loc, llvm::AtomicOrdering AO,
+                         SyncScope Scope);
 
   /// Emit task region for the task directive. The task region is
   /// emitted in several steps:
@@ -2028,7 +2032,8 @@ class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
   /// Emit flush of the variables specified in 'omp flush' directive.
   /// \param Vars List of variables to flush.
   void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
-                 SourceLocation Loc, llvm::AtomicOrdering AO) override;
+                 SourceLocation Loc, llvm::AtomicOrdering AO,
+                 SyncScope Scope) override;
 
   /// Emit task region for the task directive. The task region is
   /// emitted in several steps:
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 6959fe4ada619a..ee660c6cf336a4 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -6261,6 +6261,26 @@ void CodeGenFunction::EmitOMPTaskgroupDirective(
   CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc());
 }
 
+/// Returns the memory scope given by the 'memscope' clause of \p S, or the
+/// device scope that applies when there is no such clause.
+static SyncScope getOMPMemscope(const OMPExecutableDirective &S) {
+  if (const auto *C = S.getSingleClause<OMPMemscopeClause>()) {
+    switch (C->getMemscopeKind()) {
+    case OMPC_MEMSCOPE_all:
+      return SyncScope::SystemScope;
+    case OMPC_MEMSCOPE_cgroup:
+      // A contention group is the threads of one team, and clang maps each
+      // team to one workgroup on a GPU.
+      return SyncScope::WorkgroupScope;
+    case OMPC_MEMSCOPE_device:
+      return SyncScope::DeviceScope;
+    case OMPC_MEMSCOPE_unknown:
+      llvm_unreachable("Unexpected memscope clause kind.");
+    }
+  }
+  return SyncScope::DeviceScope;
+}
+
 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
   // A flush with a list is a strong flush. Without a list the flush behaves as
   // if the memory-order clause is seq_cst unless another one is given.
@@ -6281,7 +6301,7 @@ void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
                                 FlushClause->varlist_end());
         return {};
       }(),
-      S.getBeginLoc(), AO);
+      S.getBeginLoc(), AO, getOMPMemscope(S));
 }
 
 void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) {
@@ -6971,7 +6991,7 @@ void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
 }
 
 static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
-                                  const Expr *X, const Expr *V,
+                                  SyncScope Scope, const Expr *X, const Expr *V,
                                   SourceLocation Loc) {
   // v = x;
   assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
@@ -6991,7 +7011,7 @@ static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
   case llvm::AtomicOrdering::AcquireRelease:
   case llvm::AtomicOrdering::SequentiallyConsistent:
     CGF.CGM.getOpenMPRuntime().emitFlush(
-        CGF, {}, Loc, llvm::AtomicOrdering::SequentiallyConsistent);
+        CGF, {}, Loc, llvm::AtomicOrdering::SequentiallyConsistent, Scope);
     break;
   case llvm::AtomicOrdering::Monotonic:
   case llvm::AtomicOrdering::Release:
@@ -7005,8 +7025,9 @@ static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
 }
 
 static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF,
-                                   llvm::AtomicOrdering ...
[truncated]

``````````

</details>


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


More information about the llvm-branch-commits mailing list