r356496 - [OPENMP]Check that global vars require predefined allocator.

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 19 11:39:11 PDT 2019


Author: abataev
Date: Tue Mar 19 11:39:11 2019
New Revision: 356496

URL: http://llvm.org/viewvc/llvm-project?rev=356496&view=rev
Log:
[OPENMP]Check that global vars require predefined allocator.

According to OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++,
if a list item has a static storage type, the allocator expression in
  the allocator clause must be a constant expression that evaluates to
  one of the predefined memory allocator values. Added check for this
  restriction.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaOpenMP.cpp
    cfe/trunk/test/OpenMP/allocate_allocator_messages.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=356496&r1=356495&r2=356496&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Mar 19 11:39:11 2019
@@ -9141,6 +9141,11 @@ def err_omp_invalid_map_this_expr : Erro
   "invalid 'this' expression on 'map' clause">;
 def err_implied_omp_allocator_handle_t_not_found : Error<
   "omp_allocator_handle_t type not found; include <omp.h>">;
+def err_omp_expected_predefined_allocator : Error<
+  "expected one of the predefined allocators for the variables with the static "
+  "storage: 'omp_default_mem_alloc', 'omp_large_cap_mem_alloc', "
+  "'omp_const_mem_alloc', 'omp_high_bw_mem_alloc', 'omp_low_lat_mem_alloc', "
+  "'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc' or 'omp_thread_mem_alloc'">;
 } // end of OpenMP category
 
 let CategoryName = "Related Result Type Issue" in {

Modified: cfe/trunk/lib/Sema/SemaOpenMP.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOpenMP.cpp?rev=356496&r1=356495&r2=356496&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOpenMP.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOpenMP.cpp Tue Mar 19 11:39:11 2019
@@ -2216,6 +2216,43 @@ Sema::DeclGroupPtrTy Sema::ActOnOpenMPAl
     if (isa<ParmVarDecl>(VD))
       continue;
 
+    // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
+    // If a list item has a static storage type, the allocator expression in the
+    // allocator clause must be a constant expression that evaluates to one of
+    // the predefined memory allocator values.
+    if (Allocator && VD->hasGlobalStorage()) {
+      bool IsPredefinedAllocator = false;
+      if (const auto *DRE =
+              dyn_cast<DeclRefExpr>(Allocator->IgnoreParenImpCasts())) {
+        if (DRE->getType().isConstant(getASTContext())) {
+          DeclarationName DN = DRE->getDecl()->getDeclName();
+          if (DN.isIdentifier()) {
+            StringRef PredefinedAllocators[] = {
+                "omp_default_mem_alloc", "omp_large_cap_mem_alloc",
+                "omp_const_mem_alloc",   "omp_high_bw_mem_alloc",
+                "omp_low_lat_mem_alloc", "omp_cgroup_mem_alloc",
+                "omp_pteam_mem_alloc",   "omp_thread_mem_alloc",
+            };
+            IsPredefinedAllocator =
+                llvm::any_of(PredefinedAllocators, [&DN](StringRef S) {
+                  return DN.getAsIdentifierInfo()->isStr(S);
+                });
+          }
+        }
+      }
+      if (!IsPredefinedAllocator) {
+        Diag(Allocator->getExprLoc(),
+             diag::err_omp_expected_predefined_allocator)
+            << Allocator->getSourceRange();
+        bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
+                      VarDecl::DeclarationOnly;
+        Diag(VD->getLocation(),
+             IsDecl ? diag::note_previous_decl : diag::note_defined_here)
+            << VD;
+        continue;
+      }
+    }
+
     Vars.push_back(RefExpr);
     Attr *A = OMPAllocateDeclAttr::CreateImplicit(Context, Allocator,
                                                   DE->getSourceRange());

Modified: cfe/trunk/test/OpenMP/allocate_allocator_messages.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/allocate_allocator_messages.cpp?rev=356496&r1=356495&r2=356496&view=diff
==============================================================================
--- cfe/trunk/test/OpenMP/allocate_allocator_messages.cpp (original)
+++ cfe/trunk/test/OpenMP/allocate_allocator_messages.cpp Tue Mar 19 11:39:11 2019
@@ -20,8 +20,10 @@ struct St1{
  int a;
  static int b;
 #pragma omp allocate(b) allocator(sss) // expected-error {{initializing 'omp_allocator_handle_t' (aka 'void *') with an expression of incompatible type 'int'}}
-} d;
+} d; // expected-note 2 {{'d' defined here}}
 
+// expected-error at +1 {{expected one of the predefined allocators for the variables with the static storage: 'omp_default_mem_alloc', 'omp_large_cap_mem_alloc', 'omp_const_mem_alloc', 'omp_high_bw_mem_alloc', 'omp_low_lat_mem_alloc', 'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc' or 'omp_thread_mem_alloc'}}
 #pragma omp allocate(d) allocator(nullptr)
 extern void *allocator;
+// expected-error at +1 {{expected one of the predefined allocators for the variables with the static storage: 'omp_default_mem_alloc', 'omp_large_cap_mem_alloc', 'omp_const_mem_alloc', 'omp_high_bw_mem_alloc', 'omp_low_lat_mem_alloc', 'omp_cgroup_mem_alloc', 'omp_pteam_mem_alloc' or 'omp_thread_mem_alloc'}}
 #pragma omp allocate(d) allocator(allocator)




More information about the cfe-commits mailing list