[llvm-branch-commits] [cfe-branch] r286968 - Merging r286944:

Alexey Bataev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 15 06:26:49 PST 2016


Author: abataev
Date: Tue Nov 15 08:26:49 2016
New Revision: 286968

URL: http://llvm.org/viewvc/llvm-project?rev=286968&view=rev
Log:
Merging r286944:
------------------------------------------------------------------------
r286944 | abataev | 2016-11-15 09:11:50 +0000 (Tue, 15 Nov 2016) | 6 lines

[OPENMP] Fixed codegen for 'omp cancel' construct.

If 'omp cancel' construct is used in a worksharing construct it may cause
hanging of the software in case if reduction clause is used. Patch fixes
this problem by avoiding extra reduction processing for branches that
were canceled.
------------------------------------------------------------------------

Modified:
    cfe/branches/release_39/   (props changed)
    cfe/branches/release_39/lib/CodeGen/CGStmtOpenMP.cpp
    cfe/branches/release_39/lib/CodeGen/CodeGenFunction.h
    cfe/branches/release_39/test/OpenMP/cancel_codegen.cpp

Propchange: cfe/branches/release_39/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Nov 15 08:26:49 2016
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:275880,275967,276102,276350,276361,276473,276653,276716,276887,276891,276900,276979,276983,277095,277138,277141,277221,277307,277522,277743,277783,277796-277797,277852,277866,277889,277900,278139,278156,278234-278235,278393,278395,278763,278786,278988,284110,286103,286106,286129,286584
+/cfe/trunk:275880,275967,276102,276350,276361,276473,276653,276716,276887,276891,276900,276979,276983,277095,277138,277141,277221,277307,277522,277743,277783,277796-277797,277852,277866,277889,277900,278139,278156,278234-278235,278393,278395,278763,278786,278988,284110,286103,286106,286129,286584,286944
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_39/lib/CodeGen/CGStmtOpenMP.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_39/lib/CodeGen/CGStmtOpenMP.cpp?rev=286968&r1=286967&r2=286968&view=diff
==============================================================================
--- cfe/branches/release_39/lib/CodeGen/CGStmtOpenMP.cpp (original)
+++ cfe/branches/release_39/lib/CodeGen/CGStmtOpenMP.cpp Tue Nov 15 08:26:49 2016
@@ -1767,9 +1767,17 @@ void CodeGenFunction::EmitOMPOuterLoop(b
   EmitBlock(LoopExit.getBlock());
 
   // Tell the runtime we are done.
-  if (!DynamicOrOrdered)
-    RT.emitForStaticFinish(*this, S.getLocEnd());
+  SourceLocation ELoc = S.getLocEnd();
+  auto &&CodeGen = [DynamicOrOrdered, ELoc](CodeGenFunction &CGF) {
+    if (!DynamicOrOrdered)
+      CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
+  };
+  CodeGen(*this);
 
+  OpenMPDirectiveKind DKind = S.getDirectiveKind();
+  if (DKind == OMPD_for || DKind == OMPD_parallel_for ||
+      DKind == OMPD_distribute_parallel_for)
+    OMPCancelStack.back().CodeGen = CodeGen;
 }
 
 void CodeGenFunction::EmitOMPForOuterLoop(
@@ -1881,6 +1889,7 @@ void CodeGenFunction::EmitOMPDistributeO
 void CodeGenFunction::EmitOMPDistributeParallelForDirective(
     const OMPDistributeParallelForDirective &S) {
   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
+  OMPCancelStackRAII CancelRegion(*this);
   CGM.getOpenMPRuntime().emitInlinedDirective(
       *this, OMPD_distribute_parallel_for,
       [&S](CodeGenFunction &CGF, PrePostActionTy &) {
@@ -2073,7 +2082,15 @@ bool CodeGenFunction::EmitOMPWorksharing
                          [](CodeGenFunction &) {});
         EmitBlock(LoopExit.getBlock());
         // Tell the runtime we are done.
-        RT.emitForStaticFinish(*this, S.getLocStart());
+        SourceLocation ELoc = S.getLocEnd();
+        auto &&CodeGen = [ELoc](CodeGenFunction &CGF) {
+          CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
+        };
+        CodeGen(*this);
+        OpenMPDirectiveKind DKind = S.getDirectiveKind();
+        if (DKind == OMPD_for || DKind == OMPD_parallel_for ||
+            DKind == OMPD_distribute_parallel_for)
+          OMPCancelStack.back().CodeGen = CodeGen;
       } else {
         const bool IsMonotonic =
             Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
@@ -2127,6 +2144,7 @@ void CodeGenFunction::EmitOMPForDirectiv
   };
   {
     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
+    OMPCancelStackRAII CancelRegion(*this);
     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
                                                 S.hasCancel());
   }
@@ -2263,7 +2281,12 @@ void CodeGenFunction::EmitSections(const
     CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
                          [](CodeGenFunction &) {});
     // Tell the runtime we are done.
-    CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
+    SourceLocation ELoc = S.getLocEnd();
+    auto &&CodeGen = [ELoc](CodeGenFunction &CGF) {
+      CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
+    };
+    CodeGen(CGF);
+    CGF.OMPCancelStack.back().CodeGen = CodeGen;
     CGF.EmitOMPReductionClauseFinal(S);
     // Emit post-update of the reduction variables if IsLastIter != 0.
     emitPostUpdateForReductionClause(
@@ -2301,6 +2324,7 @@ void CodeGenFunction::EmitSections(const
 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
   {
     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
+    OMPCancelStackRAII CancelRegion(*this);
     EmitSections(S);
   }
   // Emit an implicit barrier at the end.
@@ -2388,6 +2412,7 @@ void CodeGenFunction::EmitOMPParallelFor
   // Emit directive as a combined directive that consists of two implicit
   // directives: 'parallel' with 'for' directive.
   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
+    OMPCancelStackRAII CancelRegion(CGF);
     CGF.EmitOMPWorksharingLoop(S);
   };
   emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen);
@@ -2408,6 +2433,7 @@ void CodeGenFunction::EmitOMPParallelSec
   // Emit directive as a combined directive that consists of two implicit
   // directives: 'parallel' with 'sections' directive.
   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
+    OMPCancelStackRAII CancelRegion(CGF);
     CGF.EmitSections(S);
   };
   emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen);
@@ -3390,8 +3416,11 @@ CodeGenFunction::getOMPCancelDestination
   if (Kind == OMPD_parallel || Kind == OMPD_task)
     return ReturnBlock;
   assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
-         Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for);
-  return BreakContinueStack.back().BreakBlock;
+         Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
+         Kind == OMPD_distribute_parallel_for);
+  if (!OMPCancelStack.back().ExitBlock.isValid())
+    OMPCancelStack.back().ExitBlock = getJumpDestInCurrentScope("cancel.exit");
+  return OMPCancelStack.back().ExitBlock;
 }
 
 // Generate the instructions for '#pragma omp target data' directive.

Modified: cfe/branches/release_39/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_39/lib/CodeGen/CodeGenFunction.h?rev=286968&r1=286967&r2=286968&view=diff
==============================================================================
--- cfe/branches/release_39/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/branches/release_39/lib/CodeGen/CodeGenFunction.h Tue Nov 15 08:26:49 2016
@@ -965,6 +965,35 @@ private:
   };
   SmallVector<BreakContinue, 8> BreakContinueStack;
 
+  /// Data for exit block for proper support of OpenMP cancellation constructs.
+  struct OMPCancel {
+    JumpDest ExitBlock;
+    llvm::function_ref<void(CodeGenFunction &CGF)> CodeGen;
+    OMPCancel() : CodeGen([](CodeGenFunction &CGF) {}) {}
+  };
+  SmallVector<OMPCancel, 8> OMPCancelStack;
+
+  /// Controls insertion of cancellation exit blocks in worksharing constructs.
+  class OMPCancelStackRAII {
+    CodeGenFunction &CGF;
+
+  public:
+    OMPCancelStackRAII(CodeGenFunction &CGF) : CGF(CGF) {
+      CGF.OMPCancelStack.push_back({});
+    }
+    ~OMPCancelStackRAII() {
+      if (CGF.HaveInsertPoint() &&
+          CGF.OMPCancelStack.back().ExitBlock.isValid()) {
+        auto CJD = CGF.getJumpDestInCurrentScope("cancel.cont");
+        CGF.EmitBranchThroughCleanup(CJD);
+        CGF.EmitBlock(CGF.OMPCancelStack.back().ExitBlock.getBlock());
+        CGF.OMPCancelStack.back().CodeGen(CGF);
+        CGF.EmitBranchThroughCleanup(CJD);
+        CGF.EmitBlock(CJD.getBlock());
+      }
+    }
+  };
+
   CodeGenPGO PGO;
 
   /// Calculate branch weights appropriate for PGO data

Modified: cfe/branches/release_39/test/OpenMP/cancel_codegen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_39/test/OpenMP/cancel_codegen.cpp?rev=286968&r1=286967&r2=286968&view=diff
==============================================================================
--- cfe/branches/release_39/test/OpenMP/cancel_codegen.cpp (original)
+++ cfe/branches/release_39/test/OpenMP/cancel_codegen.cpp Tue Nov 15 08:26:49 2016
@@ -91,9 +91,11 @@ for (int i = 0; i < argc; ++i) {
   }
 }
 // CHECK: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(
-#pragma omp parallel for
+int r = 0;
+#pragma omp parallel for reduction(+:r)
 for (int i = 0; i < argc; ++i) {
 #pragma omp cancel for
+  r += i;
 }
 // CHECK: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(
   return argc;
@@ -164,6 +166,9 @@ for (int i = 0; i < argc; ++i) {
 // CHECK: [[CONTINUE]]
 // CHECK: br label
 // CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call i32 @__kmpc_reduce_nowait(
+// CHECK: call void @__kmpc_end_reduce_nowait(
+// CHECK: call void @__kmpc_for_static_fini(
 // CHECK: ret void
 
 #endif




More information about the llvm-branch-commits mailing list