[polly] Bypass some profitabilty check when polly adds vectorize.enable (PR #227535)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 30 00:10:58 PDT 2026
https://github.com/rahulana-quic updated https://github.com/llvm/llvm-project/pull/227535
>From 27c6c04f72f7c74b473a785949938de8734571f7 Mon Sep 17 00:00:00 2001
From: Rahul Anand Radhakrishnan <rahulana at qti.qualcomm.com>
Date: Sun, 27 Sep 2026 22:46:39 -0700
Subject: [PATCH 1/3] Bypass some profitabilty check when polly adds
vectorize.enable
When using -polly-annotate-metadata-vectorize, selectively bypass
some profitability checks since they block some vectorization even
in genuine cases.
---
polly/include/polly/ScopDetection.h | 1 +
polly/lib/Analysis/ScopDetection.cpp | 13 +++++-
polly/lib/Analysis/ScopInfo.cpp | 2 +-
polly/lib/CodeGen/IslAst.cpp | 2 -
polly/lib/CodeGen/IslNodeBuilder.cpp | 3 --
polly/lib/CodeGen/LoopGenerators.cpp | 5 ---
.../test/CodeGen/Metadata/vec-bypass-prof.ll | 41 +++++++++++++++++++
7 files changed, 55 insertions(+), 12 deletions(-)
create mode 100644 polly/test/CodeGen/Metadata/vec-bypass-prof.ll
diff --git a/polly/include/polly/ScopDetection.h b/polly/include/polly/ScopDetection.h
index 823f6e9a86606..dbdba69c22d3d 100644
--- a/polly/include/polly/ScopDetection.h
+++ b/polly/include/polly/ScopDetection.h
@@ -119,6 +119,7 @@ extern bool PollyTrackFailures;
extern bool PollyDelinearize;
extern bool PollyUseRuntimeAliasChecks;
extern bool PollyProcessUnprofitable;
+extern bool PollyVectorizeMetadata;
extern bool PollyInvariantLoadHoisting;
extern bool PollyAllowUnsignedOperations;
extern bool PollyAllowFullFunction;
diff --git a/polly/lib/Analysis/ScopDetection.cpp b/polly/lib/Analysis/ScopDetection.cpp
index 12cbee3a95a49..a4137cdaf551c 100644
--- a/polly/lib/Analysis/ScopDetection.cpp
+++ b/polly/lib/Analysis/ScopDetection.cpp
@@ -108,6 +108,17 @@ static cl::opt<bool, true> XPollyProcessUnprofitable(
"Process scops that are unlikely to benefit from Polly optimizations."),
cl::location(PollyProcessUnprofitable), cl::cat(PollyCategory));
+// This option defines a polly mode that has differences in the polly decisions
+// with a view of vectorizing eventually. This is done by adding a
+// vectorize.enable metadata to the loops analysed and found fit by polly so
+// that the loop vectorizer can handle them.
+bool polly::PollyVectorizeMetadata;
+static cl::opt<bool, true> XPollyVectorizeMetadata(
+ "polly-annotate-metadata-vectorize",
+ cl::desc("Append vectorize enable/disable metadata from polly"),
+ cl::location(PollyVectorizeMetadata), cl::init(false),
+ cl::cat(PollyCategory));
+
static cl::list<std::string> OnlyFunctions(
"polly-only-func",
cl::desc("Only run on functions that match a regex. "
@@ -1736,7 +1747,7 @@ bool ScopDetection::hasPossiblyDistributableLoop(
bool ScopDetection::isProfitableRegion(DetectionContext &Context) const {
Region &CurRegion = Context.CurRegion;
- if (PollyProcessUnprofitable)
+ if (PollyVectorizeMetadata || PollyProcessUnprofitable)
return true;
// We can probably not do a lot on scops that only write or only read
diff --git a/polly/lib/Analysis/ScopInfo.cpp b/polly/lib/Analysis/ScopInfo.cpp
index 6757f3ba057ea..c780c7bc0c935 100644
--- a/polly/lib/Analysis/ScopInfo.cpp
+++ b/polly/lib/Analysis/ScopInfo.cpp
@@ -1942,7 +1942,7 @@ isl::set Scop::getAssumedContext() const {
}
bool Scop::isProfitable(bool ScalarsAreUnprofitable) const {
- if (PollyProcessUnprofitable)
+ if (PollyVectorizeMetadata || PollyProcessUnprofitable)
return true;
if (isEmpty())
diff --git a/polly/lib/CodeGen/IslAst.cpp b/polly/lib/CodeGen/IslAst.cpp
index 500d3783772fb..5ee7f771b59f9 100644
--- a/polly/lib/CodeGen/IslAst.cpp
+++ b/polly/lib/CodeGen/IslAst.cpp
@@ -81,8 +81,6 @@ static cl::opt<bool> DetectParallel("polly-ast-detect-parallel",
cl::desc("Detect parallelism"), cl::Hidden,
cl::cat(PollyCategory));
-extern cl::opt<bool> PollyVectorizeMetadata;
-
static cl::opt<bool>
PollyPrintAst("polly-print-ast",
cl::desc("Print the ISL abstract syntax tree"),
diff --git a/polly/lib/CodeGen/IslNodeBuilder.cpp b/polly/lib/CodeGen/IslNodeBuilder.cpp
index 126cb756a4144..1b719f1502ed6 100644
--- a/polly/lib/CodeGen/IslNodeBuilder.cpp
+++ b/polly/lib/CodeGen/IslNodeBuilder.cpp
@@ -74,9 +74,6 @@
using namespace llvm;
using namespace polly;
-// Declared in LoopGenerators.cpp
-extern llvm::cl::opt<bool> PollyVectorizeMetadata;
-
#define DEBUG_TYPE "polly-codegen"
STATISTIC(VersionedScops, "Number of SCoPs that required versioning.");
diff --git a/polly/lib/CodeGen/LoopGenerators.cpp b/polly/lib/CodeGen/LoopGenerators.cpp
index e467abea8322c..983b005bb8091 100644
--- a/polly/lib/CodeGen/LoopGenerators.cpp
+++ b/polly/lib/CodeGen/LoopGenerators.cpp
@@ -35,11 +35,6 @@ static cl::opt<int, true>
cl::Hidden, cl::location(polly::PollyNumThreads),
cl::init(0), cl::cat(PollyCategory));
-cl::opt<bool> PollyVectorizeMetadata(
- "polly-annotate-metadata-vectorize",
- cl::desc("Append vectorize enable/disable metadata from polly"),
- cl::init(false), cl::cat(PollyCategory));
-
static cl::opt<OMPGeneralSchedulingType, true> XPollyScheduling(
"polly-scheduling",
cl::desc("Scheduling type of parallel OpenMP for loops"),
diff --git a/polly/test/CodeGen/Metadata/vec-bypass-prof.ll b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
new file mode 100644
index 0000000000000..03eaa464ae954
--- /dev/null
+++ b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
@@ -0,0 +1,41 @@
+; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
+; RUN: -polly-annotate-metadata-vectorize < %s | FileCheck %s --check-prefix=VECTORIZED
+; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
+; RUN: < %s | FileCheck %s --check-prefix=NO-VECTORIZE
+
+; A simple single-level loop is unprofitable without -polly-annotate-metadata-vectorize
+;
+; VECTORIZED-LABEL: define{{.*}} @simple_add(
+; VECTORIZED-DAG: polly.stmt.for.body:
+; VECTORIZED-DAG: br {{.*}} !llvm.loop [[SIMPLE_LOOP:![0-9]+]]
+; VECTORIZED-DAG: [[SIMPLE_LOOP]] = distinct !{[[SIMPLE_LOOP]],
+; VECTORIZED-DAG: !{!"llvm.loop.vectorize.enable"}
+;
+; NO-VECTORIZE-LABEL: define{{.*}} @simple_add(
+; NO-VECTORIZE: for.body:
+; NO-VECTORIZE-NOT: polly.stmt.for.body:
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "aarch64-unknown-linux-gnu"
+
+define void @simple_add(ptr %A, i64 %n) {
+entry:
+ %cmp = icmp sgt i64 %n, 0
+ br i1 %cmp, label %for.body, label %for.end
+
+for.body:
+ %i = phi i64 [ 0, %entry ], [ %next, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i64 %i
+ %v = load i32, ptr %arrayidx, align 4
+ %inc = add nsw i32 %v, 1
+ store i32 %inc, ptr %arrayidx, align 4
+ %next = add nuw nsw i64 %i, 1
+ %cond = icmp slt i64 %next, %n
+ br i1 %cond, label %for.body, label %for.end, !llvm.loop !0
+
+for.end:
+ ret void
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.mustprogress"}
>From 338736516f3826ee58fae3d903e0b60aecc06c7a Mon Sep 17 00:00:00 2001
From: Rahul Anand Radhakrishnan <rahulana at qti.qualcomm.com>
Date: Tue, 29 Sep 2026 21:57:09 -0700
Subject: [PATCH 2/3] Explicitly pass -polly-process-unprofitable=false
---
polly/test/CodeGen/Metadata/vec-bypass-prof.ll | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/polly/test/CodeGen/Metadata/vec-bypass-prof.ll b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
index 03eaa464ae954..8e743e62f7a82 100644
--- a/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
+++ b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
@@ -1,7 +1,7 @@
; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
-; RUN: -polly-annotate-metadata-vectorize < %s | FileCheck %s --check-prefix=VECTORIZED
+; RUN: -polly-annotate-metadata-vectorize -polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=VECTORIZED
; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
-; RUN: < %s | FileCheck %s --check-prefix=NO-VECTORIZE
+; RUN: -polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=NO-VECTORIZE
; A simple single-level loop is unprofitable without -polly-annotate-metadata-vectorize
;
>From 898ce49954e375f5b3719302004d6c58a4dbdc30 Mon Sep 17 00:00:00 2001
From: Rahul Anand Radhakrishnan <rahulana at qti.qualcomm.com>
Date: Tue, 29 Sep 2026 23:13:26 -0700
Subject: [PATCH 3/3] Pass option via plugin argument
---
polly/test/CodeGen/Metadata/vec-bypass-prof.ll | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/polly/test/CodeGen/Metadata/vec-bypass-prof.ll b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
index 8e743e62f7a82..46350fe5eb7d0 100644
--- a/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
+++ b/polly/test/CodeGen/Metadata/vec-bypass-prof.ll
@@ -1,7 +1,7 @@
; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
-; RUN: -polly-annotate-metadata-vectorize -polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=VECTORIZED
+; RUN: -polly-annotate-metadata-vectorize -polly-process-unprofitable=false -plugin-arg=Polly,-polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=VECTORIZED
; RUN: opt %loadNPMPolly -S '-passes=polly<no-default-opts>' \
-; RUN: -polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=NO-VECTORIZE
+; RUN: -polly-process-unprofitable=false -plugin-arg=Polly,-polly-process-unprofitable=false < %s | FileCheck %s --check-prefix=NO-VECTORIZE
; A simple single-level loop is unprofitable without -polly-annotate-metadata-vectorize
;
More information about the llvm-commits
mailing list