[mlir] Add support for specifying printing flags when adding an op to a Diagnostic

This removes edge cases where the default flags we want to use
during printing (e.g. local scope, eliding attributes, etc.)
get missed/dropped.

Differential Revision: https://reviews.llvm.org/D111761
This commit is contained in:
River Riddle 2021-10-18 15:07:23 +00:00
parent 1156bd4fc3
commit a77cd55dea
3 changed files with 11 additions and 12 deletions

View File

@ -29,6 +29,7 @@ struct LogicalResult;
class MLIRContext;
class Operation;
class OperationName;
class OpPrintingFlags;
class Type;
class Value;
@ -218,6 +219,8 @@ public:
Diagnostic &operator<<(Operation *val) {
return *this << *val;
}
/// Append an operation with the given printing flags.
Diagnostic &appendOp(Operation &val, const OpPrintingFlags &flags);
/// Stream in a Value.
Diagnostic &operator<<(Value val);

View File

@ -127,9 +127,13 @@ Diagnostic &Diagnostic::operator<<(OperationName val) {
/// Stream in an Operation.
Diagnostic &Diagnostic::operator<<(Operation &val) {
return appendOp(val, OpPrintingFlags());
}
Diagnostic &Diagnostic::appendOp(Operation &val, const OpPrintingFlags &flags) {
std::string str;
llvm::raw_string_ostream os(str);
val.print(os, OpPrintingFlags().useLocalScope().elideLargeElementsAttrs());
val.print(os,
OpPrintingFlags(flags).useLocalScope().elideLargeElementsAttrs());
return *this << os.str();
}

View File

@ -276,17 +276,9 @@ void Operation::insertOperands(unsigned index, ValueRange operands) {
InFlightDiagnostic Operation::emitError(const Twine &message) {
InFlightDiagnostic diag = mlir::emitError(getLoc(), message);
if (getContext()->shouldPrintOpOnDiagnostic()) {
// Print out the operation explicitly here so that we can print the generic
// form.
// TODO: It would be nice if we could instead provide the
// specific printing flags when adding the operation as an argument to the
// diagnostic.
std::string printedOp;
{
llvm::raw_string_ostream os(printedOp);
print(os, OpPrintingFlags().printGenericOpForm().useLocalScope());
}
diag.attachNote(getLoc()) << "see current operation: " << printedOp;
diag.attachNote(getLoc())
.append("see current operation: ")
.appendOp(*this, OpPrintingFlags().printGenericOpForm());
}
return diag;
}