summaryrefslogtreecommitdiffstats
path: root/mlir/lib/IR
diff options
context:
space:
mode:
authorRiver Riddle <riverriddle@google.com>2019-11-12 11:57:47 -0800
committerA. Unique TensorFlower <gardener@tensorflow.org>2019-11-12 11:59:19 -0800
commit626e1fd95e626c47154c5fe5d546f3d36b39e319 (patch)
tree5b2172eb1f88ed79c97bae056456b95b45332eca /mlir/lib/IR
parentaa9dc9446e21b0de1389d460a41867ade7bc4683 (diff)
downloadbcm5719-llvm-626e1fd95e626c47154c5fe5d546f3d36b39e319.tar.gz
bcm5719-llvm-626e1fd95e626c47154c5fe5d546f3d36b39e319.zip
Add an option to print an operation if a diagnostic is emitted on it
It is often helpful to inspect the operation that the error/warning/remark/etc. originated from, especially in the context of debugging or in the case of a verifier failure. This change adds an option 'mlir-print-op-on-diagnostic' that attaches the operation as a note to any diagnostic that is emitted on it via Operation::emit(Error|Warning|Remark). In the case of an error, the operation is printed in the generic form. PiperOrigin-RevId: 280021438
Diffstat (limited to 'mlir/lib/IR')
-rw-r--r--mlir/lib/IR/Operation.cpp46
1 files changed, 40 insertions, 6 deletions
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 96c488cbefe..aa13a71dcfc 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -26,10 +26,22 @@
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/StandardTypes.h"
#include "mlir/IR/TypeUtilities.h"
+#include "llvm/Support/CommandLine.h"
#include <numeric>
using namespace mlir;
+static llvm::cl::opt<bool> printOpOnDiagnostic(
+ "mlir-print-op-on-diagnostic",
+ llvm::cl::desc("When a diagnostic is emitted on an operation, also print "
+ "the operation as an attached note"));
+
+OpAsmParser::~OpAsmParser() {}
+
+//===----------------------------------------------------------------------===//
+// OperationName
+//===----------------------------------------------------------------------===//
+
/// Form the OperationName for an op with the specified string. This either is
/// a reference to an AbstractOperation if one is known, or a uniqued Identifier
/// if not.
@@ -60,8 +72,6 @@ OperationName OperationName::getFromOpaquePointer(void *pointer) {
return OperationName(RepresentationUnion::getFromOpaqueValue(pointer));
}
-OpAsmParser::~OpAsmParser() {}
-
//===----------------------------------------------------------------------===//
// OpResult
//===----------------------------------------------------------------------===//
@@ -301,27 +311,51 @@ void Operation::replaceUsesOfWith(Value *from, Value *to) {
}
//===----------------------------------------------------------------------===//
-// Other
+// Diagnostics
//===----------------------------------------------------------------------===//
/// Emit an error about fatal conditions with this operation, reporting up to
/// any diagnostic handlers that may be listening.
InFlightDiagnostic Operation::emitError(const Twine &message) {
- return mlir::emitError(getLoc(), message);
+ InFlightDiagnostic diag = mlir::emitError(getLoc(), message);
+ if (printOpOnDiagnostic) {
+ // Print out the operation explicitly here so that we can print the generic
+ // form.
+ // TODO(riverriddle) 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;
+ }
+ return diag;
}
/// Emit a warning about this operation, reporting up to any diagnostic
/// handlers that may be listening.
InFlightDiagnostic Operation::emitWarning(const Twine &message) {
- return mlir::emitWarning(getLoc(), message);
+ InFlightDiagnostic diag = mlir::emitWarning(getLoc(), message);
+ if (printOpOnDiagnostic)
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
+ return diag;
}
/// Emit a remark about this operation, reporting up to any diagnostic
/// handlers that may be listening.
InFlightDiagnostic Operation::emitRemark(const Twine &message) {
- return mlir::emitRemark(getLoc(), message);
+ InFlightDiagnostic diag = mlir::emitRemark(getLoc(), message);
+ if (printOpOnDiagnostic)
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
+ return diag;
}
+//===----------------------------------------------------------------------===//
+// Other
+//===----------------------------------------------------------------------===//
+
/// Given an operation 'other' that is within the same parent block, return
/// whether the current operation is before 'other' in the operation list
/// of the parent block.
OpenPOWER on IntegriCloud