diff options
| author | River Riddle <riverriddle@google.com> | 2019-12-04 15:49:09 -0800 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-12-04 15:49:42 -0800 |
| commit | 2c930f8d9daa04576ac49e14c19dc540ebf823fe (patch) | |
| tree | 6dcb8515f827477ef49752a5d19818404988e53c /mlir/lib/IR/Attributes.cpp | |
| parent | b3f7cf80a7dc7e9edd5b53827a942bada4a6aeb2 (diff) | |
| download | bcm5719-llvm-2c930f8d9daa04576ac49e14c19dc540ebf823fe.tar.gz bcm5719-llvm-2c930f8d9daa04576ac49e14c19dc540ebf823fe.zip | |
Add emitOptional(Error|Warning|Remark) functions to simplify emission with an optional location.
In some situations a diagnostic may optionally be emitted by the presence of a location, e.g. attribute and type verification. These situations currently require extra 'if(loc) emitError(...); return failure()' wrappers that make verification clunky. These new overloads take an optional location and a list of arguments to the diagnostic, and return a LogicalResult. We take the arguments directly and return LogicalResult instead of returning InFlightDiagnostic because we cannot create a valid diagnostic with a null location. This creates an awkward situation where a user may try to treat the, potentially null, diagnostic as a valid one and encounter crashes when attaching notes/etc. Below is an example of how these methods simplify some existing usages:
Before:
if (loc)
emitError(*loc, "this is my diagnostic with argument: ") << 5;
return failure();
After:
return emitOptionalError(loc, "this is my diagnostic with argument: ", 5);
PiperOrigin-RevId: 283853599
Diffstat (limited to 'mlir/lib/IR/Attributes.cpp')
| -rw-r--r-- | mlir/lib/IR/Attributes.cpp | 43 |
1 files changed, 19 insertions, 24 deletions
diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp index 5d7a4f08d1e..f2f3d41f980 100644 --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -214,35 +214,31 @@ double FloatAttr::getValueAsDouble(APFloat value) { } /// Verify construction invariants. -static LogicalResult verifyFloatTypeInvariants(llvm::Optional<Location> loc, +static LogicalResult verifyFloatTypeInvariants(Optional<Location> loc, Type type) { - if (!type.isa<FloatType>()) { - if (loc) - emitError(*loc, "expected floating point type"); - return failure(); - } + if (!type.isa<FloatType>()) + return emitOptionalError(loc, "expected floating point type"); return success(); } -LogicalResult FloatAttr::verifyConstructionInvariants( - llvm::Optional<Location> loc, MLIRContext *ctx, Type type, double value) { +LogicalResult FloatAttr::verifyConstructionInvariants(Optional<Location> loc, + MLIRContext *ctx, + Type type, double value) { return verifyFloatTypeInvariants(loc, type); } -LogicalResult -FloatAttr::verifyConstructionInvariants(llvm::Optional<Location> loc, - MLIRContext *ctx, Type type, - const APFloat &value) { +LogicalResult FloatAttr::verifyConstructionInvariants(Optional<Location> loc, + MLIRContext *ctx, + Type type, + const APFloat &value) { // Verify that the type is correct. if (failed(verifyFloatTypeInvariants(loc, type))) return failure(); // Verify that the type semantics match that of the value. if (&type.cast<FloatType>().getFloatSemantics() != &value.getSemantics()) { - if (loc) - emitError(*loc, - "FloatAttr type doesn't match the type implied by its value"); - return failure(); + return emitOptionalError( + loc, "FloatAttr type doesn't match the type implied by its value"); } return success(); } @@ -330,14 +326,13 @@ Identifier OpaqueAttr::getDialectNamespace() const { StringRef OpaqueAttr::getAttrData() const { return getImpl()->attrData; } /// Verify the construction of an opaque attribute. -LogicalResult OpaqueAttr::verifyConstructionInvariants( - llvm::Optional<Location> loc, MLIRContext *context, Identifier dialect, - StringRef attrData, Type type) { - if (!Dialect::isValidNamespace(dialect.strref())) { - if (loc) - emitError(*loc) << "invalid dialect namespace '" << dialect << "'"; - return failure(); - } +LogicalResult OpaqueAttr::verifyConstructionInvariants(Optional<Location> loc, + MLIRContext *context, + Identifier dialect, + StringRef attrData, + Type type) { + if (!Dialect::isValidNamespace(dialect.strref())) + return emitOptionalError(loc, "invalid dialect namespace '", dialect, "'"); return success(); } |

