Author: abataev Date: Tue Mar 6 10:59:43 2018 New Revision: 326827 URL: http://llvm.org/viewvc/llvm-project?rev=326827&view=rev Log: [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. Modified: cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp cfe/trunk/lib/CodeGen/CGOpenMPRuntime.h cfe/trunk/test/OpenMP/taskgroup_task_reduction_codegen.cpp Modified: cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp?rev=326827&r1=326826&r2=326827&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp (original) +++ cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp Tue Mar 6 10:59:43 2018 @@ -1101,11 +1101,9 @@ static Address castToBase(CodeGenFunctio 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<OMPArraySectionExpr>(ClausesData[N].Ref)) { + if (auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { auto *Base = OASE->getBase()->IgnoreParenImpCasts(); while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) Base = TempOASE->getBase()->IgnoreParenImpCasts(); @@ -1113,14 +1111,20 @@ Address ReductionCodeGen::adjustPrivateA Base = TempASE->getBase()->IgnoreParenImpCasts(); DE = cast<DeclRefExpr>(Base); OrigVD = cast<VarDecl>(DE->getDecl()); - } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(ClausesData[N].Ref)) { + } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { auto *Base = ASE->getBase()->IgnoreParenImpCasts(); while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) Base = TempASE->getBase()->IgnoreParenImpCasts(); DE = cast<DeclRefExpr>(Base); OrigVD = cast<VarDecl>(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(Code } /// Generates unique name for artificial threadprivate variables. -/// Format is: <Prefix> "." <Loc_raw_encoding> "_" <N> -static std::string generateUniqueName(StringRef Prefix, SourceLocation Loc, - unsigned N) { +/// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" +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<VarDecl>(cast<DeclRefExpr>(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 *emitReduceInitFuncti 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 *emitReduceInitFuncti 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 *emitReduceCombFuncti 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 *emitReduceFiniFuncti 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::emitTaskReductionF /*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), Modified: cfe/trunk/lib/CodeGen/CGOpenMPRuntime.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGOpenMPRuntime.h?rev=326827&r1=326826&r2=326827&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGOpenMPRuntime.h (original) +++ cfe/trunk/lib/CodeGen/CGOpenMPRuntime.h Tue Mar 6 10:59:43 2018 @@ -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; Modified: cfe/trunk/test/OpenMP/taskgroup_task_reduction_codegen.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/taskgroup_task_reduction_codegen.cpp?rev=326827&r1=326826&r2=326827&view=diff ============================================================================== --- cfe/trunk/test/OpenMP/taskgroup_task_reduction_codegen.cpp (original) +++ cfe/trunk/test/OpenMP/taskgroup_task_reduction_codegen.cpp Tue Mar 6 10:59:43 2018 @@ -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; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits