[clang] c0a6433 - Simplify OpenMP Lambda use

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 7 10:23:30 PST 2022


Author: David Blaikie
Date: 2022-03-07T18:23:20Z
New Revision: c0a6433f2b5120d79f03431e9bf61b4da1184412

URL: https://github.com/llvm/llvm-project/commit/c0a6433f2b5120d79f03431e9bf61b4da1184412
DIFF: https://github.com/llvm/llvm-project/commit/c0a6433f2b5120d79f03431e9bf61b4da1184412.diff

LOG: Simplify OpenMP Lambda use

* Use default ref capture for non-escaping lambdas (this makes
  maintenance easier by allowing new uses, removing uses, having
  conditional uses (such as in assertions) not require updates to an
  explicit capture list)
* Simplify addPrivate API not to take a lambda, since it calls it
  unconditionally/immediately anyway - most callers are simply passing
  in a named value or short expression anyway and the lambda syntax just
  adds noise/overhead

Reviewed By: ABataev

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

Added: 
    

Modified: 
    clang/lib/CodeGen/CGOpenMPRuntime.cpp
    clang/lib/CodeGen/CGStmtOpenMP.cpp
    clang/lib/CodeGen/CodeGenFunction.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 399b2f63e8443..3089cdc4e4a92 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -370,8 +370,7 @@ class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo {
                       /*RefersToEnclosingVariableOrCapture=*/false,
                       VD->getType().getNonReferenceType(), VK_LValue,
                       C.getLocation());
-      PrivScope.addPrivate(
-          VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(CGF); });
+      PrivScope.addPrivate(VD, CGF.EmitLValue(&DRE).getAddress(CGF));
     }
     (void)PrivScope.Privatize();
   }
@@ -634,10 +633,8 @@ static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
     const auto *RHSDRE =
         cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
-    PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
-                            [=]() { return Private; });
-    PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
-                            [=]() { return Original; });
+    PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), Private);
+    PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), Original);
     (void)PrivateScope.Privatize();
     RValue Func = RValue::get(Reduction.second);
     CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
@@ -1141,15 +1138,13 @@ emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty,
                     Out->getLocation());
   CodeGenFunction::OMPPrivateScope Scope(CGF);
   Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm);
-  Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() {
-    return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
-        .getAddress(CGF);
-  });
+  Scope.addPrivate(
+      In, CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
+              .getAddress(CGF));
   Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm);
-  Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() {
-    return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
-        .getAddress(CGF);
-  });
+  Scope.addPrivate(
+      Out, CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
+               .getAddress(CGF));
   (void)Scope.Privatize();
   if (!IsCombiner && Out->hasInit() &&
       !CGF.isTrivialInitializer(Out->getInit())) {
@@ -3938,8 +3933,7 @@ static void emitPrivatesInit(CodeGenFunction &CGF,
                                                   Address SrcElement) {
                   // Clean up any temporaries needed by the initialization.
                   CodeGenFunction::OMPPrivateScope InitScope(CGF);
-                  InitScope.addPrivate(
-                      Elem, [SrcElement]() -> Address { return SrcElement; });
+                  InitScope.addPrivate(Elem, SrcElement);
                   (void)InitScope.Privatize();
                   // Emit initialization for single element.
                   CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(
@@ -3951,9 +3945,7 @@ static void emitPrivatesInit(CodeGenFunction &CGF,
           }
         } else {
           CodeGenFunction::OMPPrivateScope InitScope(CGF);
-          InitScope.addPrivate(Elem, [SharedRefLValue, &CGF]() -> Address {
-            return SharedRefLValue.getAddress(CGF);
-          });
+          InitScope.addPrivate(Elem, SharedRefLValue.getAddress(CGF));
           (void)InitScope.Privatize();
           CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo);
           CGF.EmitExprAsInit(Init, VD, PrivateLValue,
@@ -4099,14 +4091,11 @@ class OMPIteratorGeneratorScope final
     for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
       Uppers.push_back(CGF.EmitScalarExpr(E->getHelper(I).Upper));
       const auto *VD = cast<VarDecl>(E->getIteratorDecl(I));
-      addPrivate(VD, [&CGF, VD]() {
-        return CGF.CreateMemTemp(VD->getType(), VD->getName());
-      });
+      addPrivate(VD, CGF.CreateMemTemp(VD->getType(), VD->getName()));
       const OMPIteratorHelperData &HelperData = E->getHelper(I);
-      addPrivate(HelperData.CounterVD, [&CGF, &HelperData]() {
-        return CGF.CreateMemTemp(HelperData.CounterVD->getType(),
-                                 "counter.addr");
-      });
+      addPrivate(
+          HelperData.CounterVD,
+          CGF.CreateMemTemp(HelperData.CounterVD->getType(), "counter.addr"));
     }
     Privatize();
 
@@ -5397,8 +5386,8 @@ static void EmitOMPAggregateReduction(
 
   // Emit copy.
   CodeGenFunction::OMPPrivateScope Scope(CGF);
-  Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; });
-  Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; });
+  Scope.addPrivate(LHSVar, LHSElementCurrent);
+  Scope.addPrivate(RHSVar, RHSElementCurrent);
   Scope.Privatize();
   RedOpGen(CGF, XExpr, EExpr, UpExpr);
   Scope.ForceCleanup();
@@ -5487,14 +5476,10 @@ llvm::Function *CGOpenMPRuntime::emitReductionFunction(
   for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) {
     const auto *RHSVar =
         cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl());
-    Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() {
-      return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar);
-    });
+    Scope.addPrivate(RHSVar, emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar));
     const auto *LHSVar =
         cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl());
-    Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() {
-      return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar);
-    });
+    Scope.addPrivate(LHSVar, emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar));
     QualType PrivTy = (*IPriv)->getType();
     if (PrivTy->isVariablyModifiedType()) {
       // Get array size and emit VLA type.
@@ -5789,14 +5774,11 @@ void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
               llvm::AtomicOrdering::Monotonic, Loc,
               [&CGF, UpExpr, VD, Loc](RValue XRValue) {
                 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
-                PrivateScope.addPrivate(
-                    VD, [&CGF, VD, XRValue, Loc]() {
-                      Address LHSTemp = CGF.CreateMemTemp(VD->getType());
-                      CGF.emitOMPSimpleStore(
-                          CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
-                          VD->getType().getNonReferenceType(), Loc);
-                      return LHSTemp;
-                    });
+                Address LHSTemp = CGF.CreateMemTemp(VD->getType());
+                CGF.emitOMPSimpleStore(
+                    CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
+                    VD->getType().getNonReferenceType(), Loc);
+                PrivateScope.addPrivate(VD, LHSTemp);
                 (void)PrivateScope.Privatize();
                 return CGF.EmitAnyExpr(UpExpr);
               });
@@ -5996,22 +5978,22 @@ static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM,
   // %lhs = bitcast void* %arg0 to <type>*
   // %rhs = bitcast void* %arg1 to <type>*
   CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
-  PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() {
-    // Pull out the pointer to the variable.
-    Address PtrAddr = CGF.EmitLoadOfPointer(
-        CGF.GetAddrOfLocalVar(&ParamInOut),
-        C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
-    return CGF.Builder.CreateElementBitCast(
-        PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType()));
-  });
-  PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() {
-    // Pull out the pointer to the variable.
-    Address PtrAddr = CGF.EmitLoadOfPointer(
-        CGF.GetAddrOfLocalVar(&ParamIn),
-        C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
-    return CGF.Builder.CreateElementBitCast(
-        PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType()));
-  });
+  PrivateScope.addPrivate(
+      LHSVD,
+      // Pull out the pointer to the variable.
+      CGF.Builder.CreateElementBitCast(
+          CGF.EmitLoadOfPointer(
+              CGF.GetAddrOfLocalVar(&ParamInOut),
+              C.getPointerType(C.VoidPtrTy).castAs<PointerType>()),
+          CGF.ConvertTypeForMem(LHSVD->getType())));
+  PrivateScope.addPrivate(
+      RHSVD,
+      // Pull out the pointer to the variable.
+      CGF.Builder.CreateElementBitCast(
+          CGF.EmitLoadOfPointer(
+              CGF.GetAddrOfLocalVar(&ParamIn),
+              C.getPointerType(C.VoidPtrTy).castAs<PointerType>()),
+          CGF.ConvertTypeForMem(RHSVD->getType())));
   PrivateScope.Privatize();
   // Emit the combiner body:
   // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs)
@@ -10101,7 +10083,7 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
                                       .alignmentOfArrayElement(ElementSize));
   // Privatize the declared variable of mapper to be the current array element.
   CodeGenFunction::OMPPrivateScope Scope(MapperCGF);
-  Scope.addPrivate(MapperVarDecl, [PtrCurrent]() { return PtrCurrent; });
+  Scope.addPrivate(MapperVarDecl, PtrCurrent);
   (void)Scope.Privatize();
 
   // Get map clause information. Fill up the arrays with all mapped variables.

diff  --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 133dcad05fccb..94e7e1a3f68bc 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -95,9 +95,7 @@ class OMPLexicalScope : public CodeGenFunction::LexicalScope {
             isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo &&
                                        InlinedShareds.isGlobalVarCaptured(VD)),
             VD->getType().getNonReferenceType(), VK_LValue, C.getLocation());
-        InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
-          return CGF.EmitLValue(&DRE).getAddress(CGF);
-        });
+        InlinedShareds.addPrivate(VD, CGF.EmitLValue(&DRE).getAddress(CGF));
       }
     }
     (void)InlinedShareds.Privatize();
@@ -273,9 +271,7 @@ class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope {
                                InlinedShareds.isGlobalVarCaptured(VD)),
                           VD->getType().getNonReferenceType(), VK_LValue,
                           C.getLocation());
-          InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
-            return CGF.EmitLValue(&DRE).getAddress(CGF);
-          });
+          InlinedShareds.addPrivate(VD, CGF.EmitLValue(&DRE).getAddress(CGF));
         }
       }
       CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt());
@@ -630,9 +626,8 @@ CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S,
   CodeGenFunction::OMPPrivateScope LocalScope(*this);
   for (const auto &LocalAddrPair : LocalAddrs) {
     if (LocalAddrPair.second.first) {
-      LocalScope.addPrivate(LocalAddrPair.second.first, [&LocalAddrPair]() {
-        return LocalAddrPair.second.second;
-      });
+      LocalScope.addPrivate(LocalAddrPair.second.first,
+                            LocalAddrPair.second.second);
     }
   }
   (void)LocalScope.Privatize();
@@ -779,8 +774,8 @@ void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
             // destination and source variables to corresponding array
             // elements.
             CodeGenFunction::OMPPrivateScope Remap(*this);
-            Remap.addPrivate(DestVD, [DestElement]() { return DestElement; });
-            Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; });
+            Remap.addPrivate(DestVD, DestElement);
+            Remap.addPrivate(SrcVD, SrcElement);
             (void)Remap.Privatize();
             EmitIgnoredExpr(Copy);
           });
@@ -788,8 +783,8 @@ void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
   } else {
     // Remap pseudo source variable to private copy.
     CodeGenFunction::OMPPrivateScope Remap(*this);
-    Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; });
-    Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; });
+    Remap.addPrivate(SrcVD, SrcAddr);
+    Remap.addPrivate(DestVD, DestAddr);
     (void)Remap.Privatize();
     // Emit copying of the whole variable.
     EmitIgnoredExpr(Copy);
@@ -878,68 +873,56 @@ bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
           // Emit VarDecl with copy init for arrays.
           // Get the address of the original variable captured in current
           // captured region.
-          IsRegistered = PrivateScope.addPrivate(
-              OrigVD, [this, VD, Type, OriginalLVal, VDInit]() {
-                AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
-                const Expr *Init = VD->getInit();
-                if (!isa<CXXConstructExpr>(Init) ||
-                    isTrivialInitializer(Init)) {
-                  // Perform simple memcpy.
-                  LValue Dest =
-                      MakeAddrLValue(Emission.getAllocatedAddress(), Type);
-                  EmitAggregateAssign(Dest, OriginalLVal, Type);
-                } else {
-                  EmitOMPAggregateAssign(
-                      Emission.getAllocatedAddress(),
-                      OriginalLVal.getAddress(*this), Type,
-                      [this, VDInit, Init](Address DestElement,
-                                           Address SrcElement) {
-                        // Clean up any temporaries needed by the
-                        // initialization.
-                        RunCleanupsScope InitScope(*this);
-                        // Emit initialization for single element.
-                        setAddrOfLocalVar(VDInit, SrcElement);
-                        EmitAnyExprToMem(Init, DestElement,
-                                         Init->getType().getQualifiers(),
-                                         /*IsInitializer*/ false);
-                        LocalDeclMap.erase(VDInit);
-                      });
-                }
-                EmitAutoVarCleanups(Emission);
-                return Emission.getAllocatedAddress();
-              });
+          AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
+          const Expr *Init = VD->getInit();
+          if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
+            // Perform simple memcpy.
+            LValue Dest = MakeAddrLValue(Emission.getAllocatedAddress(), Type);
+            EmitAggregateAssign(Dest, OriginalLVal, Type);
+          } else {
+            EmitOMPAggregateAssign(
+                Emission.getAllocatedAddress(), OriginalLVal.getAddress(*this),
+                Type,
+                [this, VDInit, Init](Address DestElement, Address SrcElement) {
+                  // Clean up any temporaries needed by the
+                  // initialization.
+                  RunCleanupsScope InitScope(*this);
+                  // Emit initialization for single element.
+                  setAddrOfLocalVar(VDInit, SrcElement);
+                  EmitAnyExprToMem(Init, DestElement,
+                                   Init->getType().getQualifiers(),
+                                   /*IsInitializer*/ false);
+                  LocalDeclMap.erase(VDInit);
+                });
+          }
+          EmitAutoVarCleanups(Emission);
+          IsRegistered =
+              PrivateScope.addPrivate(OrigVD, Emission.getAllocatedAddress());
         } else {
           Address OriginalAddr = OriginalLVal.getAddress(*this);
-          IsRegistered =
-              PrivateScope.addPrivate(OrigVD, [this, VDInit, OriginalAddr, VD,
-                                               ThisFirstprivateIsLastprivate,
-                                               OrigVD, &Lastprivates, IRef]() {
-                // Emit private VarDecl with copy init.
-                // Remap temp VDInit variable to the address of the original
-                // variable (for proper handling of captured global variables).
-                setAddrOfLocalVar(VDInit, OriginalAddr);
-                EmitDecl(*VD);
-                LocalDeclMap.erase(VDInit);
-                if (ThisFirstprivateIsLastprivate &&
-                    Lastprivates[OrigVD->getCanonicalDecl()] ==
-                        OMPC_LASTPRIVATE_conditional) {
-                  // Create/init special variable for lastprivate conditionals.
-                  Address VDAddr =
-                      CGM.getOpenMPRuntime().emitLastprivateConditionalInit(
-                          *this, OrigVD);
-                  llvm::Value *V = EmitLoadOfScalar(
-                      MakeAddrLValue(GetAddrOfLocalVar(VD), (*IRef)->getType(),
-                                     AlignmentSource::Decl),
-                      (*IRef)->getExprLoc());
-                  EmitStoreOfScalar(V,
-                                    MakeAddrLValue(VDAddr, (*IRef)->getType(),
-                                                   AlignmentSource::Decl));
-                  LocalDeclMap.erase(VD);
-                  setAddrOfLocalVar(VD, VDAddr);
-                  return VDAddr;
-                }
-                return GetAddrOfLocalVar(VD);
-              });
+          // Emit private VarDecl with copy init.
+          // Remap temp VDInit variable to the address of the original
+          // variable (for proper handling of captured global variables).
+          setAddrOfLocalVar(VDInit, OriginalAddr);
+          EmitDecl(*VD);
+          LocalDeclMap.erase(VDInit);
+          Address VDAddr = GetAddrOfLocalVar(VD);
+          if (ThisFirstprivateIsLastprivate &&
+              Lastprivates[OrigVD->getCanonicalDecl()] ==
+                  OMPC_LASTPRIVATE_conditional) {
+            // Create/init special variable for lastprivate conditionals.
+            llvm::Value *V =
+                EmitLoadOfScalar(MakeAddrLValue(VDAddr, (*IRef)->getType(),
+                                                AlignmentSource::Decl),
+                                 (*IRef)->getExprLoc());
+            VDAddr = CGM.getOpenMPRuntime().emitLastprivateConditionalInit(
+                *this, OrigVD);
+            EmitStoreOfScalar(V, MakeAddrLValue(VDAddr, (*IRef)->getType(),
+                                                AlignmentSource::Decl));
+            LocalDeclMap.erase(VD);
+            setAddrOfLocalVar(VD, VDAddr);
+          }
+          IsRegistered = PrivateScope.addPrivate(OrigVD, VDAddr);
         }
         assert(IsRegistered &&
                "firstprivate var already registered as private");
@@ -965,11 +948,10 @@ void CodeGenFunction::EmitOMPPrivateClause(
       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
-        bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() {
-          // Emit private VarDecl with copy init.
-          EmitDecl(*VD);
-          return GetAddrOfLocalVar(VD);
-        });
+        EmitDecl(*VD);
+        // Emit private VarDecl with copy init.
+        bool IsRegistered =
+            PrivateScope.addPrivate(OrigVD, GetAddrOfLocalVar(VD));
         assert(IsRegistered && "private var already registered as private");
         // Silence the warning about unused variable.
         (void)IsRegistered;
@@ -1080,31 +1062,27 @@ bool CodeGenFunction::EmitOMPLastprivateClauseInit(
       if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
         const auto *DestVD =
             cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
-        PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() {
-          DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
-                          /*RefersToEnclosingVariableOrCapture=*/
-                          CapturedStmtInfo->lookup(OrigVD) != nullptr,
-                          (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
-          return EmitLValue(&DRE).getAddress(*this);
-        });
+        DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
+                        /*RefersToEnclosingVariableOrCapture=*/
+                        CapturedStmtInfo->lookup(OrigVD) != nullptr,
+                        (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
+        PrivateScope.addPrivate(DestVD, EmitLValue(&DRE).getAddress(*this));
         // Check if the variable is also a firstprivate: in this case IInit is
         // not generated. Initialization of this variable will happen in codegen
         // for 'firstprivate' clause.
         if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
           const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
-          bool IsRegistered =
-              PrivateScope.addPrivate(OrigVD, [this, VD, C, OrigVD]() {
-                if (C->getKind() == OMPC_LASTPRIVATE_conditional) {
-                  Address VDAddr =
-                      CGM.getOpenMPRuntime().emitLastprivateConditionalInit(
-                          *this, OrigVD);
-                  setAddrOfLocalVar(VD, VDAddr);
-                  return VDAddr;
-                }
-                // Emit private VarDecl with copy init.
-                EmitDecl(*VD);
-                return GetAddrOfLocalVar(VD);
-              });
+          Address VDAddr = Address::invalid();
+          if (C->getKind() == OMPC_LASTPRIVATE_conditional) {
+            VDAddr = CGM.getOpenMPRuntime().emitLastprivateConditionalInit(
+                *this, OrigVD);
+            setAddrOfLocalVar(VD, VDAddr);
+          } else {
+            // Emit private VarDecl with copy init.
+            EmitDecl(*VD);
+            VDAddr = GetAddrOfLocalVar(VD);
+          }
+          bool IsRegistered = PrivateScope.addPrivate(OrigVD, VDAddr);
           assert(IsRegistered &&
                  "lastprivate var already registered as private");
           (void)IsRegistered;
@@ -1258,8 +1236,8 @@ void CodeGenFunction::EmitOMPReductionClauseInit(
     EmitAutoVarCleanups(Emission);
     Address BaseAddr = RedCG.adjustPrivateAddress(
         *this, Count, Emission.getAllocatedAddress());
-    bool IsRegistered = PrivateScope.addPrivate(
-        RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; });
+    bool IsRegistered =
+        PrivateScope.addPrivate(RedCG.getBaseDecl(Count), BaseAddr);
     assert(IsRegistered && "private var already registered as private");
     // Silence the warning about unused variable.
     (void)IsRegistered;
@@ -1271,23 +1249,19 @@ void CodeGenFunction::EmitOMPReductionClauseInit(
     if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) {
       // Store the address of the original variable associated with the LHS
       // implicit variable.
-      PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() {
-        return RedCG.getSharedLValue(Count).getAddress(*this);
-      });
-      PrivateScope.addPrivate(
-          RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); });
+      PrivateScope.addPrivate(LHSVD,
+                              RedCG.getSharedLValue(Count).getAddress(*this));
+      PrivateScope.addPrivate(RHSVD, GetAddrOfLocalVar(PrivateVD));
     } else if ((isaOMPArraySectionExpr && Type->isScalarType()) ||
                isa<ArraySubscriptExpr>(IRef)) {
       // Store the address of the original variable associated with the LHS
       // implicit variable.
-      PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() {
-        return RedCG.getSharedLValue(Count).getAddress(*this);
-      });
-      PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() {
-        return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD),
-                                            ConvertTypeForMem(RHSVD->getType()),
-                                            "rhs.begin");
-      });
+      PrivateScope.addPrivate(LHSVD,
+                              RedCG.getSharedLValue(Count).getAddress(*this));
+      PrivateScope.addPrivate(RHSVD, Builder.CreateElementBitCast(
+                                         GetAddrOfLocalVar(PrivateVD),
+                                         ConvertTypeForMem(RHSVD->getType()),
+                                         "rhs.begin"));
     } else {
       QualType Type = PrivateVD->getType();
       bool IsArray = getContext().getAsArrayType(Type) != nullptr;
@@ -1298,13 +1272,12 @@ void CodeGenFunction::EmitOMPReductionClauseInit(
         OriginalAddr = Builder.CreateElementBitCast(
             OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
       }
-      PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; });
-      PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD, IsArray]() {
-        return IsArray ? Builder.CreateElementBitCast(
-                             GetAddrOfLocalVar(PrivateVD),
-                             ConvertTypeForMem(RHSVD->getType()), "rhs.begin")
-                       : GetAddrOfLocalVar(PrivateVD);
-      });
+      PrivateScope.addPrivate(LHSVD, OriginalAddr);
+      PrivateScope.addPrivate(
+          RHSVD, IsArray ? Builder.CreateElementBitCast(
+                               GetAddrOfLocalVar(PrivateVD),
+                               ConvertTypeForMem(RHSVD->getType()), "rhs.begin")
+                         : GetAddrOfLocalVar(PrivateVD));
     }
     ++ILHS;
     ++IRHS;
@@ -2183,7 +2156,7 @@ void CodeGenFunction::EmitOMPLinearClauseFinal(
                       (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
       Address OrigAddr = EmitLValue(&DRE).getAddress(*this);
       CodeGenFunction::OMPPrivateScope VarScope(*this);
-      VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; });
+      VarScope.addPrivate(OrigVD, OrigAddr);
       (void)VarScope.Privatize();
       EmitIgnoredExpr(F);
       ++IC;
@@ -2242,20 +2215,15 @@ void CodeGenFunction::EmitOMPPrivateLoopCounters(
     AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD);
     EmitAutoVarCleanups(VarEmission);
     LocalDeclMap.erase(PrivateVD);
-    (void)LoopScope.addPrivate(
-        VD, [&VarEmission]() { return VarEmission.getAllocatedAddress(); });
+    (void)LoopScope.addPrivate(VD, VarEmission.getAllocatedAddress());
     if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
         VD->hasGlobalStorage()) {
-      (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() {
-        DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD),
-                        LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
-                        E->getType(), VK_LValue, E->getExprLoc());
-        return EmitLValue(&DRE).getAddress(*this);
-      });
+      DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD),
+                      LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
+                      E->getType(), VK_LValue, E->getExprLoc());
+      (void)LoopScope.addPrivate(PrivateVD, EmitLValue(&DRE).getAddress(*this));
     } else {
-      (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() {
-        return VarEmission.getAllocatedAddress();
-      });
+      (void)LoopScope.addPrivate(PrivateVD, VarEmission.getAllocatedAddress());
     }
     ++I;
   }
@@ -2270,9 +2238,8 @@ void CodeGenFunction::EmitOMPPrivateLoopCounters(
       // Override only those variables that can be captured to avoid re-emission
       // of the variables declared within the loops.
       if (DRE->refersToEnclosingVariableOrCapture()) {
-        (void)LoopScope.addPrivate(VD, [this, DRE, VD]() {
-          return CreateMemTemp(DRE->getType(), VD->getName());
-        });
+        (void)LoopScope.addPrivate(
+            VD, CreateMemTemp(DRE->getType(), VD->getName()));
       }
     }
   }
@@ -2335,11 +2302,10 @@ void CodeGenFunction::EmitOMPLinearClause(
       const auto *PrivateVD =
           cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
       if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
-        bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() {
-          // Emit private VarDecl with copy init.
-          EmitVarDecl(*PrivateVD);
-          return GetAddrOfLocalVar(PrivateVD);
-        });
+        // Emit private VarDecl with copy init.
+        EmitVarDecl(*PrivateVD);
+        bool IsRegistered =
+            PrivateScope.addPrivate(VD, GetAddrOfLocalVar(PrivateVD));
         assert(IsRegistered && "linear var already registered as private");
         // Silence the warning about unused variable.
         (void)IsRegistered;
@@ -2430,7 +2396,7 @@ void CodeGenFunction::EmitOMPSimdFinal(
         OrigAddr = EmitLValue(&DRE).getAddress(*this);
       }
       OMPPrivateScope VarScope(*this);
-      VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; });
+      VarScope.addPrivate(OrigVD, OrigAddr);
       (void)VarScope.Privatize();
       EmitIgnoredExpr(F);
     }
@@ -3619,7 +3585,7 @@ static void emitScanBasedDirective(
               RValue::get(IVal));
           LHSAddr = CGF.EmitLValue(CopyArrayElem).getAddress(CGF);
         }
-        PrivScope.addPrivate(LHSVD, [LHSAddr]() { return LHSAddr; });
+        PrivScope.addPrivate(LHSVD, LHSAddr);
         Address RHSAddr = Address::invalid();
         {
           llvm::Value *OffsetIVal = CGF.Builder.CreateNUWSub(IVal, Pow2K);
@@ -3630,7 +3596,7 @@ static void emitScanBasedDirective(
               RValue::get(OffsetIVal));
           RHSAddr = CGF.EmitLValue(CopyArrayElem).getAddress(CGF);
         }
-        PrivScope.addPrivate(RHSVD, [RHSAddr]() { return RHSAddr; });
+        PrivScope.addPrivate(RHSVD, RHSAddr);
         ++ILHS;
         ++IRHS;
       }
@@ -4665,15 +4631,13 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(
                         CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr,
                         Pair.second->getType(), VK_LValue,
                         Pair.second->getExprLoc());
-        Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
-          return CGF.EmitLValue(&DRE).getAddress(CGF);
-        });
+        Scope.addPrivate(Pair.first, CGF.EmitLValue(&DRE).getAddress(CGF));
       }
       for (const auto &Pair : PrivatePtrs) {
         Address Replacement =
             Address::deprecated(CGF.Builder.CreateLoad(Pair.second),
                                 CGF.getContext().getDeclAlign(Pair.first));
-        Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
+        Scope.addPrivate(Pair.first, Replacement);
         if (auto *DI = CGF.getDebugInfo())
           if (CGF.CGM.getCodeGenOpts().hasReducedDebugInfo())
             (void)DI->EmitDeclareOfAutoVariable(
@@ -4705,8 +4669,7 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(
         Address Replacement =
             Address::deprecated(CGF.Builder.CreateLoad(Pair.second),
                                 CGF.getContext().getDeclAlign(Pair.first));
-        FirstprivateScope.addPrivate(Pair.first,
-                                     [Replacement]() { return Replacement; });
+        FirstprivateScope.addPrivate(Pair.first, Replacement);
       }
       (void)FirstprivateScope.Privatize();
       OMPLexicalScope LexScope(CGF, S, CapturedRegion);
@@ -4732,8 +4695,7 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(
                                      Data.ReductionCopies[Cnt]->getExprLoc()),
             Replacement.getAlignment());
         Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
-        Scope.addPrivate(RedCG.getBaseDecl(Cnt),
-                         [Replacement]() { return Replacement; });
+        Scope.addPrivate(RedCG.getBaseDecl(Cnt), Replacement);
       }
     }
     // Privatize all private variables except for in_reduction items.
@@ -4787,8 +4749,7 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(
                 InRedPrivs[Cnt]->getExprLoc()),
             Replacement.getAlignment());
         Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
-        InRedScope.addPrivate(RedCG.getBaseDecl(Cnt),
-                              [Replacement]() { return Replacement; });
+        InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), Replacement);
       }
     }
     (void)InRedScope.Privatize();
@@ -4884,20 +4845,16 @@ void CodeGenFunction::EmitOMPTargetTaskBasedDirective(
         /*IndexTypeQuals=*/0);
     SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD,
                                             S.getBeginLoc());
-    TargetScope.addPrivate(
-        BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; });
-    TargetScope.addPrivate(PVD,
-                           [&InputInfo]() { return InputInfo.PointersArray; });
-    TargetScope.addPrivate(SVD,
-                           [&InputInfo]() { return InputInfo.SizesArray; });
+    TargetScope.addPrivate(BPVD, InputInfo.BasePointersArray);
+    TargetScope.addPrivate(PVD, InputInfo.PointersArray);
+    TargetScope.addPrivate(SVD, InputInfo.SizesArray);
     // If there is no user-defined mapper, the mapper array will be nullptr. In
     // this case, we don't need to privatize it.
     if (!isa_and_nonnull<llvm::ConstantPointerNull>(
             InputInfo.MappersArray.getPointer())) {
       MVD = createImplicitFirstprivateForType(
           getContext(), Data, BaseAndPointerAndMapperType, CD, S.getBeginLoc());
-      TargetScope.addPrivate(MVD,
-                             [&InputInfo]() { return InputInfo.MappersArray; });
+      TargetScope.addPrivate(MVD, InputInfo.MappersArray);
     }
   }
   (void)TargetScope.Privatize();
@@ -4942,7 +4899,7 @@ void CodeGenFunction::EmitOMPTargetTaskBasedDirective(
         Address Replacement =
             Address::deprecated(CGF.Builder.CreateLoad(Pair.second),
                                 CGF.getContext().getDeclAlign(Pair.first));
-        Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
+        Scope.addPrivate(Pair.first, Replacement);
       }
     }
     // Privatize all private variables except for in_reduction items.
@@ -6923,30 +6880,26 @@ void CodeGenFunction::EmitOMPUseDevicePtrClause(
     if (InitAddrIt == CaptureDeviceAddrMap.end())
       continue;
 
-    bool IsRegistered = PrivateScope.addPrivate(
-        OrigVD, [this, OrigVD, InitAddrIt, InitVD, PvtVD]() {
-          // Initialize the temporary initialization variable with the address
-          // we get from the runtime library. We have to cast the source address
-          // because it is always a void *. References are materialized in the
-          // privatization scope, so the initialization here disregards the fact
-          // the original variable is a reference.
-          llvm::Type *Ty =
-              ConvertTypeForMem(OrigVD->getType().getNonReferenceType());
-          Address InitAddr =
-              Builder.CreateElementBitCast(InitAddrIt->second, Ty);
-          setAddrOfLocalVar(InitVD, InitAddr);
-
-          // Emit private declaration, it will be initialized by the value we
-          // declaration we just added to the local declarations map.
-          EmitDecl(*PvtVD);
-
-          // The initialization variables reached its purpose in the emission
-          // of the previous declaration, so we don't need it anymore.
-          LocalDeclMap.erase(InitVD);
-
-          // Return the address of the private variable.
-          return GetAddrOfLocalVar(PvtVD);
-        });
+    // Initialize the temporary initialization variable with the address
+    // we get from the runtime library. We have to cast the source address
+    // because it is always a void *. References are materialized in the
+    // privatization scope, so the initialization here disregards the fact
+    // the original variable is a reference.
+    llvm::Type *Ty = ConvertTypeForMem(OrigVD->getType().getNonReferenceType());
+    Address InitAddr = Builder.CreateElementBitCast(InitAddrIt->second, Ty);
+    setAddrOfLocalVar(InitVD, InitAddr);
+
+    // Emit private declaration, it will be initialized by the value we
+    // declaration we just added to the local declarations map.
+    EmitDecl(*PvtVD);
+
+    // The initialization variables reached its purpose in the emission
+    // of the previous declaration, so we don't need it anymore.
+    LocalDeclMap.erase(InitVD);
+
+    // Return the address of the private variable.
+    bool IsRegistered =
+        PrivateScope.addPrivate(OrigVD, GetAddrOfLocalVar(PvtVD));
     assert(IsRegistered && "firstprivate var already registered as private");
     // Silence the warning about unused variable.
     (void)IsRegistered;
@@ -7007,7 +6960,7 @@ void CodeGenFunction::EmitOMPUseDeviceAddrClause(
     PrivAddr =
         Builder.CreatePointerBitCastOrAddrSpaceCast(PrivAddr, RealTy, RealElTy);
 
-    (void)PrivateScope.addPrivate(OrigVD, [PrivAddr]() { return PrivAddr; });
+    (void)PrivateScope.addPrivate(OrigVD, PrivAddr);
   }
 }
 
@@ -7279,8 +7232,7 @@ static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
                      const ImplicitParamDecl *PVD,
                      CodeGenFunction::OMPPrivateScope &Privates) {
   const auto *VDecl = cast<VarDecl>(Helper->getDecl());
-  Privates.addPrivate(VDecl,
-                      [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); });
+  Privates.addPrivate(VDecl, CGF.GetAddrOfLocalVar(PVD));
 }
 
 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
@@ -7572,8 +7524,7 @@ void CodeGenFunction::EmitSimpleOMPExecutableDirective(
             continue;
           if (!CGF.LocalDeclMap.count(VD)) {
             LValue GlobLVal = CGF.EmitLValue(Ref);
-            GlobalsScope.addPrivate(
-                VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); });
+            GlobalsScope.addPrivate(VD, GlobLVal.getAddress(CGF));
           }
         }
       }
@@ -7588,8 +7539,7 @@ void CodeGenFunction::EmitSimpleOMPExecutableDirective(
           const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
           if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) {
             LValue GlobLVal = CGF.EmitLValue(E);
-            GlobalsScope.addPrivate(
-                VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); });
+            GlobalsScope.addPrivate(VD, GlobLVal.getAddress(CGF));
           }
           if (isa<OMPCapturedExprDecl>(VD)) {
             // Emit only those that were not explicitly referenced in clauses.

diff  --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 51a849a836f77..2636a4db88fdc 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -1068,15 +1068,14 @@ class CodeGenFunction : public CodeGenTypeCache {
     /// Enter a new OpenMP private scope.
     explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
 
-    /// Registers \p LocalVD variable as a private and apply \p PrivateGen
-    /// function for it to generate corresponding private variable. \p
-    /// PrivateGen returns an address of the generated private variable.
+    /// Registers \p LocalVD variable as a private with \p Addr as the address
+    /// of the corresponding private variable. \p
+    /// PrivateGen is the address of the generated private variable.
     /// \return true if the variable is registered as private, false if it has
     /// been privatized already.
-    bool addPrivate(const VarDecl *LocalVD,
-                    const llvm::function_ref<Address()> PrivateGen) {
+    bool addPrivate(const VarDecl *LocalVD, Address Addr) {
       assert(PerformCleanup && "adding private to dead scope");
-      return MappedVars.setVarAddr(CGF, LocalVD, PrivateGen());
+      return MappedVars.setVarAddr(CGF, LocalVD, Addr);
     }
 
     /// Privatizes local variables previously registered as private.


        


More information about the cfe-commits mailing list