[Mlir-commits] [mlir] 4975587 - [mlir][ODS]: Add per-op cppNamespace.

Sean Silva llvmlistbot at llvm.org
Tue May 11 10:51:25 PDT 2021


Author: Sean Silva
Date: 2021-05-11T10:48:05-07:00
New Revision: 49755871ad0c24ed970c0a4f2c51f90488b0ddd2

URL: https://github.com/llvm/llvm-project/commit/49755871ad0c24ed970c0a4f2c51f90488b0ddd2
DIFF: https://github.com/llvm/llvm-project/commit/49755871ad0c24ed970c0a4f2c51f90488b0ddd2.diff

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

This is useful for dialects that have logical subparts.

Differential Revision: https://reviews.llvm.org/D102200

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpBase.td
    mlir/include/mlir/TableGen/CodeGenHelpers.h
    mlir/include/mlir/TableGen/Operator.h
    mlir/lib/TableGen/Operator.cpp
    mlir/test/mlir-tblgen/dialect.td
    mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 9436602fef57b..885ead981c774 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -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 = "";
 

diff  --git a/mlir/include/mlir/TableGen/CodeGenHelpers.h b/mlir/include/mlir/TableGen/CodeGenHelpers.h
index 9d651ac08e56e..acd80698b3463 100644
--- a/mlir/include/mlir/TableGen/CodeGenHelpers.h
+++ b/mlir/include/mlir/TableGen/CodeGenHelpers.h
@@ -41,9 +41,10 @@ class NamespaceEmitter {
   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 @@ class NamespaceEmitter {
   }
 
 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;
 };

diff  --git a/mlir/include/mlir/TableGen/Operator.h b/mlir/include/mlir/TableGen/Operator.h
index 3da693dde0be9..7187daa53d90c 100644
--- a/mlir/include/mlir/TableGen/Operator.h
+++ b/mlir/include/mlir/TableGen/Operator.h
@@ -58,6 +58,9 @@ class Operator {
   // 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 @@ class Operator {
   // 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;
 

diff  --git a/mlir/lib/TableGen/Operator.cpp b/mlir/lib/TableGen/Operator.cpp
index c1ba549e8abfb..11f95c0957ef8 100644
--- a/mlir/lib/TableGen/Operator.cpp
+++ b/mlir/lib/TableGen/Operator.cpp
@@ -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();

diff  --git a/mlir/test/mlir-tblgen/dialect.td b/mlir/test/mlir-tblgen/dialect.td
index a9acfaaf7104f..f35ce345a90a8 100644
--- a/mlir/test/mlir-tblgen/dialect.td
+++ b/mlir/test/mlir-tblgen/dialect.td
@@ -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

diff  --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 413d45a6b8d65..78e84d7dba9f0 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -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);


        


More information about the Mlir-commits mailing list