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