From 650dc4bb657a50df037250404df93426d7b120c4 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Sat, 22 Jun 2019 14:39:16 -0700 Subject: [PATCH] Allow converting Diagnostic to LogicalResult::Failure. This matches the behavior of InFlightDiagnostic and enables notes to be converted to failure. PiperOrigin-RevId: 254579098 --- mlir/include/mlir/IR/Diagnostics.h | 3 +++ mlir/include/mlir/IR/OpDefinition.h | 1 + mlir/lib/AffineOps/AffineOps.cpp | 11 +++++------ mlir/lib/GPU/IR/GPUDialect.cpp | 11 +++++------ mlir/lib/IR/Diagnostics.cpp | 3 +++ mlir/lib/Linalg/IR/LinalgOps.cpp | 11 +++++------ mlir/lib/Parser/Parser.cpp | 3 +-- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h index 6038a23..090e96f 100644 --- a/mlir/include/mlir/IR/Diagnostics.h +++ b/mlir/include/mlir/IR/Diagnostics.h @@ -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; diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h index 52aa617..4407ec9 100644 --- a/mlir/include/mlir/IR/OpDefinition.h +++ b/mlir/include/mlir/IR/OpDefinition.h @@ -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); } diff --git a/mlir/lib/AffineOps/AffineOps.cpp b/mlir/lib/AffineOps/AffineOps.cpp index 592519a..f886c5a 100644 --- a/mlir/lib/AffineOps/AffineOps.cpp +++ b/mlir/lib/AffineOps/AffineOps.cpp @@ -704,12 +704,11 @@ static LogicalResult checkHasAffineTerminator(OpState &op, Block &block) { if (block.empty() || isa(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 diff --git a/mlir/lib/GPU/IR/GPUDialect.cpp b/mlir/lib/GPU/IR/GPUDialect.cpp index ddfdd29..272ffce 100644 --- a/mlir/lib/GPU/IR/GPUDialect.cpp +++ b/mlir/lib/GPU/IR/GPUDialect.cpp @@ -138,12 +138,11 @@ LogicalResult LaunchOp::verify() { if (block.back().getNumSuccessors() != 0) continue; if (!isa(&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"; } } diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp index a224465..7078aad 100644 --- a/mlir/lib/IR/Diagnostics.cpp +++ b/mlir/lib/IR/Diagnostics.cpp @@ -164,6 +164,9 @@ Diagnostic &Diagnostic::attachNote(llvm::Optional noteLoc) { return *notes.back(); } +/// Allow a diagnostic to be converted to 'failure'. +Diagnostic::operator LogicalResult() const { return failure(); } + //===----------------------------------------------------------------------===// // InFlightDiagnostic //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Linalg/IR/LinalgOps.cpp b/mlir/lib/Linalg/IR/LinalgOps.cpp index a54b57f..5937931 100644 --- a/mlir/lib/Linalg/IR/LinalgOps.cpp +++ b/mlir/lib/Linalg/IR/LinalgOps.cpp @@ -115,12 +115,11 @@ static LogicalResult checkHasTerminator(OpState &op, Block &block) { if (block.empty() || isa(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 diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index fb77f43..f5108b5 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -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 -- 2.7.4