xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_spmm.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
1//--------------------------------------------------------------------------------------------------
2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS.
3//
4// Set-up that's shared across all tests in this directory. In principle, this
5// config could be moved to lit.local.cfg. However, there are downstream users that
6//  do not use these LIT config files. Hence why this is kept inline.
7//
8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true
9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts}
10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"
11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"
12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils
13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils
14// DEFINE: %{run_opts} = -e main -entry-point-result=void
15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs}
16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve}
17//
18// DEFINE: %{env} =
19//--------------------------------------------------------------------------------------------------
20
21// REDEFINE: %{env} = TENSOR0="%mlir_src_dir/test/Integration/data/wide.mtx"
22// RUN: %{compile} | env %{env} %{run} | FileCheck %s
23//
24// Do the same run, but now with direct IR generation.
25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
26// RUN: %{compile} | env %{env} %{run} | FileCheck %s
27//
28// Do the same run, but now with vectorization.
29// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
30// RUN: %{compile} | env %{env} %{run} | FileCheck %s
31//
32// Do the same run, but now with  VLA vectorization.
33// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | env %{env} %{run_sve} | FileCheck %s %}
34
35!Filename = !llvm.ptr
36
37#SparseMatrix = #sparse_tensor.encoding<{
38  map = (d0, d1) -> (d0 : dense, d1 : compressed)
39}>
40
41#spmm = {
42  indexing_maps = [
43    affine_map<(i,j,k) -> (i,k)>, // A
44    affine_map<(i,j,k) -> (k,j)>, // B
45    affine_map<(i,j,k) -> (i,j)>  // X (out)
46  ],
47  iterator_types = ["parallel", "parallel", "reduction"],
48  doc = "X(i,j) += A(i,k) * B(k,j)"
49}
50
51//
52// Integration test that lowers a kernel annotated as sparse to
53// actual sparse code, initializes a matching sparse storage scheme
54// from file, and runs the resulting code with the JIT compiler.
55//
56module {
57  //
58  // A kernel that multiplies a sparse matrix A with a dense matrix B
59  // into a dense matrix X.
60  //
61  func.func @kernel_spmm(%arga: tensor<?x?xf64, #SparseMatrix>,
62                         %argb: tensor<?x?xf64>,
63                         %argx: tensor<?x?xf64>) -> tensor<?x?xf64> {
64    %0 = linalg.generic #spmm
65      ins(%arga, %argb: tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>)
66      outs(%argx: tensor<?x?xf64>) {
67      ^bb(%a: f64, %b: f64, %x: f64):
68        %0 = arith.mulf %a, %b : f64
69        %1 = arith.addf %x, %0 : f64
70        linalg.yield %1 : f64
71    } -> tensor<?x?xf64>
72    return %0 : tensor<?x?xf64>
73  }
74
75  func.func private @getTensorFilename(index) -> (!Filename)
76
77  //
78  // Main driver that reads matrix from file and calls the sparse kernel.
79  //
80  func.func @main() {
81    %i0 = arith.constant 0.0 : f64
82    %c0 = arith.constant 0 : index
83    %c1 = arith.constant 1 : index
84    %c4 = arith.constant 4 : index
85    %c256 = arith.constant 256 : index
86
87    // Read the sparse matrix from file, construct sparse storage.
88    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
89    %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix>
90
91    // Initialize dense tensors.
92    %b = tensor.generate %c256, %c4 {
93    ^bb0(%i : index, %j : index):
94      %k0 = arith.muli %i, %c4 : index
95      %k1 = arith.addi %j, %k0 : index
96      %k2 = arith.index_cast %k1 : index to i32
97      %k = arith.sitofp %k2 : i32 to f64
98      tensor.yield %k : f64
99    } : tensor<?x?xf64>
100
101    %x = tensor.generate %c4, %c4 {
102    ^bb0(%i : index, %j : index):
103      tensor.yield %i0 : f64
104    } : tensor<?x?xf64>
105
106    // Call kernel.
107    %0 = call @kernel_spmm(%a, %b, %x)
108      : (tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64>
109
110    // Print the result for verification.
111    //
112    // CHECK: ( ( 3548, 3550, 3552, 3554 ), ( 6052, 6053, 6054, 6055 ), ( -56, -63, -70, -77 ), ( -13704, -13709, -13714, -13719 ) )
113    //
114    %v = vector.transfer_read %0[%c0, %c0], %i0: tensor<?x?xf64>, vector<4x4xf64>
115    vector.print %v : vector<4x4xf64>
116
117    // Release the resources.
118    bufferization.dealloc_tensor %a : tensor<?x?xf64, #SparseMatrix>
119    bufferization.dealloc_tensor %b : tensor<?x?xf64>
120    bufferization.dealloc_tensor %0 : tensor<?x?xf64>
121
122    return
123  }
124}
125