[PATCH] D73645: Add IntegerAttr::verifyConstructionInvariants.

Sean Silva via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 10:17:04 PST 2020


silvas created this revision.
Herald added subscribers: llvm-commits, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a reviewer: nicolasvasilache.
Herald added a reviewer: rriddle.
Herald added a project: LLVM.

This will help catch improper use of the MLIR API's. In particular, this
catches an error that was manifesting as nondeterministic assertion
failures (the nondeterminism was due to the failure happening only when the
StorageUniquer's DenseMap's probing happened to compare two specific
keys).

No test. The fact that all the existing tests pass with this additional
invariant gives confidence that it is correct/useful.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73645

Files:
  mlir/include/mlir/IR/Attributes.h
  mlir/lib/IR/Attributes.cpp


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73645.241209.patch
Type: text/x-patch
Size: 2518 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200129/d5163ed2/attachment.bin>


More information about the llvm-commits mailing list