diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -359,6 +359,13 @@ static bool kindof(unsigned kind) { return kind == StandardAttributes::Integer; } + + static LogicalResult verifyConstructionInvariants(Optional loc, + MLIRContext *ctx, Type type, + int64_t value); + static LogicalResult verifyConstructionInvariants(Optional loc, + MLIRContext *ctx, Type type, + const APInt &value); }; //===----------------------------------------------------------------------===// diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -281,6 +281,33 @@ int64_t IntegerAttr::getInt() const { return getValue().getSExtValue(); } +static LogicalResult verifyIntegerTypeInvariants(Optional loc, + Type type) { + if (type.isa() || type.isa()) + return success(); + return emitOptionalError(loc, "expected integer or index type"); +} + +LogicalResult verifyConstructionInvariants(Optional loc, + MLIRContext *ctx, Type type, + int64_t value) { + return verifyIntegerTypeInvariants(loc, type); +} + +LogicalResult IntegerAttr::verifyConstructionInvariants(Optional loc, + MLIRContext *ctx, + Type type, + const APInt &value) { + if (failed(verifyIntegerTypeInvariants(loc, type))) + return failure(); + if (auto integerType = type.dyn_cast()) + if (integerType.getWidth() != value.getBitWidth()) + return emitOptionalError( + loc, "integer type bit width (", integerType.getWidth(), + ") doesn't match value bit width (", value.getBitWidth(), ")"); + return success(); +} + //===----------------------------------------------------------------------===// // IntegerSetAttr //===----------------------------------------------------------------------===//