1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: mlir-cpu-runner \ 3// RUN: -e entry -entry-point-result=void \ 4// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 5// RUN: FileCheck %s 6// 7// Do the same run, but now with SIMDization as well. This should not change the outcome. 8// 9// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=2" | \ 10// RUN: mlir-cpu-runner \ 11// RUN: -e entry -entry-point-result=void \ 12// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 13// RUN: FileCheck %s 14 15#SV = #sparse_tensor.encoding<{ dimLevelType = [ "compressed" ] }> 16 17#trait_cast = { 18 indexing_maps = [ 19 affine_map<(i) -> (i)>, // A (in) 20 affine_map<(i) -> (i)> // X (out) 21 ], 22 iterator_types = ["parallel"], 23 doc = "X(i) = cast A(i)" 24} 25 26// 27// Integration test that lowers a kernel annotated as sparse to actual sparse 28// code, initializes a matching sparse storage scheme from a dense vector, 29// and runs the resulting code with the JIT compiler. 30// 31module { 32 // 33 // Various kernels that cast a sparse vector from one type to another. 34 // Arithmetic supports the following casts. 35 // sitofp 36 // uitofp 37 // fptosi 38 // fptoui 39 // extf 40 // truncf 41 // extsi 42 // extui 43 // trunci 44 // bitcast 45 // Since all casts are "zero preserving" unary operations, lattice computation 46 // and conversion to sparse code is straightforward. 47 // 48 func.func @sparse_cast_s32_to_f32(%arga: tensor<10xi32, #SV>) -> tensor<10xf32> { 49 %argx = arith.constant dense<0.0> : tensor<10xf32> 50 %0 = linalg.generic #trait_cast 51 ins(%arga: tensor<10xi32, #SV>) 52 outs(%argx: tensor<10xf32>) { 53 ^bb(%a: i32, %x : f32): 54 %cst = arith.sitofp %a : i32 to f32 55 linalg.yield %cst : f32 56 } -> tensor<10xf32> 57 return %0 : tensor<10xf32> 58 } 59 func.func @sparse_cast_u32_to_f32(%arga: tensor<10xi32, #SV>) -> tensor<10xf32> { 60 %argx = arith.constant dense<0.0> : tensor<10xf32> 61 %0 = linalg.generic #trait_cast 62 ins(%arga: tensor<10xi32, #SV>) 63 outs(%argx: tensor<10xf32>) { 64 ^bb(%a: i32, %x : f32): 65 %cst = arith.uitofp %a : i32 to f32 66 linalg.yield %cst : f32 67 } -> tensor<10xf32> 68 return %0 : tensor<10xf32> 69 } 70 func.func @sparse_cast_f32_to_s32(%arga: tensor<10xf32, #SV>) -> tensor<10xi32> { 71 %argx = arith.constant dense<0> : tensor<10xi32> 72 %0 = linalg.generic #trait_cast 73 ins(%arga: tensor<10xf32, #SV>) 74 outs(%argx: tensor<10xi32>) { 75 ^bb(%a: f32, %x : i32): 76 %cst = arith.fptosi %a : f32 to i32 77 linalg.yield %cst : i32 78 } -> tensor<10xi32> 79 return %0 : tensor<10xi32> 80 } 81 func.func @sparse_cast_f64_to_u32(%arga: tensor<10xf64, #SV>) -> tensor<10xi32> { 82 %argx = arith.constant dense<0> : tensor<10xi32> 83 %0 = linalg.generic #trait_cast 84 ins(%arga: tensor<10xf64, #SV>) 85 outs(%argx: tensor<10xi32>) { 86 ^bb(%a: f64, %x : i32): 87 %cst = arith.fptoui %a : f64 to i32 88 linalg.yield %cst : i32 89 } -> tensor<10xi32> 90 return %0 : tensor<10xi32> 91 } 92 func.func @sparse_cast_f32_to_f64(%arga: tensor<10xf32, #SV>) -> tensor<10xf64> { 93 %argx = arith.constant dense<0.0> : tensor<10xf64> 94 %0 = linalg.generic #trait_cast 95 ins(%arga: tensor<10xf32, #SV>) 96 outs(%argx: tensor<10xf64>) { 97 ^bb(%a: f32, %x : f64): 98 %cst = arith.extf %a : f32 to f64 99 linalg.yield %cst : f64 100 } -> tensor<10xf64> 101 return %0 : tensor<10xf64> 102 } 103 func.func @sparse_cast_f64_to_f32(%arga: tensor<10xf64, #SV>) -> tensor<10xf32> { 104 %argx = arith.constant dense<0.0> : tensor<10xf32> 105 %0 = linalg.generic #trait_cast 106 ins(%arga: tensor<10xf64, #SV>) 107 outs(%argx: tensor<10xf32>) { 108 ^bb(%a: f64, %x : f32): 109 %cst = arith.truncf %a : f64 to f32 110 linalg.yield %cst : f32 111 } -> tensor<10xf32> 112 return %0 : tensor<10xf32> 113 } 114 func.func @sparse_cast_s32_to_u64(%arga: tensor<10xi32, #SV>) -> tensor<10xi64> { 115 %argx = arith.constant dense<0> : tensor<10xi64> 116 %0 = linalg.generic #trait_cast 117 ins(%arga: tensor<10xi32, #SV>) 118 outs(%argx: tensor<10xi64>) { 119 ^bb(%a: i32, %x : i64): 120 %cst = arith.extsi %a : i32 to i64 121 linalg.yield %cst : i64 122 } -> tensor<10xi64> 123 return %0 : tensor<10xi64> 124 } 125 func.func @sparse_cast_u32_to_s64(%arga: tensor<10xi32, #SV>) -> tensor<10xi64> { 126 %argx = arith.constant dense<0> : tensor<10xi64> 127 %0 = linalg.generic #trait_cast 128 ins(%arga: tensor<10xi32, #SV>) 129 outs(%argx: tensor<10xi64>) { 130 ^bb(%a: i32, %x : i64): 131 %cst = arith.extui %a : i32 to i64 132 linalg.yield %cst : i64 133 } -> tensor<10xi64> 134 return %0 : tensor<10xi64> 135 } 136 func.func @sparse_cast_i32_to_i8(%arga: tensor<10xi32, #SV>) -> tensor<10xi8> { 137 %argx = arith.constant dense<0> : tensor<10xi8> 138 %0 = linalg.generic #trait_cast 139 ins(%arga: tensor<10xi32, #SV>) 140 outs(%argx: tensor<10xi8>) { 141 ^bb(%a: i32, %x : i8): 142 %cst = arith.trunci %a : i32 to i8 143 linalg.yield %cst : i8 144 } -> tensor<10xi8> 145 return %0 : tensor<10xi8> 146 } 147 func.func @sparse_cast_f32_as_s32(%arga: tensor<10xf32, #SV>) -> tensor<10xi32> { 148 %argx = arith.constant dense<0> : tensor<10xi32> 149 %0 = linalg.generic #trait_cast 150 ins(%arga: tensor<10xf32, #SV>) 151 outs(%argx: tensor<10xi32>) { 152 ^bb(%a: f32, %x : i32): 153 %cst = arith.bitcast %a : f32 to i32 154 linalg.yield %cst : i32 155 } -> tensor<10xi32> 156 return %0 : tensor<10xi32> 157 } 158 159 // 160 // Main driver that converts a dense tensor into a sparse tensor 161 // and then calls the sparse casting kernel. 162 // 163 func.func @entry() { 164 %z = arith.constant 0 : index 165 %b = arith.constant 0 : i8 166 %i = arith.constant 0 : i32 167 %l = arith.constant 0 : i64 168 %f = arith.constant 0.0 : f32 169 %d = arith.constant 0.0 : f64 170 171 // Initialize dense tensors, convert to a sparse vectors. 172 %0 = arith.constant dense<[ -4, -3, -2, -1, 0, 1, 2, 3, 4, 305 ]> : tensor<10xi32> 173 %1 = sparse_tensor.convert %0 : tensor<10xi32> to tensor<10xi32, #SV> 174 %2 = arith.constant dense<[ -4.4, -3.3, -2.2, -1.1, 0.0, 1.1, 2.2, 3.3, 4.4, 305.5 ]> : tensor<10xf32> 175 %3 = sparse_tensor.convert %2 : tensor<10xf32> to tensor<10xf32, #SV> 176 %4 = arith.constant dense<[ -4.4, -3.3, -2.2, -1.1, 0.0, 1.1, 2.2, 3.3, 4.4, 305.5 ]> : tensor<10xf64> 177 %5 = sparse_tensor.convert %4 : tensor<10xf64> to tensor<10xf64, #SV> 178 %6 = arith.constant dense<[ 4294967295.0, 4294967294.0, 4294967293.0, 4294967292.0, 179 0.0, 1.1, 2.2, 3.3, 4.4, 305.5 ]> : tensor<10xf64> 180 %7 = sparse_tensor.convert %6 : tensor<10xf64> to tensor<10xf64, #SV> 181 182 // 183 // CHECK: ( -4, -3, -2, -1, 0, 1, 2, 3, 4, 305 ) 184 // 185 %c0 = call @sparse_cast_s32_to_f32(%1) : (tensor<10xi32, #SV>) -> tensor<10xf32> 186 %m0 = bufferization.to_memref %c0 : memref<10xf32> 187 %v0 = vector.transfer_read %m0[%z], %f: memref<10xf32>, vector<10xf32> 188 vector.print %v0 : vector<10xf32> 189 190 // 191 // CHECK: ( 4.29497e+09, 4.29497e+09, 4.29497e+09, 4.29497e+09, 0, 1, 2, 3, 4, 305 ) 192 // 193 %c1 = call @sparse_cast_u32_to_f32(%1) : (tensor<10xi32, #SV>) -> tensor<10xf32> 194 %m1 = bufferization.to_memref %c1 : memref<10xf32> 195 %v1 = vector.transfer_read %m1[%z], %f: memref<10xf32>, vector<10xf32> 196 vector.print %v1 : vector<10xf32> 197 198 // 199 // CHECK: ( -4, -3, -2, -1, 0, 1, 2, 3, 4, 305 ) 200 // 201 %c2 = call @sparse_cast_f32_to_s32(%3) : (tensor<10xf32, #SV>) -> tensor<10xi32> 202 %m2 = bufferization.to_memref %c2 : memref<10xi32> 203 %v2 = vector.transfer_read %m2[%z], %i: memref<10xi32>, vector<10xi32> 204 vector.print %v2 : vector<10xi32> 205 206 // 207 // CHECK: ( 4294967295, 4294967294, 4294967293, 4294967292, 0, 1, 2, 3, 4, 305 ) 208 // 209 %c3 = call @sparse_cast_f64_to_u32(%7) : (tensor<10xf64, #SV>) -> tensor<10xi32> 210 %m3 = bufferization.to_memref %c3 : memref<10xi32> 211 %v3 = vector.transfer_read %m3[%z], %i: memref<10xi32>, vector<10xi32> 212 %vu = vector.bitcast %v3 : vector<10xi32> to vector<10xui32> 213 vector.print %vu : vector<10xui32> 214 215 // 216 // CHECK: ( -4.4, -3.3, -2.2, -1.1, 0, 1.1, 2.2, 3.3, 4.4, 305.5 ) 217 // 218 %c4 = call @sparse_cast_f32_to_f64(%3) : (tensor<10xf32, #SV>) -> tensor<10xf64> 219 %m4 = bufferization.to_memref %c4 : memref<10xf64> 220 %v4 = vector.transfer_read %m4[%z], %d: memref<10xf64>, vector<10xf64> 221 vector.print %v4 : vector<10xf64> 222 223 // 224 // CHECK: ( -4.4, -3.3, -2.2, -1.1, 0, 1.1, 2.2, 3.3, 4.4, 305.5 ) 225 // 226 %c5 = call @sparse_cast_f64_to_f32(%5) : (tensor<10xf64, #SV>) -> tensor<10xf32> 227 %m5 = bufferization.to_memref %c5 : memref<10xf32> 228 %v5 = vector.transfer_read %m5[%z], %f: memref<10xf32>, vector<10xf32> 229 vector.print %v5 : vector<10xf32> 230 231 // 232 // CHECK: ( -4, -3, -2, -1, 0, 1, 2, 3, 4, 305 ) 233 // 234 %c6 = call @sparse_cast_s32_to_u64(%1) : (tensor<10xi32, #SV>) -> tensor<10xi64> 235 %m6 = bufferization.to_memref %c6 : memref<10xi64> 236 %v6 = vector.transfer_read %m6[%z], %l: memref<10xi64>, vector<10xi64> 237 vector.print %v6 : vector<10xi64> 238 239 // 240 // CHECK: ( 4294967292, 4294967293, 4294967294, 4294967295, 0, 1, 2, 3, 4, 305 ) 241 // 242 %c7 = call @sparse_cast_u32_to_s64(%1) : (tensor<10xi32, #SV>) -> tensor<10xi64> 243 %m7 = bufferization.to_memref %c7 : memref<10xi64> 244 %v7 = vector.transfer_read %m7[%z], %l: memref<10xi64>, vector<10xi64> 245 vector.print %v7 : vector<10xi64> 246 247 // 248 // CHECK: ( -4, -3, -2, -1, 0, 1, 2, 3, 4, 49 ) 249 // 250 %c8 = call @sparse_cast_i32_to_i8(%1) : (tensor<10xi32, #SV>) -> tensor<10xi8> 251 %m8 = bufferization.to_memref %c8 : memref<10xi8> 252 %v8 = vector.transfer_read %m8[%z], %b: memref<10xi8>, vector<10xi8> 253 vector.print %v8 : vector<10xi8> 254 255 // 256 // CHECK: ( -1064514355, -1068289229, -1072902963, -1081291571, 0, 1066192077, 1074580685, 1079194419, 1082969293, 1134084096 ) 257 // 258 %c9 = call @sparse_cast_f32_as_s32(%3) : (tensor<10xf32, #SV>) -> tensor<10xi32> 259 %m9 = bufferization.to_memref %c9 : memref<10xi32> 260 %v9 = vector.transfer_read %m9[%z], %i: memref<10xi32>, vector<10xi32> 261 vector.print %v9 : vector<10xi32> 262 263 // Release the resources. 264 sparse_tensor.release %1 : tensor<10xi32, #SV> 265 sparse_tensor.release %3 : tensor<10xf32, #SV> 266 sparse_tensor.release %5 : tensor<10xf64, #SV> 267 sparse_tensor.release %7 : tensor<10xf64, #SV> 268 memref.dealloc %m0 : memref<10xf32> 269 memref.dealloc %m1 : memref<10xf32> 270 memref.dealloc %m2 : memref<10xi32> 271 memref.dealloc %m3 : memref<10xi32> 272 memref.dealloc %m4 : memref<10xf64> 273 memref.dealloc %m5 : memref<10xf32> 274 memref.dealloc %m6 : memref<10xi64> 275 memref.dealloc %m7 : memref<10xi64> 276 memref.dealloc %m8 : memref<10xi8> 277 memref.dealloc %m9 : memref<10xi32> 278 279 return 280 } 281} 282