1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: mlir-cpu-runner -e entry -entry-point-result=void \ 3// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 4// RUN: FileCheck %s 5 6#DCSR = #sparse_tensor.encoding<{ 7 dimLevelType = [ "compressed", "compressed" ] 8}> 9 10#DCSC = #sparse_tensor.encoding<{ 11 dimLevelType = [ "compressed", "compressed" ], 12 dimOrdering = affine_map<(i,j) -> (j,i)> 13}> 14 15#transpose_trait = { 16 indexing_maps = [ 17 affine_map<(i,j) -> (j,i)>, // A 18 affine_map<(i,j) -> (i,j)> // X 19 ], 20 iterator_types = ["parallel", "parallel"], 21 doc = "X(i,j) = A(j,i)" 22} 23 24module { 25 26 // 27 // Transposing a sparse row-wise matrix into another sparse row-wise 28 // matrix would fail direct codegen, since it introduces a cycle in 29 // the iteration graph. This can be avoided by converting the incoming 30 // matrix into a sparse column-wise matrix first. 31 // 32 func @sparse_transpose(%arga: tensor<3x4xf64, #DCSR>) -> tensor<4x3xf64, #DCSR> { 33 %t = sparse_tensor.convert %arga : tensor<3x4xf64, #DCSR> to tensor<3x4xf64, #DCSC> 34 35 %c3 = arith.constant 3 : index 36 %c4 = arith.constant 4 : index 37 %i = sparse_tensor.init [%c4, %c3] : tensor<4x3xf64, #DCSR> 38 39 %0 = linalg.generic #transpose_trait 40 ins(%t: tensor<3x4xf64, #DCSC>) 41 outs(%i: tensor<4x3xf64, #DCSR>) { 42 ^bb(%a: f64, %x: f64): 43 linalg.yield %a : f64 44 } -> tensor<4x3xf64, #DCSR> 45 46 sparse_tensor.release %t : tensor<3x4xf64, #DCSC> 47 48 return %0 : tensor<4x3xf64, #DCSR> 49 } 50 51 // 52 // Main driver. 53 // 54 func @entry() { 55 %c0 = arith.constant 0 : index 56 %c1 = arith.constant 1 : index 57 %c4 = arith.constant 4 : index 58 %du = arith.constant 0.0 : f64 59 60 // Setup input sparse matrix from compressed constant. 61 %d = arith.constant dense <[ 62 [ 1.1, 1.2, 0.0, 1.4 ], 63 [ 0.0, 0.0, 0.0, 0.0 ], 64 [ 3.1, 0.0, 3.3, 3.4 ] 65 ]> : tensor<3x4xf64> 66 %a = sparse_tensor.convert %d : tensor<3x4xf64> to tensor<3x4xf64, #DCSR> 67 68 // Call the kernel. 69 %0 = call @sparse_transpose(%a) : (tensor<3x4xf64, #DCSR>) -> tensor<4x3xf64, #DCSR> 70 71 // 72 // Verify result. 73 // 74 // CHECK: ( 1.1, 0, 3.1 ) 75 // CHECK-NEXT: ( 1.2, 0, 0 ) 76 // CHECK-NEXT: ( 0, 0, 3.3 ) 77 // CHECK-NEXT: ( 1.4, 0, 3.4 ) 78 // 79 %x = sparse_tensor.convert %0 : tensor<4x3xf64, #DCSR> to tensor<4x3xf64> 80 %m = bufferization.to_memref %x : memref<4x3xf64> 81 scf.for %i = %c0 to %c4 step %c1 { 82 %v = vector.transfer_read %m[%i, %c0], %du: memref<4x3xf64>, vector<3xf64> 83 vector.print %v : vector<3xf64> 84 } 85 86 // Release resources. 87 sparse_tensor.release %a : tensor<3x4xf64, #DCSR> 88 sparse_tensor.release %0 : tensor<4x3xf64, #DCSR> 89 memref.dealloc %m : memref<4x3xf64> 90 91 return 92 } 93} 94