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 = !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) * 2" 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 multiplied 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 %c2 = arith.constant 2.0 : f64 48 %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #SparseMatrix> 49 %d1 = tensor.dim %arga, %c1 : tensor<?x?xf64, #SparseMatrix> 50 %init = bufferization.alloc_tensor(%d0, %d1) : tensor<?x?xf64, #DenseMatrix> 51 %0 = linalg.generic #trait_assign 52 ins(%arga: tensor<?x?xf64, #SparseMatrix>) 53 outs(%init: tensor<?x?xf64, #DenseMatrix>) { 54 ^bb(%a: f64, %x: f64): 55 %0 = arith.mulf %a, %c2 : f64 56 linalg.yield %0 : f64 57 } -> tensor<?x?xf64, #DenseMatrix> 58 return %0 : tensor<?x?xf64, #DenseMatrix> 59 } 60 61 func.func private @getTensorFilename(index) -> (!Filename) 62 63 // 64 // Main driver that reads matrix from file and calls the kernel. 65 // 66 func.func @entry() { 67 %d0 = arith.constant 0.0 : f64 68 %c0 = arith.constant 0 : index 69 %c1 = arith.constant 1 : index 70 71 // Read the sparse matrix from file, construct sparse storage. 72 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 73 %a = sparse_tensor.new %fileName 74 : !Filename to tensor<?x?xf64, #SparseMatrix> 75 76 // Call the kernel. 77 %0 = call @dense_output(%a) 78 : (tensor<?x?xf64, #SparseMatrix>) -> tensor<?x?xf64, #DenseMatrix> 79 80 // 81 // Print the linearized 5x5 result for verification. 82 // 83 // CHECK: ( 2, 0, 0, 2.8, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 8.2, 0, 0, 8, 0, 0, 10.4, 0, 0, 10 ) 84 // 85 %m = sparse_tensor.values %0 86 : tensor<?x?xf64, #DenseMatrix> to memref<?xf64> 87 %v = vector.load %m[%c0] : memref<?xf64>, vector<25xf64> 88 vector.print %v : vector<25xf64> 89 90 // Release the resources. 91 bufferization.dealloc_tensor %a : tensor<?x?xf64, #SparseMatrix> 92 bufferization.dealloc_tensor %0 : tensor<?x?xf64, #DenseMatrix> 93 94 return 95 } 96} 97