From e6aa4694de2d11fdc3e352a29b1491740bfcb5e5 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Thu, 13 Sep 2018 16:54:05 +0000 Subject: [PATCH] [OPENMP] Fix PR38903: Crash on instantiation of the non-dependent declare reduction. If the declare reduction construct with the non-dependent type is defined in the template construct, the compiler might crash on the template instantition. Reworked the whole instantiation scheme for the declare reduction constructs to fix this problem correctly. llvm-svn: 342151 --- clang/include/clang/AST/DeclOpenMP.h | 34 +++++++++- clang/lib/CodeGen/CGOpenMPRuntime.cpp | 31 ++++------ clang/lib/CodeGen/CGOpenMPRuntime.h | 6 +- clang/lib/Parse/ParseOpenMP.cpp | 10 ++- clang/lib/Sema/SemaOpenMP.cpp | 10 +++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 79 ++++++++++++++---------- clang/lib/Serialization/ASTReaderDecl.cpp | 15 +++-- clang/lib/Serialization/ASTWriterDecl.cpp | 4 ++ clang/test/OpenMP/declare_reduction_messages.cpp | 22 ++++++- 9 files changed, 144 insertions(+), 67 deletions(-) diff --git a/clang/include/clang/AST/DeclOpenMP.h b/clang/include/clang/AST/DeclOpenMP.h index 1f2aaa1..33fe0e1 100644 --- a/clang/include/clang/AST/DeclOpenMP.h +++ b/clang/include/clang/AST/DeclOpenMP.h @@ -112,9 +112,17 @@ public: private: friend class ASTDeclReader; /// Combiner for declare reduction construct. - Expr *Combiner; + Expr *Combiner = nullptr; /// Initializer for declare reduction construct. - Expr *Initializer; + Expr *Initializer = nullptr; + /// In parameter of the combiner. + Expr *In = nullptr; + /// Out parameter of the combiner. + Expr *Out = nullptr; + /// Priv parameter of the initializer. + Expr *Priv = nullptr; + /// Orig parameter of the initializer. + Expr *Orig = nullptr; /// Reference to the previous declare reduction construct in the same /// scope with the same name. Required for proper templates instantiation if @@ -143,8 +151,19 @@ public: /// Get combiner expression of the declare reduction construct. Expr *getCombiner() { return Combiner; } const Expr *getCombiner() const { return Combiner; } + /// Get In variable of the combiner. + Expr *getCombinerIn() { return In; } + const Expr *getCombinerIn() const { return In; } + /// Get Out variable of the combiner. + Expr *getCombinerOut() { return Out; } + const Expr *getCombinerOut() const { return Out; } /// Set combiner expression for the declare reduction construct. void setCombiner(Expr *E) { Combiner = E; } + /// Set combiner In and Out vars. + void setCombinerData(Expr *InE, Expr *OutE) { + In = InE; + Out = OutE; + } /// Get initializer expression (if specified) of the declare reduction /// construct. @@ -154,11 +173,22 @@ public: InitKind getInitializerKind() const { return static_cast(OMPDeclareReductionDeclBits.InitializerKind); } + /// Get Orig variable of the initializer. + Expr *getInitOrig() { return Orig; } + const Expr *getInitOrig() const { return Orig; } + /// Get Priv variable of the initializer. + Expr *getInitPriv() { return Priv; } + const Expr *getInitPriv() const { return Priv; } /// Set initializer expression for the declare reduction construct. void setInitializer(Expr *E, InitKind IK) { Initializer = E; OMPDeclareReductionDeclBits.InitializerKind = IK; } + /// Set initializer Orig and Priv vars. + void setInitializerData(Expr *OrigE, Expr *PrivE) { + Orig = OrigE; + Priv = PrivE; + } /// Get reference to previous declare reduction construct in the same /// scope with the same name. diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index 6c37c6c..9452e96 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -1295,27 +1295,19 @@ void CGOpenMPRuntime::emitUserDefinedReduction( CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { if (UDRMap.count(D) > 0) return; - ASTContext &C = CGM.getContext(); - if (!In || !Out) { - In = &C.Idents.get("omp_in"); - Out = &C.Idents.get("omp_out"); - } llvm::Function *Combiner = emitCombinerOrInitializer( - CGM, D->getType(), D->getCombiner(), cast(D->lookup(In).front()), - cast(D->lookup(Out).front()), + CGM, D->getType(), D->getCombiner(), + cast(cast(D->getCombinerIn())->getDecl()), + cast(cast(D->getCombinerOut())->getDecl()), /*IsCombiner=*/true); llvm::Function *Initializer = nullptr; if (const Expr *Init = D->getInitializer()) { - if (!Priv || !Orig) { - Priv = &C.Idents.get("omp_priv"); - Orig = &C.Idents.get("omp_orig"); - } Initializer = emitCombinerOrInitializer( CGM, D->getType(), D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init : nullptr, - cast(D->lookup(Orig).front()), - cast(D->lookup(Priv).front()), + cast(cast(D->getInitOrig())->getDecl()), + cast(cast(D->getInitPriv())->getDecl()), /*IsCombiner=*/false); } UDRMap.try_emplace(D, Combiner, Initializer); @@ -8052,19 +8044,19 @@ void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, } bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { - const auto *FD = cast(GD.getDecl()); - // If emitting code for the host, we do not process FD here. Instead we do // the normal code generation. if (!CGM.getLangOpts().OpenMPIsDevice) return false; // Try to detect target regions in the function. - scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); + const ValueDecl *VD = cast(GD.getDecl()); + if (const auto *FD = dyn_cast(VD)) + scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); // Do not to emit function if it is not marked as declare target. - return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD) && - AlreadyEmittedTargetFunctions.count(FD->getCanonicalDecl()) == 0; + return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && + AlreadyEmittedTargetFunctions.count(VD->getCanonicalDecl()) == 0; } bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { @@ -8152,7 +8144,8 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, } bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { - if (isa(GD.getDecl())) + if (isa(GD.getDecl()) || + isa(GD.getDecl())) return emitTargetFunctions(GD); return emitTargetGlobalVariable(GD); diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h index 46fa774..e0685d9 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -315,10 +315,6 @@ private: SmallVector> FunctionUDRMapTy; FunctionUDRMapTy FunctionUDRMap; - IdentifierInfo *In = nullptr; - IdentifierInfo *Out = nullptr; - IdentifierInfo *Priv = nullptr; - IdentifierInfo *Orig = nullptr; /// Type kmp_critical_name, originally defined as typedef kmp_int32 /// kmp_critical_name[8]; llvm::ArrayType *KmpCriticalNameTy; @@ -600,7 +596,7 @@ private: OffloadEntriesInfoManagerTy OffloadEntriesInfoManager; bool ShouldMarkAsGlobal = true; - llvm::SmallDenseSet AlreadyEmittedTargetFunctions; + llvm::SmallDenseSet AlreadyEmittedTargetFunctions; /// List of variables that can become declare target implicitly and, thus, /// must be emitted. diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp index 9871e5b..cdcbf79 100644 --- a/clang/lib/Parse/ParseOpenMP.cpp +++ b/clang/lib/Parse/ParseOpenMP.cpp @@ -353,8 +353,14 @@ Parser::ParseOpenMPDeclareReductionDirective(AccessSpecifier AS) { // Check if initializer is omp_priv or something else. if (Tok.is(tok::identifier) && Tok.getIdentifierInfo()->isStr("omp_priv")) { - ConsumeToken(); - ParseOpenMPReductionInitializerForDecl(OmpPrivParm); + if (Actions.getLangOpts().CPlusPlus) { + InitializerResult = Actions.ActOnFinishFullExpr( + ParseAssignmentExpression().get(), D->getLocation(), + /*DiscardedValue=*/true); + } else { + ConsumeToken(); + ParseOpenMPReductionInitializerForDecl(OmpPrivParm); + } } else { InitializerResult = Actions.ActOnFinishFullExpr( ParseAssignmentExpression().get(), D->getLocation(), diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp index 728719468..ffcb4ba 100644 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -12763,6 +12763,11 @@ void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { DRD->addDecl(OmpInParm); DRD->addDecl(OmpOutParm); } + Expr *InE = + ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); + Expr *OutE = + ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); + DRD->setCombinerData(InE, OutE); } void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { @@ -12818,6 +12823,11 @@ VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { DRD->addDecl(OmpPrivParm); DRD->addDecl(OmpOrigParm); } + Expr *OrigE = + ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); + Expr *PrivE = + ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); + DRD->setInitializerData(OrigE, PrivE); return OmpPrivParm; } diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 2c29b8c..09aa25c 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2733,10 +2733,19 @@ Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( OMPDeclareReductionDecl *D) { // Instantiate type and check if it is allowed. - QualType SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( - D->getLocation(), - ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs, - D->getLocation(), DeclarationName()))); + const bool RequiresInstantiation = + D->getType()->isDependentType() || + D->getType()->isInstantiationDependentType() || + D->getType()->containsUnexpandedParameterPack(); + QualType SubstReductionType; + if (RequiresInstantiation) { + SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( + D->getLocation(), + ParsedType::make(SemaRef.SubstType( + D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); + } else { + SubstReductionType = D->getType(); + } if (SubstReductionType.isNull()) return nullptr; bool IsCorrect = !SubstReductionType.isNull(); @@ -2753,25 +2762,35 @@ Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), PrevDeclInScope); auto *NewDRD = cast(DRD.get().getSingleDecl()); - if (isDeclWithinFunction(NewDRD)) - SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); + SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); + if (!RequiresInstantiation) { + if (Expr *Combiner = D->getCombiner()) { + NewDRD->setCombinerData(D->getCombinerIn(), D->getCombinerOut()); + NewDRD->setCombiner(Combiner); + if (Expr *Init = D->getInitializer()) { + NewDRD->setInitializerData(D->getInitOrig(), D->getInitPriv()); + NewDRD->setInitializer(Init, D->getInitializerKind()); + } + } + (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( + /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); + return NewDRD; + } Expr *SubstCombiner = nullptr; Expr *SubstInitializer = nullptr; // Combiners instantiation sequence. if (D->getCombiner()) { SemaRef.ActOnOpenMPDeclareReductionCombinerStart( /*S=*/nullptr, NewDRD); - const char *Names[] = {"omp_in", "omp_out"}; - for (auto &Name : Names) { - DeclarationName DN(&SemaRef.Context.Idents.get(Name)); - auto OldLookup = D->lookup(DN); - auto Lookup = NewDRD->lookup(DN); - if (!OldLookup.empty() && !Lookup.empty()) { - assert(Lookup.size() == 1 && OldLookup.size() == 1); - SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldLookup.front(), - Lookup.front()); - } - } + SemaRef.CurrentInstantiationScope->InstantiatedLocal( + cast(D->getCombinerIn())->getDecl(), + cast(NewDRD->getCombinerIn())->getDecl()); + SemaRef.CurrentInstantiationScope->InstantiatedLocal( + cast(D->getCombinerOut())->getDecl(), + cast(NewDRD->getCombinerOut())->getDecl()); + auto *ThisContext = dyn_cast_or_null(Owner); + Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, /*TypeQuals*/ 0, + ThisContext); SubstCombiner = SemaRef.SubstExpr(D->getCombiner(), TemplateArgs).get(); SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); // Initializers instantiation sequence. @@ -2779,19 +2798,12 @@ Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( VarDecl *OmpPrivParm = SemaRef.ActOnOpenMPDeclareReductionInitializerStart( /*S=*/nullptr, NewDRD); - const char *Names[] = {"omp_orig", "omp_priv"}; - for (auto &Name : Names) { - DeclarationName DN(&SemaRef.Context.Idents.get(Name)); - auto OldLookup = D->lookup(DN); - auto Lookup = NewDRD->lookup(DN); - if (!OldLookup.empty() && !Lookup.empty()) { - assert(Lookup.size() == 1 && OldLookup.size() == 1); - auto *OldVD = cast(OldLookup.front()); - auto *NewVD = cast(Lookup.front()); - SemaRef.InstantiateVariableInitializer(NewVD, OldVD, TemplateArgs); - SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldVD, NewVD); - } - } + SemaRef.CurrentInstantiationScope->InstantiatedLocal( + cast(D->getInitOrig())->getDecl(), + cast(NewDRD->getInitOrig())->getDecl()); + SemaRef.CurrentInstantiationScope->InstantiatedLocal( + cast(D->getInitPriv())->getDecl(), + cast(NewDRD->getInitPriv())->getDecl()); if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { SubstInitializer = SemaRef.SubstExpr(D->getInitializer(), TemplateArgs).get(); @@ -2808,8 +2820,9 @@ Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( SubstInitializer) || (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && !SubstInitializer && !SubstInitializer)); - } else + } else { IsCorrect = false; + } (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd(/*S=*/nullptr, DRD, IsCorrect); @@ -4897,7 +4910,9 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, return D; if (isa(D) || isa(D) || isa(D) || isa(D) || - (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) || + ((ParentDC->isFunctionOrMethod() || + isa(ParentDC)) && + ParentDC->isDependentContext()) || (isa(D) && cast(D)->isLambda())) { // D is a local of some kind. Look into the map of local // declarations to their instantiations. diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 8295107..d06a622 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -2633,10 +2633,17 @@ void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { VisitValueDecl(D); D->setLocation(ReadSourceLocation()); - D->setCombiner(Record.readExpr()); - D->setInitializer( - Record.readExpr(), - static_cast(Record.readInt())); + Expr *In = Record.readExpr(); + Expr *Out = Record.readExpr(); + D->setCombinerData(In, Out); + Expr *Combiner = Record.readExpr(); + D->setCombiner(Combiner); + Expr *Orig = Record.readExpr(); + Expr *Priv = Record.readExpr(); + D->setInitializerData(Orig, Priv); + Expr *Init = Record.readExpr(); + auto IK = static_cast(Record.readInt()); + D->setInitializer(Init, IK); D->PrevDeclInScope = ReadDeclID(); } diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index 8eb6c61..d757f60 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -1742,7 +1742,11 @@ void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { VisitValueDecl(D); Record.AddSourceLocation(D->getBeginLoc()); + Record.AddStmt(D->getCombinerIn()); + Record.AddStmt(D->getCombinerOut()); Record.AddStmt(D->getCombiner()); + Record.AddStmt(D->getInitOrig()); + Record.AddStmt(D->getInitPriv()); Record.AddStmt(D->getInitializer()); Record.push_back(D->getInitializerKind()); Record.AddDeclRef(D->getPrevDeclInScope()); diff --git a/clang/test/OpenMP/declare_reduction_messages.cpp b/clang/test/OpenMP/declare_reduction_messages.cpp index f22f924..21c03fa 100644 --- a/clang/test/OpenMP/declare_reduction_messages.cpp +++ b/clang/test/OpenMP/declare_reduction_messages.cpp @@ -142,17 +142,33 @@ int main() { #if __cplusplus == 201103L struct A { A() {} - // expected-note@+1 {{copy constructor is implicitly deleted because 'A' has a user-declared move assignment operator}} A& operator=(A&&) = default; }; int A_TEST() { A test; -// expected-error@+1 {{call to implicitly-deleted copy constructor of 'A'}} #pragma omp declare reduction(+ : A : omp_out) initializer(omp_priv = A()) -// expected-error@+1 {{invalid operands to binary expression ('A' and 'A')}} #pragma omp parallel reduction(+ : test) {} return 0; } + +struct U +{ + void foo(U&, bool); + U(); +}; +template +struct S +{ + int s; + // expected-note@+1 {{'foo' declared here}} + void foo(S &x) {}; + // expected-error@+1 {{too many arguments to function call, expected single argument 'x', have 2 arguments}} + #pragma omp declare reduction (foo : U, S : omp_out.foo(omp_in, false)) +}; +// expected-warning@+2 {{extra tokens at the end of '#pragma omp declare reduction' are ignored}} +// expected-note@+1 {{in instantiation of template class 'S<1>' requested here}} +#pragma omp declare reduction (bar : S<1> : omp_out.foo(omp_in)) + #endif -- 2.7.4