vitalybuka created this revision.
vitalybuka added a reviewer: ABataev.
vitalybuka added a subscriber: cfe-commits.

It introduced stack-use-after-return detected by ASAN.

Here:
'''

  auto &&CodeGen = [ELoc](CodeGenFunction &CGF) {
    CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
  };
  CodeGen(CGF);
  CGF.OMPCancelStack.back().CodeGen = CodeGen;

'''

CodeGen is function on the stack and CGF.OMPCancelStack.back().CodeGen is a
reference which will be used after return from the function with CodeGen.

This reverts commit r286944.


https://reviews.llvm.org/D26695

Files:
  lib/CodeGen/CGStmtOpenMP.cpp
  lib/CodeGen/CodeGenFunction.h
  test/OpenMP/cancel_codegen.cpp

Index: test/OpenMP/cancel_codegen.cpp
===================================================================
--- test/OpenMP/cancel_codegen.cpp
+++ test/OpenMP/cancel_codegen.cpp
@@ -90,11 +90,9 @@
   }
 }
 // CHECK: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(
-int r = 0;
-#pragma omp parallel for reduction(+:r)
+#pragma omp parallel for
 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;
@@ -165,9 +163,6 @@
 // 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
Index: lib/CodeGen/CodeGenFunction.h
===================================================================
--- lib/CodeGen/CodeGenFunction.h
+++ lib/CodeGen/CodeGenFunction.h
@@ -982,35 +982,6 @@
   };
   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
Index: lib/CodeGen/CGStmtOpenMP.cpp
===================================================================
--- lib/CodeGen/CGStmtOpenMP.cpp
+++ lib/CodeGen/CGStmtOpenMP.cpp
@@ -1781,17 +1781,9 @@
   EmitBlock(LoopExit.getBlock());
 
   // Tell the runtime we are done.
-  SourceLocation ELoc = S.getLocEnd();
-  auto &&CodeGen = [DynamicOrOrdered, ELoc](CodeGenFunction &CGF) {
-    if (!DynamicOrOrdered)
-      CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
-  };
-  CodeGen(*this);
+  if (!DynamicOrOrdered)
+    RT.emitForStaticFinish(*this, S.getLocEnd());
 
-  OpenMPDirectiveKind DKind = S.getDirectiveKind();
-  if (DKind == OMPD_for || DKind == OMPD_parallel_for ||
-      DKind == OMPD_distribute_parallel_for)
-    OMPCancelStack.back().CodeGen = CodeGen;
 }
 
 void CodeGenFunction::EmitOMPForOuterLoop(
@@ -1903,7 +1895,6 @@
 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 &) {
@@ -2132,15 +2123,7 @@
                          [](CodeGenFunction &) {});
         EmitBlock(LoopExit.getBlock());
         // Tell the runtime we are done.
-        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;
+        RT.emitForStaticFinish(*this, S.getLocStart());
       } else {
         const bool IsMonotonic =
             Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
@@ -2194,7 +2177,6 @@
   };
   {
     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
-    OMPCancelStackRAII CancelRegion(*this);
     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
                                                 S.hasCancel());
   }
@@ -2331,12 +2313,7 @@
     CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
                          [](CodeGenFunction &) {});
     // Tell the runtime we are done.
-    SourceLocation ELoc = S.getLocEnd();
-    auto &&CodeGen = [ELoc](CodeGenFunction &CGF) {
-      CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, ELoc);
-    };
-    CodeGen(CGF);
-    CGF.OMPCancelStack.back().CodeGen = CodeGen;
+    CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
     CGF.EmitOMPReductionClauseFinal(S);
     // Emit post-update of the reduction variables if IsLastIter != 0.
     emitPostUpdateForReductionClause(
@@ -2374,7 +2351,6 @@
 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
   {
     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
-    OMPCancelStackRAII CancelRegion(*this);
     EmitSections(S);
   }
   // Emit an implicit barrier at the end.
@@ -2462,7 +2438,6 @@
   // 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);
@@ -2483,7 +2458,6 @@
   // 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);
@@ -3464,11 +3438,8 @@
   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 ||
-         Kind == OMPD_distribute_parallel_for);
-  if (!OMPCancelStack.back().ExitBlock.isValid())
-    OMPCancelStack.back().ExitBlock = getJumpDestInCurrentScope("cancel.exit");
-  return OMPCancelStack.back().ExitBlock;
+         Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for);
+  return BreakContinueStack.back().BreakBlock;
 }
 
 void CodeGenFunction::EmitOMPUseDevicePtrClause(
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to