Failed to legalize operation 'tensor.reshape'

Hi Everyone,
I am trying to bufferize tensor.reshape operation but it failed.

func @test_reshape() -> () {
   %c0 = arith.constant 0 : index
    %c2 = arith.constant 2 : index
   %filter=  tensor.from_elements %c0, %c0,%c0,%c0 :  tensor<4xindex>
    %shape = tensor.from_elements %c2, %c2: tensor<2xindex>
    %filter2d = tensor.reshape %filter(%shape) : (tensor<4xindex>,tensor<2xindex>)->tensor<2x2xindex>
  return 
}

I used the following passes
mlir-opt --tensor-bufferize --func-bufferize --finalizing-bufferize

  1. If you don’t return anything, the body of this function is essentially dead code, so it gets eliminated.
  2. You need to bufferize the arith.constant ops.
  3. For this case you can use tensor.expand_shape instead of tensor.reshape.

The following worked for me with mlir-opt --arith-bufferize --tensor-bufferize --func-bufferize --finalizing-bufferize:

module {
  func @test_reshape() -> tensor<2x2xindex> {
    %c0 = arith.constant 0 : index
    %filter = tensor.from_elements %c0, %c0, %c0, %c0 :  tensor<4xindex>
    %filter2d = tensor.expand_shape %filter [[0, 1]] : tensor<4xindex> into tensor<2x2xindex>
    return %filter2d : tensor<2x2xindex>
  }
}

I hope this helps (although it might not be the answer to your question)!

thank you @laszlo-luminous for your last reply. I have another similar example where mlir-opt --arith-bufferize --tensor-bufferize --func-bufferize --finalizing-bufferize fails to bufferize tensor.expand_shape when one of the dimension is dynamic:

 func @test_reshape(%filter:tensor<?xindex> ) -> tensor<2x?xindex> {
    %filter2d = tensor.expand_shape %filter [[0, 1]] : tensor<?xindex> into tensor<2x?xindex>
    return %filter2d : tensor<2x?xindex>
  }

I succeed to bufferize this one after adding --linalg-bufferize, but I do not see why I need linalg-bufferize here and I will prefere to not to use it at this point.

Bonnus question: is it possible to specify functions to be bufferized without bufferizing others?