1// RUN: mlir-opt %s --test-tensor-copy-insertion --pre-sparsification-rewrite --sparse-reinterpret-map --sparsification --cse | FileCheck %s 2 3#SM = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }> 4 5#trait_matmul = { 6 indexing_maps = [ 7 affine_map<(d0, d1, d2) -> (d1, d0)>, 8 affine_map<(d0, d1, d2) -> (d0, d2)>, 9 affine_map<(d0, d1, d2) -> (d1, d2)> 10 ], 11 iterator_types = ["reduction", "parallel", "parallel"] 12} 13 14#trait_scale = { 15 indexing_maps = [ 16 affine_map<(d0, d1) -> (d0, d1)>, 17 affine_map<(d0, d1) -> (d0, d1)>, 18 affine_map<(d0, d1) -> (d0, d1)> 19 ], 20 iterator_types = ["parallel", "parallel"] 21} 22 23// CHECK-LABEL: func.func @fold_yield_arg_zero() -> tensor<1024x1024xf64> { 24// CHECK: %[[C0:.*]] = arith.constant dense<0.000000e+00> : tensor<1024x1024xf64> 25// CHECK: return %[[C0]] : tensor<1024x1024xf64> 26// CHECK: } 27func.func @fold_yield_arg_zero() -> tensor<1024x1024xf64> { 28 %cst = arith.constant 0.000000e+00 : f64 29 %0 = tensor.empty() : tensor<1024x1024xf64> 30 %1 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> ()>, 31 affine_map<(d0, d1) -> (d0, d1)>], 32 iterator_types = ["parallel", "parallel"]} 33 ins(%cst : f64) 34 outs(%0 : tensor<1024x1024xf64>) { 35 ^bb0(%a: f64, %x: f64): 36 linalg.yield %a : f64 37 } -> tensor<1024x1024xf64> 38 return %1 : tensor<1024x1024xf64> 39} 40 41// CHECK-LABEL: func.func @fold_yield_direct_zero() -> tensor<32xf64> { 42// CHECK: %[[C0:.*]] = arith.constant dense<0.000000e+00> : tensor<32xf64> 43// CHECK: return %[[C0]] : tensor<32xf64> 44// CHECK: } 45func.func @fold_yield_direct_zero() -> tensor<32xf64> { 46 %cst = arith.constant 0.000000e+00 : f64 47 %0 = tensor.empty() : tensor<32xf64> 48 %1 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>], 49 iterator_types = ["parallel"]} 50 outs(%0 : tensor<32xf64>) { 51 ^bb0(%x: f64): 52 linalg.yield %cst : f64 53 } -> tensor<32xf64> 54 return %1 : tensor<32xf64> 55} 56 57// CHECK-LABEL: func.func @sampled_dd_unfused( 58// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x8xf64, #sparse{{[0-9]*}}>, 59// CHECK-SAME: %[[VAL_1:.*]]: tensor<8x8xf64>, 60// CHECK-SAME: %[[VAL_2:.*]]: tensor<8x8xf64>) -> tensor<8x8xf64> { 61// CHECK-DAG: %[[VAL_3:.*]] = arith.constant 8 : index 62// CHECK-DAG: %[[VAL_4:.*]] = arith.constant 0 : index 63// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 1 : index 64// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<0.000000e+00> : tensor<8x8xf64> 65// CHECK-DAG: %[[VAL_7:.*]] = bufferization.alloc_tensor() copy(%[[VAL_6]]) : tensor<8x8xf64> 66// CHECK-DAG: %[[VAL_8:.*]] = bufferization.alloc_tensor() copy(%[[VAL_6]]) : tensor<8x8xf64> 67// CHECK-DAG: %[[VAL_9:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<8x8xf64> to memref<8x8xf64> 68// CHECK-DAG: %[[VAL_10:.*]] = bufferization.to_memref %[[VAL_2]] : tensor<8x8xf64> to memref<8x8xf64> 69// CHECK-DAG: %[[VAL_11:.*]] = sparse_tensor.positions %[[VAL_0]] {level = 0 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 70// CHECK-DAG: %[[VAL_12:.*]] = sparse_tensor.coordinates %[[VAL_0]] {level = 0 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 71// CHECK-DAG: %[[VAL_13:.*]] = sparse_tensor.positions %[[VAL_0]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 72// CHECK-DAG: %[[VAL_14:.*]] = sparse_tensor.coordinates %[[VAL_0]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 73// CHECK-DAG: %[[VAL_15:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xf64> 74// CHECK-DAG: %[[VAL_16:.*]] = bufferization.to_memref %[[VAL_8]] : tensor<8x8xf64> to memref<8x8xf64> 75// CHECK: %[[VAL_17:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_4]]] : memref<?xindex> 76// CHECK: %[[VAL_18:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_5]]] : memref<?xindex> 77// CHECK: scf.for %[[VAL_19:.*]] = %[[VAL_17]] to %[[VAL_18]] step %[[VAL_5]] { 78// CHECK: %[[VAL_20:.*]] = memref.load %[[VAL_12]]{{\[}}%[[VAL_19]]] : memref<?xindex> 79// CHECK: scf.for %[[VAL_21:.*]] = %[[VAL_4]] to %[[VAL_3]] step %[[VAL_5]] { 80// CHECK: %[[VAL_22:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_20]], %[[VAL_21]]] : memref<8x8xf64> 81// CHECK: %[[VAL_23:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_19]]] : memref<?xindex> 82// CHECK: %[[VAL_24:.*]] = arith.addi %[[VAL_19]], %[[VAL_5]] : index 83// CHECK: %[[VAL_25:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_24]]] : memref<?xindex> 84// CHECK: scf.for %[[VAL_26:.*]] = %[[VAL_23]] to %[[VAL_25]] step %[[VAL_5]] { 85// CHECK: %[[VAL_27:.*]] = memref.load %[[VAL_14]]{{\[}}%[[VAL_26]]] : memref<?xindex> 86// CHECK: %[[VAL_28:.*]] = memref.load %[[VAL_16]]{{\[}}%[[VAL_20]], %[[VAL_27]]] : memref<8x8xf64> 87// CHECK: %[[VAL_29:.*]] = memref.load %[[VAL_10]]{{\[}}%[[VAL_21]], %[[VAL_27]]] : memref<8x8xf64> 88// CHECK: %[[VAL_30:.*]] = arith.mulf %[[VAL_22]], %[[VAL_29]] : f64 89// CHECK: %[[VAL_31:.*]] = memref.load %[[VAL_15]]{{\[}}%[[VAL_26]]] : memref<?xf64> 90// CHECK: %[[VAL_32:.*]] = arith.mulf %[[VAL_30]], %[[VAL_31]] : f64 91// CHECK: %[[VAL_33:.*]] = arith.addf %[[VAL_28]], %[[VAL_32]] : f64 92// CHECK: memref.store %[[VAL_33]], %[[VAL_16]]{{\[}}%[[VAL_20]], %[[VAL_27]]] : memref<8x8xf64> 93// CHECK: } 94// CHECK: } 95// CHECK: } 96// CHECK: %[[VAL_34:.*]] = bufferization.to_tensor %[[VAL_16]] : memref<8x8xf64> 97// CHECK: return %[[VAL_34]] : tensor<8x8xf64> 98// CHECK: } 99func.func @sampled_dd_unfused(%args: tensor<8x8xf64, #SM>, 100 %arga: tensor<8x8xf64>, 101 %argb: tensor<8x8xf64>) -> tensor<8x8xf64> { 102 // Perform dense-dense matrix matrix multiplication. 103 %1 = arith.constant dense<0.0> : tensor<8x8xf64> 104 %2 = linalg.generic #trait_matmul 105 ins(%arga, %argb : tensor<8x8xf64>, tensor<8x8xf64>) 106 outs(%1 : tensor<8x8xf64>) { 107 ^bb0(%a: f64, %b: f64, %x: f64): 108 %p = arith.mulf %a, %b : f64 109 %q = arith.addf %x, %p : f64 110 linalg.yield %q : f64 111 } -> tensor<8x8xf64> 112 // Sample the result with elements-wise multiplication with sparse matrix. 113 %3 = linalg.generic #trait_scale 114 ins(%2, %args : tensor<8x8xf64>, tensor<8x8xf64, #SM>) 115 outs(%1 : tensor<8x8xf64>) { 116 ^bb0(%t: f64, %s: f64, %x: f64): 117 %r = arith.mulf %t, %s : f64 118 linalg.yield %r : f64 119 } -> tensor<8x8xf64> 120 return %3 : tensor<8x8xf64> 121} 122 123// CHECK-LABEL: func.func @sparse_sampled_dd_unfused( 124// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x8xf64, #sparse{{[0-9]*}}>, 125// CHECK-SAME: %[[VAL_1:.*]]: tensor<8x8xf64>, 126// CHECK-SAME: %[[VAL_2:.*]]: tensor<8x8xf64>) -> tensor<8x8xf64, #sparse{{[0-9]*}}> { 127// CHECK-DAG: %[[VAL_3:.*]] = arith.constant 8 : index 128// CHECK-DAG: %[[VAL_4:.*]] = arith.constant 0 : index 129// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 1 : index 130// CHECK-DAG: %[[VAL_6:.*]] = arith.constant false 131// CHECK-DAG: %[[VAL_7:.*]] = arith.constant true 132// CHECK-DAG: %[[VAL_8:.*]] = arith.constant dense<0.000000e+00> : tensor<8x8xf64> 133// CHECK-DAG: %[[VAL_9:.*]] = bufferization.alloc_tensor() copy(%[[VAL_8]]) : tensor<8x8xf64> 134// CHECK-DAG: %[[VAL_10:.*]] = tensor.empty() : tensor<8x8xf64, #sparse{{[0-9]*}}> 135// CHECK-DAG: %[[VAL_11:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<8x8xf64> to memref<8x8xf64> 136// CHECK-DAG: %[[VAL_12:.*]] = bufferization.to_memref %[[VAL_2]] : tensor<8x8xf64> to memref<8x8xf64> 137// CHECK-DAG: %[[VAL_13:.*]] = sparse_tensor.positions %[[VAL_0]] {level = 0 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 138// CHECK-DAG: %[[VAL_14:.*]] = sparse_tensor.coordinates %[[VAL_0]] {level = 0 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 139// CHECK-DAG: %[[VAL_15:.*]] = sparse_tensor.positions %[[VAL_0]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 140// CHECK-DAG: %[[VAL_16:.*]] = sparse_tensor.coordinates %[[VAL_0]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex> 141// CHECK-DAG: %[[VAL_17:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xf64> 142// CHECK: %[[VAL_18:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_4]]] : memref<?xindex> 143// CHECK: %[[VAL_19:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_5]]] : memref<?xindex> 144// CHECK: %[[VAL_20:.*]] = scf.for %[[VAL_21:.*]] = %[[VAL_18]] to %[[VAL_19]] step %[[VAL_5]] iter_args(%[[VAL_22:.*]] = %[[VAL_10]]) -> (tensor<8x8xf64, #sparse{{[0-9]*}}>) { 145// CHECK: %[[VAL_23:.*]] = memref.load %[[VAL_14]]{{\[}}%[[VAL_21]]] : memref<?xindex> 146// CHECK: %[[VAL_24:.*]], %[[VAL_25:.*]], %[[VAL_26:.*]], %[[VAL_27:.*]] = sparse_tensor.expand %[[VAL_10]] : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xf64>, memref<?xi1>, memref<?xindex> 147// CHECK: %[[VAL_28:.*]] = scf.for %[[VAL_29:.*]] = %[[VAL_4]] to %[[VAL_3]] step %[[VAL_5]] iter_args(%[[VAL_30:.*]] = %[[VAL_27]]) -> (index) { 148// CHECK: %[[VAL_31:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_23]], %[[VAL_29]]] : memref<8x8xf64> 149// CHECK: %[[VAL_32:.*]] = memref.load %[[VAL_15]]{{\[}}%[[VAL_21]]] : memref<?xindex> 150// CHECK: %[[VAL_33:.*]] = arith.addi %[[VAL_21]], %[[VAL_5]] : index 151// CHECK: %[[VAL_34:.*]] = memref.load %[[VAL_15]]{{\[}}%[[VAL_33]]] : memref<?xindex> 152// CHECK: %[[VAL_35:.*]] = scf.for %[[VAL_36:.*]] = %[[VAL_32]] to %[[VAL_34]] step %[[VAL_5]] iter_args(%[[VAL_37:.*]] = %[[VAL_30]]) -> (index) { 153// CHECK: %[[VAL_38:.*]] = memref.load %[[VAL_16]]{{\[}}%[[VAL_36]]] : memref<?xindex> 154// CHECK: %[[VAL_39:.*]] = memref.load %[[VAL_24]]{{\[}}%[[VAL_38]]] : memref<?xf64> 155// CHECK: %[[VAL_40:.*]] = memref.load %[[VAL_12]]{{\[}}%[[VAL_29]], %[[VAL_38]]] : memref<8x8xf64> 156// CHECK: %[[VAL_41:.*]] = arith.mulf %[[VAL_31]], %[[VAL_40]] : f64 157// CHECK: %[[VAL_42:.*]] = memref.load %[[VAL_17]]{{\[}}%[[VAL_36]]] : memref<?xf64> 158// CHECK: %[[VAL_43:.*]] = arith.mulf %[[VAL_41]], %[[VAL_42]] : f64 159// CHECK: %[[VAL_44:.*]] = arith.addf %[[VAL_39]], %[[VAL_43]] : f64 160// CHECK: %[[VAL_45:.*]] = memref.load %[[VAL_25]]{{\[}}%[[VAL_38]]] : memref<?xi1> 161// CHECK: %[[VAL_46:.*]] = arith.cmpi eq, %[[VAL_45]], %[[VAL_6]] : i1 162// CHECK: %[[VAL_47:.*]] = scf.if %[[VAL_46]] -> (index) { 163// CHECK: memref.store %[[VAL_7]], %[[VAL_25]]{{\[}}%[[VAL_38]]] : memref<?xi1> 164// CHECK: memref.store %[[VAL_38]], %[[VAL_26]]{{\[}}%[[VAL_37]]] : memref<?xindex> 165// CHECK: %[[VAL_48:.*]] = arith.addi %[[VAL_37]], %[[VAL_5]] : index 166// CHECK: scf.yield %[[VAL_48]] : index 167// CHECK: } else { 168// CHECK: scf.yield %[[VAL_37]] : index 169// CHECK: } 170// CHECK: memref.store %[[VAL_44]], %[[VAL_24]]{{\[}}%[[VAL_38]]] : memref<?xf64> 171// CHECK: scf.yield %[[VAL_47]] : index 172// CHECK: } 173// CHECK: scf.yield %[[VAL_35]] : index 174// CHECK: } 175// CHECK: %[[VAL_49:.*]] = sparse_tensor.compress %[[VAL_24]], %[[VAL_25]], %[[VAL_26]], %[[VAL_28]] into %[[VAL_22]]{{\[}}%[[VAL_23]]] : memref<?xf64>, memref<?xi1>, memref<?xindex>, tensor<8x8xf64, #sparse{{[0-9]*}}> 176// CHECK: scf.yield %[[VAL_49]] : tensor<8x8xf64, #sparse{{[0-9]*}}> 177// CHECK: } 178// CHECK: %[[VAL_50:.*]] = sparse_tensor.load %[[VAL_20]] hasInserts : tensor<8x8xf64, #sparse{{[0-9]*}}> 179// CHECK: return %[[VAL_50]] : tensor<8x8xf64, #sparse{{[0-9]*}}> 180// CHECK: } 181func.func @sparse_sampled_dd_unfused(%args: tensor<8x8xf64, #SM>, 182 %arga: tensor<8x8xf64>, 183 %argb: tensor<8x8xf64>) -> tensor<8x8xf64, #SM> { 184 // Perform dense-dense matrix matrix multiplication. 185 %1 = arith.constant dense<0.0> : tensor<8x8xf64> 186 %2 = linalg.generic #trait_matmul 187 ins(%arga, %argb : tensor<8x8xf64>, tensor<8x8xf64>) 188 outs(%1 : tensor<8x8xf64>) { 189 ^bb0(%a: f64, %b: f64, %x: f64): 190 %p = arith.mulf %a, %b : f64 191 %q = arith.addf %x, %p : f64 192 linalg.yield %q : f64 193 } -> tensor<8x8xf64> 194 // Sample the result with elements-wise multiplication with sparse matrix. 195 %3 = tensor.empty() : tensor<8x8xf64, #SM> 196 %4 = linalg.generic #trait_scale 197 ins(%2, %args : tensor<8x8xf64>, tensor<8x8xf64, #SM>) 198 outs(%3 : tensor<8x8xf64, #SM>) { 199 ^bb0(%t: f64, %s: f64, %x: f64): 200 %r = arith.mulf %t, %s : f64 201 linalg.yield %r : f64 202 } -> tensor<8x8xf64, #SM> 203 return %4 : tensor<8x8xf64, #SM> 204} 205