From 91433f6877482a71fe3d04735d69b107a53edd26 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Tue, 26 Jun 2018 17:24:03 +0000 Subject: [OPENMP, NVPTX] Reduce the number of the globalized variables. Patch tries to make better analysis of the variables that should be globalized. From now, instead of all parallel directives it will check only distribute parallel .. directives and check only for firstprivte/lastprivate variables if they must be globalized. llvm-svn: 335632 --- clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp | 52 ++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'clang/lib/CodeGen') diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp index 5b7f0c3e43c..8cf5bb2f44b 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp @@ -187,7 +187,7 @@ class CheckVarsEscapingDeclContext final RecordDecl *GlobalizedRD = nullptr; llvm::SmallDenseMap MappedDeclsFields; bool AllEscaped = false; - bool IsForParallelRegion = false; + bool IsForCombinedParallelRegion = false; static llvm::Optional isDeclareTargetDeclaration(const ValueDecl *VD) { @@ -210,7 +210,7 @@ class CheckVarsEscapingDeclContext final if (const FieldDecl *FD = CSI->lookup(cast(VD))) { // Check if need to capture the variable that was already captured by // value in the outer region. - if (!IsForParallelRegion) { + if (!IsForCombinedParallelRegion) { if (!FD->hasAttrs()) return; const auto *Attr = FD->getAttr(); @@ -225,13 +225,13 @@ class CheckVarsEscapingDeclContext final assert(!VD->getType()->isVariablyModifiedType() && "Parameter captured by value with variably modified type"); EscapedParameters.insert(VD); - } else if (!IsForParallelRegion) { + } else if (!IsForCombinedParallelRegion) { return; } } } if ((!CGF.CapturedStmtInfo || - (IsForParallelRegion && CGF.CapturedStmtInfo)) && + (IsForCombinedParallelRegion && CGF.CapturedStmtInfo)) && VD->getType()->isReferenceType()) // Do not globalize variables with reference type. return; @@ -253,18 +253,49 @@ class CheckVarsEscapingDeclContext final } } } - void VisitOpenMPCapturedStmt(const CapturedStmt *S, bool IsParallelRegion) { + void VisitOpenMPCapturedStmt(const CapturedStmt *S, + ArrayRef Clauses, + bool IsCombinedParallelRegion) { if (!S) return; for (const CapturedStmt::Capture &C : S->captures()) { if (C.capturesVariable() && !C.capturesVariableByCopy()) { const ValueDecl *VD = C.getCapturedVar(); - bool SavedIsParallelRegion = IsForParallelRegion; - IsForParallelRegion = IsParallelRegion; + bool SavedIsForCombinedParallelRegion = IsForCombinedParallelRegion; + if (IsCombinedParallelRegion) { + // Check if the variable is privatized in the combined construct and + // those private copies must be shared in the inner parallel + // directive. + IsForCombinedParallelRegion = false; + for (const OMPClause *C : Clauses) { + if (!isOpenMPPrivate(C->getClauseKind()) || + C->getClauseKind() == OMPC_reduction || + C->getClauseKind() == OMPC_linear || + C->getClauseKind() == OMPC_private) + continue; + ArrayRef Vars; + if (const auto *PC = dyn_cast(C)) + Vars = PC->getVarRefs(); + else if (const auto *PC = dyn_cast(C)) + Vars = PC->getVarRefs(); + else + llvm_unreachable("Unexpected clause."); + for (const auto *E : Vars) { + const Decl *D = + cast(E)->getDecl()->getCanonicalDecl(); + if (D == VD->getCanonicalDecl()) { + IsForCombinedParallelRegion = true; + break; + } + } + if (IsForCombinedParallelRegion) + break; + } + } markAsEscaped(VD); if (isa(VD)) VisitValueDecl(VD); - IsForParallelRegion = SavedIsParallelRegion; + IsForCombinedParallelRegion = SavedIsForCombinedParallelRegion; } } } @@ -341,7 +372,10 @@ public: VisitStmt(S->getCapturedStmt()); return; } - VisitOpenMPCapturedStmt(S, CaptureRegions.back() == OMPD_parallel); + VisitOpenMPCapturedStmt( + S, D->clauses(), + CaptureRegions.back() == OMPD_parallel && + isOpenMPDistributeDirective(D->getDirectiveKind())); } } void VisitCapturedStmt(const CapturedStmt *S) { -- cgit v1.2.3