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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Problems in slices of Python bindings of BlockArgumentList
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          ingomueller-net
      </td>
    </tr>
</table>

<pre>
    I am running into two problems that I think might both be due to a problem of how slices of the Python bindings of `BlockArgumentList` are handled.

The first problem occurs in this snippet:

```python
print(type(loop))
# <class 'mlir.dialects._ods_common.ForOp'>
print(len(loop.inner_iter_args))
# 1
print([str(a) for a in loop.inner_iter_args])
# ["Value(<block argument> of type 'tensor<1000x2000xf64>' at index: 1)"]
print(len(loop.inner_iter_args.types))
# 2
print(loop.inner_iter_args.types)
# [Type(index), Type(tensor<1000x2000xf64>)]
```

Notice that `loop.inner_iter_args` consists of one "inner" loop argument but not the induction variable of the loop but taking `.types` of that expression suddenly *does* include the induction variable (of type `index`).

The second problem occurs in this snippet (run after the previous one):

```python
print([str(a) for a in wrapped_func.arguments])
# ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)", "Value(<block argument> of type 'tensor<1000x2000xf64>' at index: 2)", "Value(<block argument> of type 'memref<?xi64>' at index: 3)"]
print([str(a) for a in wrapped_func.arguments[:-num_results - 1]])
# ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)"]
print([str(a) for a in wrapped_func.arguments[:-num_results - 1] + loop.inner_iter_args])
# ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)", "Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)"]
```

Notice that `loop.inner_iter_args` has type `tensor<1000x2000xf64>` but the last element of the last expression, which is supposed to consist of that value, is of type `tensor<1000x1500xf64>`. Also, if I print the IR, the last value refers to the wrong SSA name.

The behavior can be reproduced on `5da5483ff` (that commit is required; without it, the surrounding code does not run due to other problems.) by applying the following patch:

```patch
diff --git a/mlir/benchmark/python/common.py b/mlir/benchmark/python/common.py
index 3d80892b0fb8..ea9cd0349a2d 100644
--- a/mlir/benchmark/python/common.py
+++ b/mlir/benchmark/python/common.py
@@ -94,10 +94,32 @@ def emit_benchmark_wrapped_main_func(kernel_func, timer_func):
         loop = scf.ForOp(zero, n_iterations, one, iter_args)
         with ir.InsertionPoint(loop.body):
             start = func.CallOp(timer_func, [])
+            print(type(loop))
+            # <class 'mlir.dialects._ods_common.ForOp'>
+            print(len(loop.inner_iter_args))
+            # 1
+            print([str(a) for a in loop.inner_iter_args])
+            # ["Value(<block argument> of type 'tensor<1000x2000xf64>' at index: 1)"]
+            print(len(loop.inner_iter_args.types))
+            # 2
+            print(loop.inner_iter_args.types)
+            # [Type(index), Type(tensor<1000x2000xf64>)]
+            print([str(a) for a in wrapped_func.arguments])
+            # ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)", "Value(<block argument> of type 'tensor<1000x2000xf64>' at index: 2)", "Value(<block argument> of type 'memref<?xi64>' at index: 3)"]
+            print([str(a) for a in wrapped_func.arguments[:-num_results - 1]])
+            # ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)"]
+            print([
+                str(a) for a in wrapped_func.arguments[:-num_results - 1] +
+                loop.inner_iter_args
+            ])
+            # ["Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)", "Value(<block argument> of type 'tensor<1500x2000xf64>' at index: 1)", "Value(<block argument> of type 'tensor<1000x1500xf64>' at index: 0)"]
             call = func.CallOp(
                 kernel_func,
                 wrapped_func.arguments[:-num_results - 1] + loop.inner_iter_args
             )
+            # args = [a for a in wrapped_func.arguments[:-num_results - 1]] + \
+            #     [a for a in loop.inner_iter_args]
+            # call = func.CallOp(kernel_func, args)
             end = func.CallOp(timer_func, [])
             time_taken = arith.SubIOp(end, start)
             memref.StoreOp(time_taken, timer_buffer, [loop.induction_variable])
```

The code can then be run with this command: `python build/bin/mlir-mbr --machine $(hostname) --revision $(git rev-parse HEAD) --stop_on_error true --result-stdout mlir/benchmark/python/`. It currently fails with `loc("-":8:10): error: 'func.call' op operand type mismatch: expected operand type 'tensor<1000x2000xf64>', but provided 'tensor<1000x1500xf64>' for operand number 2` because the wrong IR is produced.

The patch also contains a work-around, which is currently commented out. It converts the `BlockArgumentList` into a Python `list`. That makes the benchmarked kernel compile (but execution fails unless you define some environment variables).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzdWVtz4jYY_TXkRYPHMZfAAw-QbKaZ6bQ7m52-MrItx2pkyZXkJPTX93wyJrBAQnaz23YZBrAtfffbEanJV7MbxitmG62lvmNSe8P8o2G1NakSlWO-5J7d4Evqe1bJu9Kz1PiSpYLljWBYzrvFzBSsNI_MKZkJR1e-FOzjypdGs1TqHBzC7d44XiiT3c_tXVMJ7X-VzuMe41awkutciTzqxVe9eN5-fgaZQlrnnzllWWMdxCXBHHNa1rXwvcF8extItu86iNDerC107CUTv6oFvpQxdS-Z0rvdkwxYb3CZKe4c6yUXlZI2yiVXIvMuWprcLTNTVUZH18b-jq0XvcGHXcpK6DXhSGot7FJ6fHB7575kdL67sTdaOG_xg2MVK4yFaaHhQUqjq12RR4tekvzBVUNKQYGU7AuDtgaGjMEd0JmU8kI7Y7HqPI7jp4Q-ivGQFEkuGNwNV4knGBMCksQJcTtRxYh47CmafLH9xY3bSn1uvdQKREQv2frWCzpMN_JuImA7LH4zHvHZBjYeHTQvgjEz2iEuQ8AaTXZLwip8B5dsjMvSxjNtfIh2SNpkXiLgH7iVHMHa5UHYQ0s9v6dUA4-1zmAW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