[flang][openmp] Separate memory-order-clause parser creating OmpClause node
authorValentin Clement <clementval@gmail.com>
Sat, 21 Nov 2020 19:31:17 +0000 (14:31 -0500)
committerclementval <clementval@gmail.com>
Sat, 21 Nov 2020 19:31:33 +0000 (14:31 -0500)
This patch introduce the separate parser for the memory-order-clause from the general
OmpClauseList. This parser still creates OmpClause node and therefore can use all the feature
from TableGen and the OmpStructureChecker.
This is applied only for the Flush construct in this patch and it should be applied for
atomic as well.

This is the approach we disscussed several time during the weekly call.

Reviewed By: kiranchandramohan, sameeranjoshi

Differential Revision: https://reviews.llvm.org/D91839

flang/include/flang/Parser/dump-parse-tree.h
flang/include/flang/Parser/parse-tree.h
flang/lib/Lower/OpenMP.cpp
flang/lib/Parser/openmp-parsers.cpp
flang/lib/Parser/unparse.cpp
flang/lib/Semantics/check-omp-structure.cpp
flang/lib/Semantics/check-omp-structure.h
flang/test/Semantics/omp-clause-validity01.f90

index e05c5d5..61c5bdd 100644 (file)
@@ -548,7 +548,7 @@ public:
   NODE(parser, OpenMPDeclareReductionConstruct)
   NODE(parser, OpenMPDeclareSimdConstruct)
   NODE(parser, OpenMPDeclareTargetConstruct)
-  NODE(parser, OmpFlushMemoryClause)
+  NODE(parser, OmpMemoryOrderClause)
   NODE(parser, OpenMPFlushConstruct)
   NODE(parser, OpenMPLoopConstruct)
   NODE(parser, OpenMPSimpleStandaloneConstruct)
index a64ca06..2dcb92b 100644 (file)
@@ -3701,8 +3701,8 @@ struct OpenMPCancelConstruct {
 // memory-order-clause -> acq_rel
 //                        release
 //                        acquire
-struct OmpFlushMemoryClause {
-  WRAPPER_CLASS_BOILERPLATE(OmpFlushMemoryClause, llvm::omp::Clause);
+struct OmpMemoryOrderClause {
+  WRAPPER_CLASS_BOILERPLATE(OmpMemoryOrderClause, OmpClause);
   CharBlock source;
 };
 
@@ -3710,7 +3710,7 @@ struct OmpFlushMemoryClause {
 struct OpenMPFlushConstruct {
   TUPLE_CLASS_BOILERPLATE(OpenMPFlushConstruct);
   CharBlock source;
-  std::tuple<Verbatim, std::optional<OmpFlushMemoryClause>,
+  std::tuple<Verbatim, std::optional<OmpMemoryOrderClause>,
       std::optional<OmpObjectList>>
       t;
 };
index 5b69800..780aea9 100644 (file)
@@ -109,9 +109,9 @@ genOMP(Fortran::lower::AbstractConverter &converter,
                     std::get<std::optional<Fortran::parser::OmpObjectList>>(
                         flushConstruct.t))
               genObjectList(*ompObjectList, converter, operandRange);
-            if (std::get<std::optional<Fortran::parser::OmpFlushMemoryClause>>(
+            if (std::get<std::optional<Fortran::parser::OmpMemoryOrderClause>>(
                     flushConstruct.t))
-              TODO("Handle OmpFlushMemoryClause");
+              TODO("Handle OmpMemoryOrderClause");
             converter.getFirOpBuilder().create<mlir::omp::FlushOp>(
                 converter.getCurrentLocation(), operandRange);
           },
index 8ad13df..0af3137 100644 (file)
@@ -302,18 +302,22 @@ TYPE_PARSER(sourced(construct<OpenMPCancellationPointConstruct>(
 TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(verbatim("CANCEL"_tok),
     Parser<OmpCancelType>{}, maybe("IF" >> parenthesized(scalarLogicalExpr)))))
 
-// 2.17.8 Flush construct [OpenMP 5.0]
-//        flush -> FLUSH [memory-order-clause] [(variable-name-list)]
-//        memory-order-clause -> acq_rel
+// 2.17.7 Atomtic construct/2.17.8 Flush construct [OpenMP 5.0]
+//        memory-order-clause ->
+//                               seq_cst
+//                               acq_rel
 //                               release
 //                               acquire
-TYPE_PARSER(sourced(construct<OmpFlushMemoryClause>(
-    "ACQ_REL" >> pure(llvm::omp::Clause::OMPC_acq_rel) ||
-    "RELEASE" >> pure(llvm::omp::Clause::OMPC_release) ||
-    "ACQUIRE" >> pure(llvm::omp::Clause::OMPC_acquire))))
+//                               relaxed
+TYPE_PARSER(sourced(construct<OmpMemoryOrderClause>(
+    sourced("SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>()) ||
+        "ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
+        "RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
+        "ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
+        "RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>())))))
 
 TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
-    maybe(Parser<OmpFlushMemoryClause>{}),
+    maybe(Parser<OmpMemoryOrderClause>{}),
     maybe(parenthesized(Parser<OmpObjectList>{})))))
 
 // Simple Standalone Directives
index 2a6b0a6..56f09f9 100644 (file)
@@ -2419,25 +2419,11 @@ public:
     Put("\n");
     EndOpenMP();
   }
-  void Unparse(const OmpFlushMemoryClause &x) {
-    switch (x.v) {
-    case llvm::omp::Clause::OMPC_acq_rel:
-      Word("ACQ_REL ");
-      break;
-    case llvm::omp::Clause::OMPC_release:
-      Word("RELEASE ");
-      break;
-    case llvm::omp::Clause::OMPC_acquire:
-      Word("ACQUIRE ");
-      break;
-    default:
-      break;
-    }
-  }
+  void Unparse(const OmpMemoryOrderClause &x) { Walk(x.v); }
   void Unparse(const OpenMPFlushConstruct &x) {
     BeginOpenMP();
     Word("!$OMP FLUSH ");
-    Walk(std::get<std::optional<OmpFlushMemoryClause>>(x.t));
+    Walk(std::get<std::optional<OmpMemoryOrderClause>>(x.t));
     Walk(" (", std::get<std::optional<OmpObjectList>>(x.t), ")");
     Put("\n");
     EndOpenMP();
index 3cf7713..d3430d7 100644 (file)
@@ -378,6 +378,11 @@ CHECK_SIMPLE_CLAUSE(To, OMPC_to)
 CHECK_SIMPLE_CLAUSE(Uniform, OMPC_uniform)
 CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied)
 CHECK_SIMPLE_CLAUSE(UseDevicePtr, OMPC_use_device_ptr)
+CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel)
+CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire)
+CHECK_SIMPLE_CLAUSE(SeqCst, OMPC_seq_cst)
+CHECK_SIMPLE_CLAUSE(Release, OMPC_release)
+CHECK_SIMPLE_CLAUSE(Relaxed, OMPC_relaxed)
 
 CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
 CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
index cf10608..8384efe 100644 (file)
@@ -150,6 +150,12 @@ public:
   void Enter(const parser::OmpClause::Uniform &);
   void Enter(const parser::OmpClause::UseDevicePtr &);
   void Enter(const parser::OmpClause::IsDevicePtr &);
+  // Memory-order-clause
+  void Enter(const parser::OmpClause::SeqCst &);
+  void Enter(const parser::OmpClause::AcqRel &);
+  void Enter(const parser::OmpClause::Release &);
+  void Enter(const parser::OmpClause::Acquire &);
+  void Enter(const parser::OmpClause::Relaxed &);
 
   void Enter(const parser::OmpAlignedClause &);
   void Enter(const parser::OmpAllocateClause &);
index 5ff748e..601e390 100644 (file)
@@ -62,7 +62,7 @@ use omp_lib
      a = 3.14
   enddo
   !$omp end parallel
-  
+
   !$omp task private(b) allocate(b)
   do i = 1, N
      z = 2
@@ -80,7 +80,7 @@ use omp_lib
      z = 2
   end do
   !$omp end target
+
   !ERROR: ALLOCATE clause is not allowed on the TARGET DATA directive
   !$omp target data map(from: b) allocate(b)
   do i = 1, N
@@ -488,6 +488,11 @@ use omp_lib
   !$omp flush release
   !$omp flush acquire
   !$omp flush release (c)
+  !ERROR: SEQ_CST clause is not allowed on the FLUSH directive
+  !$omp flush seq_cst
+  !ERROR: RELAXED clause is not allowed on the FLUSH directive
+  !$omp flush relaxed
+
   !$omp cancel DO
   !$omp cancellation point parallel