[clang] 55eca28 - [OpenMP][NFC] Minimize memory usage and copying of `OMPTraitInfo`s
Johannes Doerfert via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 23 12:25:06 PDT 2020
Author: Johannes Doerfert
Date: 2020-03-23T14:23:46-05:00
New Revision: 55eca2853e4f096be189208770034a8a4fb72666
URL: https://github.com/llvm/llvm-project/commit/55eca2853e4f096be189208770034a8a4fb72666
DIFF: https://github.com/llvm/llvm-project/commit/55eca2853e4f096be189208770034a8a4fb72666.diff
LOG: [OpenMP][NFC] Minimize memory usage and copying of `OMPTraitInfo`s
See rational here: https://reviews.llvm.org/D71830#1922656
Reviewed By: rnk
Differential Revision: https://reviews.llvm.org/D76173
Added:
Modified:
clang/include/clang/AST/ASTContext.h
clang/include/clang/AST/OpenMPClause.h
clang/include/clang/Basic/Attr.td
clang/include/clang/Serialization/ASTRecordReader.h
clang/include/clang/Serialization/ASTRecordWriter.h
clang/lib/AST/ASTContext.cpp
clang/lib/AST/AttrImpl.cpp
clang/lib/AST/OpenMPClause.cpp
clang/lib/CodeGen/CGOpenMPRuntime.cpp
clang/lib/Parse/ParseOpenMP.cpp
clang/lib/Sema/SemaOpenMP.cpp
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTReaderDecl.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/utils/TableGen/ClangAttrEmitter.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index d74edb8a8adb..d9e2945ebbea 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -116,6 +116,7 @@ class ObjCPropertyDecl;
class ObjCPropertyImplDecl;
class ObjCProtocolDecl;
class ObjCTypeParamDecl;
+struct OMPTraitInfo;
struct ParsedTargetAttr;
class Preprocessor;
class Stmt;
@@ -2962,6 +2963,14 @@ OPT_LIST(V)
};
llvm::StringMap<SectionInfo> SectionInfos;
+
+ /// Return a new OMPTraitInfo object owned by this context.
+ OMPTraitInfo &getNewOMPTraitInfo();
+
+private:
+ /// All OMPTraitInfo objects live in this collection, one per
+ /// `pragma omp [begin] declare variant` directive.
+ SmallVector<OMPTraitInfo *, 4> OMPTraitInfoVector;
};
/// Utility function for constructing a nullary selector.
diff --git a/clang/include/clang/AST/OpenMPClause.h b/clang/include/clang/AST/OpenMPClause.h
index acf450f07e66..8b92cb62634a 100644
--- a/clang/include/clang/AST/OpenMPClause.h
+++ b/clang/include/clang/AST/OpenMPClause.h
@@ -7116,22 +7116,27 @@ class OMPClausePrinter final : public OMPClauseVisitor<OMPClausePrinter> {
/// collection of selector sets, each with an associated kind and an ordered
/// collection of selectors. A selector has a kind, an optional score/condition,
/// and an ordered collection of properties.
-struct OMPTraitInfo {
+class OMPTraitInfo {
+ /// Private constructor accesible only by ASTContext.
+ OMPTraitInfo() {}
+ friend class ASTContext;
+
+public:
struct OMPTraitProperty {
llvm::omp::TraitProperty Kind = llvm::omp::TraitProperty::invalid;
};
struct OMPTraitSelector {
Expr *ScoreOrCondition = nullptr;
llvm::omp::TraitSelector Kind = llvm::omp::TraitSelector::invalid;
- llvm::SmallVector<OMPTraitProperty, 4> Properties;
+ llvm::SmallVector<OMPTraitProperty, 1> Properties;
};
struct OMPTraitSet {
llvm::omp::TraitSet Kind = llvm::omp::TraitSet::invalid;
- llvm::SmallVector<OMPTraitSelector, 4> Selectors;
+ llvm::SmallVector<OMPTraitSelector, 2> Selectors;
};
/// The outermost level of selector sets.
- llvm::SmallVector<OMPTraitSet, 4> Sets;
+ llvm::SmallVector<OMPTraitSet, 2> Sets;
bool anyScoreOrCondition(
llvm::function_ref<bool(Expr *&, bool /* IsScore */)> Cond) {
@@ -7157,6 +7162,7 @@ struct OMPTraitInfo {
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI);
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI);
} // namespace clang
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index c8c35cbe5b70..db1589bea814 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -3314,7 +3314,7 @@ def OMPDeclareVariant : InheritableAttr {
OMPTraitInfoArgument<"TraitInfos">,
];
let AdditionalMembers = [{
- OMPTraitInfo &getTraitInfo() { return traitInfos; }
+ OMPTraitInfo &getTraitInfo() { return *traitInfos; }
void printPrettyPragma(raw_ostream & OS, const PrintingPolicy &Policy)
const;
}];
diff --git a/clang/include/clang/Serialization/ASTRecordReader.h b/clang/include/clang/Serialization/ASTRecordReader.h
index 7d4ef7c43a9d..362296024a97 100644
--- a/clang/include/clang/Serialization/ASTRecordReader.h
+++ b/clang/include/clang/Serialization/ASTRecordReader.h
@@ -260,7 +260,7 @@ class ASTRecordReader
}
/// Read an OMPTraitInfo object, advancing Idx.
- OMPTraitInfo readOMPTraitInfo();
+ OMPTraitInfo *readOMPTraitInfo();
/// Read an OpenMP clause, advancing Idx.
OMPClause *readOMPClause();
diff --git a/clang/include/clang/Serialization/ASTRecordWriter.h b/clang/include/clang/Serialization/ASTRecordWriter.h
index 924aa5d4b758..491207c9de90 100644
--- a/clang/include/clang/Serialization/ASTRecordWriter.h
+++ b/clang/include/clang/Serialization/ASTRecordWriter.h
@@ -267,7 +267,7 @@ class ASTRecordWriter
void AddCXXDefinitionData(const CXXRecordDecl *D);
/// Write an OMPTraitInfo object.
- void writeOMPTraitInfo(const OMPTraitInfo &TI);
+ void writeOMPTraitInfo(const OMPTraitInfo *TI);
void writeOMPClause(OMPClause *C);
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 77d5e59d9f0c..390abda466a2 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1005,6 +1005,9 @@ ASTContext::~ASTContext() {
for (APValue *Value : APValueCleanups)
Value->~APValue();
+
+ // Destroy the OMPTraitInfo objects that life here.
+ llvm::DeleteContainerPointers(OMPTraitInfoVector);
}
void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) {
@@ -10838,3 +10841,8 @@ void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
Target->getTargetOpts().Features);
}
}
+
+OMPTraitInfo &ASTContext::getNewOMPTraitInfo() {
+ OMPTraitInfoVector.push_back(new OMPTraitInfo());
+ return *OMPTraitInfoVector.back();
+}
diff --git a/clang/lib/AST/AttrImpl.cpp b/clang/lib/AST/AttrImpl.cpp
index 2c76f86713fb..a5ff68c18778 100644
--- a/clang/lib/AST/AttrImpl.cpp
+++ b/clang/lib/AST/AttrImpl.cpp
@@ -159,7 +159,7 @@ void OMPDeclareVariantAttr::printPrettyPragma(
OS << ")";
}
OS << " match(";
- traitInfos.print(OS, Policy);
+ traitInfos->print(OS, Policy);
OS << ")";
}
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index 5f9da85a9f07..87ab3e333e22 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -1964,3 +1964,7 @@ llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
TI.print(OS, Policy);
return OS;
}
+llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
+ const OMPTraitInfo *TI) {
+ return TI ? OS << *TI : OS;
+}
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index b00027586767..58c5334776f3 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -11393,7 +11393,7 @@ static const FunctionDecl *getDeclareVariantFunction(CodeGenModule &CGM,
SmallVector<Expr *, 8> VariantExprs;
SmallVector<VariantMatchInfo, 8> VMIs;
for (const auto *A : FD->specific_attrs<OMPDeclareVariantAttr>()) {
- const OMPTraitInfo &TI = A->getTraitInfos();
+ const OMPTraitInfo &TI = *A->getTraitInfos();
VMIs.push_back(VariantMatchInfo());
TI.getAsVariantMatchInfo(CGM.getContext(), VMIs.back());
VariantExprs.push_back(A->getVariantFuncRef());
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index acc1e55eb721..33b07bad8935 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -1365,7 +1365,7 @@ void Parser::ParseOMPDeclareVariantClauses(Parser::DeclGroupPtrTy Ptr,
}
// Parse inner context selectors.
- OMPTraitInfo TI;
+ OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo();
parseOMPContextSelectors(Loc, TI);
// Parse ')'
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index d9a4a17f1866..c9e2f395ec06 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -5703,7 +5703,7 @@ void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD,
OMPTraitInfo &TI,
SourceRange SR) {
auto *NewAttr =
- OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, TI, SR);
+ OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR);
FD->addAttr(NewAttr);
}
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 875eb2bfb940..502c0babab8d 100755
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -398,7 +398,8 @@ static void instantiateOMPDeclareVariantAttr(
// Copy the template version of the OMPTraitInfo and run substitute on all
// score and condition expressiosn.
- OMPTraitInfo TI = Attr.getTraitInfos();
+ OMPTraitInfo &TI = S.getASTContext().getNewOMPTraitInfo();
+ TI = *Attr.getTraitInfos();
// Try to substitute template parameters in score and condition expressions.
auto SubstScoreOrConditionExpr = [&S, Subst](Expr *&E, bool) {
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 77f15bcd910a..fbd68929b2f1 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -12684,8 +12684,8 @@ void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
C->setKindKwLoc(Record.readSourceLocation());
}
-OMPTraitInfo ASTRecordReader::readOMPTraitInfo() {
- OMPTraitInfo TI;
+OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
+ OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
TI.Sets.resize(readUInt32());
for (auto &Set : TI.Sets) {
Set.Kind = readEnum<llvm::omp::TraitSet>();
@@ -12700,5 +12700,5 @@ OMPTraitInfo ASTRecordReader::readOMPTraitInfo() {
Property.Kind = readEnum<llvm::omp::TraitProperty>();
}
}
- return TI;
+ return &TI;
}
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index bb2ecb53c7ce..3bd7b825cdc8 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -2744,7 +2744,7 @@ class AttrReader {
return Reader.readVersionTuple();
}
- OMPTraitInfo readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
+ OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
return Reader.GetLocalDeclAs<T>(LocalID);
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 9ccebae9567d..f30824622346 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -6624,9 +6624,9 @@ void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
Record.AddSourceLocation(C->getKindKwLoc());
}
-void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo &TI) {
- writeUInt32(TI.Sets.size());
- for (const auto &Set : TI.Sets) {
+void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
+ writeUInt32(TI->Sets.size());
+ for (const auto &Set : TI->Sets) {
writeEnum(Set.Kind);
writeUInt32(Set.Selectors.size());
for (const auto &Selector : Set.Selectors) {
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index f640c9eecaee..dcb2f323122c 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -107,7 +107,7 @@ static std::string ReadPCHRecord(StringRef type) {
.Case("IdentifierInfo *", "Record.readIdentifier()")
.Case("StringRef", "Record.readString()")
.Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())")
- .Case("OMPTraitInfo", "Record.readOMPTraitInfo()")
+ .Case("OMPTraitInfo *", "Record.readOMPTraitInfo()")
.Default("Record.readInt()");
}
@@ -131,7 +131,7 @@ static std::string WritePCHRecord(StringRef type, StringRef name) {
.Case("StringRef", "AddString(" + std::string(name) + ");\n")
.Case("ParamIdx",
"push_back(" + std::string(name) + ".serialize());\n")
- .Case("OMPTraitInfo",
+ .Case("OMPTraitInfo *",
"writeOMPTraitInfo(" + std::string(name) + ");\n")
.Default("push_back(" + std::string(name) + ");\n");
}
@@ -363,7 +363,7 @@ namespace {
OS << " if (SA->get" << getUpperName() << "().isValid())\n ";
OS << " OS << \" \" << SA->get" << getUpperName()
<< "().getSourceIndex();\n";
- } else if (type == "OMPTraitInfo") {
+ } else if (type == "OMPTraitInfo *") {
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
} else {
llvm_unreachable("Unknown SimpleArgument type!");
@@ -1331,7 +1331,7 @@ createArgument(const Record &Arg, StringRef Attr,
else if (ArgName == "VersionArgument")
Ptr = std::make_unique<VersionArgument>(Arg, Attr);
else if (ArgName == "OMPTraitInfoArgument")
- Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "OMPTraitInfo");
+ Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "OMPTraitInfo *");
if (!Ptr) {
// Search in reverse order so that the most-derived type is handled first.
More information about the cfe-commits
mailing list