Refactor generic op printing: extract a public printFunctionalType() on OpAsmPrinter (NFC)

PiperOrigin-RevId: 253674584
This commit is contained in:
Mehdi Amini 2019-06-17 15:31:53 -07:00 committed by Mehdi Amini
parent 980bf61b4b
commit 74df13fdda
4 changed files with 22 additions and 15 deletions

View File

@ -98,6 +98,23 @@ public:
os << ')'; os << ')';
} }
/// Print the complete type of an operation in functional form.
void printFunctionalType(Operation *op) {
auto &os = getStream();
os << "(";
interleaveComma(op->getNonSuccessorOperands(), os,
[&](Value *operand) { printType(operand->getType()); });
os << ") -> ";
if (op->getNumResults() == 1 &&
!op->getResult(0)->getType().isa<FunctionType>()) {
printType(op->getResult(0)->getType());
} else {
os << '(';
interleaveComma(op->getResultTypes(), os);
os << ')';
}
}
private: private:
OpAsmPrinter(const OpAsmPrinter &) = delete; OpAsmPrinter(const OpAsmPrinter &) = delete;
void operator=(const OpAsmPrinter &) = delete; void operator=(const OpAsmPrinter &) = delete;

View File

@ -320,6 +320,7 @@ public:
return getOperand(getSuccessorOperandIndex(succIndex) + opIndex); return getOperand(getSuccessorOperandIndex(succIndex) + opIndex);
} }
bool hasSuccessors() { return numSuccs != 0; }
unsigned getNumSuccessors() { return numSuccs; } unsigned getNumSuccessors() { return numSuccs; }
unsigned getNumSuccessorOperands(unsigned index) { unsigned getNumSuccessorOperands(unsigned index) {
assert(!isKnownNonTerminator() && "only terminators may have successors"); assert(!isKnownNonTerminator() && "only terminators may have successors");

View File

@ -1575,20 +1575,8 @@ void FunctionPrinter::printGenericOp(Operation *op) {
printOptionalAttrDict(attrs); printOptionalAttrDict(attrs);
// Print the type signature of the operation. // Print the type signature of the operation.
os << " : ("; os << " : ";
interleaveComma(properOperands, printFunctionalType(op);
[&](Value *value) { printType(value->getType()); });
os << ") -> ";
if (op->getNumResults() == 1 &&
!op->getResult(0)->getType().isa<FunctionType>()) {
printType(op->getResult(0)->getType());
} else {
os << '(';
interleaveComma(op->getResults(),
[&](Value *result) { printType(result->getType()); });
os << ')';
}
} }
void FunctionPrinter::printSuccessorAndUseList(Operation *term, void FunctionPrinter::printSuccessorAndUseList(Operation *term,

View File

@ -495,7 +495,8 @@ void Operation::setSuccessor(Block *block, unsigned index) {
auto Operation::getNonSuccessorOperands() -> operand_range { auto Operation::getNonSuccessorOperands() -> operand_range {
return {operand_iterator(this, 0), return {operand_iterator(this, 0),
operand_iterator(this, getSuccessorOperandIndex(0))}; operand_iterator(this, hasSuccessors() ? getSuccessorOperandIndex(0)
: getNumOperands())};
} }
/// Get the index of the first operand of the successor at the provided /// Get the index of the first operand of the successor at the provided