<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56401>56401</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR IntegerAttr] APInt create method doesn't catch incorrect bitwidths
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:core
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lattner
      </td>
    </tr>
</table>

<pre>
    I ran into a non-deterministic crash in MLIR when using something like this (but with variables instead of constants for the values):

```
x = IntegerAttr::get(builtinIndexType, APInt(1,1));
```

This would crash with the stack trace below when `StorageUniquer` checked for equality between the constants and found they had different widths.  This is a bozo bug in my case, but shouldn't this reproduce if uniquing both "APInt(32, 1) : index" and "APInt(64, 1) : index" as a `IntegerAttr`?  Maybe IntegerAttr should force the value to be 64-bits always on construction?


The stack trace:
```
Assertion failed: (BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"), function operator==, file APInt.h, line 1007.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: xyz-opt -allow-unregistered-dialect ops.mlir
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
5  libsystem_c.dylib        0x00000001a98cc340 abort + 168
6  libsystem_c.dylib        0x00000001a98cb754 err + 0
7  xyz-opt                 0x0000000100f22ae4 bool llvm::function_ref<bool (mlir::StorageUniquer::BaseStorage const*)>::callback_fn<mlir::detail::IntegerAttrStorage* mlir::StorageUniquer::get<mlir::detail::IntegerAttrStorage, mlir::IntegerType&, llvm::APSInt&>(llvm::function_ref<void (mlir::detail::IntegerAttrStorage*)>, mlir::TypeID, mlir::IntegerType&, llvm::APSInt&)::'lambda'(mlir::StorageUniquer::BaseStorage const*)>(long, mlir::StorageUniquer::BaseStorage const*) (.cold.1) + 0
8  xyz-opt                 0x0000000100cae320 mlir::StorageUniquer::BaseStorage* llvm::function_ref<mlir::StorageUniquer::BaseStorage* (mlir::StorageUniquer::StorageAllocator&)>::callback_fn<mlir::detail::IntegerAttrStorage* mlir::StorageUniquer::get<mlir::detail::IntegerAttrStorage, mlir::Type&, llvm::APInt&>(llvm::function_ref<void (mlir::detail::IntegerAttrStorage*)>, mlir::TypeID, mlir::Type&, llvm::APInt&)::'lambda'(mlir::StorageUniquer::StorageAllocator&)>(long, mlir::StorageUniquer::StorageAllocator&) + 0
9  xyz-opt                 0x0000000100de70c8 std::__1::pair<llvm::detail::DenseSetImpl<(anonymous namespace)::ParametricStorageUniquer::HashedStorage, llvm::DenseMap<(anonymous namespace)::ParametricStorageUniquer::HashedStorage, llvm::detail::DenseSetEmpty, (anonymous namespace)::ParametricStorageUniquer::StorageKeyInfo, llvm::detail::DenseSetPair<(anonymous namespace)::ParametricStorageUniquer::HashedStorage>>, (anonymous namespace)::ParametricStorageUniquer::StorageKeyInfo>::Iterator, bool> llvm::detail::DenseSetImpl<(anonymous namespace)::ParametricStorageUniquer::HashedStorage, llvm::DenseMap<(anonymous namespace)::ParametricStorageUniquer::HashedStorage, llvm::detail::DenseSetEmpty, (anonymous namespace)::ParametricStorageUniquer::StorageKeyInfo, llvm::detail::DenseSetPair<(anonymous namespace)::ParametricStorageUniquer::HashedStorage>>, (anonymous namespace)::ParametricStorageUniquer::StorageKeyInfo>::insert_as<(anonymous namespace)::ParametricStorageUniquer::LookupKey>((anonymous namespace)::ParametricStorageUniquer::HashedStorage&&, (anonymous namespace)::ParametricStorageUniquer::LookupKey const&) + 164
10 xyz-opt                 0x0000000100de5764 mlir::detail::StorageUniquerImpl::getOrCreate(mlir::TypeID, unsigned int, llvm::function_ref<bool (mlir::StorageUniquer::BaseStorage const*)>, llvm::function_ref<mlir::StorageUniquer::BaseStorage* (mlir::StorageUniquer::StorageAllocator&)>) + 688
11 xyz-opt                 0x0000000100cae22c mlir::detail::IntegerAttrStorage* mlir::StorageUniquer::get<mlir::detail::IntegerAttrStorage, mlir::Type&, llvm::APInt&>(llvm::function_ref<void (mlir::detail::IntegerAttrStorage*)>, mlir::TypeID, mlir::Type&, llvm::APInt&) + 256
12 xyz-opt                 0x0000000100cae020 std::__1::enable_if<!std::is_same<mlir::IntegerAttr::ImplType, mlir::AttributeStorage>::value, mlir::IntegerAttr>::type mlir::detail::AttributeUniquer::getWithTypeID<mlir::IntegerAttr, mlir::Type&, llvm::APInt&>(mlir::MLIRContext*, mlir::TypeID, mlir::Type&, llvm::APInt&) + 96
13 xyz-opt                 0x0000000100ca08c0 mlir::IntegerAttr mlir::detail::StorageUserBase<mlir::IntegerAttr, mlir::Attribute, mlir::detail::IntegerAttrStorage, mlir::detail::AttributeUniquer>::get<mlir::Type, llvm::APInt>(mlir::MLIRContext*, mlir::Type, llvm::APInt) + 224
14 xyz-opt                 0x0000000100ca07ac mlir::IntegerAttr::get(mlir::Type, llvm::APInt const&) + 180
15 xyz-opt                 0x0000000100dc3540 mlir::detail::Parser::parseDecOrHexAttr(mlir::Type, bool) + 788
16 xyz-opt                 0x0000000100dc2264 mlir::detail::Parser::parseAttribute(mlir::Type) + 1016
17 xyz-opt                 0x0000000100dd6b84 mlir::detail::AsmParserImpl<mlir::OpAsmParser>::parseAttribute(mlir::Attribute&, mlir::Type) + 28
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWW1v4jgQ_jXwxSpKHAjwgQ-Utiq6Vq22e7e6-4IcZwBfk5i1nbbsr78ZJ7y10NLd3ot0i1CbxPb4mWeeGTsm0elyMGZGFEwVTjPBCl2cpODA5KpQ1inJpBF2js3s-mr8iT3OoWClVcWMWZ2Dm9NVpu6B4aVlDd5LSscelZuzB2GUSDKwONg6ECnTUyY1XovCWTbVBscAdstKsA3eb0TDRnDWCFZ_46D--tsn1ojO2LhwMAMzdM5Q92g4A-fnVJlTxbhI4enzcgENPmLDW-yMbSHehGSeZjjda7v6-5kceNRlltY-ey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