Skip to content

Commit 3271922

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 907734e commit 3271922

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
@@ -1466,7 +1466,7 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
14661466
// to remove them.
14671467

14681468
SmallVector<Instruction *, 4> LifetimeMarkers;
1469-
SmallSet<Instruction *, 4> NoAliasInstrs;
1469+
SmallSet<Instruction *, 4> AAMetadataInstrs;
14701470
bool SrcNotDom = false;
14711471

14721472
// Recursively track the user and check whether modified alias exist.
@@ -1521,8 +1521,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
15211521
continue;
15221522
}
15231523
}
1524-
if (UI->hasMetadata(LLVMContext::MD_noalias))
1525-
NoAliasInstrs.insert(UI);
1524+
AAMetadataInstrs.insert(UI);
1525+
15261526
if (!ModRefCallback(UI))
15271527
return false;
15281528
}
@@ -1627,11 +1627,16 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
16271627
}
16281628

16291629
// As this transformation can cause memory accesses that didn't previously
1630-
// alias to begin to alias one another, we remove !noalias metadata from any
1631-
// uses of either alloca. This is conservative, but more precision doesn't
1632-
// seem worthwhile right now.
1633-
for (Instruction *I : NoAliasInstrs)
1630+
// alias to begin to alias one another, we remove !alias.scope, !noalias,
1631+
// !tbaa and !tbaa_struct metadata from any uses of either alloca.
1632+
// This is conservative, but more precision doesn't seem worthwhile
1633+
// right now.
1634+
for (Instruction *I : AAMetadataInstrs) {
1635+
I->setMetadata(LLVMContext::MD_alias_scope, nullptr);
16341636
I->setMetadata(LLVMContext::MD_noalias, nullptr);
1637+
I->setMetadata(LLVMContext::MD_tbaa, nullptr);
1638+
I->setMetadata(LLVMContext::MD_tbaa_struct, nullptr);
1639+
}
16351640

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

0 commit comments

Comments
 (0)