// RUN: mlir-opt %s -finalizing-bufferize -split-input-file -verify-diagnostics | FileCheck %s // CHECK-LABEL: func @eliminate_materializations( // CHECK-SAME: %[[ARG:.*]]: memref) -> memref { // CHECK: return %[[ARG]] : memref func.func @eliminate_materializations(%arg0: memref) -> memref { %0 = bufferization.to_tensor %arg0 : memref %1 = bufferization.to_memref %0 : memref return %1 : memref } // ----- func.func @unable_to_convert_lone_buffer_cast() -> memref { // expected-error @+1 {{failed to legalize operation 'test.source'}} %0 = "test.source"() : () -> tensor %1 = bufferization.to_memref %0 : memref return %1 : memref } // ----- func.func @unable_to_convert_lone_tensor_load(%arg0: memref) { %0 = bufferization.to_tensor %arg0 : memref // expected-error @+1 {{failed to legalize operation 'test.sink'}} "test.sink"(%0) : (tensor) -> () return } // ----- // CHECK: #[[$map1:.*]] = affine_map<(d0)[s0] -> (d0 + s0)> // CHECK-LABEL: func @dyn_layout_to_no_layout_cast( // CHECK-SAME: %[[arg:.*]]: memref) // CHECK: %[[c0:.*]] = arith.constant 0 : index // CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] // CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref // CHECK: memref.copy %[[arg]], %[[alloc]] // CHECK: return %[[alloc]] #map1 = affine_map<(d0)[s0] -> (d0 + s0)> func.func @dyn_layout_to_no_layout_cast(%m: memref) -> memref { %0 = bufferization.to_tensor %m : memref %1 = bufferization.to_memref %0 : memref return %1 : memref } // ----- // CHECK: #[[$map2:.*]] = affine_map<(d0)[s0] -> (d0 * 100 + s0)> // CHECK-LABEL: func @fancy_layout_to_no_layout_cast( // CHECK-SAME: %[[arg:.*]]: memref) // CHECK: %[[c0:.*]] = arith.constant 0 : index // CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] // CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref // CHECK: memref.copy %[[arg]], %[[alloc]] // CHECK: return %[[alloc]] #map2 = affine_map<(d0)[s0] -> (d0 * 100 + s0)> func.func @fancy_layout_to_no_layout_cast(%m: memref) -> memref { %0 = bufferization.to_tensor %m : memref %1 = bufferization.to_memref %0 : memref return %1 : memref } // ----- // CHECK: #[[$map3:.*]] = affine_map<(d0)[s0] -> (d0 + 25)> // CHECK-LABEL: func @static_layout_to_no_layout_cast( // CHECK-SAME: %[[arg:.*]]: memref) // CHECK: %[[c0:.*]] = arith.constant 0 : index // CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] // CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref // CHECK: memref.copy %[[arg]], %[[alloc]] // CHECK: return %[[alloc]] #map3 = affine_map<(d0)[s0] -> (d0 + 25)> func.func @static_layout_to_no_layout_cast(%m: memref) -> memref { %0 = bufferization.to_tensor %m : memref %1 = bufferization.to_memref %0 : memref return %1 : memref } // ----- // TODO: to_memref with layout maps not supported yet. This should fold to a // memref.cast. #map4 = affine_map<(d0)[s0] -> (d0 + s0)> func.func @no_layout_to_dyn_layout_cast(%m: memref) -> memref { %0 = bufferization.to_tensor %m : memref // expected-error @+1 {{failed to materialize conversion for result #0 of operation 'bufferization.to_memref' that remained live after conversion}} %1 = bufferization.to_memref %0 : memref // expected-note @+1 {{see existing live user here}} return %1 : memref } // ----- func.func @illegal_unranked_to_rank(%m: memref<*xf32>) -> memref { // expected-note @+1 {{prior use here}} %0 = bufferization.to_tensor %m : memref<*xf32> // expected-error @+1 {{expects different type than prior uses: 'tensor' vs 'tensor<*xf32>'}} %1 = bufferization.to_memref %0 : memref return %1 : memref }