Allow converting Diagnostic to LogicalResult::Failure. This matches the behavior...
authorRiver Riddle <riverriddle@google.com>
Sat, 22 Jun 2019 21:39:16 +0000 (14:39 -0700)
committerjpienaar <jpienaar@google.com>
Mon, 24 Jun 2019 20:44:22 +0000 (13:44 -0700)
PiperOrigin-RevId: 254579098

mlir/include/mlir/IR/Diagnostics.h
mlir/include/mlir/IR/OpDefinition.h
mlir/lib/AffineOps/AffineOps.cpp
mlir/lib/GPU/IR/GPUDialect.cpp
mlir/lib/IR/Diagnostics.cpp
mlir/lib/Linalg/IR/LinalgOps.cpp
mlir/lib/Parser/Parser.cpp

index 6038a23..090e96f 100644 (file)
@@ -289,6 +289,9 @@ public:
     return {notes.begin(), notes.end()};
   }
 
+  /// Allow a diagnostic to be converted to 'failure'.
+  operator LogicalResult() const;
+
 private:
   Diagnostic(const Diagnostic &rhs) = delete;
   Diagnostic &operator=(const Diagnostic &rhs) = delete;
index 52aa617..4407ec9 100644 (file)
@@ -48,6 +48,7 @@ public:
 
   // Allow diagnostics emitted during parsing to be converted to failure.
   ParseResult(const InFlightDiagnostic &) : LogicalResult(failure()) {}
+  ParseResult(const Diagnostic &) : LogicalResult(failure()) {}
 
   /// Failure is true in a boolean context.
   explicit operator bool() const { return failed(*this); }
index 592519a..f886c5a 100644 (file)
@@ -704,12 +704,11 @@ static LogicalResult checkHasAffineTerminator(OpState &op, Block &block) {
   if (block.empty() || isa<AffineTerminatorOp>(block.back()))
     return success();
 
-  op.emitOpError("expects regions to end with '" +
-                 AffineTerminatorOp::getOperationName() + "'")
-          .attachNote()
-      << "in custom textual format, the absence of terminator implies '"
-      << AffineTerminatorOp::getOperationName() << "'";
-  return failure();
+  return op.emitOpError("expects regions to end with '" +
+                        AffineTerminatorOp::getOperationName() + "'")
+             .attachNote()
+         << "in custom textual format, the absence of terminator implies '"
+         << AffineTerminatorOp::getOperationName() << "'";
 }
 
 // Insert `affine.terminator` at the end of the region's only block if it does
index ddfdd29..272ffce 100644 (file)
@@ -138,12 +138,11 @@ LogicalResult LaunchOp::verify() {
     if (block.back().getNumSuccessors() != 0)
       continue;
     if (!isa<gpu::Return>(&block.back())) {
-      block.back()
-              .emitError("expected 'gpu.terminator' or a terminator with "
-                         "successors")
-              .attachNote(getLoc())
-          << "in '" << getOperationName() << "' body region";
-      return failure();
+      return block.back()
+                 .emitError("expected 'gpu.terminator' or a terminator with "
+                            "successors")
+                 .attachNote(getLoc())
+             << "in '" << getOperationName() << "' body region";
     }
   }
 
index a224465..7078aad 100644 (file)
@@ -164,6 +164,9 @@ Diagnostic &Diagnostic::attachNote(llvm::Optional<Location> noteLoc) {
   return *notes.back();
 }
 
+/// Allow a diagnostic to be converted to 'failure'.
+Diagnostic::operator LogicalResult() const { return failure(); }
+
 //===----------------------------------------------------------------------===//
 // InFlightDiagnostic
 //===----------------------------------------------------------------------===//
index a54b57f..5937931 100644 (file)
@@ -115,12 +115,11 @@ static LogicalResult checkHasTerminator(OpState &op, Block &block) {
   if (block.empty() || isa<TerminatorOp>(block.back()))
     return success();
 
-  op.emitOpError("expects regions to end with '" +
-                 TerminatorOp::getOperationName() + "'")
-          .attachNote()
-      << "in custom textual format, the absence of terminator implies '"
-      << TerminatorOp::getOperationName() << "'";
-  return failure();
+  return op.emitOpError("expects regions to end with '" +
+                        TerminatorOp::getOperationName() + "'")
+             .attachNote()
+         << "in custom textual format, the absence of terminator implies '"
+         << TerminatorOp::getOperationName() << "'";
 }
 
 // Insert `linalg.terminator` at the end of the ForOp only region's only block
index fb77f43..f5108b5 100644 (file)
@@ -2479,11 +2479,10 @@ ParseResult OperationParser::addDefinition(SSAUseInfo useInfo, Value *value) {
   // or a forward reference.
   if (auto *existing = entries[useInfo.number].first) {
     if (!isForwardRefPlaceholder(existing)) {
-      emitError(useInfo.loc)
+      return emitError(useInfo.loc)
           .append("redefinition of SSA value '", useInfo.name, "'")
           .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
           .append("previously defined here");
-      return failure();
     }
 
     // If it was a forward reference, update everything that used it to use