[llvm-branch-commits] [llvm] [Cloning] Move undeclared scopes into cloned disjoint domains (PR #242034)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 12 12:13:56 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Krzysztof Drewniak (krzysz00)
<details>
<summary>Changes</summary>
When a scope from a disjoint-scope domain is cloned (because its
llvm.experimental.noalias.scope.decl is in the code being duplicated),
the clone is placed into a clone of the domain. Scopes of that domain
without a declaration in the duplicated code, however, were left in the
original domain. InstCombine removes declarations of scopes it considers
unused, so this happens in practice: an !alias.scope list {b, c} where
only c is still declared became {b, c'} when copying for loop unrolling, with c' in
the new domain. ScopedNoAliasAA then only compared {c'} against a
!noalias {c'} list in that domain and concluded NoAlias, while the
original {b, c} list was not noalias with {c}.
Fix this by also cloning, into the cloned domain, any scope of that
domain used by the adapted instructions. Renaming a whole disjoint
domain consistently within the duplicated code preserves the relations
inside it. I don't expect the theoretical precision loss here to be an
issue in practice.
Fixes #<!-- -->241246.
AI disclosure: Claude wrote this code, which I reviewed, and wrote
much of this commit message (which I've tweaked, but I was fairly
happy with the wording). The original bug was also an AI fuzz result.
Co-Authored-By: Claude Opus 5.5 <noreply@<!-- -->anthropic.com>
---
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
---
Full diff: https://github.com/llvm/llvm-project/pull/242034.diff
5 Files Affected:
- (modified) llvm/include/llvm/Transforms/Utils/Cloning.h (+14-6)
- (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+8-4)
- (modified) llvm/lib/Transforms/Utils/CloneFunction.cpp (+41-19)
- (modified) llvm/test/Transforms/JumpThreading/noalias-scope-decl.ll (+4-3)
- (modified) llvm/test/Transforms/LoopUnroll/noalias-disjoint-domain.ll (+28-26)
``````````diff
diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h
index 119084eea7238..afc0b5f7031a5 100644
--- a/llvm/include/llvm/Transforms/Utils/Cloning.h
+++ b/llvm/include/llvm/Transforms/Utils/Cloning.h
@@ -421,18 +421,26 @@ identifyNoAliasScopesToClone(BasicBlock::iterator Start,
/// MDNode onto the cloned scope.
/// Be aware that the cloned scopes are still part of the original scope domain,
/// unless that domain has disjoint scopes, in which case they are placed in a
-/// clone of the domain.
-LLVM_ABI void cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
- DenseMap<MDNode *, MDNode *> &ClonedScopes,
- StringRef Ext, LLVMContext &Context);
+/// clone of the domain. ClonedDomains contains the mapping of such domains onto
+/// their clones.
+LLVM_ABI void
+cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
+ DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ DenseMap<const MDNode *, MDNode *> &ClonedDomains,
+ StringRef Ext, LLVMContext &Context);
/// Adapt the metadata for the specified instruction according to the
/// provided mapping. This is normally used after cloning an instruction, when
/// some noalias scopes needed to be cloned.
+/// Scopes that belong to a domain in ClonedDomains but are not in ClonedScopes
+/// (because they have no declaration in the cloned code) are also cloned into
+/// the cloned domain, using 'Ext' as an extension to their name, and added to
+/// ClonedScopes.
LLVM_ABI void
adaptNoAliasScopes(llvm::Instruction *I,
- const DenseMap<MDNode *, MDNode *> &ClonedScopes,
- LLVMContext &Context);
+ DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ const DenseMap<const MDNode *, MDNode *> &ClonedDomains,
+ StringRef Ext, LLVMContext &Context);
/// Clone the specified noalias decl scopes. Then adapt all instructions in the
/// NewBlocks basicblocks to the cloned versions.
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 7f49cb33901d2..e31814da6a09e 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2084,9 +2084,11 @@ void JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
// visible at the same time.
SmallVector<MDNode *> NoAliasScopes;
DenseMap<MDNode *, MDNode *> ClonedScopes;
+ DenseMap<const MDNode *, MDNode *> ClonedDomains;
LLVMContext &Context = PredBB->getContext();
identifyNoAliasScopesToClone(BI, BE, NoAliasScopes);
- cloneNoAliasScopes(NoAliasScopes, ClonedScopes, "thread", Context);
+ cloneNoAliasScopes(NoAliasScopes, ClonedScopes, ClonedDomains, "thread",
+ Context);
auto CloneAndRemapDbgInfo = [&](Instruction *NewInst, Instruction *From) {
auto DVRRange = NewInst->cloneDebugInfoFrom(From);
@@ -2102,7 +2104,7 @@ void JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
New->setName(BI->getName());
New->insertInto(NewBB, NewBB->end());
ValueMapping[&*BI] = New;
- adaptNoAliasScopes(New, ClonedScopes, Context);
+ adaptNoAliasScopes(New, ClonedScopes, ClonedDomains, "thread", Context);
CloneAndRemapDbgInfo(New, &*BI);
if (const DebugLoc &DL = New->getDebugLoc())
@@ -2705,16 +2707,18 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
// could conclude two accesses on different paths do not alias when they may.
SmallVector<MDNode *> NoAliasScopes;
DenseMap<MDNode *, MDNode *> ClonedScopes;
+ DenseMap<const MDNode *, MDNode *> ClonedDomains;
LLVMContext &Context = PredBB->getContext();
identifyNoAliasScopesToClone(BI, BB->end(), NoAliasScopes);
- cloneNoAliasScopes(NoAliasScopes, ClonedScopes, "thread", Context);
+ cloneNoAliasScopes(NoAliasScopes, ClonedScopes, ClonedDomains, "thread",
+ Context);
// Clone the non-phi instructions of BB into PredBB, keeping track of the
// mapping and using it to remap operands in the cloned instructions.
for (; BI != BB->end(); ++BI) {
Instruction *New = BI->clone();
New->insertInto(PredBB, OldPredBranch->getIterator());
- adaptNoAliasScopes(New, ClonedScopes, Context);
+ adaptNoAliasScopes(New, ClonedScopes, ClonedDomains, "thread", Context);
// Remap operands to patch up intra-block references.
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp
index 83abfc15b60a6..c8a0ec0f7dabd 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -1155,25 +1155,30 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
return NewBB;
}
+/// Create a clone of the scope \p SNANode in \p Domain, whose name is that of
+/// the original scope extended with \p Ext.
+static MDNode *createClonedScope(AliasScopeNode SNANode, MDNode *Domain,
+ StringRef Ext, MDBuilder &MDB) {
+ std::string Name;
+ auto ScopeName = SNANode.getName();
+ if (!ScopeName.empty())
+ Name = (Twine(ScopeName) + ":" + Ext).str();
+ else
+ Name = std::string(Ext);
+ return MDB.createAnonymousAliasScope(Domain, Name);
+}
+
void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ DenseMap<const MDNode *, MDNode *> &ClonedDomains,
StringRef Ext, LLVMContext &Context) {
MDBuilder MDB(Context);
- DenseMap<const MDNode *, MDNode *> ClonedDomains;
-
for (MDNode *ScopeList : NoAliasDeclScopes) {
for (const MDOperand &MDOp : ScopeList->operands()) {
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
AliasScopeNode SNANode(MD);
- std::string Name;
- auto ScopeName = SNANode.getName();
- if (!ScopeName.empty())
- Name = (Twine(ScopeName) + ":" + Ext).str();
- else
- Name = std::string(Ext);
-
// A cloned scope has to go into a clone of its domain if that domain
// has disjoint scopes: the copies of a duplicated access are in the
// same memory region, so they must not become implicitly noalias with
@@ -1189,22 +1194,35 @@ void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
NewDomain = ClonedDomain;
}
- MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
- ClonedScopes.insert(std::make_pair(MD, NewScope));
+ ClonedScopes.insert(std::make_pair(
+ MD, createClonedScope(SNANode, NewDomain, Ext, MDB)));
}
}
}
}
-void llvm::adaptNoAliasScopes(Instruction *I,
- const DenseMap<MDNode *, MDNode *> &ClonedScopes,
- LLVMContext &Context) {
+void llvm::adaptNoAliasScopes(
+ Instruction *I, DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ const DenseMap<const MDNode *, MDNode *> &ClonedDomains, StringRef Ext,
+ LLVMContext &Context) {
+ MDBuilder MDB(Context);
+
auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
bool NeedsReplacement = false;
SmallVector<Metadata *, 8> NewScopeList;
for (const MDOperand &MDOp : ScopeList->operands()) {
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
- if (auto *NewMD = ClonedScopes.lookup(MD)) {
+ MDNode *NewMD = ClonedScopes.lookup(MD);
+ // Undeclared scopes must follow their domain: a list split across the
+ // original and cloned domains can be wrongly considered noalias.
+ if (!NewMD && !ClonedDomains.empty()) {
+ AliasScopeNode SNANode(MD);
+ if (MDNode *NewDomain = ClonedDomains.lookup(SNANode.getDomain())) {
+ NewMD = createClonedScope(SNANode, NewDomain, Ext, MDB);
+ ClonedScopes[MD] = NewMD;
+ }
+ }
+ if (NewMD) {
NewScopeList.push_back(NewMD);
NeedsReplacement = true;
continue;
@@ -1237,14 +1255,16 @@ void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
return;
DenseMap<MDNode *, MDNode *> ClonedScopes;
+ DenseMap<const MDNode *, MDNode *> ClonedDomains;
LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
<< NoAliasDeclScopes.size() << " node(s)\n");
- cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
+ cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, ClonedDomains, Ext,
+ Context);
// Identify instructions using metadata that needs adaptation
for (BasicBlock *NewBlock : NewBlocks)
for (Instruction &I : *NewBlock)
- adaptNoAliasScopes(&I, ClonedScopes, Context);
+ adaptNoAliasScopes(&I, ClonedScopes, ClonedDomains, Ext, Context);
}
void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
@@ -1254,17 +1274,19 @@ void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
return;
DenseMap<MDNode *, MDNode *> ClonedScopes;
+ DenseMap<const MDNode *, MDNode *> ClonedDomains;
LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
<< NoAliasDeclScopes.size() << " node(s)\n");
- cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
+ cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, ClonedDomains, Ext,
+ Context);
// Identify instructions using metadata that needs adaptation
assert(IStart->getParent() == IEnd->getParent() && "different basic block ?");
auto ItStart = IStart->getIterator();
auto ItEnd = IEnd->getIterator();
++ItEnd; // IEnd is included, increment ItEnd to get the end of the range
for (auto &I : llvm::make_range(ItStart, ItEnd))
- adaptNoAliasScopes(&I, ClonedScopes, Context);
+ adaptNoAliasScopes(&I, ClonedScopes, ClonedDomains, Ext, Context);
}
void llvm::identifyNoAliasScopesToClone(
diff --git a/llvm/test/Transforms/JumpThreading/noalias-scope-decl.ll b/llvm/test/Transforms/JumpThreading/noalias-scope-decl.ll
index a4bc9e572d01b..25f02b86f0355 100644
--- a/llvm/test/Transforms/JumpThreading/noalias-scope-decl.ll
+++ b/llvm/test/Transforms/JumpThreading/noalias-scope-decl.ll
@@ -166,7 +166,7 @@ define void @clone_disjoint_domain_undeclared_scope(ptr %p, i1 %c) {
; CHECK-NEXT: ret void
; CHECK: fb:
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META14]])
-; CHECK-NEXT: store i8 0, ptr [[P]], align 1, !alias.scope [[META23:![0-9]+]]
+; CHECK-NEXT: store i8 0, ptr [[P]], align 1, !alias.scope [[META24:![0-9]+]]
; CHECK-NEXT: ret void
;
entry:
@@ -227,6 +227,7 @@ declare void @llvm.experimental.noalias.scope.decl(metadata)
; CHECK: [[META19]] = !{[[META20:![0-9]+]]}
; CHECK: [[META20]] = distinct !{[[META20]], [[META21:![0-9]+]], !"disjoint scope1:thread"}
; CHECK: [[META21]] = distinct !{[[META21]], i1 true, !"disjoint domain"}
-; CHECK: [[META22]] = !{[[META20]], [[META18]]}
-; CHECK: [[META23]] = !{[[META15]], [[META18]]}
+; CHECK: [[META22]] = !{[[META20]], [[META23:![0-9]+]]}
+; CHECK: [[META23]] = distinct !{[[META23]], [[META21]], !"disjoint scope2:thread"}
+; CHECK: [[META24]] = !{[[META15]], [[META18]]}
;.
diff --git a/llvm/test/Transforms/LoopUnroll/noalias-disjoint-domain.ll b/llvm/test/Transforms/LoopUnroll/noalias-disjoint-domain.ll
index d898bc65af4c6..308dcf3efbc83 100644
--- a/llvm/test/Transforms/LoopUnroll/noalias-disjoint-domain.ll
+++ b/llvm/test/Transforms/LoopUnroll/noalias-disjoint-domain.ll
@@ -42,10 +42,10 @@ define void @undeclared_noalias_scope_in_cloned_domain(ptr %p) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
-; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META9:![0-9]+]])
-; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !noalias [[META12:![0-9]+]]
-; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META14:![0-9]+]])
-; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !noalias [[META17:![0-9]+]]
+; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META10:![0-9]+]])
+; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !noalias [[META13:![0-9]+]]
+; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META15:![0-9]+]])
+; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !noalias [[META18:![0-9]+]]
; CHECK-NEXT: ret void
;
entry:
@@ -71,10 +71,10 @@ define void @undeclared_scope_in_non_disjoint_domain(ptr %p) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
-; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META18:![0-9]+]])
-; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !alias.scope [[META21:![0-9]+]]
-; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META23:![0-9]+]])
-; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !alias.scope [[META25:![0-9]+]]
+; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META20:![0-9]+]])
+; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !alias.scope [[META23:![0-9]+]]
+; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META25:![0-9]+]])
+; CHECK-NEXT: store i32 0, ptr [[P]], align 4, !alias.scope [[META27:![0-9]+]]
; CHECK-NEXT: ret void
;
entry:
@@ -122,22 +122,24 @@ declare void @llvm.experimental.noalias.scope.decl(metadata)
; CHECK: [[META5]] = !{[[META6:![0-9]+]]}
; CHECK: [[META6]] = distinct !{[[META6]], [[META7:![0-9]+]], !"c:It1"}
; CHECK: [[META7]] = distinct !{[[META7]], i1 true, !"disjoint domain"}
-; CHECK: [[META8]] = !{[[META4]], [[META6]]}
-; CHECK: [[META9]] = !{[[META10:![0-9]+]]}
-; CHECK: [[META10]] = distinct !{[[META10]], [[META11:![0-9]+]], !"src"}
-; CHECK: [[META11]] = distinct !{[[META11]], i1 true, !"disjoint domain 2"}
-; CHECK: [[META12]] = !{[[META13:![0-9]+]], [[META10]]}
-; CHECK: [[META13]] = distinct !{[[META13]], [[META11]], !"other"}
-; CHECK: [[META14]] = !{[[META15:![0-9]+]]}
-; CHECK: [[META15]] = distinct !{[[META15]], [[META16:![0-9]+]], !"src:It1"}
-; CHECK: [[META16]] = distinct !{[[META16]], i1 true, !"disjoint domain 2"}
-; CHECK: [[META17]] = !{[[META13]], [[META15]]}
-; CHECK: [[META18]] = !{[[META19:![0-9]+]]}
-; CHECK: [[META19]] = distinct !{[[META19]], [[META20:![0-9]+]], !"c"}
-; CHECK: [[META20]] = distinct !{[[META20]], i1 false, !"domain"}
-; CHECK: [[META21]] = !{[[META22:![0-9]+]], [[META19]]}
-; CHECK: [[META22]] = distinct !{[[META22]], [[META20]], !"b"}
-; CHECK: [[META23]] = !{[[META24:![0-9]+]]}
-; CHECK: [[META24]] = distinct !{[[META24]], [[META20]], !"c:It1"}
-; CHECK: [[META25]] = !{[[META22]], [[META24]]}
+; CHECK: [[META8]] = !{[[META9:![0-9]+]], [[META6]]}
+; CHECK: [[META9]] = distinct !{[[META9]], [[META7]], !"b:It1"}
+; CHECK: [[META10]] = !{[[META11:![0-9]+]]}
+; CHECK: [[META11]] = distinct !{[[META11]], [[META12:![0-9]+]], !"src"}
+; CHECK: [[META12]] = distinct !{[[META12]], i1 true, !"disjoint domain 2"}
+; CHECK: [[META13]] = !{[[META14:![0-9]+]], [[META11]]}
+; CHECK: [[META14]] = distinct !{[[META14]], [[META12]], !"other"}
+; CHECK: [[META15]] = !{[[META16:![0-9]+]]}
+; CHECK: [[META16]] = distinct !{[[META16]], [[META17:![0-9]+]], !"src:It1"}
+; CHECK: [[META17]] = distinct !{[[META17]], i1 true, !"disjoint domain 2"}
+; CHECK: [[META18]] = !{[[META19:![0-9]+]], [[META16]]}
+; CHECK: [[META19]] = distinct !{[[META19]], [[META17]], !"other:It1"}
+; CHECK: [[META20]] = !{[[META21:![0-9]+]]}
+; CHECK: [[META21]] = distinct !{[[META21]], [[META22:![0-9]+]], !"c"}
+; CHECK: [[META22]] = distinct !{[[META22]], i1 false, !"domain"}
+; CHECK: [[META23]] = !{[[META24:![0-9]+]], [[META21]]}
+; CHECK: [[META24]] = distinct !{[[META24]], [[META22]], !"b"}
+; CHECK: [[META25]] = !{[[META26:![0-9]+]]}
+; CHECK: [[META26]] = distinct !{[[META26]], [[META22]], !"c:It1"}
+; CHECK: [[META27]] = !{[[META24]], [[META26]]}
;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/242034
More information about the llvm-branch-commits
mailing list