[Mlir-commits] [mlir] [mlir][arith] Implement ValueBoundsOpInterface for IndexCastUI and ExtSI ops (PR #204476)
Max Graey
llvmlistbot at llvm.org
Wed Jun 24 05:05:43 PDT 2026
================
@@ -17,6 +17,43 @@ namespace mlir {
namespace arith {
namespace {
+struct ConstantOpInterface
+ : public ValueBoundsOpInterface::ExternalModel<ConstantOpInterface,
+ ConstantOp> {
+ void populateBoundsForIndexValue(Operation *op, Value value,
+ ValueBoundsConstraintSet &cstr) const {
+ auto constantOp = cast<ConstantOp>(op);
+ assert(value == constantOp.getResult() && "invalid value");
+
+ if (auto attr = llvm::dyn_cast<IntegerAttr>(constantOp.getValue()))
+ cstr.bound(value) == attr.getInt();
+ }
+};
+
+struct IndexCastUIOpInterface
+ : public ValueBoundsOpInterface::ExternalModel<IndexCastUIOpInterface,
+ IndexCastUIOp> {
+ void populateBoundsForIndexValue(Operation *op, Value value,
+ ValueBoundsConstraintSet &cstr) const {
+ auto indexCastOp = cast<IndexCastUIOp>(op);
+ assert(value == indexCastOp.getOut() && "invalid value");
+ cstr.bound(value) >= 0;
----------------
MaxGraey wrote:
Good example and yes this will be incorrect. I see only two options how to fix this. First one is compleately remove this model. Second one is try to utilize DLTI interface and compare actual index width with defined via DLTI:
```cpp
DataLayout dataLayout = DataLayout::closest(op);
auto getBitWidth = [&](Type type) -> uint64_t {
if (auto intType = dyn_cast<IntegerType>(type))
return intType.getWidth();
assert(isa<IndexType>(type) && "expected integer or index type");
return dataLayout.getTypeSizeInBits(type).getFixedValue();
};
uint64_t srcBitWidth = getBitWidth(indexCastOp.getIn().getType());
uint64_t dstBitWidth = getBitWidth(indexCastOp.getOut().getType());
if (srcBitWidth < dstBitWidth)
cstr.bound(value) >= 0;
```
WDYT?
https://github.com/llvm/llvm-project/pull/204476
More information about the Mlir-commits
mailing list