[mlir][ODS]: Add per-op cppNamespace.

This is useful for dialects that have logical subparts.

Differential Revision: https://reviews.llvm.org/D102200
This commit is contained in:
Sean Silva 2021-05-10 14:30:22 -07:00
parent 68de58cd64
commit 49755871ad
6 changed files with 44 additions and 9 deletions

View File

@ -2123,6 +2123,9 @@ class Op<Dialect dialect, string mnemonic, list<OpTrait> props = []> {
// The mnemonic of the op.
string opName = mnemonic;
// The C++ namespace to use for this op.
string cppNamespace = dialect.cppNamespace;
// One-line human-readable description of what the op does.
string summary = "";

View File

@ -41,9 +41,10 @@ public:
NamespaceEmitter(raw_ostream &os, const Dialect &dialect) : os(os) {
if (!dialect)
return;
llvm::SplitString(dialect.getCppNamespace(), namespaces, "::");
for (StringRef ns : namespaces)
os << "namespace " << ns << " {\n";
emitNamespaceStarts(os, dialect.getCppNamespace());
}
NamespaceEmitter(raw_ostream &os, StringRef cppNamespace) : os(os) {
emitNamespaceStarts(os, cppNamespace);
}
~NamespaceEmitter() {
@ -52,6 +53,11 @@ public:
}
private:
void emitNamespaceStarts(raw_ostream &os, StringRef cppNamespace) {
llvm::SplitString(cppNamespace, namespaces, "::");
for (StringRef ns : namespaces)
os << "namespace " << ns << " {\n";
}
raw_ostream &os;
SmallVector<StringRef, 2> namespaces;
};

View File

@ -58,6 +58,9 @@ public:
// Returns this op's C++ class name prefixed with namespaces.
std::string getQualCppClassName() const;
// Returns this op's C++ namespace.
StringRef getCppNamespace() const;
// Returns the name of op's adaptor C++ class.
std::string getAdaptorName() const;
@ -304,6 +307,9 @@ private:
// The unqualified C++ class name of the op.
StringRef cppClassName;
// The C++ namespace for this op.
StringRef cppNamespace;
// The operands of the op.
SmallVector<NamedTypeConstraint, 4> operands;

View File

@ -50,6 +50,8 @@ Operator::Operator(const llvm::Record &def)
cppClassName = prefix;
}
cppNamespace = def.getValueAsString("cppNamespace");
populateOpStructure();
}
@ -70,12 +72,13 @@ StringRef Operator::getDialectName() const { return dialect.getName(); }
StringRef Operator::getCppClassName() const { return cppClassName; }
std::string Operator::getQualCppClassName() const {
auto prefix = dialect.getCppNamespace();
if (prefix.empty())
if (cppNamespace.empty())
return std::string(cppClassName);
return std::string(llvm::formatv("{0}::{1}", prefix, cppClassName));
return std::string(llvm::formatv("{0}::{1}", cppNamespace, cppClassName));
}
StringRef Operator::getCppNamespace() const { return cppNamespace; }
int Operator::getNumResults() const {
DagInit *results = def.getValueAsDag("results");
return results->getNumArgs();

View File

@ -34,20 +34,37 @@ def D_Dialect : Dialect {
def D_DSomeOp : Op<D_Dialect, "some_op", []>;
// Check op with namespace override.
def E_Dialect : Dialect {
let name = "e";
let cppNamespace = "ENS";
}
def E_SomeOp : Op<E_Dialect, "some_op", []>;
def E_SpecialNSOp : Op<E_Dialect, "special_ns_op", []> {
let cppNamespace = "::E::SPECIAL_NS";
}
// DEF-LABEL: GET_OP_LIST
// DEF: a::SomeOp
// DEF-NEXT: BNS::SomeOp
// DEF-NEXT: ::C::CC::SomeOp
// DEF-NEXT: DSomeOp
// DEF-NEXT: ENS::SomeOp
// DEF-NEXT: ::E::SPECIAL_NS::SpecialNSOp
// DEF-LABEL: GET_OP_CLASSES
// DEF: a::SomeOp definitions
// DEF: BNS::SomeOp definitions
// DEF: ::C::CC::SomeOp definitions
// DEF: DSomeOp definitions
// DEF: ENS::SomeOp definitions
// DEF: ::E::SPECIAL_NS::SpecialNSOp definitions
// DECL-LABEL: GET_OP_CLASSES
// DECL: a::SomeOp declarations
// DECL: BNS::SomeOp declarations
// DECL: ::C::CC::SomeOp declarations
// DECL: DSomeOp declarations
// DECL: ENS::SomeOp declarations
// DECL: ::E::SPECIAL_NS::SpecialNSOp declarations

View File

@ -174,7 +174,7 @@ StaticVerifierFunctionEmitter::StaticVerifierFunctionEmitter(
llvm::Optional<NamespaceEmitter> namespaceEmitter;
if (!emitDecl) {
os << formatv(opCommentHeader, "Local Utility Method", "Definitions");
namespaceEmitter.emplace(os, Operator(*opDefs[0]).getDialect());
namespaceEmitter.emplace(os, Operator(*opDefs[0]).getCppNamespace());
}
emitTypeConstraintMethods(opDefs, os, emitDecl);
@ -2423,7 +2423,7 @@ static void emitOpClasses(const RecordKeeper &recordKeeper,
os << "#undef GET_OP_FWD_DEFINES\n";
for (auto *def : defs) {
Operator op(*def);
NamespaceEmitter emitter(os, op.getDialect());
NamespaceEmitter emitter(os, op.getCppNamespace());
os << "class " << op.getCppClassName() << ";\n";
}
os << "#endif\n\n";
@ -2438,7 +2438,7 @@ static void emitOpClasses(const RecordKeeper &recordKeeper,
emitDecl);
for (auto *def : defs) {
Operator op(*def);
NamespaceEmitter emitter(os, op.getDialect());
NamespaceEmitter emitter(os, op.getCppNamespace());
if (emitDecl) {
os << formatv(opCommentHeader, op.getQualCppClassName(), "declarations");
OpOperandAdaptorEmitter::emitDecl(op, os);