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  pointerBitWidth = 8,
18  indexBitWidth = 8
19}>
20
21#matvec = {
22  indexing_maps = [
23    affine_map<(i,j) -> (i,j)>, // A
24    affine_map<(i,j) -> (j)>,   // b
25    affine_map<(i,j) -> (i)>    // x (out)
26  ],
27  iterator_types = ["parallel", "reduction"],
28  doc = "X(i) += A(i,j) * B(j)"
29}
30
31//
32// Integration test that lowers a kernel annotated as sparse to
33// actual sparse code, initializes a matching sparse storage scheme
34// from file, and runs the resulting code with the JIT compiler.
35//
36module {
37  //
38  // A kernel that multiplies a sparse matrix A with a dense vector b
39  // into a dense vector x.
40  //
41  func @kernel_matvec(%arga: tensor<?x?xi32, #SparseMatrix>,
42                      %argb: tensor<?xi32>,
43                      %argx: tensor<?xi32>) -> tensor<?xi32> {
44    %0 = linalg.generic #matvec
45      ins(%arga, %argb: tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>)
46      outs(%argx: tensor<?xi32>) {
47      ^bb(%a: i32, %b: i32, %x: i32):
48        %0 = muli %a, %b : i32
49        %1 = addi %x, %0 : i32
50        linalg.yield %1 : i32
51    } -> tensor<?xi32>
52    return %0 : tensor<?xi32>
53  }
54
55  func private @getTensorFilename(index) -> (!Filename)
56
57  //
58  // Main driver that reads matrix from file and calls the sparse kernel.
59  //
60  func @entry() {
61    %i0 = constant 0 : i32
62    %c0 = constant 0 : index
63    %c1 = constant 1 : index
64    %c4 = constant 4 : index
65    %c256 = constant 256 : index
66
67    // Read the sparse matrix from file, construct sparse storage.
68    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
69    %a = sparse_tensor.new %fileName : !llvm.ptr<i8> to tensor<?x?xi32, #SparseMatrix>
70
71    // Initialize dense vectors.
72    %bdata = memref.alloc(%c256) : memref<?xi32>
73    %xdata = memref.alloc(%c4) : memref<?xi32>
74    scf.for %i = %c0 to %c256 step %c1 {
75      %k = addi %i, %c1 : index
76      %j = index_cast %k : index to i32
77      memref.store %j, %bdata[%i] : memref<?xi32>
78    }
79    scf.for %i = %c0 to %c4 step %c1 {
80      memref.store %i0, %xdata[%i] : memref<?xi32>
81    }
82    %b = memref.tensor_load %bdata : memref<?xi32>
83    %x = memref.tensor_load %xdata : memref<?xi32>
84
85    // Call kernel.
86    %0 = call @kernel_matvec(%a, %b, %x)
87      : (tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>, tensor<?xi32>) -> tensor<?xi32>
88
89    // Print the result for verification.
90    //
91    // CHECK: ( 889, 1514, -21, -3431 )
92    //
93    %m = memref.buffer_cast %0 : memref<?xi32>
94    %v = vector.transfer_read %m[%c0], %i0: memref<?xi32>, vector<4xi32>
95    vector.print %v : vector<4xi32>
96
97    // Release the resources.
98    memref.dealloc %bdata : memref<?xi32>
99    memref.dealloc %xdata : memref<?xi32>
100
101    return
102  }
103}
104