Issue 233729
Summary [mlir][arith] arith.constant with a dense_resource value never sets its integer range -> -int-range-optimizations folds an scf.if to the other branch
Labels mlir
Assignees
Reporter wildoranges
    `InferIntRangeInterface` requires that "`setValueRange` should be called on all result values for the operation". `arith::ConstantOp::inferResultRanges` only handles `IntegerAttr` and `DenseIntElementsAttr`. For any other integer constant, such as a `dense_resource` vector, it returns without setting the result range:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp#L35-L62

The result lattice therefore stays uninitialized ("no value reaches here"). `IntegerRangeAnalysis` then ignores that branch when joining the results of an `scf.if`, and `-int-range-optimizations` replaces the `scf.if` with the other branch's constant. #173716 fixed the same problem for `arith.select` and NVVM.

`dense_resource.mlir`:
```mlir
func.func @f(%c: i1) -> vector<4xi32> {
  %r = arith.constant dense_resource<blob1> : vector<4xi32>
  %five = arith.constant dense<5> : vector<4xi32>
  %x = scf.if %c -> vector<4xi32> {
    scf.yield %r : vector<4xi32>
  } else {
 scf.yield %five : vector<4xi32>
  }
  return %x : vector<4xi32>
}
func.func @main() {
  %t = arith.constant true
  %v = func.call @f(%t) : (i1) -> vector<4xi32>
  vector.print %v : vector<4xi32>
 return
}
{-#
  dialect_resources: {
    builtin: {
      blob1: "0x04000000010000000200000003000000FFFFFFFF"
    }
 }
#-}
```

(`$LLVM_BUILD` is the LLVM/MLIR build directory.)

```console
$ LOWER="-convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -finalize-memref-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt dense_resource.mlir | mlir-opt $LOWER | $RUN
( 1, 2, 3, -1 )
$ mlir-opt dense_resource.mlir -int-range-optimizations | mlir-opt $LOWER | $RUN
( 5, 5, 5, 5 )
$ mlir-opt dense_resource.mlir -int-range-optimizations
module {
  func.func @f(%arg0: i1) -> vector<4xi32> {
    %cst = arith.constant dense<5> : vector<4xi32>
 return %cst : vector<4xi32>
  }
  ...
```

The input verifies and is UB-free: it contains only constants and a branch.

The same pattern of "result range never set" appears in at least two more places. I have not executed these, because they need scalable vectors or `gpu.launch` clusters (sm_90+):
- `vector::StepOp::inferResultRanges` returns early for scalable vectors without setting the range;
- `gpu::LaunchOp::inferResultRanges` never sets the cluster-id and cluster-size block arguments of `gpu.launch clusters(...)`.

At the IR level, `-int-range-optimizations` folds `scf.if %c { yield %cluster_id_x } else { yield 7 }` inside such a launch to the constant `7`, and `scf.if %c { yield vector.step : vector<[4]xindex> } else { yield dense<7> }` to `dense<7>`. I can provide these inputs.

Possible fix: in `ConstantOp::inferResultRanges`, fall back to the maximal range (`ConstantIntRanges::maxRange`) for constants it cannot inspect. More generally, the driver could treat results that an implementation did not set as "unknown" rather than "unreachable".

**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ 4c407e843c975298b6892d50afd9e72cfb0cbe46 (2026-10-20).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to