[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
This commit is contained in:
Chris Lattner 2020-12-29 11:05:45 -08:00
parent 25bf4a8f42
commit 25f23a6039
3 changed files with 39 additions and 11 deletions

View File

@ -128,16 +128,15 @@ public:
}
/// 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 @@ public:
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())

View File

@ -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.
//===--------------------------------------------------------------------===//

View File

@ -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();