1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: mlir-cpu-runner \
3// RUN:  -e entry -entry-point-result=void  \
4// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
5// RUN: FileCheck %s
6
7#DCSR = #sparse_tensor.encoding<{
8  dimLevelType = [ "compressed", "compressed" ]
9}>
10
11#trait_mult_elt = {
12  indexing_maps = [
13    affine_map<(i,j) -> (i,j)>,  // A
14    affine_map<(i,j) -> (i,j)>,  // B
15    affine_map<(i,j) -> (i,j)>   // X (out)
16  ],
17  iterator_types = ["parallel", "parallel"],
18  doc = "X(i,j) = A(i,j) * B(i,j)"
19}
20
21module {
22  // Sparse kernel.
23  func.func @sparse_mult_elt(
24      %arga: tensor<32x16xf32, #DCSR>, %argb: tensor<32x16xf32, #DCSR>) -> tensor<32x16xf32, #DCSR> {
25    %argx = bufferization.alloc_tensor() : tensor<32x16xf32, #DCSR>
26    %0 = linalg.generic #trait_mult_elt
27      ins(%arga, %argb: tensor<32x16xf32, #DCSR>, tensor<32x16xf32, #DCSR>)
28      outs(%argx: tensor<32x16xf32, #DCSR>) {
29        ^bb(%a: f32, %b: f32, %x: f32):
30          %1 = arith.mulf %a, %b : f32
31          linalg.yield %1 : f32
32    } -> tensor<32x16xf32, #DCSR>
33    return %0 : tensor<32x16xf32, #DCSR>
34  }
35
36  // Driver method to call and verify kernel.
37  func.func @entry() {
38    %c0 = arith.constant 0 : index
39    %f1 = arith.constant -1.0 : f32
40
41    // Setup very sparse matrices.
42    %ta = arith.constant sparse<
43       [ [2,2], [15,15], [31,0], [31,14] ], [ 2.0, 3.0, -2.0, 4.0 ]
44    > : tensor<32x16xf32>
45    %tb = arith.constant sparse<
46       [ [1,1], [2,0], [2,2], [2,15], [31,0], [31,15] ], [ 5.0, 6.0, 7.0, 8.0, -10.0, 9.0 ]
47    > : tensor<32x16xf32>
48    %sta = sparse_tensor.convert %ta
49      : tensor<32x16xf32> to tensor<32x16xf32, #DCSR>
50    %stb = sparse_tensor.convert %tb
51      : tensor<32x16xf32> to tensor<32x16xf32, #DCSR>
52
53    // Call kernel.
54    %0 = call @sparse_mult_elt(%sta, %stb)
55      : (tensor<32x16xf32, #DCSR>,
56         tensor<32x16xf32, #DCSR>) -> tensor<32x16xf32, #DCSR>
57
58    //
59    // Verify results. Only two entries stored in result!
60    //
61    // CHECK: ( 14, 20, -1, -1 )
62    //
63    %val = sparse_tensor.values %0 : tensor<32x16xf32, #DCSR> to memref<?xf32>
64    %vv = vector.transfer_read %val[%c0], %f1: memref<?xf32>, vector<4xf32>
65    vector.print %vv : vector<4xf32>
66
67    // Release the resources.
68    bufferization.dealloc_tensor %sta : tensor<32x16xf32, #DCSR>
69    bufferization.dealloc_tensor %stb : tensor<32x16xf32, #DCSR>
70    bufferization.dealloc_tensor %0   : tensor<32x16xf32, #DCSR>
71    return
72  }
73}
74