1// RUN: mlir-opt %s --linalg-fuse-elementwise-ops --sparse-reinterpret-map --sparsification | FileCheck %s 2 3#SV = #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed) }> 4 5#trait = { 6 indexing_maps = [ 7 affine_map<(i) -> (i)>, // A 8 affine_map<(i) -> (i)> // B (out) 9 ], 10 iterator_types = ["parallel"], 11 doc = "B(i) = OP A(i)" 12} 13 14 15// CHECK-LABEL: func.func @sparse_fusion( 16// CHECK-SAME: %[[VAL_0:.*]]: tensor<100xf64, #sparse>) -> tensor<100xf64> { 17// CHECK-DAG: %[[VAL_1:.*]] = arith.constant true 18// CHECK-DAG: %[[VAL_2:.*]] = arith.constant 1 : index 19// CHECK-DAG: %[[VAL_3:.*]] = arith.constant 0 : index 20// CHECK-DAG: %[[VAL_4:.*]] = arith.constant 0.000000e+00 : f64 21// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 100 : index 22// CHECK-DAG: %[[VAL_6:.*]] = arith.constant 1.000000e+00 : f64 23// CHECK-DAG: %[[VAL_7:.*]] = arith.constant 1.000000e+02 : f64 24// CHECK-DAG: %[[VAL_8:.*]] = tensor.empty() : tensor<100xf64> 25// CHECK-DAG: %[[VAL_9:.*]] = sparse_tensor.positions %[[VAL_0]] {level = 0 : index} : tensor<100xf64, #sparse> to memref<?xindex> 26// CHECK-DAG: %[[VAL_10:.*]] = sparse_tensor.coordinates %[[VAL_0]] {level = 0 : index} : tensor<100xf64, #sparse> to memref<?xindex> 27// CHECK-DAG: %[[VAL_11:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<100xf64, #sparse> to memref<?xf64> 28// CHECK-DAG: %[[VAL_12:.*]] = bufferization.to_memref %[[VAL_8]] : 29// CHECK-DAG: linalg.fill ins(%[[VAL_4]] : f64) outs(%[[VAL_12]] : memref<100xf64>) 30// CHECK-DAG: %[[VAL_13:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_3]]] : memref<?xindex> 31// CHECK-DAG: %[[VAL_14:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_2]]] : memref<?xindex> 32// CHECK: %[[VAL_15:.*]]:2 = scf.while (%[[VAL_16:.*]] = %[[VAL_13]], %[[VAL_17:.*]] = %[[VAL_3]]) : (index, index) -> (index, index) { 33// CHECK: %[[VAL_18:.*]] = arith.cmpi ult, %[[VAL_16]], %[[VAL_14]] : index 34// CHECK: scf.condition(%[[VAL_18]]) %[[VAL_16]], %[[VAL_17]] : index, index 35// CHECK: } do { 36// CHECK: ^bb0(%[[VAL_19:.*]]: index, %[[VAL_20:.*]]: index): 37// CHECK: %[[VAL_21:.*]] = memref.load %[[VAL_10]]{{\[}}%[[VAL_19]]] : memref<?xindex> 38// CHECK: %[[VAL_22:.*]] = arith.cmpi eq, %[[VAL_21]], %[[VAL_20]] : index 39// CHECK: scf.if %[[VAL_22]] { 40// CHECK: %[[VAL_23:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_19]]] : memref<?xf64> 41// CHECK: %[[VAL_24:.*]] = arith.addf %[[VAL_23]], %[[VAL_6]] : f64 42// CHECK: %[[VAL_25:.*]] = math.exp %[[VAL_24]] : f64 43// CHECK: %[[VAL_26:.*]] = arith.maximumf %[[VAL_25]], %[[VAL_7]] : f64 44// CHECK: memref.store %[[VAL_26]], %[[VAL_12]]{{\[}}%[[VAL_20]]] : memref<100xf64> 45// CHECK: } else { 46// CHECK: scf.if %[[VAL_1]] { 47// CHECK: memref.store %[[VAL_7]], %[[VAL_12]]{{\[}}%[[VAL_20]]] : memref<100xf64> 48// CHECK: } else { 49// CHECK: } 50// CHECK: } 51// CHECK: %[[VAL_27:.*]] = arith.cmpi eq, %[[VAL_21]], %[[VAL_20]] : index 52// CHECK: %[[VAL_28:.*]] = arith.addi %[[VAL_19]], %[[VAL_2]] : index 53// CHECK: %[[VAL_29:.*]] = arith.select %[[VAL_27]], %[[VAL_28]], %[[VAL_19]] : index 54// CHECK: %[[VAL_30:.*]] = arith.addi %[[VAL_20]], %[[VAL_2]] : index 55// CHECK: scf.yield %[[VAL_29]], %[[VAL_30]] : index, index 56// CHECK: } 57// CHECK: scf.for %[[VAL_31:.*]] = %[[VAL_32:.*]]#1 to %[[VAL_5]] step %[[VAL_2]] { 58// CHECK: memref.store %[[VAL_7]], %[[VAL_12]]{{\[}}%[[VAL_31]]] : memref<100xf64> 59// CHECK: } 60// CHECK: %[[VAL_33:.*]] = bufferization.to_tensor %[[VAL_12]] : 61// CHECK: return %[[VAL_33]] : tensor<100xf64> 62// CHECK: } 63func.func @sparse_fusion(%argA: tensor<100xf64, #SV>) -> tensor<100xf64> { 64 %c1 = arith.constant 1.0 : f64 65 %c100 = arith.constant 100.0 : f64 66 67 %t0 = tensor.empty() : tensor<100xf64> 68 %l0 = linalg.generic #trait 69 ins(%argA: tensor<100xf64, #SV>) outs(%t0: tensor<100xf64>) { 70 ^bb0(%in0: f64, %out0: f64): 71 %b0 = arith.addf %in0, %c1 : f64 72 linalg.yield %b0 : f64 73 } -> tensor<100xf64> 74 %t1 = tensor.empty() : tensor<100xf64> 75 %l1 = linalg.generic #trait 76 ins(%l0: tensor<100xf64>) outs(%t1: tensor<100xf64>) { 77 ^bb0(%in1: f64, %out1: f64): 78 %b1 = math.exp %in1 : f64 79 linalg.yield %b1 : f64 80 } -> tensor<100xf64> 81 %t2 = tensor.empty() : tensor<100xf64> 82 %l2 = linalg.generic #trait 83 ins(%l1: tensor<100xf64>) outs(%t2: tensor<100xf64>) { 84 ^bb0(%in2: f64, %out2: f64): 85 %b2 = arith.maximumf %in2, %c100 : f64 86 linalg.yield %b2 : f64 87 } -> tensor<100xf64> 88 89 return %l2 : tensor<100xf64> 90} 91