[flang-commits] [flang] [llvm] [flang] Support static-unit array slices in FIR LoopVersioning (PR #222723)
Sergey Shcherbinin via flang-commits
flang-commits at lists.llvm.org
Tue Oct 6 03:06:42 PDT 2026
================
@@ -260,6 +258,47 @@ static mlir::Value getLowerBound(fir::ArrayCoorOp coop, unsigned dim) {
return {};
}
+/// A fir.slice triple whose upper bound is fir.undefined selects a single
+/// element instead of a section. XArrayCoor lowering then ignores the triple's
+/// lower bound and step, and only uses the fir.array_coor index of that
+/// dimension.
+static bool isScalarSliceDim(mlir::ValueRange triples, unsigned dim) {
+ return mlir::isa_and_nonnull<fir::UndefOp>(
+ triples[3 * dim + 1].getDefiningOp());
+}
+
+/// Return the section lower bound that dimension \p dim of \p coop's slice
+/// adds to the coordinate, or a null value if it adds nothing.
+static mlir::Value getSliceLowerBound(fir::ArrayCoorOp coop, unsigned dim) {
+ if (!coop.getSlice())
+ return {};
+ auto slice = mlir::cast<fir::SliceOp>(coop.getSlice().getDefiningOp());
+ if (isScalarSliceDim(slice.getTriples(), dim))
+ return {};
+ return slice.getTriples()[3 * dim];
+}
+
+/// Match XArrayCoor's signed integerCast before computing sliced coordinates.
+/// FIR conversion zero-extends builtin i1 and unsigned integers, whereas the
+/// generic array-coordinate lowering sign-extends their bit patterns.
+static mlir::Value toSliceIndex(fir::FirOpBuilder &builder, mlir::Location loc,
+ mlir::Value value) {
+ if (auto type = mlir::dyn_cast<mlir::IntegerType>(value.getType())) {
+ if (type.getWidth() == 1) {
+ value = builder.createConvert(loc, builder.getI1Type(), value);
+ value = mlir::arith::ExtSIOp::create(builder, loc,
+ builder.getIntegerType(2), value);
+ } else if (type.isUnsigned()) {
+ value = builder.createConvert(
+ loc, builder.getIntegerType(type.getWidth()), value);
+ }
+ }
+ if (value.getType().isSignlessInteger())
+ return mlir::arith::IndexCastOp::create(builder, loc,
+ builder.getIndexType(), value);
+ return builder.createConvert(loc, builder.getIndexType(), value);
+}
----------------
SergeyShch01 wrote:
Thanks for pointing this out. I added this handling only to match the current XArrayCoor lowering.
https://github.com/llvm/llvm-project/pull/222723
More information about the flang-commits
mailing list