[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:49 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 =
+ integerCast(loc, rewriter, idxTy, operands[shapeOffset]);
+ // Compute upper bound: ub = lb + extent - 1.
+ mlir::Value lbPlusExtent =
+ mlir::LLVM::AddOp::create(rewriter, loc, idxTy, lb, extent, nsw);
+ mlir::Value ub = mlir::LLVM::SubOp::create(rewriter, loc, idxTy,
+ lbPlusExtent, one, nsw);
+ // Create bounds check conditions.
+ mlir::Value lbCheck = mlir::LLVM::ICmpOp::create(
+ rewriter, loc, mlir::LLVM::ICmpPredicate::sge, index, lb);
+ mlir::Value ubCheck = mlir::LLVM::ICmpOp::create(
+ rewriter, loc, mlir::LLVM::ICmpPredicate::sle, index, ub);
+ // Emit assumes for lower and upper bounds with array_bounds marker.
+ auto lbAssume = mlir::LLVM::AssumeOp::create(rewriter, loc, lbCheck);
+ lbAssume->setAttr("llvm.array_bounds", rewriter.getUnitAttr());
----------------
jeanPerier wrote:
I suggest defining the "llvm.array_bounds" inside LLVMDialect.td (have a look [here](https://github.com/llvm/llvm-project/blob/369e78774aef4f576a4fce37b55b8179170ce3f8/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td#L39)) and use the new mlir::LLVM::LLVMDialect::getLoopArrayBoundsAttrName() here.
If the string is only meaningful for `llvm.assume`, maybe the string should be declared in an extra class declaration of the LLVM_AssumeOp in mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td.
https://github.com/llvm/llvm-project/pull/178811
More information about the Mlir-commits
mailing list