[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:45:11 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-vector

Author: qyingwu

<details>
<summary>Changes</summary>

Fixes #<!-- -->208198.

`ConvertVectorTransferRead` in the vector narrow type emulation pass could
call
`fitsInMultiByteContainerTy` with a non-sub-byte element type, such as `f16`.
That helper asserts that the vector element type is sub-byte, so the test pass
could crash on unsupported input instead of failing conversion cleanly.

Add an explicit supported-width check before calling the helper. The check
uses a shared width predicate because existing patterns support both integer and
float element types with 2-bit and 4-bit widths.

Also add a regression test for the reported `vector.transfer_read` case to
verify that the pass emits a normal legalization failure instead of asserting.

Verification:
- `cmake --build /tmp/mlir-208198 --target mlir-opt`
- `/tmp/mlir-208198/bin/mlir-opt --test-emulate-narrow-int="memref-load-
bitwidth=32" --cse --verify-diagnostics --split-input-file mlir/test/Dialect/
Vector/vector-emulate-narrow-type-invalid.mlir`
- `/tmp/mlir-208198/bin/llvm-lit -sv mlir/test/Dialect/Vector/vector-emulate-
narrow-type-invalid.mlir`
- `/tmp/mlir-208198/bin/llvm-lit -sv mlir/test/Dialect/Vector/vector-emulate-
narrow-type.mlir`

AI tool usage:
I used ChatGPT/Codex to help analyze the assertion path, draft the patch, and
run focused verification. I manually reviewed the final diff and test results.

---
Full diff: https://github.com/llvm/llvm-project/pull/208873.diff


2 Files Affected:

- (modified) mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp (+13-1) 
- (added) mlir/test/Dialect/Vector/vector-emulate-narrow-type-invalid.mlir (+10) 


``````````diff
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
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/208873


More information about the Mlir-commits mailing list