diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp index 4a9092842858b..b2b569a43038c 100644 --- a/clang/lib/CodeGen/CGLoopInfo.cpp +++ b/clang/lib/CodeGen/CGLoopInfo.cpp @@ -221,18 +221,6 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, return createUnrollAndJamMetadata(Attrs, LoopProperties, HasUserTransforms); } - // Apply all loop properties to the vectorized loop. - SmallVector FollowupLoopProperties; - FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end()); - - // Don't vectorize an already vectorized loop. - FollowupLoopProperties.push_back( - MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized"))); - - bool FollowupHasTransforms = false; - SmallVector Followup = createUnrollAndJamMetadata( - Attrs, FollowupLoopProperties, FollowupHasTransforms); - SmallVector Args; Args.append(LoopProperties.begin(), LoopProperties.end()); @@ -286,22 +274,46 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, // 5) it is implied when vectorize.width is unset (0) and the user // explicitly requested fixed-width vectorization, i.e. // vectorize.scalable.enable is false. + bool VectorizeEnabled = false; if (Attrs.VectorizeEnable != LoopAttributes::Unspecified || (IsVectorPredicateEnabled && Attrs.VectorizeWidth != 1) || Attrs.VectorizeWidth > 1 || Attrs.VectorizeScalable == LoopAttributes::Enable || (Attrs.VectorizeScalable == LoopAttributes::Disable && Attrs.VectorizeWidth != 1)) { - bool AttrVal = Attrs.VectorizeEnable != LoopAttributes::Disable; + VectorizeEnabled = Attrs.VectorizeEnable != LoopAttributes::Disable; Args.push_back( MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"), ConstantAsMetadata::get(ConstantInt::get( - llvm::Type::getInt1Ty(Ctx), AttrVal))})); + llvm::Type::getInt1Ty(Ctx), VectorizeEnabled))})); } - if (FollowupHasTransforms) - Args.push_back( - createFollowupMetadata("llvm.loop.vectorize.followup_all", Followup)); + // Apply all loop properties to the vectorized loop. + SmallVector FollowupLoopProperties; + + // If vectorization is not explicitly enabled, the follow-up metadata will be + // directly appended to the list currently being created. In that case, adding + // LoopProperties to FollowupLoopProperties would result in duplication. + if (VectorizeEnabled) + FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end()); + + // Don't vectorize an already vectorized loop. + FollowupLoopProperties.push_back( + MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized"))); + + bool FollowupHasTransforms = false; + SmallVector Followup = createUnrollAndJamMetadata( + Attrs, FollowupLoopProperties, FollowupHasTransforms); + + if (FollowupHasTransforms) { + // If vectorization is explicitly enabled, we create a follow-up metadata, + // otherwise directly add the contents of it to Args. + if (VectorizeEnabled) + Args.push_back( + createFollowupMetadata("llvm.loop.vectorize.followup_all", Followup)); + else + Args.append(Followup.begin(), Followup.end()); + } HasUserTransforms = true; return Args; diff --git a/clang/test/CodeGenCXX/pragma-loop.cpp b/clang/test/CodeGenCXX/pragma-loop.cpp index 4857299f1c037..8cb3346247daf 100644 --- a/clang/test/CodeGenCXX/pragma-loop.cpp +++ b/clang/test/CodeGenCXX/pragma-loop.cpp @@ -203,6 +203,43 @@ void for_test_scalable_1(int *List, int Length) { } } +// Verify for loop is not performing vectorization +void for_test_width_1(int *List, int Length) { +#pragma clang loop vectorize_width(1) interleave_count(4) unroll(disable) distribute(disable) + for (int i = 0; i < Length; i++) { + // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_20:.*]] + List[i] = i * 2; + } +} + +// Verify for loop is not performing vectorization +void for_test_fixed_1(int *List, int Length) { +#pragma clang loop vectorize_width(1, fixed) interleave_count(4) unroll(disable) distribute(disable) + for (int i = 0; i < Length; i++) { + // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_21:.*]] + List[i] = i * 2; + } +} + + +// Verify unroll attributes are directly attached to the loop metadata +void for_test_vectorize_disable_unroll(int *List, int Length) { +#pragma clang loop vectorize(disable) unroll_count(8) + for (int i = 0; i < Length; i++) { + // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_22:.*]] + List[i] = i * 2; + } +} + +// Verify unroll attributes are directly attached to the loop metadata +void for_test_interleave_vectorize_disable_unroll(int *List, int Length) { +#pragma clang loop vectorize(disable) interleave_count(4) unroll_count(8) + for (int i = 0; i < Length; i++) { + // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_23:.*]] + List[i] = i * 2; + } +} + // CHECK-DAG: ![[MP:[0-9]+]] = !{!"llvm.loop.mustprogress"} // CHECK-DAG: ![[UNROLL_DISABLE:[0-9]+]] = !{!"llvm.loop.unroll.disable"} @@ -270,3 +307,7 @@ void for_test_scalable_1(int *List, int Length) { // CHECK-DAG: ![[LOOP_17]] = distinct !{![[LOOP_17]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[FIXED_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]} // CHECK-DAG: ![[LOOP_18]] = distinct !{![[LOOP_18]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]} // CHECK-DAG: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4]], ![[VECTORIZE_ENABLE]]} +// CHECK-DAG: ![[LOOP_20]] = distinct !{![[LOOP_20]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[FIXED_VEC]], ![[INTERLEAVE_4]]} +// CHECK-DAG: ![[LOOP_21]] = distinct !{![[LOOP_21]], ![[MP]], ![[UNROLL_DISABLE]], ![[DISTRIBUTE_DISABLE]], ![[WIDTH_1]], ![[FIXED_VEC]], ![[INTERLEAVE_4]]} +// CHECK-DAG: ![[LOOP_22]] = distinct !{![[LOOP_22]], ![[MP]], ![[WIDTH_1]], ![[ISVECTORIZED]], ![[UNROLL_8]]} +// CHECK-DAG: ![[LOOP_23]] = distinct !{![[LOOP_23]], ![[MP]], ![[WIDTH_1]], ![[INTERLEAVE_4]], ![[ISVECTORIZED]], ![[UNROLL_8]]}