]> granicus.if.org Git - clang/commitdiff
[OPENMP] Initial parsing and sema analysis for 'read' clause in 'atomic' directive.
authorAlexey Bataev <a.bataev@hotmail.com>
Wed, 23 Jul 2014 02:27:21 +0000 (02:27 +0000)
committerAlexey Bataev <a.bataev@hotmail.com>
Wed, 23 Jul 2014 02:27:21 +0000 (02:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@213717 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
include/clang/AST/DataRecursiveASTVisitor.h
include/clang/AST/OpenMPClause.h
include/clang/AST/RecursiveASTVisitor.h
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/OpenMPKinds.def
include/clang/Sema/Sema.h
lib/AST/StmtPrinter.cpp
lib/AST/StmtProfile.cpp
lib/Basic/OpenMPKinds.cpp
lib/Parse/ParseOpenMP.cpp
lib/Sema/SemaOpenMP.cpp
lib/Sema/TreeTransform.h
lib/Serialization/ASTReaderStmt.cpp
lib/Serialization/ASTWriterStmt.cpp
test/OpenMP/atomic_ast_print.cpp
test/OpenMP/atomic_messages.cpp
tools/libclang/CIndex.cpp

index 28fb848bdd4f1000e5f90cc38869e85635f025a5..579d24cfa93317e284f217e123e33e492cd7af88 100644 (file)
@@ -2420,6 +2420,11 @@ RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
   return true;
 }
 
+template <typename Derived>
+bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
+  return true;
+}
+
 template <typename Derived>
 template <typename T>
 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
index 3345959653a6b11d5b7e8de8732c5ad68ad6b0ad..08b137918752155d6f7f4c56564c97720a6b2da7 100644 (file)
@@ -770,6 +770,34 @@ public:
   StmtRange children() { return StmtRange(); }
 };
 
+/// \brief This represents 'read' clause in the '#pragma omp atomic' directive.
+///
+/// \code
+/// #pragma omp atomic read
+/// \endcode
+/// In this example directive '#pragma omp atomic' has 'read' clause.
+///
+class OMPReadClause : public OMPClause {
+public:
+  /// \brief Build 'read' clause.
+  ///
+  /// \param StartLoc Starting location of the clause.
+  /// \param EndLoc Ending location of the clause.
+  ///
+  OMPReadClause(SourceLocation StartLoc, SourceLocation EndLoc)
+      : OMPClause(OMPC_read, StartLoc, EndLoc) {}
+
+  /// \brief Build an empty clause.
+  ///
+  OMPReadClause() : OMPClause(OMPC_read, SourceLocation(), SourceLocation()) {}
+
+  static bool classof(const OMPClause *T) {
+    return T->getClauseKind() == OMPC_read;
+  }
+
+  StmtRange children() { return StmtRange(); }
+};
+
 /// \brief This represents clause 'private' in the '#pragma omp ...' directives.
 ///
 /// \code
index e3f68196cea584d33a04babcb4c6aeb60e0016ad..cef019b76ed06e7e2f3ddbfffa344d1796c7a2e6 100644 (file)
@@ -2442,6 +2442,11 @@ RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) {
   return true;
 }
 
+template <typename Derived>
+bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) {
+  return true;
+}
+
 template <typename Derived>
 template <typename T>
 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {
index 7e6b196359bf0eaa044449364f5b25371ae57ac9..8e5fe2713b5e455143a52f5328409c578d7736f5 100644 (file)
@@ -7146,6 +7146,9 @@ def err_omp_parallel_sections_substmt_not_section : Error<
   "statement in 'omp parallel sections' directive must be enclosed into a section region">;
 def err_omp_parallel_reduction_in_task_firstprivate : Error<
   "argument of a reduction clause of a %0 construct must not appear in a firstprivate clause on a task construct">;
+def err_omp_atomic_read_not_expression_statement : Error<
+  "the statement for 'atomic read' must be an expression statement of form 'v = x;',"
+  " where v and x are both l-value expressions with scalar type">;
 } // end of OpenMP category
 
 let CategoryName = "Related Result Type Issue" in {
index 4d19ea77bd676c5341521865ccd5762d862d54a2..9fe3fe938e085f5f49e8d3da00233662a6baa1ea 100644 (file)
@@ -101,6 +101,7 @@ OPENMP_CLAUSE(nowait, OMPNowaitClause)
 OPENMP_CLAUSE(untied, OMPUntiedClause)
 OPENMP_CLAUSE(mergeable, OMPMergeableClause)
 OPENMP_CLAUSE(flush, OMPFlushClause)
+OPENMP_CLAUSE(read, OMPReadClause)
 
 // Clauses allowed for OpenMP directive 'parallel'.
 OPENMP_PARALLEL_CLAUSE(if)
@@ -198,6 +199,9 @@ OPENMP_TASK_CLAUSE(shared)
 OPENMP_TASK_CLAUSE(untied)
 OPENMP_TASK_CLAUSE(mergeable)
 
+// TODO More clauses allowed for OpenMP directive 'atomic'.
+OPENMP_ATOMIC_CLAUSE(read)
+
 #undef OPENMP_SCHEDULE_KIND
 #undef OPENMP_PROC_BIND_KIND
 #undef OPENMP_DEFAULT_KIND
index 34515506b595ffddf09c093670fec8cab9db14af..b08c27882dcca75eca6742eef4f8b7bfc871b15e 100644 (file)
@@ -7482,6 +7482,9 @@ public:
   /// \brief Called on well-formed 'mergeable' clause.
   OMPClause *ActOnOpenMPMergeableClause(SourceLocation StartLoc,
                                         SourceLocation EndLoc);
+  /// \brief Called on well-formed 'read' clause.
+  OMPClause *ActOnOpenMPReadClause(SourceLocation StartLoc,
+                                   SourceLocation EndLoc);
 
   OMPClause *
   ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> Vars,
index 959c45acc6a840d8036709e47b2b8e058d17f282..2a704c3a8493180e51527a068414a7b1fc6de56b 100644 (file)
@@ -665,6 +665,8 @@ void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
   OS << "mergeable";
 }
 
+void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
+
 template<typename T>
 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
   for (typename T::varlist_iterator I = Node->varlist_begin(),
index c2bfa46531d141e79b3b2d496d1be1cbaa4a6f61..4cf7077ee842c40a09f61da50e3424825ff92583 100644 (file)
@@ -310,6 +310,8 @@ void OMPClauseProfiler::VisitOMPUntiedClause(const OMPUntiedClause *) {}
 
 void OMPClauseProfiler::VisitOMPMergeableClause(const OMPMergeableClause *) {}
 
+void OMPClauseProfiler::VisitOMPReadClause(const OMPReadClause *) {}
+
 template<typename T>
 void OMPClauseProfiler::VisitOMPClauseList(T *Node) {
   for (auto *I : Node->varlists())
index 92145d6012957e2c146ad560596a5d35cf791c0d..78cfa18d8fef0ffb5923f0e2e82b275a9de8477d 100644 (file)
@@ -108,6 +108,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
   case OMPC_untied:
   case OMPC_mergeable:
   case OMPC_flush:
+  case OMPC_read:
     break;
   }
   llvm_unreachable("Invalid OpenMP simple clause kind");
@@ -167,6 +168,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
   case OMPC_untied:
   case OMPC_mergeable:
   case OMPC_flush:
+  case OMPC_read:
     break;
   }
   llvm_unreachable("Invalid OpenMP simple clause kind");
index f5ccb7c14e4cb2c4ea3a81de651b01b263418733..9ba18b4e98ab52ceb6d90be21f4953a3caa729e8 100644 (file)
@@ -342,7 +342,7 @@ bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
 ///       | linear-clause | aligned-clause | collapse-clause |
 ///       lastprivate-clause | reduction-clause | proc_bind-clause |
 ///       schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
-///       mergeable-clause | flush-clause
+///       mergeable-clause | flush-clause | read-clause
 ///
 OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
                                      OpenMPClauseKind CKind, bool FirstClause) {
@@ -405,6 +405,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
   case OMPC_nowait:
   case OMPC_untied:
   case OMPC_mergeable:
+  case OMPC_read:
     // OpenMP [2.7.1, Restrictions, p. 9]
     //  Only one ordered clause can appear on a loop directive.
     // OpenMP [2.7.1, Restrictions, C/C++, p. 4]
@@ -527,6 +528,9 @@ OMPClause *Parser::ParseOpenMPSimpleClause(OpenMPClauseKind Kind) {
 ///    mergeable-clause:
 ///         'mergeable'
 ///
+///    read-clause:
+///         'read'
+///
 OMPClause *Parser::ParseOpenMPClause(OpenMPClauseKind Kind) {
   SourceLocation Loc = Tok.getLocation();
   ConsumeAnyToken();
index c7c7654250166d6d849549dd3c82bb7966060f0c..7ec0d2b944a8aa270163300e8fc1efc7a4724c26 100644 (file)
@@ -92,15 +92,16 @@ private:
     Scope *CurScope;
     SourceLocation ConstructLoc;
     bool OrderedRegion;
+    SourceLocation AtomicClauseLoc;
     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
                  Scope *CurScope, SourceLocation Loc)
         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
-          ConstructLoc(Loc), OrderedRegion(false) {}
+          ConstructLoc(Loc), OrderedRegion(false), AtomicClauseLoc() {}
     SharingMapTy()
         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
-          ConstructLoc(), OrderedRegion(false) {}
+          ConstructLoc(), OrderedRegion(false), AtomicClauseLoc() {}
   };
 
   typedef SmallVector<SharingMapTy, 64> StackTy;
@@ -207,6 +208,22 @@ public:
     return false;
   }
 
+  /// \brief Checks if the 'atomic' construct has explicitly specified 'read',
+  /// 'update', 'write' or 'capture' clause.
+  bool hasAtomicClause() const {
+    return Stack.back().AtomicClauseLoc.isValid();
+  }
+  /// \brief Gets location of explicitly specified clause for 'atomic'
+  /// construct.
+  SourceLocation getAtomicClauseLoc() const {
+    return Stack.back().AtomicClauseLoc;
+  }
+  /// \brief Sets location of explicitly specified clause for 'atomic'
+  /// directive.
+  void setAtomicClauseLoc(SourceLocation Loc) {
+    Stack.back().AtomicClauseLoc = Loc;
+  }
+
   Scope *getCurScope() const { return Stack.back().CurScope; }
   Scope *getCurScope() { return Stack.back().CurScope; }
   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
@@ -2379,12 +2396,22 @@ StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
                                             SourceLocation StartLoc,
                                             SourceLocation EndLoc) {
   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
+  auto CS = cast<CapturedStmt>(AStmt);
   // 1.2.2 OpenMP Language Terminology
   // Structured block - An executable statement with a single entry at the
   // top and a single exit at the bottom.
   // The point of exit cannot be a branch out of the structured block.
   // longjmp() and throw() must not violate the entry/exit criteria.
   // TODO further analysis of associated statements and clauses.
+  for (auto *C : Clauses) {
+    if (C->getClauseKind() == OMPC_read) {
+      if (!isa<Expr>(CS->getCapturedStmt())) {
+          Diag(CS->getCapturedStmt()->getLocStart(),
+               diag::err_omp_atomic_read_not_expression_statement);
+        return StmtError();
+      }
+    }
+  }
 
   getCurFunction()->setHasBranchProtectedScope();
 
@@ -2430,6 +2457,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2633,6 +2661,7 @@ OMPClause *Sema::ActOnOpenMPSimpleClause(
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2748,6 +2777,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2827,6 +2857,9 @@ OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
   case OMPC_mergeable:
     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
     break;
+  case OMPC_read:
+    Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
+    break;
   case OMPC_if:
   case OMPC_final:
   case OMPC_num_threads:
@@ -2873,6 +2906,12 @@ OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
 }
 
+OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
+                                       SourceLocation EndLoc) {
+  DSAStack->setAtomicClauseLoc(StartLoc);
+  return new (Context) OMPReadClause(StartLoc, EndLoc);
+}
+
 OMPClause *Sema::ActOnOpenMPVarListClause(
     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
@@ -2926,6 +2965,7 @@ OMPClause *Sema::ActOnOpenMPVarListClause(
   case OMPC_untied:
   case OMPC_mergeable:
   case OMPC_threadprivate:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -4226,5 +4266,3 @@ OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
 
   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
 }
-
-#undef DSAStack
index 42bd1b0da0f795a8c829958b668ab4eb0c7ae45e..fff0e57ee74c6ac146755c7a1dfa2ff207f8d50c 100644 (file)
@@ -6761,6 +6761,12 @@ TreeTransform<Derived>::TransformOMPMergeableClause(OMPMergeableClause *C) {
   return C;
 }
 
+template <typename Derived>
+OMPClause *TreeTransform<Derived>::TransformOMPReadClause(OMPReadClause *C) {
+  // No need to rebuild this clause, no template-dependent parameters.
+  return C;
+}
+
 template <typename Derived>
 OMPClause *
 TreeTransform<Derived>::TransformOMPPrivateClause(OMPPrivateClause *C) {
index 6c60745aa09d5b03429d4a03a02499298e1254d4..ae870176d3517a042a66dc7ce7abbc25bfd960a4 100644 (file)
@@ -1715,6 +1715,9 @@ OMPClause *OMPClauseReader::readClause() {
   case OMPC_mergeable:
     C = new (Context) OMPMergeableClause();
     break;
+  case OMPC_read:
+    C = new (Context) OMPReadClause();
+    break;
   case OMPC_private:
     C = OMPPrivateClause::CreateEmpty(Context, Record[Idx++]);
     break;
@@ -1809,6 +1812,8 @@ void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
 
 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
 
+void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
+
 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
   C->setLParenLoc(Reader->ReadSourceLocation(Record, Idx));
   unsigned NumVars = C->varlist_size();
index 70d834dedf05f3869dc4cbf2b71fad2feaf096cf..d42f9d65134104f8bd385d0aedac1d5096647f44 100644 (file)
@@ -1735,6 +1735,8 @@ void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
 
 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
 
+void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
+
 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
   Record.push_back(C->varlist_size());
   Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
index 47c723469aab038c1be1bd1f4a4abfe4882281c8..957d4ae7711f0584ab458e00202833e535fcd6b2 100644 (file)
@@ -11,23 +11,33 @@ T foo(T arg) {
   T a;
 #pragma omp atomic
   a++;
+#pragma omp atomic read
+  a = arg;
   return T();
 }
 
 // CHECK: int a;
 // CHECK-NEXT: #pragma omp atomic
 // CHECK-NEXT: a++;
+// CHECK-NEXT: #pragma omp atomic read
+// CHECK-NEXT: a = arg;
 // CHECK: T a;
 // CHECK-NEXT: #pragma omp atomic
 // CHECK-NEXT: a++;
+// CHECK-NEXT: #pragma omp atomic read
+// CHECK-NEXT: a = arg;
 
 int main(int argc, char **argv) {
   int a;
 // CHECK: int a;
 #pragma omp atomic
   a++;
+#pragma omp atomic read
+  a = argc;
   // CHECK-NEXT: #pragma omp atomic
   // CHECK-NEXT: a++;
+  // CHECK-NEXT: #pragma omp atomic read
+  // CHECK-NEXT: a = argc;
   return foo(a);
 }
 
index 6eef8f227df3bbdc52934b51aee4e47dd244293e..7b35e42dc793687231b75110035298917b584b48 100644 (file)
@@ -1,20 +1,47 @@
 // RUN: %clang_cc1 -verify -fopenmp=libiomp5 -ferror-limit 100 %s
 
 int foo() {
-  L1:
-    foo();
-  #pragma omp atomic
+L1:
+  foo();
+#pragma omp atomic
   {
     foo();
     goto L1; // expected-error {{use of undeclared label 'L1'}}
   }
   goto L2; // expected-error {{use of undeclared label 'L2'}}
-  #pragma omp atomic
+#pragma omp atomic
   {
     foo();
-    L2:
+  L2:
     foo();
   }
 
   return 0;
 }
+
+template <class T>
+T read() {
+  T a, b = 0;
+// Test for atomic read
+#pragma omp atomic read
+// expected-error@+1 {{the statement for 'atomic read' must be an expression statement of form 'v = x;', where v and x are both l-value expressions with scalar type}}
+  ;
+// expected-error@+1 {{directive '#pragma omp atomic' cannot contain more than one 'read' clause}}
+#pragma omp atomic read read
+  a = b;
+
+  return T();
+}
+
+int read() {
+  int a, b = 0;
+// Test for atomic read
+#pragma omp atomic read
+// expected-error@+1 {{the statement for 'atomic read' must be an expression statement of form 'v = x;', where v and x are both l-value expressions with scalar type}}
+  ;
+// expected-error@+1 {{directive '#pragma omp atomic' cannot contain more than one 'read' clause}}
+#pragma omp atomic read read
+  a = b;
+
+  return read<int>();
+}
index c124b1ab0de67e96f57ff9b846ac7bad4f489afd..099302fe2c634e0b401534c5a43d2e6b18a08396 100644 (file)
@@ -1981,6 +1981,8 @@ void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
 
 void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
 
+void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
+
 template<typename T>
 void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
   for (const auto *I : Node->varlists())