Skip to content

Commit 45a330f

Browse files
committed
Only keep attributes that are valid for scf loop op.
1 parent 316714c commit 45a330f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

flang/lib/Optimizer/Transforms/FIRToSCF.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ struct DoLoopConversion : public mlir::OpRewritePattern<fir::DoLoopOp> {
9292
hasFinalValue ? scfLoopLikeOp.getRegionIterArgs().drop_front()
9393
: scfLoopLikeOp.getRegionIterArgs());
9494

95-
// Copy all the attributes from the old to new op.
96-
scfLoopOp->setAttrs(doLoopOp->getAttrs());
95+
// Copy loop annotations from the fir.do_loop to scf loop op.
96+
if (auto ann = doLoopOp.getLoopAnnotation())
97+
scfLoopOp->setAttr("loop_annotation", *ann);
98+
9799
rewriter.replaceOp(doLoopOp, scfLoopOp);
98100
return mlir::success();
99101
}

flang/test/Fir/FirToSCF/do-loop.fir

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt %s --fir-to-scf | FileCheck %s
1+
// RUN: fir-opt %s --fir-to-scf --split-input-file | FileCheck %s
22

33
// CHECK-LABEL: func.func @simple_loop(
44
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>) {
@@ -31,6 +31,8 @@ func.func @simple_loop(%arg0: !fir.ref<!fir.array<100xi32>>) {
3131
return
3232
}
3333

34+
// -----
35+
3436
// CHECK-LABEL: func.func @loop_with_negtive_step(
3537
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>) {
3638
// CHECK: %[[VAL_0:.*]] = arith.constant 100 : index
@@ -64,6 +66,8 @@ func.func @loop_with_negtive_step(%arg0: !fir.ref<!fir.array<100xi32>>) {
6466
return
6567
}
6668

69+
// -----
70+
6771
// CHECK-LABEL: func.func @loop_with_results(
6872
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>,
6973
// CHECK-SAME: %[[ARG1:.*]]: !fir.ref<i32>) {
@@ -102,6 +106,8 @@ func.func @loop_with_results(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !fir.r
102106
return
103107
}
104108

109+
// -----
110+
105111
// CHECK-LABEL: func.func @loop_with_final_value(
106112
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>,
107113
// CHECK-SAME: %[[ARG1:.*]]: !fir.ref<i32>) {
@@ -146,6 +152,8 @@ func.func @loop_with_final_value(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !f
146152
return
147153
}
148154

155+
// -----
156+
149157
// CHECK-LABEL: func.func @loop_with_unordered_attr(
150158
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>) {
151159
// CHECK: %[[CONSTANT_0:.*]] = arith.constant 1 : index
@@ -163,7 +171,7 @@ func.func @loop_with_final_value(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !f
163171
// CHECK: %[[ARRAY_COOR_0:.*]] = fir.array_coor %[[ARG0]](%[[SHAPE_0]]) %[[ADDI_1]] : (!fir.ref<!fir.array<100xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
164172
// CHECK: fir.store %[[CONSTANT_2]] to %[[ARRAY_COOR_0]] : !fir.ref<i32>
165173
// CHECK: scf.reduce
166-
// CHECK: } {unordered}
174+
// CHECK: }
167175
// CHECK: return
168176
// CHECK: }
169177
func.func @loop_with_unordered_attr(%arg0: !fir.ref<!fir.array<100xi32>>) {
@@ -178,6 +186,10 @@ func.func @loop_with_unordered_attr(%arg0: !fir.ref<!fir.array<100xi32>>) {
178186
return
179187
}
180188

189+
// -----
190+
191+
// CHECK: #[[$ATTR_0:.+]] = #llvm.loop_vectorize<disable = false>
192+
// CHECK: #[[$ATTR_1:.+]] = #llvm.loop_annotation<vectorize = #[[$ATTR_0]]>
181193
// CHECK-LABEL: func.func @loop_with_attribute(
182194
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>,
183195
// CHECK-SAME: %[[ARG1:.*]]: !fir.ref<i32>) {
@@ -199,16 +211,19 @@ func.func @loop_with_unordered_attr(%arg0: !fir.ref<!fir.array<100xi32>>) {
199211
// CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
200212
// CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_15]], %[[VAL_14]] : i32
201213
// CHECK: fir.store %[[VAL_16]] to %[[VAL_3]] : !fir.ref<i32>
202-
// CHECK: } {operandSegmentSizes = array<i32: 1, 1, 1, 1, 0>, reduceAttrs = [#fir.reduce_attr<add>]}
214+
// CHECK: } {loop_annotation = #[[$ATTR_1]]}
203215
// CHECK: return
204216
// CHECK: }
217+
218+
#loop_vectorize = #llvm.loop_vectorize<disable = false>
219+
#loop_annotation = #llvm.loop_annotation<vectorize = #loop_vectorize>
205220
func.func @loop_with_attribute(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !fir.ref<i32>) {
206221
%c1 = arith.constant 1 : index
207222
%c0_i32 = arith.constant 0 : i32
208223
%c100 = arith.constant 100 : index
209224
%0 = fir.alloca i32
210225
%1 = fir.shape %c100 : (index) -> !fir.shape<1>
211-
fir.do_loop %arg2 = %c1 to %c100 step %c1 reduce(#fir.reduce_attr<add> -> %0 : !fir.ref<i32>) {
226+
fir.do_loop %arg2 = %c1 to %c100 step %c1 attributes {loopAnnotation = #loop_annotation} {
212227
%2 = fir.array_coor %arg0(%1) %arg2 : (!fir.ref<!fir.array<100xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
213228
%3 = fir.load %2 : !fir.ref<i32>
214229
%4 = fir.load %0 : !fir.ref<i32>
@@ -219,6 +234,8 @@ func.func @loop_with_attribute(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !fir
219234
return
220235
}
221236

237+
// -----
238+
222239
// CHECK-LABEL: func.func @nested_loop(
223240
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100x100xi32>>) {
224241
// CHECK: %[[VAL_0:.*]] = arith.constant 1 : index

0 commit comments

Comments
 (0)