1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \
3// RUN: mlir-cpu-runner \
4// RUN:  -e entry -entry-point-result=void  \
5// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
6// RUN: FileCheck %s
7//
8// Do the same run, but now with SIMDization as well. This should not change the outcome.
9//
10// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \
11// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \
12// RUN: mlir-cpu-runner \
13// RUN:  -e entry -entry-point-result=void  \
14// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
15// RUN: FileCheck %s
16
17!Filename = !llvm.ptr<i8>
18
19#SparseMatrix = #sparse_tensor.encoding<{
20  dimLevelType = [ "dense", "compressed" ]
21}>
22
23#spmm = {
24  indexing_maps = [
25    affine_map<(i,j,k) -> (i,k)>, // A
26    affine_map<(i,j,k) -> (k,j)>, // B
27    affine_map<(i,j,k) -> (i,j)>  // X (out)
28  ],
29  iterator_types = ["parallel", "parallel", "reduction"],
30  doc = "X(i,j) += A(i,k) * B(k,j)"
31}
32
33//
34// Integration test that lowers a kernel annotated as sparse to
35// actual sparse code, initializes a matching sparse storage scheme
36// from file, and runs the resulting code with the JIT compiler.
37//
38module {
39  //
40  // A kernel that multiplies a sparse matrix A with a dense matrix B
41  // into a dense matrix X.
42  //
43  func.func @kernel_spmm(%arga: tensor<?x?xf64, #SparseMatrix>,
44                         %argb: tensor<?x?xf64>,
45                         %argx: tensor<?x?xf64>) -> tensor<?x?xf64> {
46    %0 = linalg.generic #spmm
47      ins(%arga, %argb: tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>)
48      outs(%argx: tensor<?x?xf64>) {
49      ^bb(%a: f64, %b: f64, %x: f64):
50        %0 = arith.mulf %a, %b : f64
51        %1 = arith.addf %x, %0 : f64
52        linalg.yield %1 : f64
53    } -> tensor<?x?xf64>
54    return %0 : tensor<?x?xf64>
55  }
56
57  func.func private @getTensorFilename(index) -> (!Filename)
58
59  //
60  // Main driver that reads matrix from file and calls the sparse kernel.
61  //
62  func.func @entry() {
63    %i0 = arith.constant 0.0 : f64
64    %c0 = arith.constant 0 : index
65    %c1 = arith.constant 1 : index
66    %c4 = arith.constant 4 : index
67    %c256 = arith.constant 256 : index
68
69    // Read the sparse matrix from file, construct sparse storage.
70    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
71    %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix>
72
73    // Initialize dense vectors.
74    %init_256_4 = bufferization.alloc_tensor(%c256, %c4) : tensor<?x?xf64>
75    %b = scf.for %i = %c0 to %c256 step %c1 iter_args(%t = %init_256_4) -> tensor<?x?xf64> {
76      %b2 = scf.for %j = %c0 to %c4 step %c1 iter_args(%t2 = %t) -> tensor<?x?xf64> {
77        %k0 = arith.muli %i, %c4 : index
78        %k1 = arith.addi %j, %k0 : index
79        %k2 = arith.index_cast %k1 : index to i32
80        %k = arith.sitofp %k2 : i32 to f64
81        %t3 = tensor.insert %k into %t2[%i, %j] : tensor<?x?xf64>
82        scf.yield %t3 : tensor<?x?xf64>
83      }
84      scf.yield %b2 : tensor<?x?xf64>
85    }
86    %init_4_4 = bufferization.alloc_tensor(%c4, %c4) : tensor<?x?xf64>
87    %x = scf.for %i = %c0 to %c4 step %c1 iter_args(%t = %init_4_4) -> tensor<?x?xf64> {
88      %x2 = scf.for %j = %c0 to %c4 step %c1 iter_args(%t2 = %t) -> tensor<?x?xf64> {
89        %t3 = tensor.insert %i0 into %t2[%i, %j] : tensor<?x?xf64>
90        scf.yield %t3 : tensor<?x?xf64>
91      }
92      scf.yield %x2 : tensor<?x?xf64>
93    }
94
95    // Call kernel.
96    %0 = call @kernel_spmm(%a, %b, %x)
97      : (tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64>
98
99    // Print the result for verification.
100    //
101    // CHECK: ( ( 3548, 3550, 3552, 3554 ), ( 6052, 6053, 6054, 6055 ), ( -56, -63, -70, -77 ), ( -13704, -13709, -13714, -13719 ) )
102    //
103    %v = vector.transfer_read %0[%c0, %c0], %i0: tensor<?x?xf64>, vector<4x4xf64>
104    vector.print %v : vector<4x4xf64>
105
106    // Release the resources.
107    bufferization.dealloc_tensor %a : tensor<?x?xf64, #SparseMatrix>
108
109    return
110  }
111}
112