1// RUN: mlir-opt %s -tensor-bufferize | FileCheck %s 2 3// CHECK-LABEL: func @dim( 4// CHECK-SAME: %[[TENSOR:.*]]: tensor<f32>, 5// CHECK-SAME: %[[INDEX:.*]]: index) -> index { 6// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<f32> 7// CHECK: %[[EXTENT:.*]] = memref.dim %[[MEMREF]], %[[INDEX]] : memref<f32> 8// CHECK: return %[[EXTENT]] : index 9func @dim(%arg0: tensor<f32>, %arg1: index) -> index { 10 %0 = tensor.dim %arg0, %arg1 : tensor<f32> 11 return %0 : index 12} 13 14// CHECK-LABEL: func @rank( 15// CHECK-SAME: %[[TENSOR:.*]]: tensor<*xf32>) -> index { 16// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] 17// CHECK: %[[EXTENT:.*]] = memref.rank %[[MEMREF]] : memref<*xf32> 18func @rank(%arg0: tensor<*xf32>) -> index { 19 %0 = tensor.rank %arg0 : tensor<*xf32> 20 return %0 : index 21} 22 23// CHECK-LABEL: func @tensor.cast( 24// CHECK-SAME: %[[TENSOR:.*]]: tensor<?xindex>) -> tensor<2xindex> { 25// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] 26// CHECK: %[[CASTED:.*]] = memref.cast %[[MEMREF]] : memref<?xindex> to memref<2xindex> 27// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[CASTED]] 28// CHECK: return %[[RET]] : tensor<2xindex> 29func @tensor.cast(%arg0: tensor<?xindex>) -> tensor<2xindex> { 30 %0 = tensor.cast %arg0 : tensor<?xindex> to tensor<2xindex> 31 return %0 : tensor<2xindex> 32} 33 34// CHECK-LABEL: func @tensor.cast_from_unranked( 35// CHECK-SAME: %[[TENSOR:.*]]: tensor<*xf32>) -> tensor<2xf32> { 36// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<*xf32> 37// CHECK: %[[CASTED_MEMREF:.*]] = memref.cast %[[MEMREF]] : memref<*xf32> to memref<2xf32> 38// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[CASTED_MEMREF]] : memref<2xf32> 39// CHECK: return %[[RET]] : tensor<2xf32> 40func @tensor.cast_from_unranked(%arg0: tensor<*xf32>) -> tensor<2xf32> { 41 %0 = tensor.cast %arg0 : tensor<*xf32> to tensor<2xf32> 42 return %0 : tensor<2xf32> 43} 44 45// CHECK-LABEL: func @tensor.cast_to_unranked( 46// CHECK-SAME: %[[TENSOR:.*]]: tensor<2xf32>) -> tensor<*xf32> { 47// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<2xf32> 48// CHECK: %[[CASTED_MEMREF:.*]] = memref.cast %[[MEMREF]] : memref<2xf32> to memref<*xf32> 49// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[CASTED_MEMREF]] : memref<*xf32> 50// CHECK: return %[[RET]] : tensor<*xf32> 51func @tensor.cast_to_unranked(%arg0: tensor<2xf32>) -> tensor<*xf32> { 52 %0 = tensor.cast %arg0 : tensor<2xf32> to tensor<*xf32> 53 return %0 : tensor<*xf32> 54} 55 56// CHECK-LABEL: func @tensor.extract( 57// CHECK-SAME: %[[TENSOR:.*]]: tensor<?xf32>, 58// CHECK-SAME: %[[IDX:.*]]: index) -> f32 { 59// CHECK: %[[MEMREF:.*]] = bufferization.to_memref %[[TENSOR]] : memref<?xf32> 60// CHECK: %[[RET:.*]] = memref.load %[[MEMREF]][%[[IDX]]] : memref<?xf32> 61// CHECK: return %[[RET]] : f32 62// CHECK: } 63func @tensor.extract(%arg0: tensor<?xf32>, %arg1: index) -> f32 { 64 %0 = tensor.extract %arg0[%arg1] : tensor<?xf32> 65 return %0 : f32 66} 67 68// CHECK-LABEL: func @tensor.from_elements( 69// CHECK-SAME: %[[ELEM0:.*]]: index, 70// CHECK-SAME: %[[ELEM1:.*]]: index) -> tensor<2xindex> { 71// CHECK: %[[MEMREF:.*]] = memref.alloc() 72// CHECK: %[[C0:.*]] = arith.constant 0 : index 73// CHECK: store %[[ELEM0]], %[[MEMREF]][%[[C0]]] 74// CHECK: %[[C1:.*]] = arith.constant 1 : index 75// CHECK: store %[[ELEM1]], %[[MEMREF]][%[[C1]]] 76// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]] 77// CHECK: return %[[RET]] : tensor<2xindex> 78func @tensor.from_elements(%arg0: index, %arg1: index) -> tensor<2xindex> { 79 %0 = tensor.from_elements %arg0, %arg1 : tensor<2xindex> 80 return %0 : tensor<2xindex> 81} 82 83// CHECK-LABEL: func @tensor.generate( 84// CHECK-SAME: %[[ARG:.*]]: tensor<*xf32>, 85// CHECK-SAME: %[[DYNAMIC_EXTENT:.*]]: index) -> tensor<?xindex> { 86// CHECK: %[[CASTED:.*]] = bufferization.to_memref %[[ARG]] : memref<*xf32> 87// CHECK: %[[MEMREF:.*]] = memref.alloc(%[[DYNAMIC_EXTENT]]) : memref<?xindex> 88// CHECK: %[[C0:.*]] = arith.constant 0 : index 89// CHECK: %[[C1:.*]] = arith.constant 1 : index 90// CHECK: scf.parallel (%[[I:.*]]) = (%[[C0]]) to (%[[DYNAMIC_EXTENT]]) step (%[[C1]]) { 91// CHECK: %[[ELEM:.*]] = memref.dim %[[CASTED]], %[[I]] : memref<*xf32> 92// CHECK: store %[[ELEM]], %[[MEMREF]][%[[I]]] : memref<?xindex> 93// CHECK: scf.yield 94// CHECK: } 95// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]] : memref<?xindex> 96// CHECK: return %[[RET]] : tensor<?xindex> 97// CHECK: } 98func @tensor.generate(%arg: tensor<*xf32>, %dynamic_extent: index) -> tensor<?xindex> { 99 %result = tensor.generate %dynamic_extent { 100 ^bb0(%i : index): 101 %elem = tensor.dim %arg, %i : tensor<*xf32> 102 tensor.yield %elem : index 103 } : tensor<?xindex> 104 return %result : tensor<?xindex> 105} 106 107// Additional test that checks the logic for intermixed static and dynamic 108// extents. 109// 110// CHECK-LABEL: func @tensor.generate_static_and_dynamic( 111// CHECK-SAME: %[[DYNAMIC_EXTENT:.*]]: index) -> tensor<16x?xindex> { 112// CHECK: %[[MEMREF:.*]] = memref.alloc(%[[DYNAMIC_EXTENT]]) : memref<16x?xindex> 113// CHECK: %[[C0:.*]] = arith.constant 0 : index 114// CHECK: %[[C1:.*]] = arith.constant 1 : index 115// CHECK: %[[C16:.*]] = arith.constant 16 : index 116// CHECK: scf.parallel (%[[I:.*]], %[[J:.*]]) = (%[[C0]], %[[C0]]) to (%[[C16]], %[[DYNAMIC_EXTENT]]) step (%[[C1]], %[[C1]]) { 117// CHECK: %[[VAL_7:.*]] = arith.addi %[[I]], %[[J]] : index 118// CHECK: store %[[VAL_7]], %[[MEMREF]][%[[I]], %[[J]]] : memref<16x?xindex> 119// CHECK: scf.yield 120// CHECK: } 121// CHECK: %[[RET:.*]] = bufferization.to_tensor %[[MEMREF]] : memref<16x?xindex> 122// CHECK: return %[[RET]] : tensor<16x?xindex> 123// CHECK: } 124func @tensor.generate_static_and_dynamic(%arg0: index) -> tensor<16x?xindex> { 125 %result = tensor.generate %arg0 { 126 ^bb0(%i: index, %j: index): 127 %sum = arith.addi %i, %j : index 128 tensor.yield %sum : index 129 } : tensor<16x?xindex> 130 return %result : tensor<16x?xindex> 131} 132 133// The tensor.generate op needs to put its body into the 134// resulting scf.parallel. To handle unknown ops in the body, it cannot clone 135// the body because that would require the cloned ops to be legalized 136// immediately, which is usually not possible since they might be from various 137// other dialects. 138// 139// CHECK-LABEL: func @tensor.generate_unknown_ops_in_body 140func @tensor.generate_unknown_ops_in_body(%arg0: index) -> tensor<?xindex> { 141 // CHECK-NOT: tensor.generate 142 %tensor = tensor.generate %arg0 { 143 ^bb0(%iv: index): 144 // CHECK: test.source 145 %0 = "test.source"() : () -> index 146 tensor.yield %0 : index 147 } : tensor<?xindex> 148 return %tensor : tensor<?xindex> 149} 150