Skip to content

Commit 0216854

Browse files
author
Sjoerd Meijer
committed
[Clang] Pragma vectorize_width() implies vectorize(enable)
Let's try this again; this has been reverted/recommited a few times. Last time this got reverted because for this loop: void a() { #pragma clang loop vectorize(disable) for (;;) ; } vectorisation was incorrectly enabled and the vectorize.enable metadata was set due to a logic error. But with this fixed, we now imply vectorisation when: 1) vectorisation is enabled, which means: VectorizeWidth > 1, 2) and don't want to add it when it is disabled or enabled, otherwise we would be incorrectly setting it or duplicating the metadata, respectively. This should fix PR27643. Differential Revision: https://reviews.llvm.org/D69628
1 parent 987e732 commit 0216854

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

clang/lib/CodeGen/CGLoopInfo.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,18 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
286286
Args.push_back(MDNode::get(Ctx, Vals));
287287
}
288288

289-
// Setting vectorize.enable
289+
// vectorize.enable is set if:
290+
// 1) loop hint vectorize.enable is set, or
291+
// 2) it is implied when vectorize.predicate is set, or
292+
// 3) it is implied when vectorize.width is set.
290293
if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
291-
IsVectorPredicateEnabled) {
292-
Metadata *Vals[] = {
293-
MDString::get(Ctx, "llvm.loop.vectorize.enable"),
294-
ConstantAsMetadata::get(ConstantInt::get(
295-
llvm::Type::getInt1Ty(Ctx),
296-
IsVectorPredicateEnabled
297-
? true
298-
: (Attrs.VectorizeEnable == LoopAttributes::Enable)))};
299-
Args.push_back(MDNode::get(Ctx, Vals));
294+
IsVectorPredicateEnabled ||
295+
Attrs.VectorizeWidth > 1 ) {
296+
bool AttrVal = Attrs.VectorizeEnable != LoopAttributes::Disable;
297+
Args.push_back(
298+
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"),
299+
ConstantAsMetadata::get(ConstantInt::get(
300+
llvm::Type::getInt1Ty(Ctx), AttrVal))}));
300301
}
301302

302303
if (FollowupHasTransforms)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
2+
3+
void loop1(int *List, int Length) {
4+
// CHECK-LABEL: @{{.*}}loop1{{.*}}(
5+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP1:.*]]
6+
7+
#pragma clang loop vectorize(enable) vectorize_width(1)
8+
for (int i = 0; i < Length; i++)
9+
List[i] = i * 2;
10+
}
11+
12+
// Here, vectorize.enable should be set, obviously, but also check that
13+
// metadata isn't added twice.
14+
void loop2(int *List, int Length) {
15+
// CHECK-LABEL: @{{.*}}loop2{{.*}}(
16+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP2:.*]]
17+
18+
#pragma clang loop vectorize(enable) vectorize_width(2)
19+
for (int i = 0; i < Length; i++)
20+
List[i] = i * 2;
21+
}
22+
23+
// Test that we do *not* imply vectorize.enable.
24+
void loop3(int *List, int Length) {
25+
// CHECK-LABEL: @{{.*}}loop3{{.*}}(
26+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP3:.*]]
27+
28+
#pragma clang loop vectorize_width(1)
29+
for (int i = 0; i < Length; i++)
30+
List[i] = i * 2;
31+
}
32+
33+
// Test that we *do* imply vectorize.enable.
34+
void loop4(int *List, int Length) {
35+
// CHECK-LABEL: @{{.*}}loop4{{.*}}(
36+
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP4:.*]]
37+
38+
#pragma clang loop vectorize_width(2)
39+
for (int i = 0; i < Length; i++)
40+
List[i] = i * 2;
41+
}
42+
43+
// CHECK: ![[LOOP1]] = distinct !{![[LOOP1]], ![[VEC_WIDTH_1:.*]], ![[VEC_ENABLE:.*]]}
44+
// CHECK: ![[VEC_WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
45+
// CHECK: ![[VEC_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
46+
47+
// CHECK: ![[LOOP2]] = distinct !{![[LOOP2]], ![[VEC_WIDTH_2:.*]], ![[VEC_ENABLE]]}
48+
// CHECK: ![[VEC_WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
49+
50+
// CHECK: ![[LOOP3]] = distinct !{![[LOOP3]], ![[VEC_WIDTH_1]]}
51+
52+
// CHECK: ![[LOOP4]] = distinct !{![[LOOP4]], ![[VEC_WIDTH_2]], ![[VEC_ENABLE]]}

clang/test/CodeGenCXX/pragma-loop.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,20 @@ void template_test(double *List, int Length) {
161161
// CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[UNROLL_FULL:.*]]}
162162
// CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
163163

164-
// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_8:.*]], ![[INTERLEAVE_4:.*]]}
164+
// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_8:.*]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
165165
// CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
166166
// CHECK: ![[DISTRIBUTE_DISABLE]] = !{!"llvm.loop.distribute.enable", i1 false}
167167
// CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
168168
// CHECK: ![[INTERLEAVE_4]] = !{!"llvm.loop.interleave.count", i32 4}
169+
// CHECK: ![[VECTORIZE_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
169170

170-
// CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[INTERLEAVE_4:.*]], ![[INTENABLE_1:.*]], ![[FOLLOWUP_VECTOR_3:.*]]}
171-
// CHECK: ![[INTENABLE_1]] = !{!"llvm.loop.vectorize.enable", i1 true}
171+
// CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE]], ![[FOLLOWUP_VECTOR_3:.*]]}
172172
// CHECK: ![[FOLLOWUP_VECTOR_3]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_3:.*]]}
173173
// CHECK: ![[AFTER_VECTOR_3]] = distinct !{![[AFTER_VECTOR_3]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
174174
// CHECK: ![[ISVECTORIZED]] = !{!"llvm.loop.isvectorized"}
175175
// CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
176176

177-
// CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]]}
177+
// CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]], ![[VECTORIZE_ENABLE]]}
178178
// CHECK: ![[WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
179179
// CHECK: ![[INTERLEAVE_2]] = !{!"llvm.loop.interleave.count", i32 2}
180180

@@ -185,7 +185,7 @@ void template_test(double *List, int Length) {
185185
// CHECK: ![[FOLLOWUP_VECTOR_6]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_6:.*]]}
186186
// CHECK: ![[AFTER_VECTOR_6]] = distinct !{![[AFTER_VECTOR_6]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
187187

188-
// CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[WIDTH_5:.*]]}
188+
// CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[WIDTH_5:.*]], ![[VECTORIZE_ENABLE]]}
189189
// CHECK: ![[WIDTH_5]] = !{!"llvm.loop.vectorize.width", i32 5}
190190

191191
// CHECK: ![[LOOP_8]] = distinct !{![[LOOP_8]], ![[WIDTH_5:.*]]}
@@ -207,11 +207,11 @@ void template_test(double *List, int Length) {
207207
// CHECK: ![[AFTER_VECTOR_12]] = distinct !{![[AFTER_VECTOR_12]], ![[ISVECTORIZED:.*]], ![[UNROLL_24:.*]]}
208208
// CHECK: ![[UNROLL_24]] = !{!"llvm.loop.unroll.count", i32 24}
209209

210-
// CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[WIDTH_8:.*]], ![[INTERLEAVE_16:.*]], ![[FOLLOWUP_VECTOR_13:.*]]}
210+
// CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[WIDTH_8:.*]], ![[INTERLEAVE_16:.*]], ![[VECTORIZE_ENABLE]], ![[FOLLOWUP_VECTOR_13:.*]]}
211211
// CHECK: ![[INTERLEAVE_16]] = !{!"llvm.loop.interleave.count", i32 16}
212212
// CHECK: ![[FOLLOWUP_VECTOR_13]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_13:.*]]}
213213
// CHECK: ![[AFTER_VECTOR_13]] = distinct !{![[AFTER_VECTOR_13]], ![[ISVECTORIZED:.*]], ![[UNROLL_32:.*]]}
214214
// CHECK: ![[UNROLL_32]] = !{!"llvm.loop.unroll.count", i32 32}
215215

216-
// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[WIDTH_10:.*]]}
216+
// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[WIDTH_10:.*]], ![[VECTORIZE_ENABLE]]}
217217
// CHECK: ![[WIDTH_10]] = !{!"llvm.loop.vectorize.width", i32 10}

0 commit comments

Comments
 (0)