Skip to content

Commit 79186af

Browse files
committed
[clang][CodeGen] Fix metadata when vectorization is disabled by pragma
There are two ways to disable vectorization with pragma, `vectorize(disable)` and `vectorize_width(1)`. The document (https://clang.llvm.org/docs/LanguageExtensions.html#vectorization-interleaving-and-predication) states: > Specifying a width/count of 1 disables the optimization, and is equivalent to `vectorize(disable)` or `interleave(disable)`. So the behavior should be the same when using either is used. However, the current implementation doesn't satisfy this. When `vectorize(disable)` is specified, it is converted internally to the same as `vectorize_width(1)`, and the metadata is generated as if vectorization is not explicitly specified as enabled or disabled. This can cause a problem when other transformations are also specified by pragma. For example, if `unroll_count(8)` is specified, the loop should have a metadata that contains the information directly, like: ``` !loop0 = !{!"loop0", !vectorize_disable, !unroll} !vectorize_disable = {...} !unroll = !{!"llvm.loop.unroll_count", i32 8} ``` But now it's attached into followup metadata for vectorization. ``` !loop0 = !{!"loop0", !vectorize_width, !followup} !vectorize_width = !{!"llvm.loop.vectorize.width", i32 1} !followup = !{!"llvm.loop.vectorize.followup_all", !unroll} !unroll = !{!"llvm.loop.unroll_count", i32 8} ``` As a result, the unroll attributes are ignored because the vectorization is not applied. This patch fixes this issue by generating metadata that disables vectorization when `vectorize(disable)` or `vectorize_width(1)` are specified. The document also says: > If the transformation is disabled (e.g. vectorize(disable)), that takes precedence over transformations option pragmas implying that transformation. Therefore, if vectorization is disabled, all other vectorize options like `vectorize_predicate` should be ignored. This patch also omits to generate attributes for vectorization when it is disabled. Fix #75839
1 parent a03b225 commit 79186af

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

clang/lib/CodeGen/CGLoopInfo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
200200
LLVMContext &Ctx = Header->getContext();
201201

202202
std::optional<bool> Enabled;
203-
if (Attrs.VectorizeEnable == LoopAttributes::Disable)
203+
if (Attrs.VectorizeEnable == LoopAttributes::Disable ||
204+
Attrs.VectorizeWidth == 1)
204205
Enabled = false;
205206
else if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
206207
Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified ||
@@ -643,9 +644,7 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
643644
case LoopHintAttr::Disable:
644645
switch (Option) {
645646
case LoopHintAttr::Vectorize:
646-
// Disable vectorization by specifying a width of 1.
647-
setVectorizeWidth(1);
648-
setVectorizeScalable(LoopAttributes::Unspecified);
647+
setVectorizeEnable(false);
649648
break;
650649
case LoopHintAttr::Interleave:
651650
// Disable interleaving by speciyfing a count of 1.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ void loop4(int *List, int Length) {
4040
List[i] = i * 2;
4141
}
4242

43-
// CHECK: ![[LOOP1]] = distinct !{![[LOOP1]], [[MP:![0-9]+]], ![[VEC_WIDTH_1:.*]], ![[FIXED_WIDTH:.*]], ![[VEC_ENABLE:.*]]}
44-
// CHECK: ![[VEC_WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
45-
// CHECK: ![[FIXED_WIDTH]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
46-
// CHECK: ![[VEC_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
43+
// CHECK: ![[LOOP1]] = distinct !{![[LOOP1]], [[MP:![0-9]+]], ![[VEC_DISABLE:[0-9]+]]}
44+
// CHECK: ![[VEC_DISABLE]] = !{!"llvm.loop.vectorize.enable", i1 false}
4745

48-
// CHECK: ![[LOOP2]] = distinct !{![[LOOP2]], [[MP]], ![[VEC_WIDTH_2:.*]], ![[FIXED_WIDTH:.*]], ![[VEC_ENABLE]]}
46+
// CHECK: ![[LOOP2]] = distinct !{![[LOOP2]], [[MP]], ![[VEC_WIDTH_2:.*]], ![[FIXED_WIDTH:[0-9]+]], ![[VEC_ENABLE:[0-9]+]]}
4947
// CHECK: ![[VEC_WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
48+
// CHECK: ![[FIXED_WIDTH]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
49+
// CHECK: ![[VEC_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
5050

51-
// CHECK: ![[LOOP3]] = distinct !{![[LOOP3]], [[MP]], ![[VEC_WIDTH_1]], ![[FIXED_WIDTH]]}
51+
// CHECK: ![[LOOP3]] = distinct !{![[LOOP3]], [[MP]], ![[VEC_DISABLE]]}
5252

5353
// CHECK: ![[LOOP4]] = distinct !{![[LOOP4]], [[MP]], ![[VEC_WIDTH_2]], ![[FIXED_WIDTH]], ![[VEC_ENABLE]]}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void test3(int *List, int Length) {
3737
List[i] = i * 2;
3838
}
3939

40-
// Check that disabling vectorization means a vectorization width of 1, and
41-
// also that vectorization_predicate isn't enabled.
40+
// Check that vectorize is disabled, and also that vectorization_predicate is
41+
// ignored.
4242
void test4(int *List, int Length) {
4343
// CHECK-LABEL: @{{.*}}test4{{.*}}(
4444
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP4:.*]]
@@ -48,7 +48,7 @@ void test4(int *List, int Length) {
4848
List[i] = i * 2;
4949
}
5050

51-
// Check that vectorize and vectorize_predicate are disabled.
51+
// Check that vectorize is disabled and vectorize_predicate is ignored.
5252
void test5(int *List, int Length) {
5353
// CHECK-LABEL: @{{.*}}test5{{.*}}(
5454
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP5:.*]]
@@ -114,16 +114,16 @@ void test9(int *List, int Length) {
114114
// CHECK-NEXT: ![[LOOP3]] = distinct !{![[LOOP3]], [[MP]], [[GEN6]], [[GEN3]]}
115115

116116
// CHECK-NEXT: ![[LOOP4]] = distinct !{![[LOOP4]], [[MP]], [[GEN10:![0-9]+]]}
117-
// CHECK-NEXT: [[GEN10]] = !{!"llvm.loop.vectorize.width", i32 1}
117+
// CHECK-NEXT: [[GEN10]] = !{!"llvm.loop.vectorize.enable", i1 false}
118118

119-
// CHECK-NEXT: ![[LOOP5]] = distinct !{![[LOOP5]], [[MP]], [[GEN6]], [[GEN10]]}
119+
// CHECK-NEXT: ![[LOOP5]] = distinct !{![[LOOP5]], [[MP]], [[GEN10]]}
120120

121-
// CHECK-NEXT: ![[LOOP6]] = distinct !{![[LOOP6]], [[MP]], [[GEN8]], [[GEN10]], [[GEN11:![0-9]+]]}
122-
// CHECK-NEXT: [[GEN11]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
121+
// CHECK-NEXT: ![[LOOP6]] = distinct !{![[LOOP6]], [[MP]], [[GEN10]]}
123122

124-
// CHECK-NEXT: ![[LOOP7]] = distinct !{![[LOOP7]], [[MP]], [[GEN8]], [[GEN12:![0-9]+]], [[GEN11]], [[GEN3]]}
125-
// CHECK-NEXT: [[GEN12]] = !{!"llvm.loop.vectorize.width", i32 4}
123+
// CHECK-NEXT: ![[LOOP7]] = distinct !{![[LOOP7]], [[MP]], [[GEN8]], [[GEN11:![0-9]+]], [[GEN12:![0-9]+]], [[GEN3]]}
124+
// CHECK-NEXT: [[GEN11]] = !{!"llvm.loop.vectorize.width", i32 4}
125+
// CHECK-NEXT: [[GEN12]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
126126

127-
// CHECK-NEXT: ![[LOOP8]] = distinct !{![[LOOP8]], [[MP]], [[GEN6]], [[GEN10]], [[GEN11]]}
127+
// CHECK-NEXT: ![[LOOP8]] = distinct !{![[LOOP8]], [[MP]], [[GEN10]]}
128128

129-
// CHECK-NEXT: ![[LOOP9]] = distinct !{![[LOOP9]], [[MP]], [[GEN6]], [[GEN12]], [[GEN11]], [[GEN3]]}
129+
// CHECK-NEXT: ![[LOOP9]] = distinct !{![[LOOP9]], [[MP]], [[GEN6]], [[GEN11]], [[GEN12]], [[GEN3]]}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ void interleave_test(int *List, int Length) {
5353
// CHECK: ![[INTERLEAVE_1]] = !{!"llvm.loop.interleave.count", i32 1}
5454
// CHECK: ![[INTENABLE_1]] = !{!"llvm.loop.vectorize.enable", i1 true}
5555
// CHECK: ![[ACCESS_GROUP_8]] = distinct !{}
56-
// CHECK: ![[LOOP2_HINTS]] = distinct !{![[LOOP2_HINTS]], [[MP]], ![[PARALLEL_ACCESSES_11:[0-9]+]], ![[UNROLL_DISABLE]], ![[WIDTH_1:[0-9]+]], ![[INTENABLE_1]]}
56+
// CHECK: ![[LOOP2_HINTS]] = distinct !{![[LOOP2_HINTS]], [[MP]], ![[PARALLEL_ACCESSES_11:[0-9]+]], ![[UNROLL_DISABLE]], ![[VECTORIZE_DISABLED:[0-9]+]]}
5757
// CHECK: ![[PARALLEL_ACCESSES_11]] = !{!"llvm.loop.parallel_accesses", ![[ACCESS_GROUP_8]]}
58-
// CHECK: ![[WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
58+
// CHECK: ![[VECTORIZE_DISABLED]] = !{!"llvm.loop.vectorize.enable", i1 false}

clang/test/CodeGenCXX/pragma-loop.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void for_test_scalable(int *List, int Length) {
194194
}
195195
}
196196

197-
// Verify for loop is performing scalable vectorization
197+
// Verify for loop is NOT performing vectorization because the width is 1
198198
void for_test_scalable_1(int *List, int Length) {
199199
#pragma clang loop vectorize_width(1, scalable) interleave_count(4) unroll(disable) distribute(disable)
200200
for (int i = 0; i < Length; i++) {
@@ -203,6 +203,24 @@ void for_test_scalable_1(int *List, int Length) {
203203
}
204204
}
205205

206+
// Verify for loop is performing scalable vectorization
207+
void for_test_scalable_2(int *List, int Length) {
208+
#pragma clang loop vectorize_width(2, scalable) interleave_count(4) unroll(disable) distribute(disable)
209+
for (int i = 0; i < Length; i++) {
210+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_20:.*]]
211+
List[i] = i * 2;
212+
}
213+
}
214+
215+
// Verify unroll attributes are directly attached to the loop metadata
216+
void for_test_vectorize_disable_unroll(int *List, int Length) {
217+
#pragma clang loop vectorize(disable) unroll_count(8)
218+
for (int i = 0; i < Length; i++) {
219+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_21:.*]]
220+
List[i] = i * 2;
221+
}
222+
}
223+
206224
// CHECK-DAG: ![[MP:[0-9]+]] = !{!"llvm.loop.mustprogress"}
207225

208226
// CHECK-DAG: ![[UNROLL_DISABLE:[0-9]+]] = !{!"llvm.loop.unroll.disable"}
@@ -220,9 +238,9 @@ void for_test_scalable_1(int *List, int Length) {
220238
// CHECK-DAG: ![[INTERLEAVE_16:[0-9]+]] = !{!"llvm.loop.interleave.count", i32 16}
221239

222240
// CHECK-DAG: ![[VECTORIZE_ENABLE:[0-9]+]] = !{!"llvm.loop.vectorize.enable", i1 true}
241+
// CHECK-DAG: ![[VECTORIZE_DISABLE:[0-9]+]] = !{!"llvm.loop.vectorize.enable", i1 false}
223242
// CHECK-DAG: ![[FIXED_VEC:[0-9]+]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
224243
// CHECK-DAG: ![[SCALABLE_VEC:[0-9]+]] = !{!"llvm.loop.vectorize.scalable.enable", i1 true}
225-
// CHECK-DAG: ![[WIDTH_1:[0-9]+]] = !{!"llvm.loop.vectorize.width", i32 1}
226244
// CHECK-DAG: ![[WIDTH_2:[0-9]+]] = !{!"llvm.loop.vectorize.width", i32 2}
227245
// CHECK-DAG: ![[WIDTH_5:[0-9]+]] = !{!"llvm.loop.vectorize.width", i32 5}
228246
// CHECK-DAG: ![[WIDTH_6:[0-9]+]] = !{!"llvm.loop.vectorize.width", i32 6}
@@ -241,7 +259,7 @@ void for_test_scalable_1(int *List, int Length) {
241259

242260
// CHECK-DAG: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2]], ![[FIXED_VEC]], ![[INTERLEAVE_2]], ![[VECTORIZE_ENABLE]]}
243261

244-
// CHECK-DAG: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]]}
262+
// CHECK-DAG: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[VECTORIZE_DISABLE]]}
245263

246264
// CHECK-DAG: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[MP]], ![[WIDTH_2]], ![[FIXED_VEC]], ![[INTERLEAVE_2]], ![[VECTORIZE_ENABLE]], ![[FOLLOWUP_VECTOR_3]]}
247265

@@ -269,4 +287,7 @@ void for_test_scalable_1(int *List, int Length) {
269287

270288
// CHECK-DAG: ![[LOOP_17]] = distinct !{![[LOOP_17]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[FIXED_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
271289
// CHECK-DAG: ![[LOOP_18]] = distinct !{![[LOOP_18]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
272-
// CHECK-DAG: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
290+
// CHECK-DAG: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[VECTORIZE_DISABLE]]}
291+
// CHECK-DAG: ![[LOOP_20]] = distinct !{![[LOOP_20]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_2]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
292+
293+
// CHECK-DAG: ![[LOOP_21]] = distinct !{![[LOOP_21]], ![[MP]], ![[VECTORIZE_DISABLE]], ![[UNROLL_8]]}

0 commit comments

Comments
 (0)