116 lines · plain
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, this5// config could be moved to lit.local.cfg. However, there are downstream users that6// do not use these LIT config files. Hence why this is kept inline.7//8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true9// 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_utils13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils14// DEFINE: %{run_opts} = -e main -entry-point-result=void15// 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/test_symmetric_complex.mtx"22// RUN: %{compile} | env %{env} %{run} | FileCheck %s23//24// Do the same run, but now with direct IR generation.25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false26// RUN: %{compile} | env %{env} %{run} | FileCheck %s27//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=true30// RUN: %{compile} | env %{env} %{run} | FileCheck %s31//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// TODO: The test currently only operates on the triangular part of the36// symmetric matrix.37 38!Filename = !llvm.ptr39 40#SparseMatrix = #sparse_tensor.encoding<{41 map = (d0, d1) -> (d0 : compressed, d1 : compressed)42}>43 44#trait_sum_reduce = {45 indexing_maps = [46 affine_map<(i,j) -> (i,j)>, // A47 affine_map<(i,j) -> ()> // x (out)48 ],49 iterator_types = ["reduction", "reduction"],50 doc = "x += A(i,j)"51}52 53//54// Integration test that lowers a kernel annotated as sparse to55// actual sparse code, initializes a matching sparse storage scheme56// from file, and runs the resulting code with the JIT compiler.57//58module {59 //60 // A kernel that sum-reduces a matrix to a single scalar.61 //62 func.func @kernel_sum_reduce(%arga: tensor<?x?xcomplex<f64>, #SparseMatrix>,63 %argx: tensor<complex<f64>>) -> tensor<complex<f64>> {64 %0 = linalg.generic #trait_sum_reduce65 ins(%arga: tensor<?x?xcomplex<f64>, #SparseMatrix>)66 outs(%argx: tensor<complex<f64>>) {67 ^bb(%a: complex<f64>, %x: complex<f64>):68 %0 = complex.add %x, %a : complex<f64>69 linalg.yield %0 : complex<f64>70 } -> tensor<complex<f64>>71 return %0 : tensor<complex<f64>>72 }73 74 func.func private @getTensorFilename(index) -> (!Filename)75 76 //77 // Main driver that reads matrix from file and calls the sparse kernel.78 //79 func.func @main() {80 //%d0 = arith.constant 0.0 : complex<f64>81 %d0 = complex.constant [0.0 : f64, 0.0 : f64] : complex<f64>82 %c0 = arith.constant 0 : index83 84 // Setup memory for a single reduction scalar,85 // initialized to zero.86 // TODO: tensor.from_elements does not support complex.87 %alloc = tensor.empty() : tensor<complex<f64>>88 %x = tensor.insert %d0 into %alloc[] : tensor<complex<f64>>89 90 // Read the sparse matrix from file, construct sparse storage.91 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)92 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xcomplex<f64>, #SparseMatrix>93 94 // Call the kernel.95 %0 = call @kernel_sum_reduce(%a, %x)96 : (tensor<?x?xcomplex<f64>, #SparseMatrix>, tensor<complex<f64>>) -> tensor<complex<f64>>97 98 // Print the result for verification.99 //100 // CHECK: 24.1101 // CHECK-NEXT: 16.1102 //103 %v = tensor.extract %0[] : tensor<complex<f64>>104 %real = complex.re %v : complex<f64>105 %imag = complex.im %v : complex<f64>106 vector.print %real : f64107 vector.print %imag : f64108 109 // Release the resources.110 bufferization.dealloc_tensor %0 : tensor<complex<f64>>111 bufferization.dealloc_tensor %a : tensor<?x?xcomplex<f64>, #SparseMatrix>112 113 return114 }115}116