| 1 |
// Test affine promotion pass |
| 2 |
|
| 3 |
// RUN: fir-opt --split-input-file --promote-to-affine --affine-loop-invariant-code-motion --cse %s | FileCheck %s |
| 4 |
|
| 5 |
!arr_d1 = !fir.ref<!fir.array<?xf32>> |
| 6 |
#arr_len = affine_map<()[j1,k1] -> (k1 - j1 + 1)> |
| 7 |
|
| 8 |
func.func @loop_with_load_and_store(%a1: !arr_d1, %a2: !arr_d1, %a3: !arr_d1) { |
| 9 |
%c1 = arith.constant 1 : index |
| 10 |
%c0 = arith.constant 0 : index |
| 11 |
%len = arith.constant 100 : index |
| 12 |
%dims = fir.shape %len : (index) -> !fir.shape<1> |
| 13 |
%siz = affine.apply #arr_len()[%c1,%len] |
| 14 |
%t1 = fir.alloca !fir.array<?xf32>, %siz |
| 15 |
|
| 16 |
fir.do_loop %i = %c1 to %len step %c1 { |
| 17 |
%a1_idx = fir.array_coor %a1(%dims) %i |
| 18 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 19 |
%a1_v = fir.load %a1_idx : !fir.ref<f32> |
| 20 |
|
| 21 |
%a2_idx = fir.array_coor %a2(%dims) %i |
| 22 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 23 |
%a2_v = fir.load %a2_idx : !fir.ref<f32> |
| 24 |
|
| 25 |
%v = arith.addf %a1_v, %a2_v : f32 |
| 26 |
%t1_idx = fir.array_coor %t1(%dims) %i |
| 27 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 28 |
|
| 29 |
fir.store %v to %t1_idx : !fir.ref<f32> |
| 30 |
} |
| 31 |
fir.do_loop %i = %c1 to %len step %c1 { |
| 32 |
%t1_idx = fir.array_coor %t1(%dims) %i |
| 33 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 34 |
%t1_v = fir.load %t1_idx : !fir.ref<f32> |
| 35 |
|
| 36 |
%a2_idx = fir.array_coor %a2(%dims) %i |
| 37 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 38 |
%a2_v = fir.load %a2_idx : !fir.ref<f32> |
| 39 |
|
| 40 |
%v = arith.mulf %t1_v, %a2_v : f32 |
| 41 |
%a3_idx = fir.array_coor %a3(%dims) %i |
| 42 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 43 |
|
| 44 |
fir.store %v to %a3_idx : !fir.ref<f32> |
| 45 |
} |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
// CHECK: func @loop_with_load_and_store(%[[VAL_0:.*]]: !fir.ref<!fir.array<?xf32>>, %[[VAL_1:.*]]: !fir.ref<!fir.array<?xf32>>, %[[VAL_2:.*]]: !fir.ref<!fir.array<?xf32>>) { |
| 50 |
// CHECK: %[[VAL_3:.*]] = arith.constant 1 : index |
| 51 |
// CHECK: %[[VAL_4:.*]] = arith.constant 100 : index |
| 52 |
// CHECK: %[[VAL_5:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1> |
| 53 |
// CHECK: %[[VAL_6:.*]] = affine.apply #{{.*}}(){{\[}}%[[VAL_3]], %[[VAL_4]]] |
| 54 |
// CHECK: %[[VAL_7:.*]] = fir.alloca !fir.array<?xf32>, %[[VAL_6]] |
| 55 |
// CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.array<?xf32>>) -> memref<?xf32> |
| 56 |
// CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<?xf32>>) -> memref<?xf32> |
| 57 |
// CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_7]] : (!fir.ref<!fir.array<?xf32>>) -> memref<?xf32> |
| 58 |
// CHECK: affine.for %[[VAL_11:.*]] = %[[VAL_3]] to #{{.*}}(){{\[}}%[[VAL_4]]] { |
| 59 |
// CHECK: %[[VAL_12:.*]] = affine.apply #{{.*}}(%[[VAL_11]]){{\[}}%[[VAL_3]], %[[VAL_4]], %[[VAL_3]]] |
| 60 |
// CHECK: %[[VAL_13:.*]] = affine.load %[[VAL_8]]{{\[}}%[[VAL_12]]] : memref<?xf32> |
| 61 |
// CHECK: %[[VAL_14:.*]] = affine.load %[[VAL_9]]{{\[}}%[[VAL_12]]] : memref<?xf32> |
| 62 |
// CHECK: %[[VAL_15:.*]] = arith.addf %[[VAL_13]], %[[VAL_14]] : f32 |
| 63 |
// CHECK: affine.store %[[VAL_15]], %[[VAL_10]]{{\[}}%[[VAL_12]]] : memref<?xf32> |
| 64 |
// CHECK: } |
| 65 |
// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<!fir.array<?xf32>>) -> memref<?xf32> |
| 66 |
// CHECK: affine.for %[[VAL_17:.*]] = %[[VAL_3]] to #{{.*}}(){{\[}}%[[VAL_4]]] { |
| 67 |
// CHECK: %[[VAL_18:.*]] = affine.apply #{{.*}}(%[[VAL_17]]){{\[}}%[[VAL_3]], %[[VAL_4]], %[[VAL_3]]] |
| 68 |
// CHECK: %[[VAL_19:.*]] = affine.load %[[VAL_10]]{{\[}}%[[VAL_18]]] : memref<?xf32> |
| 69 |
// CHECK: %[[VAL_20:.*]] = affine.load %[[VAL_9]]{{\[}}%[[VAL_18]]] : memref<?xf32> |
| 70 |
// CHECK: %[[VAL_21:.*]] = arith.mulf %[[VAL_19]], %[[VAL_20]] : f32 |
| 71 |
// CHECK: affine.store %[[VAL_21]], %[[VAL_16]]{{\[}}%[[VAL_18]]] : memref<?xf32> |
| 72 |
// CHECK: } |
| 73 |
// CHECK: return |
| 74 |
// CHECK: } |
| 75 |
|
| 76 |
// ----- |
| 77 |
|
| 78 |
!arr_d1 = !fir.ref<!fir.array<?xf32>> |
| 79 |
#arr_len = affine_map<()[j1,k1] -> (k1 - j1 + 1)> |
| 80 |
|
| 81 |
func.func @loop_with_if(%a: !arr_d1, %v: f32) { |
| 82 |
%c0 = arith.constant 0 : index |
| 83 |
%c1 = arith.constant 1 : index |
| 84 |
%c2 = arith.constant 2 : index |
| 85 |
%len = arith.constant 100 : index |
| 86 |
%dims = fir.shape %len : (index) -> !fir.shape<1> |
| 87 |
|
| 88 |
fir.do_loop %i = %c1 to %len step %c1 { |
| 89 |
fir.do_loop %j = %c1 to %len step %c1 { |
| 90 |
fir.do_loop %k = %c1 to %len step %c1 { |
| 91 |
%im2 = arith.subi %i, %c2 : index |
| 92 |
%cond = arith.cmpi "sgt", %im2, %c0 : index |
| 93 |
fir.if %cond { |
| 94 |
%a_idx = fir.array_coor %a(%dims) %i |
| 95 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 96 |
fir.store %v to %a_idx : !fir.ref<f32> |
| 97 |
} |
| 98 |
%aj_idx = fir.array_coor %a(%dims) %j |
| 99 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 100 |
fir.store %v to %aj_idx : !fir.ref<f32> |
| 101 |
%ak_idx = fir.array_coor %a(%dims) %k |
| 102 |
: (!arr_d1, !fir.shape<1>, index) -> !fir.ref<f32> |
| 103 |
fir.store %v to %ak_idx : !fir.ref<f32> |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return |
| 108 |
} |
| 109 |
|
| 110 |
// CHECK: func @loop_with_if(%[[VAL_0:.*]]: !fir.ref<!fir.array<?xf32>>, %[[VAL_1:.*]]: f32) { |
| 111 |
// CHECK: %[[VAL_2:.*]] = arith.constant 0 : index |
| 112 |
// CHECK: %[[VAL_3:.*]] = arith.constant 1 : index |
| 113 |
// CHECK: %[[VAL_4:.*]] = arith.constant 2 : index |
| 114 |
// CHECK: %[[VAL_5:.*]] = arith.constant 100 : index |
| 115 |
// CHECK: %[[VAL_6:.*]] = fir.shape %[[VAL_5]] : (index) -> !fir.shape<1> |
| 116 |
// CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.array<?xf32>>) -> memref<?xf32> |
| 117 |
// CHECK: affine.for %[[VAL_8:.*]] = %[[VAL_3]] to #{{.*}}(){{\[}}%[[VAL_5]]] { |
| 118 |
// CHECK: %[[VAL_9:.*]] = affine.apply #{{.*}}(%[[VAL_8]]){{\[}}%[[VAL_3]], %[[VAL_5]], %[[VAL_3]]] |
| 119 |
// CHECK: affine.store %[[VAL_1]], %[[VAL_7]]{{\[}}%[[VAL_9]]] : memref<?xf32> |
| 120 |
// CHECK: } |
| 121 |
// CHECK: affine.for %[[VAL_10:.*]] = %[[VAL_3]] to #{{.*}}(){{\[}}%[[VAL_5]]] { |
| 122 |
// CHECK: %[[VAL_11:.*]] = affine.apply #{{.*}}(%[[VAL_10]]){{\[}}%[[VAL_3]], %[[VAL_5]], %[[VAL_3]]] |
| 123 |
// CHECK: affine.store %[[VAL_1]], %[[VAL_7]]{{\[}}%[[VAL_11]]] : memref<?xf32> |
| 124 |
// CHECK: } |
| 125 |
// CHECK: affine.for %[[VAL_12:.*]] = %[[VAL_3]] to #{{.*}}(){{\[}}%[[VAL_5]]] { |
| 126 |
// CHECK: %[[VAL_13:.*]] = arith.subi %[[VAL_12]], %[[VAL_4]] : index |
| 127 |
// CHECK: affine.if #set(%[[VAL_12]]) { |
| 128 |
// CHECK: %[[VAL_14:.*]] = affine.apply #{{.*}}(%[[VAL_12]]){{\[}}%[[VAL_3]], %[[VAL_5]], %[[VAL_3]]] |
| 129 |
// CHECK: affine.store %[[VAL_1]], %[[VAL_7]]{{\[}}%[[VAL_14]]] : memref<?xf32> |
| 130 |
// CHECK: } |
| 131 |
// CHECK: } |
| 132 |
// CHECK: return |
| 133 |
// CHECK: } |