[OPENMP] Rename OMPCapturedFieldDecl to OMPCapturedExprDecl, NFC.

OMPCapturedExprDecl allows caopturing not only of fielddecls, but also
other expressions. It also allows to simplify codegen for several
clauses.

llvm-svn: 260492
This commit is contained in:
Alexey Bataev 2016-02-11 05:35:55 +00:00
parent fe26def35c
commit 4244be25bd
17 changed files with 82 additions and 92 deletions

View File

@ -87,33 +87,33 @@ public:
static bool classofKind(Kind K) { return K == OMPThreadPrivate; }
};
/// Pseudo declaration for capturing of non-static data members in non-static
/// member functions.
/// Pseudo declaration for capturing expressions. Also is used for capturing of
/// non-static data members in non-static member functions.
///
/// Clang supports capturing of variables only, but OpenMP 4.5 allows to
/// privatize non-static members of current class in non-static member
/// functions. This pseudo-declaration allows properly handle this kind of
/// capture by wrapping captured expression into a variable-like declaration.
class OMPCapturedFieldDecl final : public VarDecl {
class OMPCapturedExprDecl final : public VarDecl {
friend class ASTDeclReader;
void anchor() override;
OMPCapturedFieldDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
QualType Type)
: VarDecl(OMPCapturedField, C, DC, SourceLocation(), SourceLocation(), Id,
OMPCapturedExprDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
QualType Type)
: VarDecl(OMPCapturedExpr, C, DC, SourceLocation(), SourceLocation(), Id,
Type, nullptr, SC_None) {
setImplicit();
}
public:
static OMPCapturedFieldDecl *Create(ASTContext &C, DeclContext *DC,
IdentifierInfo *Id, QualType T);
static OMPCapturedExprDecl *Create(ASTContext &C, DeclContext *DC,
IdentifierInfo *Id, QualType T);
static OMPCapturedFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
static OMPCapturedExprDecl *CreateDeserialized(ASTContext &C, unsigned ID);
// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classofKind(Kind K) { return K == OMPCapturedField; }
static bool classofKind(Kind K) { return K == OMPCapturedExpr; }
};
} // end namespace clang

View File

@ -1434,7 +1434,7 @@ DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
}
})
DEF_TRAVERSE_DECL(OMPCapturedFieldDecl, { TRY_TO(TraverseVarHelper(D)); })
DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); })
// A helper method for TemplateDecl's children.
template <typename Derived>

View File

@ -51,7 +51,7 @@ def Named : Decl<1>;
: DDecl<VarTemplateSpecialization>;
def ImplicitParam : DDecl<Var>;
def ParmVar : DDecl<Var>;
def OMPCapturedField : DDecl<Var>;
def OMPCapturedExpr : DDecl<Var>;
def NonTypeTemplateParm : DDecl<Declarator>;
def Template : DDecl<Named, 1>;
def RedeclarableTemplate : DDecl<Template, 1>;

View File

@ -1163,8 +1163,8 @@ namespace clang {
DECL_EMPTY,
/// \brief An ObjCTypeParamDecl record.
DECL_OBJC_TYPE_PARAM,
/// \brief An OMPCapturedFieldDecl record.
DECL_OMP_CAPTUREDFIELD,
/// \brief An OMPCapturedExprDecl record.
DECL_OMP_CAPTUREDEXPR
};
/// \brief Record codes for each kind of statement or expression.

View File

@ -655,7 +655,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
case ObjCCategoryImpl:
case Import:
case OMPThreadPrivate:
case OMPCapturedField:
case OMPCapturedExpr:
case Empty:
// Never looked up by name.
return 0;

View File

@ -7,7 +7,7 @@
//
//===----------------------------------------------------------------------===//
/// \file
/// \brief This file implements OMPThreadPrivateDecl, OMPCapturedFieldDecl
/// \brief This file implements OMPThreadPrivateDecl, OMPCapturedExprDecl
/// classes.
///
//===----------------------------------------------------------------------===//
@ -54,20 +54,19 @@ void OMPThreadPrivateDecl::setVars(ArrayRef<Expr *> VL) {
}
//===----------------------------------------------------------------------===//
// OMPCapturedFieldDecl Implementation.
// OMPCapturedExprDecl Implementation.
//===----------------------------------------------------------------------===//
void OMPCapturedFieldDecl::anchor() {}
void OMPCapturedExprDecl::anchor() {}
OMPCapturedFieldDecl *OMPCapturedFieldDecl::Create(ASTContext &C,
DeclContext *DC,
IdentifierInfo *Id,
QualType T) {
return new (C, DC) OMPCapturedFieldDecl(C, DC, Id, T);
OMPCapturedExprDecl *OMPCapturedExprDecl::Create(ASTContext &C, DeclContext *DC,
IdentifierInfo *Id,
QualType T) {
return new (C, DC) OMPCapturedExprDecl(C, DC, Id, T);
}
OMPCapturedFieldDecl *OMPCapturedFieldDecl::CreateDeserialized(ASTContext &C,
unsigned ID) {
return new (C, ID) OMPCapturedFieldDecl(C, nullptr, nullptr, QualType());
OMPCapturedExprDecl *OMPCapturedExprDecl::CreateDeserialized(ASTContext &C,
unsigned ID) {
return new (C, ID) OMPCapturedExprDecl(C, nullptr, nullptr, QualType());
}

View File

@ -92,7 +92,7 @@ namespace {
void VisitUsingDecl(UsingDecl *D);
void VisitUsingShadowDecl(UsingShadowDecl *D);
void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
void VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D);
void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
void PrintTemplateParameters(const TemplateParameterList *Params,
const TemplateArgumentList *Args = nullptr);
@ -1367,7 +1367,7 @@ void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
}
}
void DeclPrinter::VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D) {
void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
}

View File

@ -768,8 +768,8 @@ void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
assert(*I && "Expected non-null Stmt");
OS << (I == Node->varlist_begin() ? StartSym : ',');
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
if (auto *CFD = dyn_cast<OMPCapturedFieldDecl>(DRE->getDecl()))
CFD->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy, 0);
if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
CED->getInit()->printPretty(OS, nullptr, Policy, 0);
else
DRE->getDecl()->printQualifiedName(OS);
} else

View File

@ -92,7 +92,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
case Decl::Label: // __label__ x;
case Decl::Import:
case Decl::OMPThreadPrivate:
case Decl::OMPCapturedField:
case Decl::OMPCapturedExpr:
case Decl::Empty:
// None of these decls require codegen support.
return;

View File

@ -1535,16 +1535,10 @@ emitScheduleClause(CodeGenFunction &CGF, const OMPLoopDirective &S,
M2 = C->getSecondScheduleModifier();
if (const auto *Ch = C->getChunkSize()) {
if (auto *ImpRef = cast_or_null<DeclRefExpr>(C->getHelperChunkSize())) {
if (OuterRegion) {
const VarDecl *ImpVar = cast<VarDecl>(ImpRef->getDecl());
CGF.EmitVarDecl(*ImpVar);
CGF.EmitStoreThroughLValue(
CGF.EmitAnyExpr(Ch),
CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(ImpVar),
ImpVar->getType()));
} else {
if (OuterRegion)
CGF.EmitVarDecl(*cast<VarDecl>(ImpRef->getDecl()));
else
Ch = ImpRef;
}
}
if (!C->getHelperChunkSize() || !OuterRegion) {
Chunk = CGF.EmitScalarExpr(Ch);

View File

@ -2874,7 +2874,7 @@ ExprResult Sema::BuildDeclarationNameExpr(
case Decl::Var:
case Decl::VarTemplateSpecialization:
case Decl::VarTemplatePartialSpecialization:
case Decl::OMPCapturedField:
case Decl::OMPCapturedExpr:
// In C, "extern void blah;" is valid and is an r-value.
if (!getLangOpts().CPlusPlus &&
!type.hasQualifiers() &&

View File

@ -1714,6 +1714,30 @@ void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
}
}
static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
Expr *CaptureExpr) {
ASTContext &C = S.getASTContext();
Expr *Init = CaptureExpr->IgnoreImpCasts();
QualType Ty = Init->getType();
if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
if (S.getLangOpts().CPlusPlus)
Ty = C.getLValueReferenceType(Ty);
else {
Ty = C.getPointerType(Ty);
ExprResult Res =
S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
if (!Res.isUsable())
return nullptr;
Init = Res.get();
}
}
auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
S.CurContext->addHiddenDecl(CED);
S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
/*TypeMayContainAuto=*/true);
return buildDeclRefExpr(S, CED, Ty.getNonReferenceType(), SourceLocation());
}
StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
ArrayRef<OMPClause *> Clauses) {
if (!S.isUsable()) {
@ -1740,13 +1764,18 @@ StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
}
DSAStack->setForceVarCapturing(/*V=*/false);
} else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
Clause->getClauseKind() == OMPC_schedule) {
(Clause->getClauseKind() == OMPC_schedule ||
Clause->getClauseKind() == OMPC_dist_schedule)) {
// Mark all variables in private list clauses as used in inner region.
// Required for proper codegen of combined directives.
// TODO: add processing for other clauses.
if (auto *E = cast_or_null<Expr>(
cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
MarkDeclarationsReferencedInExpr(E);
if (auto *SC = dyn_cast<OMPScheduleClause>(Clause)) {
if (SC->getHelperChunkSize())
MarkDeclarationsReferencedInExpr(SC->getHelperChunkSize());
} else if (auto *DSC = dyn_cast<OMPDistScheduleClause>(Clause)) {
if (DSC->getHelperChunkSize())
MarkDeclarationsReferencedInExpr(DSC->getHelperChunkSize());
}
}
if (Clause->getClauseKind() == OMPC_schedule)
SC = cast<OMPScheduleClause>(Clause);
@ -6735,12 +6764,8 @@ OMPClause *Sema::ActOnOpenMPScheduleClause(
return nullptr;
}
} else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
ChunkSize->getType(), ".chunk.");
auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
ChunkSize->getExprLoc(),
/*RefersToCapture=*/true);
HelperValExpr = ImpVarRef;
HelperValExpr =
buildCapture(*this, &Context.Idents.get(".chunk."), ValExpr);
}
}
}
@ -6973,30 +6998,6 @@ OMPClause *Sema::ActOnOpenMPVarListClause(
return Res;
}
static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
Expr *CaptureExpr) {
ASTContext &C = S.getASTContext();
Expr *Init = CaptureExpr->IgnoreImpCasts();
QualType Ty = Init->getType();
if (CaptureExpr->getObjectKind() == OK_Ordinary) {
if (S.getLangOpts().CPlusPlus)
Ty = C.getLValueReferenceType(Ty);
else {
Ty = C.getPointerType(Ty);
ExprResult Res =
S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
if (!Res.isUsable())
return nullptr;
Init = Res.get();
}
}
auto *CFD = OMPCapturedFieldDecl::Create(C, S.CurContext, Id, Ty);
S.CurContext->addHiddenDecl(CFD);
S.AddInitializerToDecl(CFD, Init, /*DirectInit=*/false,
/*TypeMayContainAuto=*/true);
return buildDeclRefExpr(S, CFD, Ty.getNonReferenceType(), SourceLocation());
}
ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
ExprObjectKind OK) {
SourceLocation Loc = Capture->getInit()->getExprLoc();
@ -9549,12 +9550,8 @@ OMPClause *Sema::ActOnOpenMPDistScheduleClause(
return nullptr;
}
} else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
ChunkSize->getType(), ".chunk.");
auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
ChunkSize->getExprLoc(),
/*RefersToCapture=*/true);
HelperValExpr = ImpVarRef;
HelperValExpr =
buildCapture(*this, &Context.Idents.get(".chunk."), ValExpr);
}
}
}

View File

@ -2483,8 +2483,8 @@ Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
return TD;
}
Decl *TemplateDeclInstantiator::VisitOMPCapturedFieldDecl(
OMPCapturedFieldDecl * /*D*/) {
Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl(
OMPCapturedExprDecl * /*D*/) {
llvm_unreachable("Should not be met in templates");
}

View File

@ -329,7 +329,7 @@ bool serialization::isRedeclarableDeclKind(unsigned Kind) {
case Decl::ClassScopeFunctionSpecialization:
case Decl::Import:
case Decl::OMPThreadPrivate:
case Decl::OMPCapturedField:
case Decl::OMPCapturedExpr:
case Decl::BuiltinTemplate:
return false;

View File

@ -350,7 +350,7 @@ namespace clang {
void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
void VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D);
void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
/// We've merged the definition \p MergedDef into the existing definition
/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
@ -2361,7 +2361,7 @@ void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
D->setVars(Vars);
}
void ASTDeclReader::VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D) {
void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
VisitVarDecl(D);
}
@ -3328,8 +3328,8 @@ Decl *ASTReader::ReadDeclRecord(DeclID ID) {
case DECL_OMP_THREADPRIVATE:
D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record[Idx++]);
break;
case DECL_OMP_CAPTUREDFIELD:
D = OMPCapturedFieldDecl::CreateDeserialized(Context, ID);
case DECL_OMP_CAPTUREDEXPR:
D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
break;
case DECL_EMPTY:
D = EmptyDecl::CreateDeserialized(Context, ID);

View File

@ -131,7 +131,7 @@ namespace clang {
void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
void VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D);
void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
/// Add an Objective-C type parameter list to the given record.
void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {
@ -1629,9 +1629,9 @@ void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
Code = serialization::DECL_OMP_THREADPRIVATE;
}
void ASTDeclWriter::VisitOMPCapturedFieldDecl(OMPCapturedFieldDecl *D) {
void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
VisitVarDecl(D);
Code = serialization::DECL_OMP_CAPTUREDFIELD;
Code = serialization::DECL_OMP_CAPTUREDEXPR;
}
//===----------------------------------------------------------------------===//

View File

@ -5670,7 +5670,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::StaticAssert:
case Decl::Block:
case Decl::Captured:
case Decl::OMPCapturedField:
case Decl::OMPCapturedExpr:
case Decl::Label: // FIXME: Is this right??
case Decl::ClassScopeFunctionSpecialization:
case Decl::Import: