Skip to content

Commit f486cc4

Browse files
[flang] Add loop annotation attributes to the loop backedge (#126082)
Flang currently adds loop metadata to a conditional branch in the loop preheader, while clang adds it to the loop latch's branch instruction. Langref says: > Currently, loop metadata is implemented as metadata attached to the branch instruction in the loop latch block. > > https://llvm.org/docs/LangRef.html#llvm-loop I misread langref a couple times, but I think this is the appropriate branch op for the LoopAnnotationAttr. In a couple examples I found that the metadata was lost entirely during canonicalization. This patch makes the codegen look more like clang's and the annotations persist through codegen. * current clang: https://godbolt.org/z/8WhbcrnG3 * current flang: https://godbolt.org/z/TrPboqqcn
1 parent 65a9a3d commit f486cc4

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,24 @@ class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
123123
: terminator->operand_begin();
124124
loopCarried.append(begin, terminator->operand_end());
125125
loopCarried.push_back(itersMinusOne);
126-
rewriter.create<mlir::cf::BranchOp>(loc, conditionalBlock, loopCarried);
126+
auto backEdge =
127+
rewriter.create<mlir::cf::BranchOp>(loc, conditionalBlock, loopCarried);
127128
rewriter.eraseOp(terminator);
128129

130+
// Copy loop annotations from the do loop to the loop back edge.
131+
if (auto ann = loop.getLoopAnnotation())
132+
backEdge->setAttr("loop_annotation", *ann);
133+
129134
// Conditional block
130135
rewriter.setInsertionPointToEnd(conditionalBlock);
131136
auto zero = rewriter.create<mlir::arith::ConstantIndexOp>(loc, 0);
132137
auto comparison = rewriter.create<mlir::arith::CmpIOp>(
133138
loc, arith::CmpIPredicate::sgt, itersLeft, zero);
134139

135-
auto cond = rewriter.create<mlir::cf::CondBranchOp>(
140+
rewriter.create<mlir::cf::CondBranchOp>(
136141
loc, comparison, firstBlock, llvm::ArrayRef<mlir::Value>(), endBlock,
137142
llvm::ArrayRef<mlir::Value>());
138143

139-
// Copy loop annotations from the do loop to the loop entry condition.
140-
if (auto ann = loop.getLoopAnnotation())
141-
cond->setAttr("loop_annotation", *ann);
142-
143144
// The result of the loop operation is the values of the condition block
144145
// arguments except the induction variable on the last iteration.
145146
auto args = loop.getFinalValue()

flang/test/Fir/vector-always.fir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ func.func @_QPvector_always() -> i32 {
1313
%c10_i32 = arith.constant 10 : i32
1414
%c1_i32 = arith.constant 1 : i32
1515
%c10 = arith.constant 10 : index
16-
// CHECK: cf.cond_br %{{.*}}, ^{{.*}}, ^{{.*}} {loop_annotation = #[[ANNOTATION]]}
16+
// CHECK: cf.cond_br
17+
// CHECK-NOT: loop_annotation
18+
// CHECK: cf.br ^{{.*}} {loop_annotation = #[[ANNOTATION]]}
1719
%8:2 = fir.do_loop %arg0 = %c1 to %c10 step %c1 iter_args(%arg1 = %c1_i32) -> (index, i32) attributes {loopAnnotation = #loop_annotation} {
1820
fir.result %c1, %c1_i32 : index, i32
1921
}

flang/test/Integration/unroll.f90

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
! CHECK-LABEL: unroll_dir
44
subroutine unroll_dir
55
integer :: a(10)
6-
!dir$ unroll
7-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[UNROLL_ENABLE_FULL_ANNO:.*]]
6+
!dir$ unroll
7+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
8+
! CHECK-NOT: !llvm.loop
9+
! CHECK: br label {{.*}}, !llvm.loop ![[UNROLL_ENABLE_FULL_ANNO:.*]]
810
do i=1,10
911
a(i)=i
1012
end do
@@ -14,7 +16,9 @@ end subroutine unroll_dir
1416
subroutine unroll_dir_0
1517
integer :: a(10)
1618
!dir$ unroll 0
17-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[UNROLL_DISABLE_ANNO:.*]]
19+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
20+
! CHECK-NOT: !llvm.loop
21+
! CHECK: br label {{.*}}, !llvm.loop ![[UNROLL_DISABLE_ANNO:.*]]
1822
do i=1,10
1923
a(i)=i
2024
end do
@@ -24,7 +28,9 @@ end subroutine unroll_dir_0
2428
subroutine unroll_dir_1
2529
integer :: a(10)
2630
!dir$ unroll 1
27-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[UNROLL_DISABLE_ANNO]]
31+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
32+
! CHECK-NOT: !llvm.loop
33+
! CHECK: br label {{.*}}, !llvm.loop ![[UNROLL_DISABLE_ANNO]]
2834
do i=1,10
2935
a(i)=i
3036
end do
@@ -34,7 +40,9 @@ end subroutine unroll_dir_1
3440
subroutine unroll_dir_2
3541
integer :: a(10)
3642
!dir$ unroll 2
37-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[UNROLL_ENABLE_COUNT_2:.*]]
43+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
44+
! CHECK-NOT: !llvm.loop
45+
! CHECK: br label {{.*}}, !llvm.loop ![[UNROLL_ENABLE_COUNT_2:.*]]
3846
do i=1,10
3947
a(i)=i
4048
end do

flang/test/Integration/unroll_and_jam.f90

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
subroutine unroll_and_jam_dir
55
integer :: a(10)
66
!dir$ unroll_and_jam 4
7-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION:.*]]
7+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
8+
! CHECK-NOT: !llvm.loop
9+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION:.*]]
810
do i=1,10
911
a(i)=i
1012
end do
@@ -14,7 +16,9 @@ end subroutine unroll_and_jam_dir
1416
subroutine unroll_and_jam_dir_0
1517
integer :: a(10)
1618
!dir$ unroll_and_jam 0
17-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE:.*]]
19+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
20+
! CHECK-NOT: !llvm.loop
21+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE:.*]]
1822
do i=1,10
1923
a(i)=i
2024
end do
@@ -24,7 +28,9 @@ end subroutine unroll_and_jam_dir_0
2428
subroutine unroll_and_jam_dir_1
2529
integer :: a(10)
2630
!dir$ unroll_and_jam 1
27-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE]]
31+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
32+
! CHECK-NOT: !llvm.loop
33+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE]]
2834
do i=1,10
2935
a(i)=i
3036
end do
@@ -34,7 +40,9 @@ end subroutine unroll_and_jam_dir_1
3440
subroutine nounroll_and_jam_dir
3541
integer :: a(10)
3642
!dir$ nounroll_and_jam
37-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE]]
43+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
44+
! CHECK-NOT: !llvm.loop
45+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE]]
3846
do i=1,10
3947
a(i)=i
4048
end do
@@ -44,7 +52,9 @@ end subroutine nounroll_and_jam_dir
4452
subroutine unroll_and_jam_dir_no_factor
4553
integer :: a(10)
4654
!dir$ unroll_and_jam
47-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION_NO_FACTOR:.*]]
55+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
56+
! CHECK-NOT: !llvm.loop
57+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION_NO_FACTOR:.*]]
4858
do i=1,10
4959
a(i)=i
5060
end do

flang/test/Integration/vector-always.f90

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
subroutine vector_always
55
integer :: a(10)
66
!dir$ vector always
7-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION:.*]]
7+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
8+
! CHECK-NOT: !llvm.loop
9+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION:.*]]
810
do i=1,10
911
a(i)=i
1012
end do
@@ -14,7 +16,9 @@ end subroutine vector_always
1416
subroutine no_vector
1517
integer :: a(10)
1618
!dir$ novector
17-
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION2:.*]]
19+
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}
20+
! CHECK-NOT: !llvm.loop
21+
! CHECK: br label {{.*}}, !llvm.loop ![[ANNOTATION2:.*]]
1822
do i=1,10
1923
a(i)=i
2024
end do

0 commit comments

Comments
 (0)