xref: /llvm-project/mlir/test/Dialect/SparseTensor/spy_sddmm.mlir (revision ced2fc7819d5ddea616ec330f18e08ff284c1868)
1// RUN: mlir-opt %s --sparse-reinterpret-map -sparsification | FileCheck %s
2
3//
4// A SDDMM implementation with "spy" function and
5// in-place update of the sampling sparse matrix.
6//
7
8#SM = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }>
9
10#trait_sampled_dense_dense = {
11  indexing_maps = [
12    affine_map<(i,j,k) -> (i,k)>,  // A
13    affine_map<(i,j,k) -> (k,j)>,  // B
14    affine_map<(i,j,k) -> (i,j)>   // S
15  ],
16  iterator_types = ["parallel", "parallel", "reduction"],
17  doc = "S(i,j) += spy[S(i,j)] x SUM_k A(i,k) B(k,j)"
18}
19
20// CHECK-LABEL: func.func @sparse_sampled_dd(
21// CHECK-SAME:    %[[VAL_0:.*0]]: tensor<8x8xf64>,
22// CHECK-SAME:    %[[VAL_1:.*1]]: tensor<8x8xf64>,
23// CHECK-SAME:    %[[VAL_2:.*2]]: tensor<8x8xf64, #sparse{{[0-9]*}}>) -> tensor<8x8xf64, #sparse{{[0-9]*}}> {
24// CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 8 : index
25// CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 0 : index
26// CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 1 : index
27// CHECK-DAG:     %[[VAL_6:.*]] = bufferization.to_memref %[[VAL_0]] : tensor<8x8xf64> to memref<8x8xf64>
28// CHECK-DAG:     %[[VAL_7:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<8x8xf64> to memref<8x8xf64>
29// CHECK-DAG:     %[[VAL_8:.*]] = sparse_tensor.positions %[[VAL_2]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex>
30// CHECK-DAG:     %[[VAL_9:.*]] = sparse_tensor.coordinates %[[VAL_2]] {level = 1 : index} : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xindex>
31// CHECK-DAG:     %[[VAL_10:.*]] = sparse_tensor.values %[[VAL_2]] : tensor<8x8xf64, #sparse{{[0-9]*}}> to memref<?xf64>
32// CHECK:         scf.for %[[VAL_11:.*]] = %[[VAL_4]] to %[[VAL_3]] step %[[VAL_5]] {
33// CHECK:           scf.for %[[VAL_12:.*]] = %[[VAL_4]] to %[[VAL_3]] step %[[VAL_5]] {
34// CHECK:             %[[VAL_13:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_11]]] : memref<?xindex>
35// CHECK:             %[[VAL_14:.*]] = arith.addi %[[VAL_11]], %[[VAL_5]] : index
36// CHECK:             %[[VAL_15:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_14]]] : memref<?xindex>
37// CHECK:             scf.for %[[VAL_16:.*]] = %[[VAL_13]] to %[[VAL_15]] step %[[VAL_5]] {
38// CHECK:               %[[VAL_17:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_16]]] : memref<?xindex>
39// CHECK:               %[[VAL_18:.*]] = memref.load %[[VAL_10]]{{\[}}%[[VAL_16]]] : memref<?xf64>
40// CHECK:               %[[VAL_19:.*]] = memref.load %[[VAL_6]]{{\[}}%[[VAL_11]], %[[VAL_12]]] : memref<8x8xf64>
41// CHECK:               %[[VAL_20:.*]] = memref.load %[[VAL_7]]{{\[}}%[[VAL_12]], %[[VAL_17]]] : memref<8x8xf64>
42// CHECK:               %[[VAL_21:.*]] = arith.mulf %[[VAL_19]], %[[VAL_20]] : f64
43// CHECK:               %[[VAL_22:.*]] = arith.addf %[[VAL_18]], %[[VAL_21]] : f64
44// CHECK:               memref.store %[[VAL_22]], %[[VAL_10]]{{\[}}%[[VAL_16]]] : memref<?xf64>
45// CHECK:             } {"Emitted from" = "linalg.generic"}
46// CHECK:           } {"Emitted from" = "linalg.generic"}
47// CHECK:         } {"Emitted from" = "linalg.generic"}
48// CHECK:         %[[VAL_23:.*]] = sparse_tensor.load %[[VAL_2]] : tensor<8x8xf64, #sparse{{[0-9]*}}>
49// CHECK:           return %[[VAL_23]] : tensor<8x8xf64, #sparse{{[0-9]*}}>
50// CHECK:       }
51func.func @sparse_sampled_dd(%argA: tensor<8x8xf64>,
52                             %argB: tensor<8x8xf64>,
53                             %argS: tensor<8x8xf64, #SM>) -> tensor<8x8xf64, #SM> {
54  %f0 = arith.constant 0.0 : f64
55  %result = linalg.generic #trait_sampled_dense_dense
56    ins(%argA, %argB: tensor<8x8xf64>, tensor<8x8xf64>) outs(%argS: tensor<8x8xf64, #SM>) {
57      ^bb(%a: f64, %b: f64, %s: f64):
58         %u = sparse_tensor.unary %s : f64 to f64
59             present={
60                ^bb0(%p: f64):
61                  %mul = arith.mulf %a, %b : f64
62                  sparse_tensor.yield %mul : f64
63             }
64             absent={}
65         %r = sparse_tensor.reduce %s, %u, %f0 : f64 {
66              ^bb0(%p: f64, %q: f64):
67                %add = arith.addf %p, %q : f64
68                sparse_tensor.yield %add : f64
69            }
70         linalg.yield %r : f64
71  } -> tensor<8x8xf64, #SM>
72  return %result : tensor<8x8xf64, #SM>
73}
74