https://github.com/alexey-bataev commented:

```
@@ static void emitScanBasedDirectiveSharedDeclsFree(CodeGenFunction &CGF,
 /// \code
+/// __kmpc_barrier();
 /// if (did_it)
 ///   __kmpc_free(buffer);
 /// \endcode
 static void emitScanBasedDirectiveSharedDeclsFree(CodeGenFunction &CGF,
                                                   const OMPLoopDirective &S,
                                                   Address DidIt) {
+  // Required even with 'nowait' and emitted outside the cancellation region of
+  // the loop, so no thread frees the arrays while the team still uses them.
+  CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(), OMPD_for,
+                                             /*EmitChecks=*/false);
   llvm::BasicBlock *ThenBB = CGF.createBasicBlock("omp.scan.free");
@@ static bool emitWorksharingDirective(CodeGenFunction &CGF,
     const auto &&FirstGen = [&S, HasCancel, EKind](CodeGenFunction &CGF) {
-      CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, EKind, HasCancel);
-      (void)CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
-                                       emitForLoopBounds,
-                                       emitDispatchForLoopBounds);
-      // Emit an implicit barrier at the end.
-      CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(),
-                                                 OMPD_for);
+      {
+        CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, EKind, 
HasCancel);
+        (void)CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
+                                         emitForLoopBounds,
+                                         emitDispatchForLoopBounds);
+      }
+      // Emit an implicit barrier at the end, reached by cancelled threads too.
+      CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(), 
OMPD_for,
+                                                 /*EmitChecks=*/false);
     };
-    const auto &&SecondGen = [&S, HasCancel, EKind, IsStandalone,
+    const auto &&SecondGen = [&S, HasCancel, EKind,
                               &HasLastprivates](CodeGenFunction &CGF) {
       CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, EKind, HasCancel);
       HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
                                                    emitForLoopBounds,
                                                    emitDispatchForLoopBounds);
-      // Emit an implicit barrier at the end, required even with 'nowait': the
-      // shared temp arrays are released after it.
-      if (IsStandalone)
-        CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(),
-                                                   OMPD_for);
     };

```
A cancelling thread skips the barrier before the free, causing a use-after-free.
```
@@ static bool emitWorksharingDirective(CodeGenFunction &CGF,
     bool IsStandalone = !isOpenMPParallelDirective(EKind);
+    // shortcut: no team-shared buffers on the device, its runtime has no
+    // __kmpc_copyprivate, __kmpc_aligned_alloc or __kmpc_free; lift once
+    // CGOpenMPRuntimeGPU implements them.
+    if (IsStandalone && CGF.CGM.getLangOpts().OpenMPIsTargetDevice) {
+      CGF.CGM.ErrorUnsupported(
+          &S, "'inscan' reduction on a worksharing loop in a target region");
+      return false;
+    }

```
Device code now calls runtime functions that do not exist on the device. 
```
 static void emitScanBasedDirectiveDecls(
     CodeGenFunction &CGF, const OMPLoopDirective &S,
-    llvm::function_ref<llvm::Value *(CodeGenFunction &)> NumIteratorsGen) {
+    llvm::function_ref<llvm::Value *(CodeGenFunction &)> NumIteratorsGen,
+    SmallVectorImpl<const VarDecl *> *SharedTemps = nullptr) {
@@
-      // Emit temp buffer.
-      CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(*ITA)->getDecl()));
+      const auto *TempVD = cast<VarDecl>(cast<DeclRefExpr>(*ITA)->getDecl());
+      // Team-shared buffers are allocated by the caller, only their sizes are
+      // emitted here.
+      if (SharedTemps) {
+        CGF.EmitVariablyModifiedType(TempVD->getType());
+        SharedTemps->push_back(TempVD);
+      } else {
+        CGF.EmitVarDecl(*TempVD);
+      }
@@ static Address emitScanBasedDirectiveSharedDecls(
   CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
-  llvm::Value *OMPScanNumIterations = CGF.Builder.CreateIntCast(
-  ... lines 4382-4423 (clause collection and the RedCG loop) ...
-  }
+  SmallVector<const VarDecl *, 4> TempVDs;
+  emitScanBasedDirectiveDecls(CGF, S, NumIteratorsGen, &TempVDs);
+  SmallVector<Address, 4> TempPtrAddrs;
+  for (const VarDecl *TempVD : TempVDs)
+    TempPtrAddrs.push_back(CGF.CreateMemTemp(CGF.getContext().VoidPtrTy,
+                                             TempVD->getName() + ".buf.addr"));

```
emitScanBasedDirectiveSharedDecls duplicates emitScanBasedDirectiveDecls

https://github.com/llvm/llvm-project/pull/242653
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to