[Mlir-commits] [mlir] [mlir][Vector] Promote buffers accessed via dynamic subviews (PR #217793)
Jianhui Li
llvmlistbot at llvm.org
Wed Oct 14 08:59:58 PDT 2026
================
@@ -39,88 +53,185 @@ using namespace mlir::memref;
using namespace mlir::vector;
//===----------------------------------------------------------------------===//
-// memref.subview aliaser
+// Utilities
//===----------------------------------------------------------------------===//
-/// Returns the offsets of `subView` as a static, contiguous, same-rank slice of
-/// its source, or nullopt if the subview is not promotable as a whole-buffer
-/// sub-slice. Promotion projects the parent buffer's vector value through
-/// `vector.extract_strided_slice` / `insert_strided_slice`, which require:
-/// * fully static offsets and sizes,
-/// * unit strides,
-/// * no rank reduction (result rank == source rank),
-/// so a dropped or dynamic dimension disqualifies the subview.
-static std::optional<SmallVector<int64_t>>
-getPromotableSubViewOffsets(memref::SubViewOp subView) {
- auto srcType = dyn_cast<MemRefType>(subView.getSource().getType());
- auto resType = dyn_cast<MemRefType>(subView.getResult().getType());
- if (!srcType || !resType || !srcType.hasStaticShape() ||
- !resType.hasStaticShape())
- return std::nullopt;
-
- // No rank reduction: extract/insert_strided_slice operate at a single rank.
- if (srcType.getRank() != resType.getRank())
- return std::nullopt;
-
- // Unit strides only.
- for (OpFoldResult stride : subView.getMixedStrides()) {
- std::optional<int64_t> s = getConstantIntValue(stride);
- if (!s || *s != 1)
- return std::nullopt;
- }
+/// Checks the requirements shared by static and dynamic subview aliases: a
+/// parent with a static shape and a valid vector element type, no rank
+/// reduction, and unit slice strides.
+static bool isUnitStrideSliceOfStaticParent(memref::SubViewOp subView) {
+ MemRefType srcType = subView.getSourceType();
+ if (!srcType.hasStaticShape())
+ return false;
+
+ if (srcType.getRank() != subView.getType().getRank())
+ return false;
+
+ if (!subView.hasUnitStride())
+ return false;
+
+ return VectorType::isValidElementType(srcType.getElementType());
+}
+
+/// Returns whether `subView` can alias a slice of the parent vector using
+/// `vector.extract_strided_slice` and `vector.insert_strided_slice`.
+/// Requires static source and result shapes, constant offsets, unit strides,
+/// and no rank reduction.
+static bool isAliasableStaticShapeSubView(memref::SubViewOp subView) {
+ if (!subView.getType().hasStaticShape())
+ return false;
- // Static offsets.
+ if (!isUnitStrideSliceOfStaticParent(subView))
+ return false;
+
+ return llvm::all_of(subView.getMixedOffsets(), [](OpFoldResult offset) {
+ return getConstantIntValue(offset).has_value();
+ });
+}
+
+/// Returns whether `subView` can be promoted as an alias of the whole parent
----------------
Jianhui-Li wrote:
added "static" and "dynamic" to help the distinction.
https://github.com/llvm/llvm-project/pull/217793
More information about the Mlir-commits
mailing list