[Mlir-commits] [mlir] [memref] Simplify loads from reinterpret_cast of 1D contiguous memrefs (PR #188459)
Andrzej WarzyĆski
llvmlistbot at llvm.org
Thu Apr 23 01:42:21 PDT 2026
================
@@ -196,6 +198,234 @@ struct CopyToScalarLoadAndStore : public OpRewritePattern<memref::CopyOp> {
}
};
+/// Describes the unique non-unit dimension of a MemRef shape.
+///
+/// This helper is only used for shapes that have at most one non-unit
+/// dimension. `exists` is false for all-ones shapes. Otherwise, `isOnLeft`
+/// indicates whether the non-unit dimension is on the left boundary.
+///
+/// If `exists` is true and `isOnLeft` is false, the non-unit dimension is on
+/// the right boundary. Rank-1 non-unit MemRefs are treated as matching both
+/// boundaries and callers that care about the right boundary must account for
+/// that from the MemRef type.
+struct SingleNonUnitDimInfo {
+ bool exists = false;
+ bool isOnLeft = false;
+};
+
+/// Returns information about a MemRef if it contains at most one non-unit
+/// dimension.
+///
+/// The single non-unit dimension, if present, must be on the left or right
+/// boundary. Rank-1 non-unit MemRefs are treated as being on both boundaries.
+static std::optional<SingleNonUnitDimInfo>
+getSingleNonUnitDimInfo(MemRefType type) {
+ ArrayRef<int64_t> shape = type.getShape();
+ int64_t nonUnitCount =
+ llvm::count_if(shape, [](int64_t dim) { return dim != 1; });
+ // Return default values if missing nonUnitDim
+ if (nonUnitCount == 0)
+ return SingleNonUnitDimInfo{};
+ // Return no info if MemRef breaks nonUnitDim requirements (more nonUnitDims)
+ if (nonUnitCount > 1)
+ return std::nullopt;
+
+ bool isOnLeft = shape.front() != 1;
+ // Return no info if MemRef breaks nonUnitDim requirements (nonUnitDim in
+ // non-boundary pos)
+ if (!isOnLeft && shape.back() == 1)
+ return std::nullopt;
+
+ return SingleNonUnitDimInfo{/*exists=*/true, isOnLeft};
----------------
banach-space wrote:
I would shorten this. In particular, this is now mixing two high-level parts of the logic:
1. Check pre-conditions (and return `nullopt` when those are not met).
2. Once pre-conditions are met, generate the return value.
```suggestion
if (shape.back() != 1 && shape.front() != 1)
return std::nulopt;
return SingleNonUnitDimInfo{/*exists=*/true, /*isOnLeft=*/shape.front() != 1};
```
WDYT? (please also note my high-level comment for this method)
https://github.com/llvm/llvm-project/pull/188459
More information about the Mlir-commits
mailing list