Skip to content

Commit 74ddacd

Browse files
malJajMeinersbur
authored andcommitted
[Clang] Ensure vector predication loop metadata is always emitted when pragma is specified.
This patch ensures that vector predication and vectorization width pragmas work together correctly/as expected. Specifically, this patch fixes the issue that when vectorization_width > 1, the vector predication behaviour (this would matter if it has NOT been disabled explicitly by a pragma) was getting ignored, which was incorrect. The fix here removes the dependence of vector predication on the vectorization width. The loop metadata corresponding to clang loop pragma vectorize_predicate is always emitted, if the pragma is specified, even if vectorization is disabled by vectorize_width(1) or vectorize(disable) since the option is also used for interleaving by the LoopVectorize pass. Reviewed By: dmgreen, Meinersbur Differential Revision: https://reviews.llvm.org/D94779
1 parent 53187f1 commit 74ddacd

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

clang/lib/CodeGen/CGLoopInfo.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,10 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
250250
Args.push_back(nullptr);
251251
Args.append(LoopProperties.begin(), LoopProperties.end());
252252

253-
// Setting vectorize.predicate
253+
// Setting vectorize.predicate when it has been specified and vectorization
254+
// has not been disabled.
254255
bool IsVectorPredicateEnabled = false;
255-
if (Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified &&
256-
Attrs.VectorizeEnable != LoopAttributes::Disable &&
257-
Attrs.VectorizeWidth < 1) {
258-
256+
if (Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified) {
259257
IsVectorPredicateEnabled =
260258
(Attrs.VectorizePredicateEnable == LoopAttributes::Enable);
261259

@@ -303,7 +301,8 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
303301
// explicitly requested fixed-width vectorization, i.e.
304302
// vectorize.scalable.enable is false.
305303
if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
306-
IsVectorPredicateEnabled || Attrs.VectorizeWidth > 1 ||
304+
(IsVectorPredicateEnabled && Attrs.VectorizeWidth != 1) ||
305+
Attrs.VectorizeWidth > 1 ||
307306
Attrs.VectorizeScalable == LoopAttributes::Enable ||
308307
(Attrs.VectorizeScalable == LoopAttributes::Disable &&
309308
Attrs.VectorizeWidth != 1)) {

clang/test/CodeGenCXX/pragma-loop-predicate.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,49 @@ void test5(int *List, int Length) {
5858
List[i] = i * 2;
5959
}
6060

61+
// Check that vectorize_predicate is ignored when vectorization width is 1
62+
void test6(int *List, int Length) {
63+
// CHECK-LABEL: @{{.*}}test6{{.*}}(
64+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP6:.*]]
65+
66+
#pragma clang loop vectorize_predicate(disable) vectorize_width(1)
67+
for (int i = 0; i < Length; i++)
68+
List[i] = i * 2;
69+
}
70+
71+
72+
// Check that vectorize_width(!=1) does not affect vectorize_predicate.
73+
void test7(int *List, int Length) {
74+
// CHECK-LABEL: @{{.*}}test7{{.*}}(
75+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP7:.*]]
76+
77+
#pragma clang loop vectorize_predicate(disable) vectorize_width(4)
78+
for (int i = 0; i < Length; i++)
79+
List[i] = i * 2;
80+
}
81+
82+
83+
// Check that vectorize_predicate is ignored when vectorization width is 1
84+
void test8(int *List, int Length) {
85+
// CHECK-LABEL: @{{.*}}test8{{.*}}(
86+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP8:.*]]
87+
88+
#pragma clang loop vectorize_predicate(enable) vectorize_width(1)
89+
for (int i = 0; i < Length; i++)
90+
List[i] = i * 2;
91+
}
92+
93+
94+
// Check that vectorize_width(!=1) does not affect vectorize_predicate.
95+
void test9(int *List, int Length) {
96+
// CHECK-LABEL: @{{.*}}test9{{.*}}(
97+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP9:.*]]
98+
99+
#pragma clang loop vectorize_predicate(enable) vectorize_width(4)
100+
for (int i = 0; i < Length; i++)
101+
List[i] = i * 2;
102+
}
103+
61104
// CHECK: ![[LOOP0]] = distinct !{![[LOOP0]], [[MP:![0-9]+]], [[GEN3:![0-9]+]]}
62105
// CHECK: [[MP]] = !{!"llvm.loop.mustprogress"}
63106
// CHECK-NEXT: [[GEN3]] = !{!"llvm.loop.vectorize.enable", i1 true}
@@ -73,4 +116,14 @@ void test5(int *List, int Length) {
73116
// CHECK-NEXT: ![[LOOP4]] = distinct !{![[LOOP4]], [[MP]], [[GEN10:![0-9]+]]}
74117
// CHECK-NEXT: [[GEN10]] = !{!"llvm.loop.vectorize.width", i32 1}
75118

76-
// CHECK-NEXT: ![[LOOP5]] = distinct !{![[LOOP5]], [[MP]], [[GEN10]]}
119+
// CHECK-NEXT: ![[LOOP5]] = distinct !{![[LOOP5]], [[MP]], [[GEN6]], [[GEN10]]}
120+
121+
// CHECK-NEXT: ![[LOOP6]] = distinct !{![[LOOP6]], [[MP]], [[GEN8]], [[GEN10]], [[GEN11:![0-9]+]]}
122+
// CHECK-NEXT: [[GEN11]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
123+
124+
// CHECK-NEXT: ![[LOOP7]] = distinct !{![[LOOP7]], [[MP]], [[GEN8]], [[GEN12:![0-9]+]], [[GEN11]], [[GEN3]]}
125+
// CHECK-NEXT: [[GEN12]] = !{!"llvm.loop.vectorize.width", i32 4}
126+
127+
// CHECK-NEXT: ![[LOOP8]] = distinct !{![[LOOP8]], [[MP]], [[GEN6]], [[GEN10]], [[GEN11]]}
128+
129+
// CHECK-NEXT: ![[LOOP9]] = distinct !{![[LOOP9]], [[MP]], [[GEN6]], [[GEN12]], [[GEN11]], [[GEN3]]}

0 commit comments

Comments
 (0)