1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \ 3// RUN: mlir-cpu-runner \ 4// RUN: -e entry -entry-point-result=void \ 5// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 6// RUN: FileCheck %s 7 8!Filename = type !llvm.ptr<i8> 9 10#DenseMatrix = #sparse_tensor.encoding<{ 11 dimLevelType = [ "dense", "dense" ], 12 dimOrdering = affine_map<(i,j) -> (i,j)> 13}> 14 15#SparseMatrix = #sparse_tensor.encoding<{ 16 dimLevelType = [ "dense", "compressed" ], 17 dimOrdering = affine_map<(i,j) -> (i,j)> 18}> 19 20#trait_assign = { 21 indexing_maps = [ 22 affine_map<(i,j) -> (i,j)>, // A 23 affine_map<(i,j) -> (i,j)> // X (out) 24 ], 25 iterator_types = ["parallel", "parallel"], 26 doc = "X(i,j) = A(i,j)" 27} 28 29// 30// Integration test that demonstrates assigning a sparse tensor 31// to an all-dense annotated "sparse" tensor, which effectively 32// result in inserting the nonzero elements into a linearized array. 33// 34// Note that there is a subtle difference between a non-annotated 35// tensor and an all-dense annotated tensor. Both tensors are assumed 36// dense, but the former remains an n-dimensional memref whereas the 37// latter is linearized into a one-dimensional memref that is further 38// lowered into a storage scheme that is backed by the runtime support 39// library. 40module { 41 // 42 // A kernel that assigns elements from A to X. 43 // 44 func.func @dense_output(%arga: tensor<?x?xf64, #SparseMatrix>) -> tensor<?x?xf64, #DenseMatrix> { 45 %c0 = arith.constant 0 : index 46 %c1 = arith.constant 1 : index 47 %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #SparseMatrix> 48 %d1 = tensor.dim %arga, %c1 : tensor<?x?xf64, #SparseMatrix> 49 %init = sparse_tensor.init [%d0, %d1] : tensor<?x?xf64, #DenseMatrix> 50 %0 = linalg.generic #trait_assign 51 ins(%arga: tensor<?x?xf64, #SparseMatrix>) 52 outs(%init: tensor<?x?xf64, #DenseMatrix>) { 53 ^bb(%a: f64, %x: f64): 54 linalg.yield %a : f64 55 } -> tensor<?x?xf64, #DenseMatrix> 56 return %0 : tensor<?x?xf64, #DenseMatrix> 57 } 58 59 func.func private @getTensorFilename(index) -> (!Filename) 60 61 // 62 // Main driver that reads matrix from file and calls the kernel. 63 // 64 func.func @entry() { 65 %d0 = arith.constant 0.0 : f64 66 %c0 = arith.constant 0 : index 67 %c1 = arith.constant 1 : index 68 69 // Read the sparse matrix from file, construct sparse storage. 70 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 71 %a = sparse_tensor.new %fileName 72 : !Filename to tensor<?x?xf64, #SparseMatrix> 73 74 // Call the kernel. 75 %0 = call @dense_output(%a) 76 : (tensor<?x?xf64, #SparseMatrix>) -> tensor<?x?xf64, #DenseMatrix> 77 78 // 79 // Print the linearized 5x5 result for verification. 80 // 81 // CHECK: ( 1, 0, 0, 1.4, 0, 0, 2, 0, 0, 2.5, 0, 0, 3, 0, 0, 4.1, 0, 0, 4, 0, 0, 5.2, 0, 0, 5 ) 82 // 83 %m = sparse_tensor.values %0 84 : tensor<?x?xf64, #DenseMatrix> to memref<?xf64> 85 %v = vector.load %m[%c0] : memref<?xf64>, vector<25xf64> 86 vector.print %v : vector<25xf64> 87 88 // Release the resources. 89 sparse_tensor.release %a : tensor<?x?xf64, #SparseMatrix> 90 sparse_tensor.release %0 : tensor<?x?xf64, #DenseMatrix> 91 92 return 93 } 94} 95