xref: /llvm-project/mlir/test/Dialect/SparseTensor/sparse_parallel_reduce.mlir (revision ced2fc7819d5ddea616ec330f18e08ff284c1868)
1// RUN: mlir-opt %s --sparse-reinterpret-map -sparsification="parallelization-strategy=any-storage-any-loop" | \
2// RUN:   FileCheck %s
3
4#CSR = #sparse_tensor.encoding<{
5  map = (d0, d1) -> (d0 : dense, d1 : compressed)
6}>
7
8#trait_matvec = {
9  indexing_maps = [
10    affine_map<(i,j) -> (i,j)>,  // A
11    affine_map<(i,j) -> (j)>,    // b
12    affine_map<(i,j) -> (i)>     // x (out)
13  ],
14  iterator_types = ["parallel", "reduction"],
15  doc = "x(i) += A(i,j) * b(j)"
16}
17// CHECK-LABEL:  func.func @matvec(
18//  CHECK-SAME:    %[[TMP_arg0:.*]]: tensor<16x32xf32, #sparse{{[0-9]*}}>,
19//  CHECK-SAME:    %[[TMP_arg1:.*]]: tensor<32xf32>,
20//  CHECK-SAME:    %[[TMP_arg2:.*]]: tensor<16xf32>) -> tensor<16xf32> {
21//   CHECK-DAG:  %[[TMP_c16:.*]] = arith.constant 16 : index
22//   CHECK-DAG:  %[[TMP_c0:.*]] = arith.constant 0 : index
23//   CHECK-DAG:  %[[TMP_c1:.*]] = arith.constant 1 : index
24//   CHECK-DAG:  %[[TMP_0:.*]] = sparse_tensor.positions %[[TMP_arg0]] {level = 1 : index}
25//   CHECK-DAG:  %[[TMP_1:.*]] = sparse_tensor.coordinates %[[TMP_arg0]] {level = 1 : index}
26//   CHECK-DAG:  %[[TMP_2:.*]] = sparse_tensor.values %[[TMP_arg0]]
27//   CHECK-DAG:  %[[TMP_3:.*]] = bufferization.to_memref %[[TMP_arg1]] : tensor<32xf32> to memref<32xf32>
28//   CHECK-DAG:  %[[TMP_4:.*]] = bufferization.to_memref %[[TMP_arg2]] : tensor<16xf32> to memref<16xf32>
29//       CHECK:  scf.parallel (%[[TMP_arg3:.*]]) = (%[[TMP_c0]]) to (%[[TMP_c16]]) step (%[[TMP_c1]]) {
30//       CHECK:    %[[TMP_6:.*]] = memref.load %[[TMP_4]][%[[TMP_arg3]]] : memref<16xf32>
31//       CHECK:    %[[TMP_7:.*]] = memref.load %[[TMP_0]][%[[TMP_arg3]]] : memref<?xindex>
32//       CHECK:    %[[TMP_8:.*]] = arith.addi %[[TMP_arg3]], %[[TMP_c1]] : index
33//       CHECK:    %[[TMP_9:.*]] = memref.load %[[TMP_0]][%[[TMP_8]]] : memref<?xindex>
34//       CHECK:    %[[TMP_10:.*]] = scf.parallel (%[[TMP_arg4:.*]]) = (%[[TMP_7]]) to (%[[TMP_9]]) step (%[[TMP_c1]]) init (%[[TMP_6]]) -> f32 {
35//       CHECK:      %[[TMP_11:.*]] = memref.load %[[TMP_1]][%[[TMP_arg4]]] : memref<?xindex>
36//       CHECK:      %[[TMP_12:.*]] = memref.load %[[TMP_2]][%[[TMP_arg4]]] : memref<?xf32>
37//       CHECK:      %[[TMP_13:.*]] = memref.load %[[TMP_3]][%[[TMP_11]]] : memref<32xf32>
38//       CHECK:      %[[TMP_14:.*]] = arith.mulf %[[TMP_12]], %[[TMP_13]] : f32
39//       CHECK:      scf.reduce(%[[TMP_14]]  : f32) {
40//       CHECK:      ^bb0(%[[TMP_arg5:.*]]: f32, %[[TMP_arg6:.*]]: f32):
41//       CHECK:        %[[TMP_15:.*]] = arith.addf %[[TMP_arg5]], %[[TMP_arg6]] : f32
42//       CHECK:        scf.reduce.return %[[TMP_15]] : f32
43//       CHECK:      }
44//       CHECK:    }
45//       CHECK:    memref.store %[[TMP_10]], %[[TMP_4]][%[[TMP_arg3]]] : memref<16xf32>
46//       CHECK:    scf.reduce
47//       CHECK:  }
48//       CHECK:  %[[TMP_5:.*]] = bufferization.to_tensor %[[TMP_4]] : memref<16xf32>
49//       CHECK:  return %[[TMP_5]] : tensor<16xf32>
50func.func @matvec(%arga: tensor<16x32xf32, #CSR>,
51                  %argb: tensor<32xf32>,
52	          %argx: tensor<16xf32>) -> tensor<16xf32> {
53  %0 = linalg.generic #trait_matvec
54      ins(%arga, %argb : tensor<16x32xf32, #CSR>, tensor<32xf32>)
55     outs(%argx: tensor<16xf32>) {
56    ^bb(%A: f32, %b: f32, %x: f32):
57      %0 = arith.mulf %A, %b : f32
58      %1 = arith.addf %0, %x : f32
59      linalg.yield %1 : f32
60  } -> tensor<16xf32>
61  return %0 : tensor<16xf32>
62}
63