[Mlir-commits] [mlir] [mlir][memref]-Add verification for MemRef::ViewOp bounds (PR #177778)
Amir Bishara
llvmlistbot at llvm.org
Fri Feb 27 10:45:57 PST 2026
================
@@ -3740,17 +3740,73 @@ void ViewOp::getAsmResultNames(function_ref<void(Value, StringRef)> setNameFn) {
setNameFn(getResult(), "view");
}
+static LogicalResult hasIdentityLayoutAndZeroOffset(
+ MemRefType memrefType, StringRef descr,
+ llvm::function_ref<InFlightDiagnostic()> emitError) {
+ if (!memrefType.getLayout().isIdentity())
+ return emitError() << "unsupported map for " << descr << " " << memrefType;
+
+ [[maybe_unused]] SmallVector<int64_t> strides;
+ int64_t offset;
+ if (failed(memrefType.getStridesAndOffset(strides, offset)))
+ return emitError() << "failed to get strides and offset for " << descr
+ << " " << memrefType;
+ if (offset != 0)
+ return emitError() << "unsupported non-zero offset for " << descr << " "
+ << memrefType;
+ return success();
+}
+
+// Verifies that a view operation's result, plus the byte shift, fits within
+// the source memref bounds. The check is only performed when both the base
+// and view memrefs have static shapes and the view element type is
+// byte-aligned.
+static LogicalResult
+checkStaticViewBounds(MemRefType baseType, MemRefType viewType,
+ Value shiftInBytes,
+ llvm::function_ref<InFlightDiagnostic()> emitError) {
+ // Skip if either the base or view has dynamic shape.
+ if (!baseType.hasStaticShape() || !viewType.hasStaticShape())
+ return success();
+
+ // Skip if the view element type is not int or float.
+ if (!viewType.getElementType().isIntOrFloat())
+ return success();
+
+ // Skip non byte-aligned view element types.
+ int64_t viewElementBitWidth =
+ viewType.getElementType().getIntOrFloatBitWidth();
+ if (viewElementBitWidth % 8 != 0)
+ return success();
+
+ int64_t baseTotalElementsInBytes = baseType.getNumElements();
+ int64_t viewTotalElements = viewType.getNumElements();
+ int64_t viewTotalElementsInBytes =
+ viewTotalElements * (viewElementBitWidth / 8);
+ // Shift in bytes may be a non static value, still we will
+ // check the sizes bounds.
+ int64_t shiftInBytesInt =
+ getConstantIntValue(getAsOpFoldResult(shiftInBytes)).value_or(0);
----------------
amirBish wrote:
Thanks @kuhar for emphasizing that, To resolve this issue I'm intending to add static_sizes for the viewOp operation like other operations SubviewOp/ReinterpreCastOp (adding you as a reviewer there also). And then will rebase upon that changes. https://github.com/llvm/llvm-project/pull/183795
https://github.com/llvm/llvm-project/pull/177778
More information about the Mlir-commits
mailing list