[Mlir-commits] [mlir] [mlir][vector] Guard transfer read narrow type emulation (PR #208873)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jul 10 20:56:08 PDT 2026
https://github.com/qyingwu updated https://github.com/llvm/llvm-project/pull/208873
>From 7bc50a3c052d3253af97bafec16793036a02d305 Mon Sep 17 00:00:00 2001
From: qyingwu <qiyingwu at utexas.edu>
Date: Fri, 10 Jul 2026 20:42:06 -0700
Subject: [PATCH] [mlir][vector] Guard transfer read narrow type emulation
---
.../Vector/Transforms/VectorEmulateNarrowType.cpp | 14 +++++++++++++-
.../Vector/vector-emulate-narrow-type-invalid.mlir | 10 ++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 mlir/test/Dialect/Vector/vector-emulate-narrow-type-invalid.mlir
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
index 9faaebdcf8f35..2286dd1cb995d 100644
--- a/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
+++ b/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp
@@ -51,6 +51,13 @@ using MemRefValue = TypedValue<MemRefType>;
// Utils
//===----------------------------------------------------------------------===//
+/// Returns true for element widths supported by this sub-byte emulation. This
+/// is intentionally width-only: both integer and float element types with these
+/// widths are handled by existing patterns.
+static bool isSupportedSubByteElementWidth(unsigned bitWidth) {
+ return bitWidth == 2 || bitWidth == 4;
+}
+
/// Returns a compressed mask for the emulated vector. For example, when
/// emulating an eight-element `i8` vector with `i32` (i.e. when the source
/// elements span two dest elements), this method compresses `vector<8xi1>`
@@ -1307,6 +1314,11 @@ struct ConvertVectorTransferRead final
cast<MemRefType>(adaptor.getBase().getType()).getElementType();
Type emulatedElemTy = op.getType().getElementType();
int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
+ if (!isSupportedSubByteElementWidth(emulatedBits))
+ return rewriter.notifyMatchFailure(
+ op, "only 2-bit and 4-bit sub-byte type is supported at this "
+ "moment");
+
int containerBits = containerElemTy.getIntOrFloatBitWidth();
// Check per-element alignment.
@@ -1683,7 +1695,7 @@ static LogicalResult alignedConversionPrecondition(PatternRewriter &rewriter,
assert(containerBits % 8 == 0 && "Not a multi-byte scalar type!");
// TODO: Add support other widths (when/if needed)
- if (subByteBits != 2 && subByteBits != 4)
+ if (!isSupportedSubByteElementWidth(subByteBits))
return rewriter.notifyMatchFailure(
op, "only 2-bit and 4-bit sub-byte type is supported at this moment");
diff --git a/mlir/test/Dialect/Vector/vector-emulate-narrow-type-invalid.mlir b/mlir/test/Dialect/Vector/vector-emulate-narrow-type-invalid.mlir
new file mode 100644
index 0000000000000..4b092331ce3df
--- /dev/null
+++ b/mlir/test/Dialect/Vector/vector-emulate-narrow-type-invalid.mlir
@@ -0,0 +1,10 @@
+// RUN: mlir-opt --test-emulate-narrow-int="memref-load-bitwidth=32" --cse --verify-diagnostics --split-input-file %s
+
+func.func @transfer_read_non_subbyte_element(
+ %arg0: memref<4x?x16xf16>, %arg1: index, %arg2: index, %arg3: index) {
+ %cst = arith.constant 3.000000e+00 : f16
+ // expected-error @below {{failed to legalize operation 'vector.transfer_read' that was explicitly marked illegal}}
+ vector.transfer_read %arg0[%arg1, %arg2, %arg3], %cst :
+ memref<4x?x16xf16>, vector<8xf16>
+ return
+}
More information about the Mlir-commits
mailing list