[OpenMP] Parsing + sema for target parallel for directive.

Summary:
This patch adds parsing + sema for the target parallel for directive along with testcases.

Reviewers: ABataev

Differential Revision: http://reviews.llvm.org/D16759

llvm-svn: 259654
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index b219e92..09cbca5 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -1630,7 +1630,8 @@
   }
   case OMPD_target_data:
   case OMPD_target:
-  case OMPD_target_parallel: {
+  case OMPD_target_parallel:
+  case OMPD_target_parallel_for: {
     Sema::CapturedParamNameType Params[] = {
         std::make_pair(StringRef(), QualType()) // __context with shared vars
     };
@@ -1807,6 +1808,8 @@
   // | parallel         | atomic          | *                                  |
   // | parallel         | target          | *                                  |
   // | parallel         | target parallel | *                                  |
+  // | parallel         | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | parallel         | target enter    | *                                  |
   // |                  | data            |                                    |
   // | parallel         | target exit     | *                                  |
@@ -1841,6 +1844,8 @@
   // | for              | atomic          | *                                  |
   // | for              | target          | *                                  |
   // | for              | target parallel | *                                  |
+  // | for              | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | for              | target enter    | *                                  |
   // |                  | data            |                                    |
   // | for              | target exit     | *                                  |
@@ -1875,6 +1880,8 @@
   // | master           | atomic          | *                                  |
   // | master           | target          | *                                  |
   // | master           | target parallel | *                                  |
+  // | master           | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | master           | target enter    | *                                  |
   // |                  | data            |                                    |
   // | master           | target exit     | *                                  |
@@ -1908,6 +1915,8 @@
   // | critical         | atomic          | *                                  |
   // | critical         | target          | *                                  |
   // | critical         | target parallel | *                                  |
+  // | critical         | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | critical         | target enter    | *                                  |
   // |                  | data            |                                    |
   // | critical         | target exit     | *                                  |
@@ -1942,6 +1951,8 @@
   // | simd             | atomic          |                                    |
   // | simd             | target          |                                    |
   // | simd             | target parallel |                                    |
+  // | simd             | target parallel |                                    |
+  // |                  | for             |                                    |
   // | simd             | target enter    |                                    |
   // |                  | data            |                                    |
   // | simd             | target exit     |                                    |
@@ -1976,6 +1987,8 @@
   // | for simd         | atomic          |                                    |
   // | for simd         | target          |                                    |
   // | for simd         | target parallel |                                    |
+  // | for simd         | target parallel |                                    |
+  // |                  | for             |                                    |
   // | for simd         | target enter    |                                    |
   // |                  | data            |                                    |
   // | for simd         | target exit     |                                    |
@@ -2010,6 +2023,8 @@
   // | parallel for simd| atomic          |                                    |
   // | parallel for simd| target          |                                    |
   // | parallel for simd| target parallel |                                    |
+  // | parallel for simd| target parallel |                                    |
+  // |                  | for             |                                    |
   // | parallel for simd| target enter    |                                    |
   // |                  | data            |                                    |
   // | parallel for simd| target exit     |                                    |
@@ -2044,6 +2059,8 @@
   // | sections         | atomic          | *                                  |
   // | sections         | target          | *                                  |
   // | sections         | target parallel | *                                  |
+  // | sections         | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | sections         | target enter    | *                                  |
   // |                  | data            |                                    |
   // | sections         | target exit     | *                                  |
@@ -2078,6 +2095,8 @@
   // | section          | atomic          | *                                  |
   // | section          | target          | *                                  |
   // | section          | target parallel | *                                  |
+  // | section          | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | section          | target enter    | *                                  |
   // |                  | data            |                                    |
   // | section          | target exit     | *                                  |
@@ -2112,6 +2131,8 @@
   // | single           | atomic          | *                                  |
   // | single           | target          | *                                  |
   // | single           | target parallel | *                                  |
+  // | single           | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | single           | target enter    | *                                  |
   // |                  | data            |                                    |
   // | single           | target exit     | *                                  |
@@ -2146,6 +2167,8 @@
   // | parallel for     | atomic          | *                                  |
   // | parallel for     | target          | *                                  |
   // | parallel for     | target parallel | *                                  |
+  // | parallel for     | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | parallel for     | target enter    | *                                  |
   // |                  | data            |                                    |
   // | parallel for     | target exit     | *                                  |
@@ -2180,6 +2203,8 @@
   // | parallel sections| atomic          | *                                  |
   // | parallel sections| target          | *                                  |
   // | parallel sections| target parallel | *                                  |
+  // | parallel sections| target parallel | *                                  |
+  // |                  | for             |                                    |
   // | parallel sections| target enter    | *                                  |
   // |                  | data            |                                    |
   // | parallel sections| target exit     | *                                  |
@@ -2214,6 +2239,8 @@
   // | task             | atomic          | *                                  |
   // | task             | target          | *                                  |
   // | task             | target parallel | *                                  |
+  // | task             | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | task             | target enter    | *                                  |
   // |                  | data            |                                    |
   // | task             | target exit     | *                                  |
@@ -2248,6 +2275,8 @@
   // | ordered          | atomic          | *                                  |
   // | ordered          | target          | *                                  |
   // | ordered          | target parallel | *                                  |
+  // | ordered          | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | ordered          | target enter    | *                                  |
   // |                  | data            |                                    |
   // | ordered          | target exit     | *                                  |
@@ -2282,6 +2311,8 @@
   // | atomic           | atomic          |                                    |
   // | atomic           | target          |                                    |
   // | atomic           | target parallel |                                    |
+  // | atomic           | target parallel |                                    |
+  // |                  | for             |                                    |
   // | atomic           | target enter    |                                    |
   // |                  | data            |                                    |
   // | atomic           | target exit     |                                    |
@@ -2316,6 +2347,8 @@
   // | target           | atomic          | *                                  |
   // | target           | target          |                                    |
   // | target           | target parallel |                                    |
+  // | target           | target parallel |                                    |
+  // |                  | for             |                                    |
   // | target           | target enter    |                                    |
   // |                  | data            |                                    |
   // | target           | target exit     |                                    |
@@ -2350,6 +2383,8 @@
   // | target parallel  | atomic          | *                                  |
   // | target parallel  | target          |                                    |
   // | target parallel  | target parallel |                                    |
+  // | target parallel  | target parallel |                                    |
+  // |                  | for             |                                    |
   // | target parallel  | target enter    |                                    |
   // |                  | data            |                                    |
   // | target parallel  | target exit     |                                    |
@@ -2362,6 +2397,69 @@
   // | target parallel  | taskloop simd   | *                                  |
   // | target parallel  | distribute      |                                    |
   // +------------------+-----------------+------------------------------------+
+  // | target parallel  | parallel        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | for             | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | for simd        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | master          | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | critical        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | simd            | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | sections        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | section         | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | single          | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | parallel for    | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  |parallel for simd| *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  |parallel sections| *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | task            | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | taskyield       | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | barrier         | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | taskwait        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | taskgroup       | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | flush           | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | ordered         | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | atomic          | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | target          |                                    |
+  // | for              |                 |                                    |
+  // | target parallel  | target parallel |                                    |
+  // | for              |                 |                                    |
+  // | target parallel  | target parallel |                                    |
+  // | for              | for             |                                    |
+  // | target parallel  | target enter    |                                    |
+  // | for              | data            |                                    |
+  // | target parallel  | target exit     |                                    |
+  // | for              | data            |                                    |
+  // | target parallel  | teams           |                                    |
+  // | for              |                 |                                    |
+  // | target parallel  | cancellation    |                                    |
+  // | for              | point           | !                                  |
+  // | target parallel  | cancel          | !                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | taskloop        | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | taskloop simd   | *                                  |
+  // | for              |                 |                                    |
+  // | target parallel  | distribute      |                                    |
+  // | for              |                 |                                    |
+  // +------------------+-----------------+------------------------------------+
   // | teams            | parallel        | *                                  |
   // | teams            | for             | +                                  |
   // | teams            | for simd        | +                                  |
@@ -2384,6 +2482,8 @@
   // | teams            | atomic          | +                                  |
   // | teams            | target          | +                                  |
   // | teams            | target parallel | +                                  |
+  // | teams            | target parallel | +                                  |
+  // |                  | for             |                                    |
   // | teams            | target enter    | +                                  |
   // |                  | data            |                                    |
   // | teams            | target exit     | +                                  |
@@ -2418,6 +2518,8 @@
   // | taskloop         | atomic          | *                                  |
   // | taskloop         | target          | *                                  |
   // | taskloop         | target parallel | *                                  |
+  // | taskloop         | target parallel | *                                  |
+  // |                  | for             |                                    |
   // | taskloop         | target enter    | *                                  |
   // |                  | data            |                                    |
   // | taskloop         | target exit     | *                                  |
@@ -2451,6 +2553,8 @@
   // | taskloop simd    | atomic          |                                    |
   // | taskloop simd    | target          |                                    |
   // | taskloop simd    | target parallel |                                    |
+  // | taskloop simd    | target parallel |                                    |
+  // |                  | for             |                                    |
   // | taskloop simd    | target enter    |                                    |
   // |                  | data            |                                    |
   // | taskloop simd    | target exit     |                                    |
@@ -2485,6 +2589,8 @@
   // | distribute       | atomic          | *                                  |
   // | distribute       | target          |                                    |
   // | distribute       | target parallel |                                    |
+  // | distribute       | target parallel |                                    |
+  // |                  | for             |                                    |
   // | distribute       | target enter    |                                    |
   // |                  | data            |                                    |
   // | distribute       | target exit     |                                    |
@@ -2561,7 +2667,8 @@
              (ParentRegion == OMPD_parallel ||
               ParentRegion == OMPD_target_parallel)) ||
             (CancelRegion == OMPD_for &&
-             (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
+             (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
+              ParentRegion == OMPD_target_parallel_for)) ||
             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
             (CancelRegion == OMPD_sections &&
              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
@@ -2924,6 +3031,12 @@
     AllowedNameModifiers.push_back(OMPD_target);
     AllowedNameModifiers.push_back(OMPD_parallel);
     break;
+  case OMPD_target_parallel_for:
+    Res = ActOnOpenMPTargetParallelForDirective(
+        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
+    AllowedNameModifiers.push_back(OMPD_target);
+    AllowedNameModifiers.push_back(OMPD_parallel);
+    break;
   case OMPD_cancellation_point:
     assert(ClausesWithImplicit.empty() &&
            "No clauses are allowed for 'omp cancellation point' directive");
@@ -5710,6 +5823,50 @@
                                             AStmt);
 }
 
+StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
+    ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
+    SourceLocation EndLoc,
+    llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
+  if (!AStmt)
+    return StmtError();
+
+  CapturedStmt *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.
+  CS->getCapturedDecl()->setNothrow();
+
+  OMPLoopDirective::HelperExprs B;
+  // In presence of clause 'collapse' or 'ordered' with number of loops, it will
+  // define the nested loops number.
+  unsigned NestedLoopCount =
+      CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
+                      getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
+                      VarsWithImplicitDSA, B);
+  if (NestedLoopCount == 0)
+    return StmtError();
+
+  assert((CurContext->isDependentContext() || B.builtAll()) &&
+         "omp target parallel for loop exprs were not built");
+
+  if (!CurContext->isDependentContext()) {
+    // Finalize the clauses that need pre-built expressions for CodeGen.
+    for (auto C : Clauses) {
+      if (auto LC = dyn_cast<OMPLinearClause>(C))
+        if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
+                                     B.NumIterations, *this, CurScope))
+          return StmtError();
+    }
+  }
+
+  getCurFunction()->setHasBranchProtectedScope();
+  return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
+                                               NestedLoopCount, Clauses, AStmt,
+                                               B, DSAStack->isCancelRegion());
+}
+
 /// \brief Check for existence of a map clause in the list of clauses.
 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();