[Mlir-commits] [mlir] 81b2dc5 - [Support] Move ParseResult from OpDefinition.h to LogicalResult.h

Chris Lattner llvmlistbot at llvm.org
Sun Apr 17 15:23:58 PDT 2022


Author: Chris Lattner
Date: 2022-04-17T15:18:33-07:00
New Revision: 81b2dc548b54832eb70dc483b69bbb8a77ecb095

URL: https://github.com/llvm/llvm-project/commit/81b2dc548b54832eb70dc483b69bbb8a77ecb095
DIFF: https://github.com/llvm/llvm-project/commit/81b2dc548b54832eb70dc483b69bbb8a77ecb095.diff

LOG: [Support] Move ParseResult from OpDefinition.h to LogicalResult.h

This class is a helper for 'parser-like' use cases of LogicalResult
where the implicit conversion to bool is tolerable.  It is used by the
operation asmparsers, but is more generic functionality that is closely
aligned with LogicalResult.  Hoist it up to LogicalResult.h to make it
more accessible.  This is part of Issue #54884

Differential Revision: https://reviews.llvm.org/D123760

Added: 
    

Modified: 
    mlir/include/mlir/IR/Diagnostics.h
    mlir/include/mlir/IR/OpDefinition.h
    mlir/include/mlir/Support/LogicalResult.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index 600a6f97ca3b5..dd75aeea48424 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -265,6 +265,9 @@ class Diagnostic {
   /// Allow a diagnostic to be converted to 'failure'.
   operator LogicalResult() const;
 
+  /// Allow a diagnostic to be converted to 'failure'.
+  operator ParseResult() const { return ParseResult(LogicalResult(*this)); }
+
   /// Allow a diagnostic to be converted to FailureOr<T>. Always results in
   /// 'failure' because this cast cannot possibly return an object of 'T'.
   template <typename T>
@@ -354,6 +357,10 @@ class InFlightDiagnostic {
   /// 'success' if this is an empty diagnostic.
   operator LogicalResult() const;
 
+  /// Allow an inflight diagnostic to be converted to 'failure', otherwise
+  /// 'success' if this is an empty diagnostic.
+  operator ParseResult() const { return ParseResult(LogicalResult(*this)); }
+
   /// Allow an inflight diagnostic to be converted to FailureOr<T>. Always
   /// results in 'failure' because this cast cannot possibly return an object of
   /// 'T'.

diff  --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 5a1bc0133d1a7..be65f5e7afb16 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -29,21 +29,6 @@ namespace mlir {
 class Builder;
 class OpBuilder;
 
-/// This class represents success/failure for operation parsing. It is
-/// essentially a simple wrapper class around LogicalResult that allows for
-/// explicit conversion to bool. This allows for the parser to chain together
-/// parse rules without the clutter of "failed/succeeded".
-class ParseResult : public LogicalResult {
-public:
-  ParseResult(LogicalResult result = success()) : LogicalResult(result) {}
-
-  // 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 class implements `Optional` functionality for ParseResult. We don't
 /// directly use Optional here, because it provides an implicit conversion
 /// to 'bool' which we want to avoid. This class is used to implement tri-state

diff  --git a/mlir/include/mlir/Support/LogicalResult.h b/mlir/include/mlir/Support/LogicalResult.h
index 2ed3e3e0b42ba..3391485433f4d 100644
--- a/mlir/include/mlir/Support/LogicalResult.h
+++ b/mlir/include/mlir/Support/LogicalResult.h
@@ -71,6 +71,25 @@ inline bool succeeded(LogicalResult result) { return result.succeeded(); }
 /// to a failure value.
 inline bool failed(LogicalResult result) { return result.failed(); }
 
+/// This class represents success/failure for parsing-like operations that find
+/// it important to chain together failable operations with `||`.  This is an
+/// extended version of `LogicalResult` that allows for explicit conversion to
+/// bool.
+///
+/// This class should not be used for general error handling cases - we prefer
+/// to keep the logic explicit with the `succeeded`/`failed` predicates.
+/// However, traditional monadic-style parsering logic can sometimes get
+/// swallowed up in boilerplate without this, so we provide this for narrow
+/// cases where it is important.
+///
+class ParseResult : public LogicalResult {
+public:
+  ParseResult(LogicalResult result = success()) : LogicalResult(result) {}
+
+  /// Failure is true in a boolean context.
+  explicit operator bool() const { return failed(); }
+};
+
 /// This class provides support for representing a failure result, or a valid
 /// value of type `T`. This allows for integrating with LogicalResult, while
 /// also providing a value on the success path.


        


More information about the Mlir-commits mailing list