I am hitting upon a pain point when using dynamically shaped tensor types, and handling of `tensor.dim`

operations. Lets say I have

```
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%0 = "my_dialect.foo" ... : tensor<?x?xf32>
%1 = tensor.dim %0, %c0 : tensor<?x?xf32>
%2 = tensor.dim %0, %c1 :tensor<?x?xf32>
%3 = "my_dialect.bar" ... %foo ... : tensor<?x?xf32>
%4 = tensor.dim %3, %c0 : tensor<?x?xf32>
%5 = tensor.dim %3, %c1 : tensor<?x?xf32>
```

Lets say `my_dialect.foo`

and `my_dialect.bar`

implement the [`ReifyRankedShapedTypeOpInterface](llvm-project/InferTypeOpInterface.td at fa56e362af475e0758cfb41c42f78db50da7235c · llvm/llvm-project · GitHub). Now, if I use the interface to resolve the `tensor.dim` it will end up creating

```
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%0 = "my_dialect.foo" ... : tensor<?x?xf32>
%1 = tensor.dim %0, %c0 : tensor<?x?xf32>
%2 = tensor.dim %0, %c1 :tensor<?x?xf32>
%c0_1 = arith.constant 0 : index
%c1_2 = arith.constant 1 : index
%3 = "my_dialect.foo" ... : tensor<?x?xf32>
%4 = tensor.dim %0, %c0_1 : tensor<?x?xf32>
%5 = tensor.dim %0, %c1_2 :tensor<?x?xf32>
```

(assuming that the dimensions of the result of `my_dialect.bar`

depend on the dimensions of `my_dialect.foo`

). In the end it doesnt matter cause it all gets CSE-ed, but this is unnecessarily creating new instructions only to be destroyed later. The problem gets worse if there are a chain of operations that implement the `ReifyRankedShapedTypeOpInterface`

.

One way I can think of addressing this it to extend the Listener here to also intercept notify for `create`

methods. Then a listener could keep track of all the `tensor.dim`

operations in the IR and just return the value of an already existing `tensor.dim`

instead of creating a new one. That would make a the “Listener” more of a “Do-er”, but it wont create new IR, just avoiding creating new instructions when not needed.

My case is particularly related to `tensor.dim`

operations, I am happy to try any solution (if the above listener related change is not kosher and if it is within my capacity to do so).

CC :