[Mlir-commits] [mlir] [mlir][Vector] Promote buffers accessed via dynamic subviews and memref.copy (PR #217793)
Jianhui Li
llvmlistbot at llvm.org
Tue Oct 13 10:58:28 PDT 2026
================
@@ -84,43 +92,156 @@ getPromotableSubViewOffsets(memref::SubViewOp subView) {
llvm::zip_equal(subView.getMixedSizes(), resType.getShape())) {
std::optional<int64_t> s = getConstantIntValue(size);
if (!s || *s != dim)
- return std::nullopt;
+ return false;
+ }
+ return true;
+}
+
+/// Returns the static offsets of `subView` within the parent vector for
+/// `vector.extract_strided_slice` and `vector.insert_strided_slice`.
+static SmallVector<int64_t> getStaticSubViewOffsets(memref::SubViewOp subView) {
+ SmallVector<int64_t> offsets;
+ for (OpFoldResult offset : subView.getMixedOffsets()) {
+ std::optional<int64_t> value = getConstantIntValue(offset);
+ assert(value && "expected a static offset");
+ offsets.push_back(*value);
}
return offsets;
}
+
+/// Returns whether `subView` can be promoted as an alias of the whole parent
+/// vector. Static shapes are handled by `isAliasableStaticShapeSubView`
+/// instead.
+static bool isAliasableDynamicShapeSubView(memref::SubViewOp subView) {
+ // The parent must be statically shaped so the slot has a fixed-shape vector
+ // type; only the subview's sizes may be dynamic.
+ auto srcType = dyn_cast<MemRefType>(subView.getSource().getType());
+ auto resType = dyn_cast<MemRefType>(subView.getResult().getType());
+ if (!srcType || !resType || !srcType.hasStaticShape())
+ return false;
+
+ // At least one dynamic result dimension; a fully static sub-slice is handled
+ // by `isAliasableStaticShapeSubView` instead.
+ if (resType.hasStaticShape())
+ return false;
+
+ // Same rank: the read/write vector matches the parent rank.
+ if (srcType.getRank() != resType.getRank())
+ return false;
+
+ // Unit strides and zero offsets: a leading, contiguous sub-region starting at
+ // the parent's origin, so the valid region on each dim is exactly [0, size).
+ if (!subView.hasUnitStride() || !subView.hasZeroOffset())
+ return false;
+
+ return VectorType::isValidElementType(srcType.getElementType());
+}
+
+/// The vector type covering `subView`'s whole parent buffer, which is the type
+/// the alias slot of a dynamically-shaped subview takes.
+static VectorType getWholeParentVectorType(memref::SubViewOp subView) {
+ auto parentType = cast<MemRefType>(subView.getSource().getType());
----------------
Jianhui-Li wrote:
memref.subview can be applied on top of other subview (recursively), as long as the lower level subview are all static. This PR support such nested subview usage cases.
There is a test: dyn_subview_of_static_subview in mem2reg-dynamic-subview.mlir
https://github.com/llvm/llvm-project/pull/217793
More information about the Mlir-commits
mailing list