From b600ae37a52ac5b6d3a350f7426546df11bb020a Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Mon, 1 Jul 2019 17:46:52 +0000 Subject: [PATCH] [OPENMP]Fix handling of lambda captures in target regions. Previously, lambda captures were processed in the function called during capturing the variables. It leads to the recursive functions calls and may result in the compiler crash. llvm-svn: 364820 --- clang/include/clang/Sema/Sema.h | 4 ++ clang/lib/Sema/SemaExpr.cpp | 2 + clang/lib/Sema/SemaOpenMP.cpp | 97 +++++++++++++--------------- clang/test/OpenMP/nvptx_lambda_capturing.cpp | 10 +-- 4 files changed, 55 insertions(+), 58 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index d2d0dbe..27e7420 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -8969,6 +8969,10 @@ private: SourceRange SrcRange = SourceRange()); public: + /// Function tries to capture lambda's captured variables in the OpenMP region + /// before the original lambda is captured. + void tryCaptureOpenMPLambdas(ValueDecl *V); + /// Return true if the provided declaration \a VD should be captured by /// reference. /// \param Level Relative level of nested OpenMP construct for that the check diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 94ee9e18..466d9fd 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -15210,6 +15210,8 @@ MarkVarDeclODRUsed(VarDecl *Var, SourceLocation Loc, Sema &SemaRef, old = Loc; } QualType CaptureType, DeclRefType; + if (SemaRef.LangOpts.OpenMP) + SemaRef.tryCaptureOpenMPLambdas(Var); SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, /*EllipsisLoc*/ SourceLocation(), /*BuildAndDiagnose*/ true, diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp index 57ce28a..20afe15 100644 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -1726,12 +1726,10 @@ bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { IsByRef = - ((DSAStack->isForceCaptureByReferenceInTargetExecutable() && - !Ty->isAnyPointerType()) || - !DSAStack->hasExplicitDSA( - D, - [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, - Level, /*NotLastprivate=*/true)) && + !DSAStack->hasExplicitDSA( + D, + [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, + Level, /*NotLastprivate=*/true) && // If the variable is artificial and must be captured by value - try to // capture by value. !(isa(D) && !D->hasAttr() && @@ -1801,53 +1799,6 @@ VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, return VD; } } - // Capture variables captured by reference in lambdas for target-based - // directives. - // FIXME: Triggering capture from here is completely inappropriate. - if (VD && !DSAStack->isClauseParsingMode()) { - if (const auto *RD = VD->getType() - .getCanonicalType() - .getNonReferenceType() - ->getAsCXXRecordDecl()) { - bool SavedForceCaptureByReferenceInTargetExecutable = - DSAStack->isForceCaptureByReferenceInTargetExecutable(); - DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true); - InParentDirectiveRAII.disable(); - if (RD->isLambda()) { - llvm::DenseMap Captures; - FieldDecl *ThisCapture; - RD->getCaptureFields(Captures, ThisCapture); - for (const LambdaCapture &LC : RD->captures()) { - if (LC.getCaptureKind() == LCK_ByRef) { - VarDecl *VD = LC.getCapturedVar(); - DeclContext *VDC = VD->getDeclContext(); - if (!VDC->Encloses(CurContext)) - continue; - DSAStackTy::DSAVarData DVarPrivate = - DSAStack->getTopDSA(VD, /*FromParent=*/false); - // Do not capture already captured variables. - if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && - DVarPrivate.CKind == OMPC_unknown && - !DSAStack->checkMappableExprComponentListsForDecl( - D, /*CurrentRegionOnly=*/true, - [](OMPClauseMappableExprCommon:: - MappableExprComponentListRef, - OpenMPClauseKind) { return true; })) - MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar()); - } else if (LC.getCaptureKind() == LCK_This) { - QualType ThisTy = getCurrentThisType(); - if (!ThisTy.isNull() && - Context.typesAreCompatible(ThisTy, ThisCapture->getType())) - CheckCXXThisCapture(LC.getLocation()); - } - } - } - if (CheckScopeInfo && DSAStack->isBodyComplete()) - InParentDirectiveRAII.enable(); - DSAStack->setForceCaptureByReferenceInTargetExecutable( - SavedForceCaptureByReferenceInTargetExecutable); - } - } if (CheckScopeInfo) { bool OpenMPFound = false; @@ -3385,6 +3336,46 @@ public: }; } // namespace +void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { + // Capture variables captured by reference in lambdas for target-based + // directives. + if (!CurContext->isDependentContext() && + (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || + isOpenMPTargetDataManagementDirective( + DSAStack->getCurrentDirective()))) { + QualType Type = V->getType(); + if (const auto *RD = Type.getCanonicalType() + .getNonReferenceType() + ->getAsCXXRecordDecl()) { + bool SavedForceCaptureByReferenceInTargetExecutable = + DSAStack->isForceCaptureByReferenceInTargetExecutable(); + DSAStack->setForceCaptureByReferenceInTargetExecutable( + /*V=*/true); + if (RD->isLambda()) { + llvm::DenseMap Captures; + FieldDecl *ThisCapture; + RD->getCaptureFields(Captures, ThisCapture); + for (const LambdaCapture &LC : RD->captures()) { + if (LC.getCaptureKind() == LCK_ByRef) { + VarDecl *VD = LC.getCapturedVar(); + DeclContext *VDC = VD->getDeclContext(); + if (!VDC->Encloses(CurContext)) + continue; + MarkVariableReferenced(LC.getLocation(), VD); + } else if (LC.getCaptureKind() == LCK_This) { + QualType ThisTy = getCurrentThisType(); + if (!ThisTy.isNull() && + Context.typesAreCompatible(ThisTy, ThisCapture->getType())) + CheckCXXThisCapture(LC.getLocation()); + } + } + } + DSAStack->setForceCaptureByReferenceInTargetExecutable( + SavedForceCaptureByReferenceInTargetExecutable); + } + } +} + StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, ArrayRef Clauses) { bool ErrorFound = false; diff --git a/clang/test/OpenMP/nvptx_lambda_capturing.cpp b/clang/test/OpenMP/nvptx_lambda_capturing.cpp index 8bf6667..4e4611b 100644 --- a/clang/test/OpenMP/nvptx_lambda_capturing.cpp +++ b/clang/test/OpenMP/nvptx_lambda_capturing.cpp @@ -12,8 +12,8 @@ #ifndef HEADER #define HEADER -// HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 4, i64 4, i64 0, i64 4, i64 40, i64 4, i64 4, i64 4, i64 8, i64 4, i64 4] -// HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 673, i64 673, i64 544, i64 33, i64 673, i64 1407374883554064, i64 1407374883554064, i64 1407374883554064, i64 1407374883554064, i64 1407374883554064, i64 288] +// HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 4, i64 4, i64 4, i64 0, i64 4, i64 40, i64 4, i64 4, i64 4, i64 8, i64 4] +// HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 288, i64 673, i64 673, i64 544, i64 33, i64 673, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720] // HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 4, i64 4, i64 4, i64 0, i64 4, i64 40, i64 4, i64 4, i64 4, i64 8, i64 4] // HOST-DAG: = private unnamed_addr constant [11 x i64] [i64 673, i64 673, i64 673, i64 544, i64 673, i64 673, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720, i64 1688849860264720] // HOST-DAG: = private unnamed_addr constant [3 x i64] [i64 4, i64 8, i64 8] @@ -73,7 +73,7 @@ struct S { } s; // FUN: define internal void @__omp_offloading_{{.+}}_main_l124_worker() -// FUN: define weak void @__omp_offloading_{{.+}}_main_l124(i32* dereferenceable(4) %{{.+}}, i32* dereferenceable(4) %{{.+}}, i32* %{{.+}}, i32* dereferenceable(4) %{{.+}}, [[CAP2]]* dereferenceable(40) %{{.+}}, i64 %{{.+}}) +// FUN: define weak void @__omp_offloading_{{.+}}_main_l124(i64 %{{.+}}, i32* dereferenceable(4) %{{.+}}, i32* dereferenceable(4) %{{.+}}, i32* %{{.+}}, i32* dereferenceable(4) %{{.+}}, [[CAP2]]* dereferenceable(40) %{{.+}}) // FUN-NOT: getelementptr // FUN: br i1 % // FUN: call void @__omp_offloading_{{.*}}_{{.*}}main{{.*}}_l124_worker() @@ -134,11 +134,11 @@ int main(int argc, char **argv) { // HOST-DAG: call i32 @__tgt_target(i64 -1, i8* @{{.+}}, i32 11, i8** [[BASES:%.+]], i8** [[PTRS:%.+]], // HOST-DAG: [[BASES:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[BASE_PTR:%.+]], i32 0, i32 0 // HOST-DAG: [[PTRS:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[PTR_PTR:%.+]], i32 0, i32 0 -// HOST-DAG: [[BASE_REF:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[BASE_PTR]], i32 0, i32 5 +// HOST-DAG: [[BASE_REF:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[BASE_PTR]], i32 0, i32 6 // HOST-DAG: [[BASE_REF_CAST:%.+]] = bitcast i8** [[BASE_REF]] to i32*** // HOST-DAG: store i32** [[BASE:%.+]], i32*** [[BASE_REF_CAST]], // HOST-DAG: [[BASE]] = getelementptr inbounds [[LAMBDA:%.+]], [[LAMBDA]]* [[LAMBDA_ADDR:%.+]], i32 0, i32 0 -// HOST-DAG: [[PTR_REF:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[PTR_PTR]], i32 0, i32 5 +// HOST-DAG: [[PTR_REF:%.+]] = getelementptr inbounds [11 x i8*], [11 x i8*]* [[PTR_PTR]], i32 0, i32 6 // HOST-DAG: [[PTR_REF_CAST:%.+]] = bitcast i8** [[PTR_REF]] to i32** // HOST-DAG: store i32* [[PTR:%.+]], i32** [[PTR_REF_CAST]], // HOST-DAG: [[PTR]] = load i32*, i32** [[PTR_REF:%.+]], -- 2.7.4