[Mlir-commits] [mlir] 7641952 - Common code preparation for tblgen-types patch

Mehdi Amini llvmlistbot at llvm.org
Fri Sep 25 19:47:59 PDT 2020


Author: John Demme
Date: 2020-09-26T02:47:48Z
New Revision: 76419525fba62c93d5c337acdb0b80d6e42b00c9

URL: https://github.com/llvm/llvm-project/commit/76419525fba62c93d5c337acdb0b80d6e42b00c9
DIFF: https://github.com/llvm/llvm-project/commit/76419525fba62c93d5c337acdb0b80d6e42b00c9.diff

LOG: Common code preparation for tblgen-types patch

Cleanup and add methods which https://reviews.llvm.org/D86904 requires. Breaking up to lower review load.

Reviewed By: mehdi_amini

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

Added: 
    mlir/include/mlir/TableGen/CodeGenHelpers.h

Modified: 
    llvm/include/llvm/TableGen/Record.h
    llvm/lib/TableGen/Record.cpp
    mlir/include/mlir/TableGen/Operator.h
    mlir/lib/TableGen/Operator.cpp
    mlir/tools/mlir-tblgen/DialectGen.cpp
    mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index cbd31037260e..a00272b46ac5 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1640,6 +1640,16 @@ class Record {
   /// or if the value is not a string.
   StringRef getValueAsString(StringRef FieldName) const;
 
+  /// This method looks up the specified field and returns
+  /// its value as a string, throwing an exception if the field if the value is
+  /// not a string and llvm::Optional() if the field does not exist.
+  llvm::Optional<StringRef> getValueAsOptionalString(StringRef FieldName) const;
+
+  /// This method looks up the specified field and returns
+  /// its value as a string, throwing an exception if the field if the value is
+  /// not a code block and llvm::Optional() if the field does not exist.
+  llvm::Optional<StringRef> getValueAsOptionalCode(StringRef FieldName) const;
+
   /// This method looks up the specified field and returns
   /// its value as a BitsInit, throwing an exception if the field does not exist
   /// or if the value is not the right type.

diff  --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 29c10fa52bcb..ae8fe0316c42 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2241,18 +2241,43 @@ Init *Record::getValueInit(StringRef FieldName) const {
 }
 
 StringRef Record::getValueAsString(StringRef FieldName) const {
-  const RecordVal *R = getValue(FieldName);
-  if (!R || !R->getValue())
+  llvm::Optional<StringRef> S = getValueAsOptionalString(FieldName);
+  if (!S.hasValue())
     PrintFatalError(getLoc(), "Record `" + getName() +
       "' does not have a field named `" + FieldName + "'!\n");
+  return S.getValue();
+}
+llvm::Optional<StringRef>
+Record::getValueAsOptionalString(StringRef FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (!R || !R->getValue())
+    return llvm::Optional<StringRef>();
+  if (isa<UnsetInit>(R->getValue()))
+    return llvm::Optional<StringRef>();
 
   if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
     return SI->getValue();
   if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
     return CI->getValue();
 
-  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + 
-                                "' exists but does not have a string value");
+  PrintFatalError(getLoc(),
+                  "Record `" + getName() + "', ` field `" + FieldName +
+                      "' exists but does not have a string initializer!");
+}
+llvm::Optional<StringRef>
+Record::getValueAsOptionalCode(StringRef FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (!R || !R->getValue())
+    return llvm::Optional<StringRef>();
+  if (isa<UnsetInit>(R->getValue()))
+    return llvm::Optional<StringRef>();
+
+  if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
+    return CI->getValue();
+
+  PrintFatalError(getLoc(),
+                  "Record `" + getName() + "', field `" + FieldName +
+                      "' exists but does not have a code initializer!");
 }
 
 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {

diff  --git a/mlir/include/mlir/TableGen/CodeGenHelpers.h b/mlir/include/mlir/TableGen/CodeGenHelpers.h
new file mode 100644
index 000000000000..7ba83aae55bb
--- /dev/null
+++ b/mlir/include/mlir/TableGen/CodeGenHelpers.h
@@ -0,0 +1,61 @@
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines common utilities for generating C++ from tablegen
+// structures.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_CODEGENHELPERS_H
+#define MLIR_TABLEGEN_CODEGENHELPERS_H
+
+#include "mlir/TableGen/Dialect.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace mlir {
+namespace tblgen {
+
+// Simple RAII helper for defining ifdef-undef-endif scopes.
+class IfDefScope {
+public:
+  IfDefScope(llvm::StringRef name, llvm::raw_ostream &os) : name(name), os(os) {
+    os << "#ifdef " << name << "\n"
+       << "#undef " << name << "\n\n";
+  }
+  ~IfDefScope() { os << "\n#endif  // " << name << "\n\n"; }
+
+private:
+  llvm::StringRef name;
+  llvm::raw_ostream &os;
+};
+
+// A helper RAII class to emit nested namespaces for this op.
+class NamespaceEmitter {
+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";
+  }
+
+  ~NamespaceEmitter() {
+    for (StringRef ns : llvm::reverse(namespaces))
+      os << "} // namespace " << ns << "\n";
+  }
+
+private:
+  raw_ostream &os;
+  SmallVector<StringRef, 2> namespaces;
+};
+
+} // namespace tblgen
+} // namespace mlir
+
+#endif // MLIR_TABLEGEN_CODEGENHELPERS_H

diff  --git a/mlir/include/mlir/TableGen/Operator.h b/mlir/include/mlir/TableGen/Operator.h
index 34c550650364..d7fac87af0be 100644
--- a/mlir/include/mlir/TableGen/Operator.h
+++ b/mlir/include/mlir/TableGen/Operator.h
@@ -242,17 +242,6 @@ class Operator {
   // debugging purposes.
   void print(llvm::raw_ostream &os) const;
 
-  // A helper RAII class to emit nested namespaces for this op.
-  class NamespaceEmitter {
-  public:
-    NamespaceEmitter(raw_ostream &os, Operator &op);
-    ~NamespaceEmitter();
-
-  private:
-    raw_ostream &os;
-    SmallVector<StringRef, 2> namespaces;
-  };
-
   // Return whether all the result types are known.
   bool allResultTypesKnown() const { return allResultsHaveKnownTypes; };
 

diff  --git a/mlir/lib/TableGen/Operator.cpp b/mlir/lib/TableGen/Operator.cpp
index 24dffa36e13e..605804baf468 100644
--- a/mlir/lib/TableGen/Operator.cpp
+++ b/mlir/lib/TableGen/Operator.cpp
@@ -566,21 +566,6 @@ void Operator::print(llvm::raw_ostream &os) const {
   }
 }
 
-Operator::NamespaceEmitter::NamespaceEmitter(raw_ostream &os, Operator &op)
-    : os(os) {
-  auto dialect = op.getDialect();
-  if (!dialect)
-    return;
-  llvm::SplitString(dialect.getCppNamespace(), namespaces, "::");
-  for (StringRef ns : namespaces)
-    os << "namespace " << ns << " {\n";
-}
-
-Operator::NamespaceEmitter::~NamespaceEmitter() {
-  for (StringRef ns : llvm::reverse(namespaces))
-    os << "} // namespace " << ns << "\n";
-}
-
 auto Operator::VariableDecoratorIterator::unwrap(llvm::Init *init)
     -> VariableDecorator {
   return VariableDecorator(cast<llvm::DefInit>(init)->getDef());

diff  --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index 4a9ec48b777e..43f3968ca9c8 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
@@ -155,12 +156,7 @@ static void emitDialectDecl(Dialect &dialect,
   }
 
   // Emit all nested namespaces.
-  StringRef cppNamespace = dialect.getCppNamespace();
-  llvm::SmallVector<StringRef, 2> namespaces;
-  llvm::SplitString(cppNamespace, namespaces, "::");
-
-  for (auto ns : namespaces)
-    os << "namespace " << ns << " {\n";
+  NamespaceEmitter nsEmitter(os, dialect);
 
   // Emit the start of the decl.
   std::string cppName = dialect.getCppClassName();
@@ -188,10 +184,6 @@ static void emitDialectDecl(Dialect &dialect,
 
   // End the dialect decl.
   os << "};\n";
-
-  // Close all nested namespaces in reverse order.
-  for (auto ns : llvm::reverse(namespaces))
-    os << "} // namespace " << ns << "\n";
 }
 
 static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,

diff  --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 2b05e25be267..5345bc598da9 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "OpFormatGen.h"
+#include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
@@ -159,23 +160,6 @@ static bool canUseUnwrappedRawValue(const tblgen::Attribute &attr) {
 // Op emitter
 //===----------------------------------------------------------------------===//
 
-namespace {
-// Simple RAII helper for defining ifdef-undef-endif scopes.
-class IfDefScope {
-public:
-  IfDefScope(StringRef name, raw_ostream &os) : name(name), os(os) {
-    os << "#ifdef " << name << "\n"
-       << "#undef " << name << "\n\n";
-  }
-
-  ~IfDefScope() { os << "\n#endif  // " << name << "\n\n"; }
-
-private:
-  StringRef name;
-  raw_ostream &os;
-};
-} // end anonymous namespace
-
 namespace {
 // Helper class to emit a record into the given output stream.
 class OpEmitter {
@@ -2181,7 +2165,7 @@ static void emitOpClasses(const std::vector<Record *> &defs, raw_ostream &os,
     os << "#undef GET_OP_FWD_DEFINES\n";
     for (auto *def : defs) {
       Operator op(*def);
-      Operator::NamespaceEmitter emitter(os, op);
+      NamespaceEmitter emitter(os, op.getDialect());
       os << "class " << op.getCppClassName() << ";\n";
     }
     os << "#endif\n\n";
@@ -2190,7 +2174,7 @@ static void emitOpClasses(const std::vector<Record *> &defs, raw_ostream &os,
   IfDefScope scope("GET_OP_CLASSES", os);
   for (auto *def : defs) {
     Operator op(*def);
-    Operator::NamespaceEmitter emitter(os, op);
+    NamespaceEmitter emitter(os, op.getDialect());
     if (emitDecl) {
       os << formatv(opCommentHeader, op.getQualCppClassName(), "declarations");
       OpOperandAdaptorEmitter::emitDecl(op, os);


        


More information about the Mlir-commits mailing list