Skip to content

Commit 9ce82ae

Browse files
DominikAdamskishiltian
authored andcommitted
[LLVM][MemCpyOpt] Unify alias tags if we optimize allocas (llvm#129537)
Optimization of alloca instructions may lead to invalid alias tags. Incorrect alias tags can result in incorrect optimization outcomes for Fortran source code compiled by Flang with flags: `-O3 -mmlir -local-alloc-tbaa -flto`. This commit removes alias tags when memcpy optimization replaces two arrays with one array, thus ensuring correct compilation of Fortran source code using flags: `-O3 -mmlir -local-alloc-tbaa -flto`. This commit is also a proposal to fix the reported issue: llvm#133984 --------- Co-authored-by: Shilei Tian <[email protected]> (cherry picked from commit 716b02d)
1 parent ccda9ec commit 9ce82ae

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
15351535
// to remove them.
15361536

15371537
SmallVector<Instruction *, 4> LifetimeMarkers;
1538-
SmallSet<Instruction *, 4> NoAliasInstrs;
1538+
SmallSet<Instruction *, 4> AAMetadataInstrs;
15391539
bool SrcNotDom = false;
15401540

15411541
// Recursively track the user and check whether modified alias exist.
@@ -1590,8 +1590,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
15901590
continue;
15911591
}
15921592
}
1593-
if (UI->hasMetadata(LLVMContext::MD_noalias))
1594-
NoAliasInstrs.insert(UI);
1593+
AAMetadataInstrs.insert(UI);
1594+
15951595
if (!ModRefCallback(UI))
15961596
return false;
15971597
}
@@ -1696,11 +1696,16 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
16961696
}
16971697

16981698
// As this transformation can cause memory accesses that didn't previously
1699-
// alias to begin to alias one another, we remove !noalias metadata from any
1700-
// uses of either alloca. This is conservative, but more precision doesn't
1701-
// seem worthwhile right now.
1702-
for (Instruction *I : NoAliasInstrs)
1699+
// alias to begin to alias one another, we remove !alias.scope, !noalias,
1700+
// !tbaa and !tbaa_struct metadata from any uses of either alloca.
1701+
// This is conservative, but more precision doesn't seem worthwhile
1702+
// right now.
1703+
for (Instruction *I : AAMetadataInstrs) {
1704+
I->setMetadata(LLVMContext::MD_alias_scope, nullptr);
17031705
I->setMetadata(LLVMContext::MD_noalias, nullptr);
1706+
I->setMetadata(LLVMContext::MD_tbaa, nullptr);
1707+
I->setMetadata(LLVMContext::MD_tbaa_struct, nullptr);
1708+
}
17041709

17051710
LLVM_DEBUG(dbgs() << "Stack Move: Performed staack-move optimization\n");
17061711
NumStackMove++;

0 commit comments

Comments
 (0)