[Mlir-commits] [flang] [mlir] [flang] Emit llvm.assume for array bounds constraints (PR #178811)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jan 30 02:59:50 PST 2026
================
@@ -2699,6 +2699,34 @@ struct XArrayCoorOpConversion
mlir::Value lb =
isShifted ? integerCast(loc, rewriter, idxTy, operands[shiftOffset])
: one;
+
+ // Emit array bounds assumes per Fortran 2018 standard section 9.5.3.3.2:
+ // "The value of each subscript expression shall be within the bounds for
+ // its dimension unless the array section has size zero."
+ // For non-boxed arrays with known shape, emit: assume(index >= lb) and
+ // assume(index <= lb + extent - 1).
+ // The llvm.array_bounds attribute marks these assumes for removal before
+ // vectorization to prevent IR bloat and avoid impacting cost models.
+ if (!baseIsBoxed && !coor.getShape().empty()) {
+ mlir::Value extent =
----------------
jeanPerier wrote:
Have you tested it with assumed-size?
```
subroutine foo(x)
real :: x(*)
call bar(x(10))
end subroutine
```
I think you will run into issues for this case because the extent will never be known and is encoded as -1, so the assume emitted here is bogus.
So this cannot be emitted for assumed-size arrays (well, you could emit it for all but the last dimension, and for the lower bound or the last dimension, but not the extent of the last dimension).
Right now the only way to detect assumed-size at that level of the compilation is to check `llvm::isa_and_nonnull<fir::AssumedSizeExtentOp>(coor.getShape().back().getDefiningOp())`, it is not ideal and I am planning to move this info in the FIR type system for more robustness.
https://github.com/llvm/llvm-project/pull/178811
More information about the Mlir-commits
mailing list