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

    <tr>
        <th>Summary</th>
        <td>
            [mlir][bufferization] Incorrect bufferization with LayoutMapOption::IdentityLayoutMap
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          ryan-holt-1
      </td>
    </tr>
</table>

<pre>
    `OneShotBufferize` incorrectly casts away non-unit layouts on function call arguments when running with the `LayoutMapOption::IdentityLayoutMap` option.

Reproducer:
```
// RUN: mlir-opt %s --one-shot-bufferize='bufferize-function-boundaries function-boundary-type-conversion=identity-layout-map

func.func @caller(%arg0: tensor<5xf32>, %offset: index, %size: index) -> (f32) {
 %extracted_slice = tensor.extract_slice %arg0[%offset] [%size] [1] : tensor<5xf32> to tensor<?xf32>
    %result = func.call @callee(%extracted_slice) : (tensor<?xf32>) -> (f32)
    return %result : f32
}
func.func private @callee(%arg0: tensor<?xf32>) -> (f32) {
 %0 = arith.constant 0 : index
    %extracted = tensor.extract %arg0[%0] : tensor<?xf32>
    return %extracted : f32
}
```

results in:

```
  func.func @caller(%arg0: memref<5xf32>, %arg1: index, %arg2: index) -> f32 {
    %subview = memref.subview %arg0[%arg1] [%arg2] [1] : memref<5xf32> to memref<?xf32, strided<[1], offset: ?>>
    %cast = memref.cast %subview : memref<?xf32, strided<[1], offset: ?>> to memref<?xf32> 
    %0 = call @callee(%cast) : (memref<?xf32>) -> f32
 return %0 : f32
  }
  func.func private @callee(%arg0: memref<?xf32>) -> f32 {
    %c0 = arith.constant 0 : index
    %0 = memref.load %arg0[%c0] : memref<?xf32>
    return %0 : f32
  }
```

Notice the `memref.cast` is eliminating the layout. This will cause `callee` to load the wrong value when `%offset` is non-zero. I would have expected a new allocation and a copy at the callsite instead of the `memref.cast`. The doc for `LayoutMapOption` indicates that a copy would be the expected behavior as well.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVl2v4jYQ_TXmZZQoOCSQBx7gskhXanelbftcOcmEuDJ2ZDtw2V9f2YYQIN3bqhIK8vhjzpw5Yw8zhh8k4ppkW5LtZqy3rdJrfWEyapWw0XxWqvqyJnnyTeJvrbLbvmlQ8x9I8gS4rJTWWFlxgYoZa4Cd2QWkklEvuQXBLqq3BpSEppeV5UpCxYQApg_9EaU1cG5Rgu6l5PIAZ25bsC0CyZNf_N5fWfetc_tIuiHp5r1Gabm9DJMOhfILYpLsSLIJ3-_YaVX3FWq3L0zkyfUXhnRP6B6-__GVpBs4Cq4j1VkgNDMQRUpiZFplo3IIN90RuhyG0S2eqFS9rJnmaODZdonspcOoUvKE2vggdvwaQRS4iY6sGwN3R8TuA2SROKpQE7oiNGP6kDikFqVRmqRv2UeTUpJ-IfTNoVZNY9C6FVzW-HG1Go98sBUQkfQLELpye2kBZLkNft1i_LCaVRbrP43gFQJJd1d38XXqNnGFk23vjrMdhLF3GUZz_z8FGqy6G0m6v8USsAA4FxpNL6xH4VnxwrmxgoGVJ8g-pNQFs5o6_SX-uz-Nttfywe0G3KKQm-XuOT2d5idm8RnQS5p-5v6R_sSHyjS3bVwpaSyTFhK4p29MzhD4RJYe85O8JGGK73v845OnKHiuI_8NpBng8l5vU6sBPhX4EY8am1eBM32YP8ub6QN9lXeT0hGzgS_TlyeOZ89W8BAPpjFb3sugZX_-k5Zf8DktD8Ybt_QNjNW8xtoZw3ZnvJcpSfcuuCfRu1t0DDKMx_g3_8vZJFinyTGIoMSpenNwRkU2cdI4C9dD79pKHjUFMKhqLIxPSusTpy-pr_5TYSVj9oVi9aM-quRFBz8vp38OebKQvirrbtjrGzgSgX9uDaDgRy6Zda-lWxRekRh-b7mBMxcCKtYbv_nKXZ64nPtI3IazVvIAJyZ6DG-vAzDc4sGJe79_oFYxvMNZ9aKGlp0Q8KNDfzEwkHgGJoSqmH_TmXT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