1// RUN: mlir-opt %s \
2// RUN:   --sparsification --sparse-tensor-conversion \
3// RUN:   --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-memref-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// Do the same run, but now with SIMDization as well. This should not change the outcome.
14//
15// RUN: mlir-opt %s \
16// RUN:   --sparsification="vectorization-strategy=2 vl=16 enable-simd-index32" --sparse-tensor-conversion \
17// RUN:   --convert-vector-to-scf --convert-scf-to-std \
18// RUN:   --func-bufferize --tensor-constant-bufferize --tensor-bufferize \
19// RUN:   --std-bufferize --finalizing-bufferize --lower-affine \
20// RUN:   --convert-vector-to-llvm --convert-memref-to-llvm --convert-std-to-llvm | \
21// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \
22// RUN: mlir-cpu-runner \
23// RUN:  -e entry -entry-point-result=void  \
24// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
25// RUN: FileCheck %s
26
27!Filename = type !llvm.ptr<i8>
28
29#SparseMatrix = #sparse_tensor.encoding<{
30  dimLevelType = [ "dense", "compressed" ],
31  pointerBitWidth = 8,
32  indexBitWidth = 8
33}>
34
35#matvec = {
36  indexing_maps = [
37    affine_map<(i,j) -> (i,j)>, // A
38    affine_map<(i,j) -> (j)>,   // b
39    affine_map<(i,j) -> (i)>    // x (out)
40  ],
41  iterator_types = ["parallel", "reduction"],
42  doc = "X(i) += A(i,j) * B(j)"
43}
44
45//
46// Integration test that lowers a kernel annotated as sparse to
47// actual sparse code, initializes a matching sparse storage scheme
48// from file, and runs the resulting code with the JIT compiler.
49//
50module {
51  //
52  // A kernel that multiplies a sparse matrix A with a dense vector b
53  // into a dense vector x.
54  //
55  func @kernel_matvec(%arga: tensor<?x?xi32, #SparseMatrix>,
56                      %argb: tensor<?xi32>,
57                      %argx: tensor<?xi32>) -> tensor<?xi32> {
58    %0 = linalg.generic #matvec
59      ins(%arga, %argb: tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>)
60      outs(%argx: tensor<?xi32>) {
61      ^bb(%a: i32, %b: i32, %x: i32):
62        %0 = muli %a, %b : i32
63        %1 = addi %x, %0 : i32
64        linalg.yield %1 : i32
65    } -> tensor<?xi32>
66    return %0 : tensor<?xi32>
67  }
68
69  func private @getTensorFilename(index) -> (!Filename)
70
71  //
72  // Main driver that reads matrix from file and calls the sparse kernel.
73  //
74  func @entry() {
75    %i0 = constant 0 : i32
76    %c0 = constant 0 : index
77    %c1 = constant 1 : index
78    %c4 = constant 4 : index
79    %c256 = constant 256 : index
80
81    // Read the sparse matrix from file, construct sparse storage.
82    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
83    %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xi32, #SparseMatrix>
84
85    // Initialize dense vectors.
86    %bdata = memref.alloc(%c256) : memref<?xi32>
87    %xdata = memref.alloc(%c4) : memref<?xi32>
88    scf.for %i = %c0 to %c256 step %c1 {
89      %k = addi %i, %c1 : index
90      %j = index_cast %k : index to i32
91      memref.store %j, %bdata[%i] : memref<?xi32>
92    }
93    scf.for %i = %c0 to %c4 step %c1 {
94      memref.store %i0, %xdata[%i] : memref<?xi32>
95    }
96    %b = memref.tensor_load %bdata : memref<?xi32>
97    %x = memref.tensor_load %xdata : memref<?xi32>
98
99    // Call kernel.
100    %0 = call @kernel_matvec(%a, %b, %x)
101      : (tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>, tensor<?xi32>) -> tensor<?xi32>
102
103    // Print the result for verification.
104    //
105    // CHECK: ( 889, 1514, -21, -3431 )
106    //
107    %m = memref.buffer_cast %0 : memref<?xi32>
108    %v = vector.transfer_read %m[%c0], %i0: memref<?xi32>, vector<4xi32>
109    vector.print %v : vector<4xi32>
110
111    // Release the resources.
112    memref.dealloc %bdata : memref<?xi32>
113    memref.dealloc %xdata : memref<?xi32>
114
115    return
116  }
117}
118