From 860fc25e85a105ef1fa9de717bb974231fab80ba Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Tue, 6 Mar 2018 18:59:43 +0000 Subject: [PATCH] [OPENMP] Fix generation of the unique names for task reduction variables. If the task has reduction construct and this construct for some variable requires unique threadprivate storage, we may generate different names for variables used in taskgroup task_reduction clause and in task in_reduction clause. Patch fixes this problem. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326827 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGOpenMPRuntime.cpp | 43 ++++++++++++------- lib/CodeGen/CGOpenMPRuntime.h | 2 + .../taskgroup_task_reduction_codegen.cpp | 4 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/CodeGen/CGOpenMPRuntime.cpp b/lib/CodeGen/CGOpenMPRuntime.cpp index 87097cc2ca..f9dab90f39 100644 --- a/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/lib/CodeGen/CGOpenMPRuntime.cpp @@ -1101,11 +1101,9 @@ static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, return Address(Addr, BaseLVAlignment); } -Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, - Address PrivateAddr) { - const DeclRefExpr *DE; +static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { const VarDecl *OrigVD = nullptr; - if (auto *OASE = dyn_cast(ClausesData[N].Ref)) { + if (auto *OASE = dyn_cast(Ref)) { auto *Base = OASE->getBase()->IgnoreParenImpCasts(); while (auto *TempOASE = dyn_cast(Base)) Base = TempOASE->getBase()->IgnoreParenImpCasts(); @@ -1113,14 +1111,20 @@ Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, Base = TempASE->getBase()->IgnoreParenImpCasts(); DE = cast(Base); OrigVD = cast(DE->getDecl()); - } else if (auto *ASE = dyn_cast(ClausesData[N].Ref)) { + } else if (auto *ASE = dyn_cast(Ref)) { auto *Base = ASE->getBase()->IgnoreParenImpCasts(); while (auto *TempASE = dyn_cast(Base)) Base = TempASE->getBase()->IgnoreParenImpCasts(); DE = cast(Base); OrigVD = cast(DE->getDecl()); } - if (OrigVD) { + return OrigVD; +} + +Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, + Address PrivateAddr) { + const DeclRefExpr *DE; + if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { BaseDecls.emplace_back(OrigVD); auto OriginalBaseLValue = CGF.EmitLValue(DE); LValue BaseLValue = @@ -5355,12 +5359,19 @@ void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, } /// Generates unique name for artificial threadprivate variables. -/// Format is: "." "_" -static std::string generateUniqueName(StringRef Prefix, SourceLocation Loc, - unsigned N) { +/// Format is: "." "_" "" +static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, + const Expr *Ref) { SmallString<256> Buffer; llvm::raw_svector_ostream Out(Buffer); - Out << Prefix << "." << Loc.getRawEncoding() << "_" << N; + const clang::DeclRefExpr *DE; + const VarDecl *D = ::getBaseDecl(Ref, DE); + if (!D) + D = cast(cast(Ref)->getDecl()); + D = D->getCanonicalDecl(); + Out << Prefix << "." + << (D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)) + << "_" << D->getCanonicalDecl()->getLocStart().getRawEncoding(); return Out.str(); } @@ -5397,7 +5408,7 @@ static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, if (RCG.getSizes(N).second) { Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().getSizeType(), - generateUniqueName("reduction_size", Loc, N)); + generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, CGM.getContext().getSizeType(), Loc); } @@ -5410,7 +5421,7 @@ static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, Address SharedAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().VoidPtrTy, - generateUniqueName("reduction", Loc, N)); + generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); } else { SharedLVal = CGF.MakeNaturalAlignAddrLValue( @@ -5466,7 +5477,7 @@ static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, if (RCG.getSizes(N).second) { Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().getSizeType(), - generateUniqueName("reduction_size", Loc, N)); + generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, CGM.getContext().getSizeType(), Loc); } @@ -5537,7 +5548,7 @@ static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, if (RCG.getSizes(N).second) { Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().getSizeType(), - generateUniqueName("reduction_size", Loc, N)); + generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, CGM.getContext().getSizeType(), Loc); } @@ -5666,14 +5677,14 @@ void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, /*isSigned=*/false); Address SizeAddr = getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().getSizeType(), - generateUniqueName("reduction_size", Loc, N)); + generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); } // Store address of the original reduction item if custom initializer is used. if (RCG.usesReductionInitializer(N)) { Address SharedAddr = getAddrOfArtificialThreadPrivate( CGF, CGM.getContext().VoidPtrTy, - generateUniqueName("reduction", Loc, N)); + generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); CGF.Builder.CreateStore( CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), diff --git a/lib/CodeGen/CGOpenMPRuntime.h b/lib/CodeGen/CGOpenMPRuntime.h index 046228e7e2..179d9b4394 100644 --- a/lib/CodeGen/CGOpenMPRuntime.h +++ b/lib/CodeGen/CGOpenMPRuntime.h @@ -191,6 +191,8 @@ public: } /// Returns the base declaration of the reduction item. const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; } + /// Returns the base declaration of the reduction item. + const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; } /// Returns true if the initialization of the reduction item uses initializer /// from declare reduction construct. bool usesReductionInitializer(unsigned N) const; diff --git a/test/OpenMP/taskgroup_task_reduction_codegen.cpp b/test/OpenMP/taskgroup_task_reduction_codegen.cpp index 29c1f46c8a..bf0dd9b096 100644 --- a/test/OpenMP/taskgroup_task_reduction_codegen.cpp +++ b/test/OpenMP/taskgroup_task_reduction_codegen.cpp @@ -12,6 +12,9 @@ #ifndef HEADER #define HEADER +// CHECK-DAG: @reduction_size.[[ID:.+]]_[[CID:[0-9]+]].artificial. +// CHECK-DAG: @reduction_size.[[ID]]_[[CID]].artificial..cache. + struct S { int a; S() : a(0) {} @@ -21,7 +24,6 @@ struct S { friend S operator+(const S&a, const S&b) {return a;} }; - int main(int argc, char **argv) { int a; float b; -- 2.50.1