diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -128,16 +128,15 @@ } /// 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 void printFunctionalType(InputRangeT &&inputs, ResultRangeT &&results) { auto &os = getStream(); - os << "("; + os << '('; llvm::interleaveComma(inputs, *this); - os << ")"; + os << ')'; printArrowTypeList(results); } @@ -414,7 +413,8 @@ virtual ParseResult parseOptionalEllipsis() = 0; /// Parse an integer value from the stream. - template ParseResult parseInteger(IntT &result) { + template + 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 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -48,8 +48,41 @@ 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 <>. + *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()) + wrapped = true; + + if (wrapped) + os << '('; + + llvm::interleaveComma(op->getResults(), os, [&](const OpResult &r) { + // Print the types of null values as <>. + *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 --- a/mlir/lib/IR/Value.cpp +++ b/mlir/lib/IR/Value.cpp @@ -32,11 +32,6 @@ /// 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()) return arg.getType();