[Mlir-commits] [mlir] 78fdbdb - Use reference for large object passed by value at the moment in MLIR TableGen (NFC)

Mehdi Amini llvmlistbot at llvm.org
Mon Jan 17 22:48:58 PST 2022


Author: Mehdi Amini
Date: 2022-01-18T06:48:33Z
New Revision: 78fdbdbf2682dbc9c2746ae22ba0a255ecb0867e

URL: https://github.com/llvm/llvm-project/commit/78fdbdbf2682dbc9c2746ae22ba0a255ecb0867e
DIFF: https://github.com/llvm/llvm-project/commit/78fdbdbf2682dbc9c2746ae22ba0a255ecb0867e.diff

LOG: Use reference for large object passed by value at the moment in MLIR TableGen (NFC)

Also make the ODS Operator class have const iterator, and use const
references for existing API taking Operator by reference.

Reviewed By: rriddle

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

Added: 
    

Modified: 
    mlir/include/mlir/TableGen/Operator.h
    mlir/lib/TableGen/Operator.cpp
    mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
    mlir/tools/mlir-tblgen/CodeGenHelpers.cpp
    mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
    mlir/tools/mlir-tblgen/OpDocGen.cpp
    mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/TableGen/Operator.h b/mlir/include/mlir/TableGen/Operator.h
index ddfb7dd0178bf..298e76b4239d4 100644
--- a/mlir/include/mlir/TableGen/Operator.h
+++ b/mlir/include/mlir/TableGen/Operator.h
@@ -95,7 +95,9 @@ class Operator {
   using var_decorator_range = llvm::iterator_range<VariableDecoratorIterator>;
 
   using value_iterator = NamedTypeConstraint *;
+  using const_value_iterator = const NamedTypeConstraint *;
   using value_range = llvm::iterator_range<value_iterator>;
+  using const_value_range = llvm::iterator_range<const_value_iterator>;
 
   // Returns true if this op has variable length operands or results.
   bool isVariadic() const;
@@ -104,9 +106,9 @@ class Operator {
   bool skipDefaultBuilders() const;
 
   // Op result iterators.
-  value_iterator result_begin();
-  value_iterator result_end();
-  value_range getResults();
+  const_value_iterator result_begin() const;
+  const_value_iterator result_end() const;
+  const_value_range getResults() const;
 
   // Returns the number of results this op produces.
   int getNumResults() const;
@@ -143,9 +145,9 @@ class Operator {
   }
 
   // Op operand iterators.
-  value_iterator operand_begin();
-  value_iterator operand_end();
-  value_range getOperands();
+  const_value_iterator operand_begin() const;
+  const_value_iterator operand_end() const;
+  const_value_range getOperands() const;
 
   int getNumOperands() const { return operands.size(); }
   NamedTypeConstraint &getOperand(int index) { return operands[index]; }

diff  --git a/mlir/lib/TableGen/Operator.cpp b/mlir/lib/TableGen/Operator.cpp
index cde617dcd30b3..6da1b7c1fe518 100644
--- a/mlir/lib/TableGen/Operator.cpp
+++ b/mlir/lib/TableGen/Operator.cpp
@@ -141,11 +141,15 @@ bool Operator::skipDefaultBuilders() const {
   return def.getValueAsBit("skipDefaultBuilders");
 }
 
-auto Operator::result_begin() -> value_iterator { return results.begin(); }
+auto Operator::result_begin() const -> const_value_iterator {
+  return results.begin();
+}
 
-auto Operator::result_end() -> value_iterator { return results.end(); }
+auto Operator::result_end() const -> const_value_iterator {
+  return results.end();
+}
 
-auto Operator::getResults() -> value_range {
+auto Operator::getResults() const -> const_value_range {
   return {result_begin(), result_end()};
 }
 
@@ -286,9 +290,13 @@ auto Operator::getAttributes() const
   return {attribute_begin(), attribute_end()};
 }
 
-auto Operator::operand_begin() -> value_iterator { return operands.begin(); }
-auto Operator::operand_end() -> value_iterator { return operands.end(); }
-auto Operator::getOperands() -> value_range {
+auto Operator::operand_begin() const -> const_value_iterator {
+  return operands.begin();
+}
+auto Operator::operand_end() const -> const_value_iterator {
+  return operands.end();
+}
+auto Operator::getOperands() const -> const_value_range {
   return {operand_begin(), operand_end()};
 }
 

diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index f3528880e5f9b..789d02b3fe217 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -52,15 +52,15 @@ static void collectAllDefs(StringRef selectedDialect,
   if (selectedDialect.empty()) {
     // If a dialect was not specified, ensure that all found defs belong to the
     // same dialect.
-    if (!llvm::is_splat(
-            llvm::map_range(defs, [](auto def) { return def.getDialect(); }))) {
+    if (!llvm::is_splat(llvm::map_range(
+            defs, [](const auto &def) { return def.getDialect(); }))) {
       llvm::PrintFatalError("defs belonging to more than one dialect. Must "
                             "select one via '--(attr|type)defs-dialect'");
     }
     resultDefs.assign(defs.begin(), defs.end());
   } else {
     // Otherwise, generate the defs that belong to the selected dialect.
-    auto dialectDefs = llvm::make_filter_range(defs, [&](auto def) {
+    auto dialectDefs = llvm::make_filter_range(defs, [&](const auto &def) {
       return def.getDialect().getName().equals(selectedDialect);
     });
     resultDefs.assign(dialectDefs.begin(), dialectDefs.end());

diff  --git a/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp b/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp
index 13357327928ed..973d50dab7ef3 100644
--- a/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp
+++ b/mlir/tools/mlir-tblgen/CodeGenHelpers.cpp
@@ -299,7 +299,7 @@ void StaticVerifierFunctionEmitter::collectConstraint(ConstraintMap &map,
 
 void StaticVerifierFunctionEmitter::collectOpConstraints(
     ArrayRef<Record *> opDefs) {
-  const auto collectTypeConstraints = [&](Operator::value_range values) {
+  const auto collectTypeConstraints = [&](Operator::const_value_range values) {
     for (const NamedTypeConstraint &value : values)
       if (value.hasPredicate())
         collectConstraint(typeConstraints, "type", value.constraint);

diff  --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index c6ffc76af9529..870a7e9dc6a55 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -396,7 +396,8 @@ class OpEmitter {
 
   // Generates verify statements for operands and results in the operation.
   // The generated code will be attached to `body`.
-  void genOperandResultVerifier(MethodBody &body, Operator::value_range values,
+  void genOperandResultVerifier(MethodBody &body,
+                                Operator::const_value_range values,
                                 StringRef valueKind);
 
   // Generates verify statements for regions in the operation.
@@ -2246,7 +2247,7 @@ void OpEmitter::genVerifier() {
 }
 
 void OpEmitter::genOperandResultVerifier(MethodBody &body,
-                                         Operator::value_range values,
+                                         Operator::const_value_range values,
                                          StringRef valueKind) {
   // Check that an optional value is at most 1 element.
   //

diff  --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp
index 525d96d06ac50..79a062fd9735a 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp
@@ -143,7 +143,7 @@ static void emitOpTraitsDoc(const Operator &op, raw_ostream &os) {
   }
 }
 
-static void emitOpDoc(Operator op, raw_ostream &os) {
+static void emitOpDoc(const Operator &op, raw_ostream &os) {
   os << llvm::formatv("### `{0}` ({1})\n", op.getOperationName(),
                       op.getQualCppClassName());
 

diff  --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
index cb2145b3d491a..0513c5e7b1ab2 100644
--- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
@@ -84,12 +84,12 @@ class InterfaceGenerator {
   InterfaceGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os)
       : defs(std::move(defs)), os(os) {}
 
-  void emitConceptDecl(Interface &interface);
-  void emitModelDecl(Interface &interface);
-  void emitModelMethodsDef(Interface &interface);
-  void emitTraitDecl(Interface &interface, StringRef interfaceName,
+  void emitConceptDecl(const Interface &interface);
+  void emitModelDecl(const Interface &interface);
+  void emitModelMethodsDef(const Interface &interface);
+  void emitTraitDecl(const Interface &interface, StringRef interfaceName,
                      StringRef interfaceTraitsName);
-  void emitInterfaceDecl(Interface interface);
+  void emitInterfaceDecl(const Interface &interface);
 
   /// The set of interface records to emit.
   std::vector<llvm::Record *> defs;
@@ -200,7 +200,7 @@ bool InterfaceGenerator::emitInterfaceDefs() {
 // GEN: Interface declarations
 //===----------------------------------------------------------------------===//
 
-void InterfaceGenerator::emitConceptDecl(Interface &interface) {
+void InterfaceGenerator::emitConceptDecl(const Interface &interface) {
   os << "  struct Concept {\n";
 
   // Insert each of the pure virtual concept methods.
@@ -220,7 +220,7 @@ void InterfaceGenerator::emitConceptDecl(Interface &interface) {
   os << "  };\n";
 }
 
-void InterfaceGenerator::emitModelDecl(Interface &interface) {
+void InterfaceGenerator::emitModelDecl(const Interface &interface) {
   // Emit the basic model and the fallback model.
   for (const char *modelClass : {"Model", "FallbackModel"}) {
     os << "  template<typename " << valueTemplate << ">\n";
@@ -280,7 +280,7 @@ void InterfaceGenerator::emitModelDecl(Interface &interface) {
   os << "  };\n";
 }
 
-void InterfaceGenerator::emitModelMethodsDef(Interface &interface) {
+void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
   for (auto &method : interface.getMethods()) {
     os << "template<typename " << valueTemplate << ">\n";
     emitCPPType(method.getReturnType(), os);
@@ -378,7 +378,7 @@ void InterfaceGenerator::emitModelMethodsDef(Interface &interface) {
   }
 }
 
-void InterfaceGenerator::emitTraitDecl(Interface &interface,
+void InterfaceGenerator::emitTraitDecl(const Interface &interface,
                                        StringRef interfaceName,
                                        StringRef interfaceTraitsName) {
   os << llvm::formatv("  template <typename {3}>\n"
@@ -425,7 +425,7 @@ void InterfaceGenerator::emitTraitDecl(Interface &interface,
   os << "  };\n";
 }
 
-void InterfaceGenerator::emitInterfaceDecl(Interface interface) {
+void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
   llvm::SmallVector<StringRef, 2> namespaces;
   llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
   for (StringRef ns : namespaces)


        


More information about the Mlir-commits mailing list