<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/115653>115653</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Bugs in patterns under `populateVectorNarrowTypeEmulationPatterns` (1D vs 2D)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:vector
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          banach-space
      </td>
    </tr>
</table>

<pre>
    All patterns in populateVectorNarrowTypeEmulationPatterns currently assume a 1-D vector load/store rather than an n-D vector load/store. This assumption is evident in ConvertVectorTransferRead, for example, [here](https://github.com/llvm/llvm-project/blob/58a17e1bbc54357385d0b89cfc5635e402c31ef6/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp#L670-L676):

```cpp
auto newRead = rewriter.create<vector::TransferReadOp>(
    loc, VectorType::get(numElements, newElementType), adaptor.getSource(),
    getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices),
    newPadding);

auto bitCast = rewriter.create<vector::BitCastOp>(
    loc, VectorType::get(numElements * scale, oldElementType), newRead);
```

Both invocations of `VectorType::get()` here generate a 1-D vector.

Attempts to use these patterns with more generic cases, such as 2-D vectors, fail. For example, trying to cast the following 2-D case to `i32`:

```mlir
func.func @vector_maskedload_2d_i8_negative(
  %idx1: index,
  %idx2: index,
  %num_elems: index,
  %passthru: vector<2x4xi8>) -> vector<2x4xi8> {

    %0 = memref.alloc() : memref<3x4xi8>
    %mask = vector.create_mask %num_elems, %num_elems : vector<2x4xi1>
    %1 = vector.maskedload %0[%idx1, %idx2], %mask, %passthru :
      memref<3x4xi8>, vector<2x4xi1>, vector<2x4xi8> into vector<2x4xi8>
    return %1 : vector<2x4xi8>

}
```

leads to the following error:

```bash
error: 'vector.bitcast' op failed to verify that all of {source, result} have same rank
    %1 = vector.maskedload %0[%idx1, %idx2], %mask, %passthru :
         ^
```

Here’s the mlir-opt invocation used:

```bash
mlir-opt --test-emulate-narrow-int="arith-compute-bitwidth=1 memref-load-bitwidth=32"
```

For further context, I’ve included a full list of reproductions as tests in this PR:

* https://github.com/llvm/llvm-project/pull/115612.

As a temporary workaround, I suggest restricting these patterns to 1-D vectors.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VlFv27oO_jXKC5HAlmMnechDmjS4BYa7YXfYayHbdKw7WTIkOmnvr7-gnLTJlu1s5-EURdpQ0ify40dSKgR9sIhrkT-IfDdRA7XOr0tlVdVOQ68qnJSufl1vjIFeEaG3AbSF3vWDUYRfsSLn_628d6cvrz0-dmzWzn66bK4G79GSeQUVwtAhKEinOzjGg2CcqoXcB3IewStq0QO1yoKyYO9vm8GXVocRreerQAfAo67REru2dfaInkbPvnhlQ4P-MzLAFhrnAV9U1xvkryJ_aNGjyHdCLluiPohsI-ReyP1BUzuUs8p1Qu6NOV7-THvv_osVCbkvjSuF3OdLlS4wLcsqn2f5IlvmdVIuV1VT5UWW4zyRVZZiUwi574z2jKP53E4rMwKNvgq5H911vgtv1pFRfKd4VvW9kNmHYpFMPxSLQsgVO53sRHL5LJLxl3dGixrIgcUT0wAi24HHk9eEflZ5VIQi245MM1K2uWbtYy-yRyGXIxAAgHEVc3cm-LXH8dABScilHbpHgx1aCrzJ4un8NW6UKzaqWvXk_OyA9B83-AoZPi69X3JA-qrMgB_9Nnq4dTaQsvRka3z52Au5vITAiGeXjLaovP4f1k-21hWG71Etnj6putb2EFl7uGYtclRq2qpAv8HRw7jz79MDQm4gVGpUojP1j0SdM3bj6yW3164_OGpB26OrYu0FcA2IIrnvAqMVCbDu4YAWvaLbopxdQ2-IsOspADkYAgK1GPC9FZw0tdC5C5SuoFIBY-rDULWgAsg34GhulDYz2N_WIflXbQ98R8XsU4vQOGPcia0MwKi8LIpEZ5LD_4nkY4VFUzPYasYfIObJ6MBzp8I3rLmbPMv6WS-fLR4U6SNeZVDIXNcvqcg2o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