1// RUN: mlir-opt %s \ 2// RUN: --test-sparsification="lower ptr-type=4 ind-type=4" \ 3// RUN: --convert-linalg-to-loops --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-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// RUN: mlir-opt %s \ 14// RUN: --test-sparsification="lower vectorization-strategy=2 ptr-type=4 ind-type=4 vl=16" \ 15// RUN: --convert-linalg-to-loops --convert-vector-to-scf --convert-scf-to-std \ 16// RUN: --func-bufferize --tensor-constant-bufferize --tensor-bufferize \ 17// RUN: --std-bufferize --finalizing-bufferize \ 18// RUN: --convert-vector-to-llvm --convert-std-to-llvm | \ 19// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.mtx" \ 20// RUN: mlir-cpu-runner \ 21// RUN: -e entry -entry-point-result=void \ 22// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 23// RUN: FileCheck %s 24 25// 26// Use descriptive names for opaque pointers. 27// 28!Filename = type !llvm.ptr<i8> 29!SparseTensor = type !llvm.ptr<i8> 30 31#matvec = { 32 indexing_maps = [ 33 affine_map<(i,j) -> (i,j)>, // A 34 affine_map<(i,j) -> (j)>, // b 35 affine_map<(i,j) -> (i)> // x (out) 36 ], 37 sparse = [ 38 [ "D", "S" ], // A 39 [ "D" ], // b 40 [ "D" ] // x 41 ], 42 iterator_types = ["parallel", "reduction"], 43 doc = "X(i) += A(i,j) * B(j)" 44} 45 46// 47// Integration test that lowers a kernel annotated as sparse to 48// actual sparse code, initializes a matching sparse storage scheme 49// from file, and runs the resulting code with the JIT compiler. 50// 51module { 52 // 53 // The kernel expressed as an annotated Linalg op. The kernel multiplies 54 // a sparse matrix A with a dense vector b into a dense vector x. 55 // 56 func @kernel_matvec(%argA: !SparseTensor, 57 %argb: tensor<?xi32>, 58 %argx: tensor<?xi32>) -> tensor<?xi32> { 59 %arga = sparse_tensor.fromPtr %argA : !SparseTensor to tensor<?x?xi32> 60 %0 = linalg.generic #matvec 61 ins(%arga, %argb: tensor<?x?xi32>, tensor<?xi32>) 62 outs(%argx: tensor<?xi32>) { 63 ^bb(%a: i32, %b: i32, %x: i32): 64 %0 = muli %a, %b : i32 65 %1 = addi %x, %0 : i32 66 linalg.yield %1 : i32 67 } -> tensor<?xi32> 68 return %0 : tensor<?xi32> 69 } 70 71 // 72 // Runtime support library that is called directly from here. 73 // 74 func private @getTensorFilename(index) -> (!Filename) 75 func private @newSparseTensor(!Filename, memref<?xi1>, index, index, index) -> (!SparseTensor) 76 func private @delSparseTensor(!SparseTensor) -> () 77 78 // 79 // Main driver that reads matrix from file and calls the sparse kernel. 80 // 81 func @entry() { 82 %i0 = constant 0 : i32 83 %c0 = constant 0 : index 84 %c1 = constant 1 : index 85 %c2 = constant 2 : index 86 %c4 = constant 4 : index 87 %c256 = constant 256 : index 88 89 // Mark inner dimension of the matrix as sparse and encode the 90 // storage scheme types (this must match the metadata in the 91 // alias above and compiler switches). In this case, we test 92 // that 8-bit indices and pointers work correctly on a matrix 93 // with i32 elements. 94 %annotations = memref.alloc(%c2) : memref<?xi1> 95 %sparse = constant true 96 %dense = constant false 97 memref.store %dense, %annotations[%c0] : memref<?xi1> 98 memref.store %sparse, %annotations[%c1] : memref<?xi1> 99 %u8 = constant 4 : index 100 %i32 = constant 3 : index 101 102 // Read the sparse matrix from file, construct sparse storage. 103 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 104 %a = call @newSparseTensor(%fileName, %annotations, %u8, %u8, %i32) 105 : (!Filename, memref<?xi1>, index, index, index) -> (!SparseTensor) 106 107 // Initialize dense vectors. 108 %bdata = memref.alloc(%c256) : memref<?xi32> 109 %xdata = memref.alloc(%c4) : memref<?xi32> 110 scf.for %i = %c0 to %c256 step %c1 { 111 %k = addi %i, %c1 : index 112 %j = index_cast %k : index to i32 113 memref.store %j, %bdata[%i] : memref<?xi32> 114 } 115 scf.for %i = %c0 to %c4 step %c1 { 116 memref.store %i0, %xdata[%i] : memref<?xi32> 117 } 118 %b = memref.tensor_load %bdata : memref<?xi32> 119 %x = memref.tensor_load %xdata : memref<?xi32> 120 121 // Call kernel. 122 %0 = call @kernel_matvec(%a, %b, %x) 123 : (!SparseTensor, tensor<?xi32>, tensor<?xi32>) -> tensor<?xi32> 124 125 // Print the result for verification. 126 // 127 // CHECK: ( 889, 1514, -21, -3431 ) 128 // 129 %m = memref.buffer_cast %0 : memref<?xi32> 130 %v = vector.transfer_read %m[%c0], %i0: memref<?xi32>, vector<4xi32> 131 vector.print %v : vector<4xi32> 132 133 // Release the resources. 134 call @delSparseTensor(%a) : (!SparseTensor) -> () 135 memref.dealloc %bdata : memref<?xi32> 136 memref.dealloc %xdata : memref<?xi32> 137 138 return 139 } 140} 141