140 lines · plain
1// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(convert-vector-to-scf,lower-affine,convert-scf-to-cf),convert-vector-to-llvm,finalize-memref-to-llvm,convert-func-to-llvm,convert-arith-to-llvm,convert-cf-to-llvm,convert-ub-to-llvm,reconcile-unrealized-casts)" | \2// RUN: mlir-runner -e entry -entry-point-result=void \3// RUN: -shared-libs=%mlir_c_runner_utils | \4// RUN: FileCheck %s5 6// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(convert-vector-to-scf{full-unroll=true},lower-affine,convert-scf-to-cf),convert-vector-to-llvm,finalize-memref-to-llvm,convert-func-to-llvm,convert-arith-to-llvm,convert-cf-to-llvm,convert-ub-to-llvm,reconcile-unrealized-casts)" | \7// RUN: mlir-runner -e entry -entry-point-result=void \8// RUN: -shared-libs=%mlir_c_runner_utils | \9// RUN: FileCheck %s10 11func.func @transfer_read_3d(%A : memref<?x?x?x?xf32>,12 %o: index, %a: index, %b: index, %c: index) {13 %fm42 = arith.constant -42.0: f3214 %f = vector.transfer_read %A[%o, %a, %b, %c], %fm4215 : memref<?x?x?x?xf32>, vector<2x5x3xf32>16 vector.print %f: vector<2x5x3xf32>17 return18}19 20func.func @transfer_read_3d_and_extract(%A : memref<?x?x?x?xf32>,21 %o: index, %a: index, %b: index, %c: index) {22 %fm42 = arith.constant -42.0: f3223 %f = vector.transfer_read %A[%o, %a, %b, %c], %fm4224 {in_bounds = [true, true, true]}25 : memref<?x?x?x?xf32>, vector<2x5x3xf32>26 %sub = vector.extract %f[0] : vector<5x3xf32> from vector<2x5x3xf32>27 vector.print %sub: vector<5x3xf32>28 return29}30 31func.func @transfer_read_3d_broadcast(%A : memref<?x?x?x?xf32>,32 %o: index, %a: index, %b: index, %c: index) {33 %fm42 = arith.constant -42.0: f3234 %f = vector.transfer_read %A[%o, %a, %b, %c], %fm4235 {permutation_map = affine_map<(d0, d1, d2, d3) -> (d1, 0, d3)>}36 : memref<?x?x?x?xf32>, vector<2x5x3xf32>37 vector.print %f: vector<2x5x3xf32>38 return39}40 41func.func @transfer_read_3d_mask_broadcast(42 %A : memref<?x?x?x?xf32>, %o: index, %a: index, %b: index, %c: index) {43 %fm42 = arith.constant -42.0: f3244 %mask = arith.constant dense<[0, 1]> : vector<2xi1>45 %f = vector.transfer_read %A[%o, %a, %b, %c], %fm42, %mask46 {permutation_map = affine_map<(d0, d1, d2, d3) -> (d1, 0, 0)>}47 : memref<?x?x?x?xf32>, vector<2x5x3xf32>48 vector.print %f: vector<2x5x3xf32>49 return50}51 52func.func @transfer_read_3d_transposed(%A : memref<?x?x?x?xf32>,53 %o: index, %a: index, %b: index, %c: index) {54 %fm42 = arith.constant -42.0: f3255 %f = vector.transfer_read %A[%o, %a, %b, %c], %fm4256 {permutation_map = affine_map<(d0, d1, d2, d3) -> (d3, d0, d1)>}57 : memref<?x?x?x?xf32>, vector<3x5x3xf32>58 vector.print %f: vector<3x5x3xf32>59 return60}61 62func.func @transfer_write_3d(%A : memref<?x?x?x?xf32>,63 %o: index, %a: index, %b: index, %c: index) {64 %fn1 = arith.constant -1.0 : f3265 %vf0 = vector.broadcast %fn1 : f32 to vector<2x9x3xf32>66 vector.transfer_write %vf0, %A[%o, %a, %b, %c]67 : vector<2x9x3xf32>, memref<?x?x?x?xf32>68 return69}70 71func.func @entry() {72 %c0 = arith.constant 0: index73 %c1 = arith.constant 1: index74 %c2 = arith.constant 2: index75 %c3 = arith.constant 3: index76 %f2 = arith.constant 2.0: f3277 %f10 = arith.constant 10.0: f3278 %first = arith.constant 5: index79 %second = arith.constant 4: index80 %third = arith.constant 2 : index81 %outer = arith.constant 10 : index82 %A = memref.alloc(%outer, %first, %second, %third) : memref<?x?x?x?xf32>83 scf.for %o = %c0 to %outer step %c1 {84 scf.for %i = %c0 to %first step %c1 {85 %i32 = arith.index_cast %i : index to i3286 %fi = arith.sitofp %i32 : i32 to f3287 %fi10 = arith.mulf %fi, %f10 : f3288 scf.for %j = %c0 to %second step %c1 {89 %j32 = arith.index_cast %j : index to i3290 %fj = arith.sitofp %j32 : i32 to f3291 %fadded = arith.addf %fi10, %fj : f3292 scf.for %k = %c0 to %third step %c1 {93 %k32 = arith.index_cast %k : index to i3294 %fk = arith.sitofp %k32 : i32 to f3295 %fk1 = arith.addf %f2, %fk : f3296 %fmul = arith.mulf %fadded, %fk1 : f3297 memref.store %fmul, %A[%o, %i, %j, %k] : memref<?x?x?x?xf32>98 }99 }100 }101 }102 103 // 1. Read 3D vector from 4D memref.104 call @transfer_read_3d(%A, %c0, %c0, %c0, %c0)105 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()106 // CHECK: ( ( ( 0, 0, -42 ), ( 2, 3, -42 ), ( 4, 6, -42 ), ( 6, 9, -42 ), ( -42, -42, -42 ) ), ( ( 20, 30, -42 ), ( 22, 33, -42 ), ( 24, 36, -42 ), ( 26, 39, -42 ), ( -42, -42, -42 ) ) )107 108 // 2. Read 3D vector from 4D memref and extract subvector from result.109 call @transfer_read_3d_and_extract(%A, %c0, %c0, %c0, %c0)110 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()111 // CHECK: ( ( 0, 0, 2 ), ( 2, 3, 4 ), ( 4, 6, 6 ), ( 6, 9, 20 ), ( 20, 30, 22 ) )112 113 // 3. Write 3D vector to 4D memref.114 call @transfer_write_3d(%A, %c0, %c0, %c1, %c1)115 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()116 117 // 4. Read memref to verify step 2.118 call @transfer_read_3d(%A, %c0, %c0, %c0, %c0)119 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()120 // CHECK: ( ( ( 0, 0, -42 ), ( 2, -1, -42 ), ( 4, -1, -42 ), ( 6, -1, -42 ), ( -42, -42, -42 ) ), ( ( 20, 30, -42 ), ( 22, -1, -42 ), ( 24, -1, -42 ), ( 26, -1, -42 ), ( -42, -42, -42 ) ) )121 122 // 5. Read 3D vector from 4D memref and transpose vector.123 call @transfer_read_3d_transposed(%A, %c0, %c0, %c0, %c0)124 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()125 // CHECK: ( ( ( 0, 20, 40 ), ( 0, 20, 40 ), ( 0, 20, 40 ), ( 0, 20, 40 ), ( 0, 20, 40 ) ), ( ( 0, 30, 60 ), ( 0, 30, 60 ), ( 0, 30, 60 ), ( 0, 30, 60 ), ( 0, 30, 60 ) ), ( ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ) ) )126 127 // 6. Read 1D vector from 4D memref and broadcast vector to 3D.128 call @transfer_read_3d_broadcast(%A, %c0, %c0, %c0, %c0)129 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()130 // CHECK: ( ( ( 0, 0, -42 ), ( 0, 0, -42 ), ( 0, 0, -42 ), ( 0, 0, -42 ), ( 0, 0, -42 ) ), ( ( 20, 30, -42 ), ( 20, 30, -42 ), ( 20, 30, -42 ), ( 20, 30, -42 ), ( 20, 30, -42 ) ) )131 132 // 7. Read 1D vector from 4D memref with mask and broadcast vector to 3D.133 call @transfer_read_3d_mask_broadcast(%A, %c0, %c0, %c0, %c0)134 : (memref<?x?x?x?xf32>, index, index, index, index) -> ()135 // CHECK: ( ( ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ), ( -42, -42, -42 ) ), ( ( 20, 20, 20 ), ( 20, 20, 20 ), ( 20, 20, 20 ), ( 20, 20, 20 ), ( 20, 20, 20 ) ) )136 137 memref.dealloc %A : memref<?x?x?x?xf32>138 return139}140