[polly] [Polly] Retain vectorization for fallback loop when RTC is false (PR #165525)
Karthika Devi C via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 7 05:07:16 PST 2025
https://github.com/kartcq updated https://github.com/llvm/llvm-project/pull/165525
>From fc03c226ccaaac9eb2b48a7e4477b9b600fe67f4 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <quic_kartc at quicinc.com>
Date: Wed, 29 Oct 2025 01:14:38 -0700
Subject: [PATCH 1/3] [Polly] Retain vectorization for fallback loop when RTC
is false
When Polly generates a false runtime condition (RTC), the associated
Polly generated loop is never executed and is eventually eliminated. As
a result, the fallback loop becomes the default execution path.
Disabling vectorization for this fallback loop will be counterproductive.
This patch ensures that vectorization is only disabled when the RTC is
not false.
Change-Id: Iec0d5d4aed970788ed3b937f5e82d6e361d05f84
---
polly/lib/CodeGen/CodeGeneration.cpp | 24 ++++++++++-------
.../CodeGen/Metadata/fallback_vec_annotate.ll | 27 +++++++++++++++++++
2 files changed, 41 insertions(+), 10 deletions(-)
create mode 100644 polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index 2d8b393cc039c..2a60fc4d22fd5 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -235,15 +235,6 @@ static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI,
NodeBuilder.allocateNewArrays(StartExitBlocks);
Annotator.buildAliasScopes(S);
- // The code below annotates the "llvm.loop.vectorize.enable" to false
- // for the code flow taken when RTCs fail. Because we don't want the
- // Loop Vectorizer to come in later and vectorize the original fall back
- // loop when Polly is enabled.
- for (Loop *L : LI.getLoopsInPreorder()) {
- if (S.contains(L))
- addStringMetadataToLoop(L, "llvm.loop.vectorize.enable", 0);
- }
-
if (PerfMonitoring) {
PerfMonitor P(S, EnteringBB->getParent()->getParent());
P.initialize();
@@ -282,9 +273,22 @@ static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI,
} else {
NodeBuilder.addParameters(S.getContext().release());
Value *RTC = NodeBuilder.createRTC(AI.getRunCondition().release());
-
Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
+ auto *CI = dyn_cast<ConstantInt>(RTC);
+ // The code below annotates the "llvm.loop.vectorize.enable" to false
+ // for the code flow taken when RTCs fail. Because we don't want the
+ // Loop Vectorizer to come in later and vectorize the original fall back
+ // loop when Polly is enabled. This also prevent's multiple loop versions
+ // created by Polly with Loop Vectorizer. Also don't do this when Polly's
+ // RTC value is false, as we are left with only one version of Loop.
+ if (!(CI && CI->isZero())) {
+ for (Loop *L : LI.getLoopsInPreorder()) {
+ if (S.contains(L))
+ addStringMetadataToLoop(L, "llvm.loop.vectorize.enable", 0);
+ }
+ }
+
// Explicitly set the insert point to the end of the block to avoid that a
// split at the builder's current
// insert position would move the malloc calls to the wrong BasicBlock.
diff --git a/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll b/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
new file mode 100644
index 0000000000000..52e2f78764b03
--- /dev/null
+++ b/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
@@ -0,0 +1,27 @@
+; RUN: opt %loadNPMPolly -S -passes=polly-codegen -polly-annotate-metadata-vectorize < %s | FileCheck %s
+; RUN: opt %loadNPMPolly -S -passes=polly-codegen < %s | FileCheck %s
+
+; Verify vectorization is not disabled when RTC of Polly is false
+
+; CHECK-NOT: {{.*}} = !{!"llvm.loop.vectorize.enable", i32 0}
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "aarch64-unknown-linux-android10000"
+
+define void @ham(i64 %arg) {
+bb:
+ br label %bb1
+
+bb1: ; preds = %bb3, %bb
+ %phi = phi ptr [ %getelementptr4, %bb3 ], [ null, %bb ]
+ br label %bb2
+
+bb2: ; preds = %bb2, %bb1
+ %getelementptr = getelementptr i8, ptr %phi, i64 1
+ store i8 0, ptr %getelementptr, align 1
+ br i1 false, label %bb2, label %bb3
+
+bb3: ; preds = %bb2
+ %getelementptr4 = getelementptr i8, ptr %phi, i64 %arg
+ br label %bb1
+}
>From 183c9b624ad1858002ef6f371f4caff1137fbf54 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <quic_kartc at quicinc.com>
Date: Wed, 29 Oct 2025 01:14:38 -0700
Subject: [PATCH 2/3] [Polly] Retain vectorization for fallback loop when RTC
is unsatisfiable
When Polly generates a false runtime condition (RTC), the associated
Polly generated loop is never executed and is eventually eliminated. As
a result, the fallback loop becomes the default execution path.
Disabling vectorization for this fallback loop will be counterproductive.
This patch ensures that vectorization is only disabled when the RTC is
not false, thereby handling cases when Code Generation fails.
Change-Id: Iec0d5d4aed970788ed3b937f5e82d6e361d05f84
---
polly/lib/CodeGen/CodeGeneration.cpp | 7 ++++---
polly/test/CodeGen/Metadata/fallback_vec_annotate.ll | 1 +
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index 2a60fc4d22fd5..110bab9cbce28 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -279,9 +279,10 @@ static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI,
// The code below annotates the "llvm.loop.vectorize.enable" to false
// for the code flow taken when RTCs fail. Because we don't want the
// Loop Vectorizer to come in later and vectorize the original fall back
- // loop when Polly is enabled. This also prevent's multiple loop versions
- // created by Polly with Loop Vectorizer. Also don't do this when Polly's
- // RTC value is false, as we are left with only one version of Loop.
+ // loop when Polly is enabled. This avoids loop versioning on fallback
+ // loop by Loop Vectorizer. Don't do this when Polly's RTC value is
+ // false (due to code generation failure), as we are left with only one
+ // version of Loop.
if (!(CI && CI->isZero())) {
for (Loop *L : LI.getLoopsInPreorder()) {
if (S.contains(L))
diff --git a/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll b/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
index 52e2f78764b03..317d30649ab1d 100644
--- a/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
+++ b/polly/test/CodeGen/Metadata/fallback_vec_annotate.ll
@@ -3,6 +3,7 @@
; Verify vectorization is not disabled when RTC of Polly is false
+; CHECK: attributes {{.*}} = { "polly-optimized" }
; CHECK-NOT: {{.*}} = !{!"llvm.loop.vectorize.enable", i32 0}
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
>From 363079bd308428c7195ecfe36822e8418c264585 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <quic_kartc at quicinc.com>
Date: Wed, 29 Oct 2025 01:14:38 -0700
Subject: [PATCH 3/3] [Polly] Retain vectorization for fallback loop when RTC
is unsatisfiable
When Polly generates a false runtime condition (RTC), the associated
Polly generated loop is never executed and is eventually eliminated. As
a result, the fallback loop becomes the default execution path.
Disabling vectorization for this fallback loop will be counterproductive.
This patch ensures that vectorization is only disabled when the RTC is
not false, thereby handling cases when Code Generation fails.
Change-Id: Iec0d5d4aed970788ed3b937f5e82d6e361d05f84
---
polly/lib/CodeGen/CodeGeneration.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index 110bab9cbce28..062cdfbcfe3b5 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -273,6 +273,7 @@ static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI,
} else {
NodeBuilder.addParameters(S.getContext().release());
Value *RTC = NodeBuilder.createRTC(AI.getRunCondition().release());
+
Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
auto *CI = dyn_cast<ConstantInt>(RTC);
More information about the llvm-commits
mailing list