[polly] [Polly] Fix codegen assertions to account for DefinedBehaviorContext (PR #209188)
Karthika Devi C via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 15 05:29:01 PDT 2026
https://github.com/kartcq updated https://github.com/llvm/llvm-project/pull/209188
>From c92c9e79a96fe41cc1b574dc9d1beca654573365 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <kartc at qti.qualcomm.com>
Date: Mon, 13 Jul 2026 04:53:05 -0700
Subject: [PATCH 1/3] [Polly] Fix codegen assertions to account for
DefinedBehaviorContext
DeLICM may produce new read access relations whose domain is restricted
to the DefinedBehaviorContext (e.g., only valid when a parameter ensures
no UB). The validation in setNewAccessRelation already accounts for this,
but the debug assertions in createNewAccesses and generateScalarLoads
did not, causing false assertion failures during code generation.
Intersect the checked domains with getBestKnownDefinedBehaviorContext()
to match the contract that DeLICM relies on.
Fixes #205732
---
polly/lib/CodeGen/BlockGenerators.cpp | 5 +++++
polly/lib/CodeGen/IslNodeBuilder.cpp | 8 ++++++++
2 files changed, 13 insertions(+)
diff --git a/polly/lib/CodeGen/BlockGenerators.cpp b/polly/lib/CodeGen/BlockGenerators.cpp
index be01f24f562b2..877bb48ad650a 100644
--- a/polly/lib/CodeGen/BlockGenerators.cpp
+++ b/polly/lib/CodeGen/BlockGenerators.cpp
@@ -555,6 +555,11 @@ void BlockGenerator::generateScalarLoads(
#ifndef NDEBUG
auto StmtDom =
Stmt.getDomain().intersect_params(Stmt.getParent()->getContext());
+ // Restrict to defined behavior context to match DeLICM's contract:
+ // new read accesses are only required to cover the defined-behavior
+ // subset of the domain.
+ StmtDom = StmtDom.intersect_params(
+ Stmt.getParent()->getBestKnownDefinedBehaviorContext());
auto AccDom = MA->getAccessRelation().domain();
assert(!StmtDom.is_subset(AccDom).is_false() &&
"Scalar must be loaded in all statement instances");
diff --git a/polly/lib/CodeGen/IslNodeBuilder.cpp b/polly/lib/CodeGen/IslNodeBuilder.cpp
index 924f6533b0a81..5d232afdf0642 100644
--- a/polly/lib/CodeGen/IslNodeBuilder.cpp
+++ b/polly/lib/CodeGen/IslNodeBuilder.cpp
@@ -803,6 +803,14 @@ IslNodeBuilder::createNewAccesses(ScopStmt *Stmt,
Stmt->getParent()->getContext().release());
SchedDom = isl_set_intersect_params(
SchedDom, Stmt->getParent()->getContext().release());
+ // Restrict to defined behavior context to match DeLICM's contract:
+ // new read accesses are only required to cover the defined-behavior
+ // subset of the domain.
+ auto *DefinedBehavior =
+ Stmt->getParent()->getBestKnownDefinedBehaviorContext().release();
+ SchedDom =
+ isl_set_intersect_params(SchedDom, isl_set_copy(DefinedBehavior));
+ Dom = isl_set_intersect_params(Dom, DefinedBehavior);
assert(isl_set_is_subset(SchedDom, AccDom) &&
"Access relation not defined on full schedule domain");
assert(isl_set_is_subset(Dom, AccDom) &&
>From face89ddaf6e10a542ed9f54ce5a075304d09df2 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <kartc at qti.qualcomm.com>
Date: Tue, 14 Jul 2026 21:37:34 -0700
Subject: [PATCH 2/3] [Polly] Use isl_bool_false in subset assertions for
clarity
---
polly/lib/CodeGen/IslNodeBuilder.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/polly/lib/CodeGen/IslNodeBuilder.cpp b/polly/lib/CodeGen/IslNodeBuilder.cpp
index 5d232afdf0642..9fba0dbfb71de 100644
--- a/polly/lib/CodeGen/IslNodeBuilder.cpp
+++ b/polly/lib/CodeGen/IslNodeBuilder.cpp
@@ -811,9 +811,9 @@ IslNodeBuilder::createNewAccesses(ScopStmt *Stmt,
SchedDom =
isl_set_intersect_params(SchedDom, isl_set_copy(DefinedBehavior));
Dom = isl_set_intersect_params(Dom, DefinedBehavior);
- assert(isl_set_is_subset(SchedDom, AccDom) &&
+ assert(isl_set_is_subset(SchedDom, AccDom) != isl_bool_false &&
"Access relation not defined on full schedule domain");
- assert(isl_set_is_subset(Dom, AccDom) &&
+ assert(isl_set_is_subset(Dom, AccDom) != isl_bool_false &&
"Access relation not defined on full domain");
isl_set_free(AccDom);
isl_set_free(SchedDom);
>From 299eca7635ea59867575c6f5e5861340a94dcbaf Mon Sep 17 00:00:00 2001
From: Karthika Devi C <kartc at qti.qualcomm.com>
Date: Wed, 15 Jul 2026 05:16:40 -0700
Subject: [PATCH 3/3] [Polly] Add regression test for issue #205732
---
polly/test/CodeGen/issue205732.ll | 40 +++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
create mode 100644 polly/test/CodeGen/issue205732.ll
diff --git a/polly/test/CodeGen/issue205732.ll b/polly/test/CodeGen/issue205732.ll
new file mode 100644
index 0000000000000..35dc786df1c4b
--- /dev/null
+++ b/polly/test/CodeGen/issue205732.ll
@@ -0,0 +1,40 @@
+; RUN: opt %loadNPMPolly -passes=polly -S %s | FileCheck %s
+;
+; https://github.com/llvm/llvm-project/issues/205732
+; When DeLICM maps a scalar to an array element, the new access relation
+; may only be valid within the DefinedBehaviorContext (i.e., for parameter
+; values where the original program has no undefined behavior). Code
+; generation must not assert failure for such partial-domain read accesses.
+;
+; CHECK: polly.start:
+
+define void @foo(i32 %w, ptr %dst, i64 %n) {
+entry:
+ br label %for.outer
+
+for.outer:
+ %i = phi i64 [ %i.next, %for.mid.exit ], [ 0, %entry ]
+ br label %for.mid
+
+for.mid:
+ br label %for.inner
+
+for.inner:
+ %0 = phi i32 [ 0, %for.mid ], [ 1, %for.inner ]
+ %1 = load i16, ptr null, align 2
+ %cond1 = icmp eq i32 0, %w
+ br i1 %cond1, label %for.inner.exit, label %for.inner
+
+for.inner.exit:
+ br i1 true, label %for.mid.exit, label %for.mid
+
+for.mid.exit:
+ %ptr = getelementptr [4 x i8], ptr %dst, i64 %i
+ store i32 %0, ptr %ptr, align 4
+ %i.next = add i64 %i, 1
+ %cond2 = icmp eq i64 %i, %n
+ br i1 %cond2, label %for.outer.exit, label %for.outer
+
+for.outer.exit:
+ ret void
+}
More information about the llvm-commits
mailing list