[Mlir-commits] [mlir] 25f23a6 - [AsmPrinter] Make OpAsmPrinter::printFunctionalType be resilient to null values.

Chris Lattner llvmlistbot at llvm.org
Wed Jan 6 20:59:42 PST 2021


Author: Chris Lattner
Date: 2021-01-06T20:59:24-08:00
New Revision: 25f23a60398ee0a1ece2d49b281003ec642d5fed

URL: https://github.com/llvm/llvm-project/commit/25f23a60398ee0a1ece2d49b281003ec642d5fed
DIFF: https://github.com/llvm/llvm-project/commit/25f23a60398ee0a1ece2d49b281003ec642d5fed.diff

LOG: [AsmPrinter] Make OpAsmPrinter::printFunctionalType be resilient to null values.

A previous patch made Value::getType() be resilient to null values which was
considered to be too sweeping.  This is a more targeted change which requires
deabstracting some templates.

A middle ground would be to make ValueTypeIterator be tolerant to null values.

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpImplementation.h
    mlir/lib/IR/AsmPrinter.cpp
    mlir/lib/IR/Value.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index f74eb52aec6d..99561c3a089b 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -128,16 +128,15 @@ class OpAsmPrinter {
   }
 
   /// Print the complete type of an operation in functional form.
-  void printFunctionalType(Operation *op) {
-    printFunctionalType(op->getOperandTypes(), op->getResultTypes());
-  }
+  void printFunctionalType(Operation *op);
+
   /// Print the two given type ranges in a functional form.
   template <typename InputRangeT, typename ResultRangeT>
   void printFunctionalType(InputRangeT &&inputs, ResultRangeT &&results) {
     auto &os = getStream();
-    os << "(";
+    os << '(';
     llvm::interleaveComma(inputs, *this);
-    os << ")";
+    os << ')';
     printArrowTypeList(results);
   }
 
@@ -414,7 +413,8 @@ class OpAsmParser {
   virtual ParseResult parseOptionalEllipsis() = 0;
 
   /// Parse an integer value from the stream.
-  template <typename IntT> ParseResult parseInteger(IntT &result) {
+  template <typename IntT>
+  ParseResult parseInteger(IntT &result) {
     auto loc = getCurrentLocation();
     OptionalParseResult parseResult = parseOptionalInteger(result);
     if (!parseResult.hasValue())

diff  --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 1c2caa0bdfd6..302c226ca800 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -48,8 +48,41 @@ void OperationName::dump() const { print(llvm::errs()); }
 
 DialectAsmPrinter::~DialectAsmPrinter() {}
 
+//===--------------------------------------------------------------------===//
+// OpAsmPrinter
+//===--------------------------------------------------------------------===//
+
 OpAsmPrinter::~OpAsmPrinter() {}
 
+void OpAsmPrinter::printFunctionalType(Operation *op) {
+  auto &os = getStream();
+  os << '(';
+  llvm::interleaveComma(op->getOperands(), os, [&](Value op) {
+    // Print the types of null values as <<NULL TYPE>>.
+    *this << (op ? op.getType() : Type());
+  });
+  os << ") -> ";
+
+  // Print the result list.  We don't parenthesize single result types unless
+  // it is a function (avoiding a grammar ambiguity).
+  auto numResults = op->getNumResults();
+  bool wrapped = numResults != 1;
+  if (!wrapped && op->getResult(0).getType() &&
+      op->getResult(0).getType().isa<FunctionType>())
+    wrapped = true;
+
+  if (wrapped)
+    os << '(';
+
+  llvm::interleaveComma(op->getResults(), os, [&](const OpResult &r) {
+    // Print the types of null values as <<NULL TYPE>>.
+    *this << (r ? r.getType() : Type());
+  });
+
+  if (wrapped)
+    os << ')';
+}
+
 //===--------------------------------------------------------------------===//
 // Operation OpAsm interface.
 //===--------------------------------------------------------------------===//

diff  --git a/mlir/lib/IR/Value.cpp b/mlir/lib/IR/Value.cpp
index b29641a084a0..fd7e5b5d64e5 100644
--- a/mlir/lib/IR/Value.cpp
+++ b/mlir/lib/IR/Value.cpp
@@ -32,11 +32,6 @@ Value::Value(Operation *op, unsigned resultNo) {
 
 /// Return the type of this value.
 Type Value::getType() const {
-  // Support a null Value so the asmprinter doesn't crash on invalid IR (e.g.
-  // operations that have dropAllReferences() called on them).
-  if (!*this)
-    return Type();
-
   if (BlockArgument arg = dyn_cast<BlockArgument>())
     return arg.getType();
 


        


More information about the Mlir-commits mailing list