Skip to content

Commit 0692572

Browse files
authored
[clang][CodeGen] Fix metadata when vectorization is disabled by pragma (#135163)
Currently, when specifying `vectorize(disable) unroll_count(8)`, the generated metadata appears as follows: ``` !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} ``` Since the metadata `!vectorize_width` implies that the vectorization is disabled, the vectorization process is skipped, and the `!followup` metadata is not processed correctly. This patch addresses the issue by directly appending properties to the metadata node when vectorization is disabled, instead of creating a new follow-up MDNode. In the above case, the generated metadata will now look like this: ``` !loop0 = !{!"loop0", !vectorize_width, !vectorize_width, !unroll} !vectorize_width = !{!"llvm.loop.vectorize.width", i32 1} !unroll = !{!"llvm.loop.unroll_count", i32 8} ```
1 parent f56211e commit 0692572

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

clang/lib/CodeGen/CGLoopInfo.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,6 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
221221
return createUnrollAndJamMetadata(Attrs, LoopProperties, HasUserTransforms);
222222
}
223223

224-
// Apply all loop properties to the vectorized loop.
225-
SmallVector<Metadata *, 4> FollowupLoopProperties;
226-
FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
227-
228-
// Don't vectorize an already vectorized loop.
229-
FollowupLoopProperties.push_back(
230-
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized")));
231-
232-
bool FollowupHasTransforms = false;
233-
SmallVector<Metadata *, 4> Followup = createUnrollAndJamMetadata(
234-
Attrs, FollowupLoopProperties, FollowupHasTransforms);
235-
236224
SmallVector<Metadata *, 4> Args;
237225
Args.append(LoopProperties.begin(), LoopProperties.end());
238226

@@ -286,22 +274,46 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
286274
// 5) it is implied when vectorize.width is unset (0) and the user
287275
// explicitly requested fixed-width vectorization, i.e.
288276
// vectorize.scalable.enable is false.
277+
bool VectorizeEnabled = false;
289278
if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
290279
(IsVectorPredicateEnabled && Attrs.VectorizeWidth != 1) ||
291280
Attrs.VectorizeWidth > 1 ||
292281
Attrs.VectorizeScalable == LoopAttributes::Enable ||
293282
(Attrs.VectorizeScalable == LoopAttributes::Disable &&
294283
Attrs.VectorizeWidth != 1)) {
295-
bool AttrVal = Attrs.VectorizeEnable != LoopAttributes::Disable;
284+
VectorizeEnabled = Attrs.VectorizeEnable != LoopAttributes::Disable;
296285
Args.push_back(
297286
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"),
298287
ConstantAsMetadata::get(ConstantInt::get(
299-
llvm::Type::getInt1Ty(Ctx), AttrVal))}));
288+
llvm::Type::getInt1Ty(Ctx), VectorizeEnabled))}));
300289
}
301290

302-
if (FollowupHasTransforms)
303-
Args.push_back(
304-
createFollowupMetadata("llvm.loop.vectorize.followup_all", Followup));
291+
// Apply all loop properties to the vectorized loop.
292+
SmallVector<Metadata *, 4> FollowupLoopProperties;
293+
294+
// If vectorization is not explicitly enabled, the follow-up metadata will be
295+
// directly appended to the list currently being created. In that case, adding
296+
// LoopProperties to FollowupLoopProperties would result in duplication.
297+
if (VectorizeEnabled)
298+
FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
299+
300+
// Don't vectorize an already vectorized loop.
301+
FollowupLoopProperties.push_back(
302+
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized")));
303+
304+
bool FollowupHasTransforms = false;
305+
SmallVector<Metadata *, 4> Followup = createUnrollAndJamMetadata(
306+
Attrs, FollowupLoopProperties, FollowupHasTransforms);
307+
308+
if (FollowupHasTransforms) {
309+
// If vectorization is explicitly enabled, we create a follow-up metadata,
310+
// otherwise directly add the contents of it to Args.
311+
if (VectorizeEnabled)
312+
Args.push_back(
313+
createFollowupMetadata("llvm.loop.vectorize.followup_all", Followup));
314+
else
315+
Args.append(Followup.begin(), Followup.end());
316+
}
305317

306318
HasUserTransforms = true;
307319
return Args;

clang/test/CodeGenCXX/pragma-loop.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,43 @@ void for_test_scalable_1(int *List, int Length) {
203203
}
204204
}
205205

206+
// Verify for loop is not performing vectorization
207+
void for_test_width_1(int *List, int Length) {
208+
#pragma clang loop vectorize_width(1) 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 for loop is not performing vectorization
216+
void for_test_fixed_1(int *List, int Length) {
217+
#pragma clang loop vectorize_width(1, fixed) interleave_count(4) unroll(disable) distribute(disable)
218+
for (int i = 0; i < Length; i++) {
219+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_21:.*]]
220+
List[i] = i * 2;
221+
}
222+
}
223+
224+
225+
// Verify unroll attributes are directly attached to the loop metadata
226+
void for_test_vectorize_disable_unroll(int *List, int Length) {
227+
#pragma clang loop vectorize(disable) unroll_count(8)
228+
for (int i = 0; i < Length; i++) {
229+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_22:.*]]
230+
List[i] = i * 2;
231+
}
232+
}
233+
234+
// Verify unroll attributes are directly attached to the loop metadata
235+
void for_test_interleave_vectorize_disable_unroll(int *List, int Length) {
236+
#pragma clang loop vectorize(disable) interleave_count(4) unroll_count(8)
237+
for (int i = 0; i < Length; i++) {
238+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_23:.*]]
239+
List[i] = i * 2;
240+
}
241+
}
242+
206243
// CHECK-DAG: ![[MP:[0-9]+]] = !{!"llvm.loop.mustprogress"}
207244

208245
// CHECK-DAG: ![[UNROLL_DISABLE:[0-9]+]] = !{!"llvm.loop.unroll.disable"}
@@ -270,3 +307,7 @@ void for_test_scalable_1(int *List, int Length) {
270307
// CHECK-DAG: ![[LOOP_17]] = distinct !{![[LOOP_17]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[FIXED_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
271308
// CHECK-DAG: ![[LOOP_18]] = distinct !{![[LOOP_18]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
272309
// CHECK-DAG: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]}
310+
// CHECK-DAG: ![[LOOP_20]] = distinct !{![[LOOP_20]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[FIXED_VEC]], ![[INTERLEAVE_4]]}
311+
// CHECK-DAG: ![[LOOP_21]] = distinct !{![[LOOP_21]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[FIXED_VEC]], ![[INTERLEAVE_4]]}
312+
// CHECK-DAG: ![[LOOP_22]] = distinct !{![[LOOP_22]], ![[MP]], ![[WIDTH_1]], ![[ISVECTORIZED]], ![[UNROLL_8]]}
313+
// CHECK-DAG: ![[LOOP_23]] = distinct !{![[LOOP_23]], ![[MP]], ![[WIDTH_1]], ![[INTERLEAVE_4]], ![[ISVECTORIZED]], ![[UNROLL_8]]}

0 commit comments

Comments
 (0)