Skip to content

Commit 2c5597c

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 6fe5256 commit 2c5597c

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

15201520
SmallVector<Instruction *, 4> LifetimeMarkers;
1521-
SmallSet<Instruction *, 4> NoAliasInstrs;
1521+
SmallSet<Instruction *, 4> AAMetadataInstrs;
15221522
bool SrcNotDom = false;
15231523

15241524
// Recursively track the user and check whether modified alias exist.
@@ -1573,8 +1573,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
15731573
continue;
15741574
}
15751575
}
1576-
if (UI->hasMetadata(LLVMContext::MD_noalias))
1577-
NoAliasInstrs.insert(UI);
1576+
AAMetadataInstrs.insert(UI);
1577+
15781578
if (!ModRefCallback(UI))
15791579
return false;
15801580
}
@@ -1679,11 +1679,16 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
16791679
}
16801680

16811681
// As this transformation can cause memory accesses that didn't previously
1682-
// alias to begin to alias one another, we remove !noalias metadata from any
1683-
// uses of either alloca. This is conservative, but more precision doesn't
1684-
// seem worthwhile right now.
1685-
for (Instruction *I : NoAliasInstrs)
1682+
// alias to begin to alias one another, we remove !alias.scope, !noalias,
1683+
// !tbaa and !tbaa_struct metadata from any uses of either alloca.
1684+
// This is conservative, but more precision doesn't seem worthwhile
1685+
// right now.
1686+
for (Instruction *I : AAMetadataInstrs) {
1687+
I->setMetadata(LLVMContext::MD_alias_scope, nullptr);
16861688
I->setMetadata(LLVMContext::MD_noalias, nullptr);
1689+
I->setMetadata(LLVMContext::MD_tbaa, nullptr);
1690+
I->setMetadata(LLVMContext::MD_tbaa_struct, nullptr);
1691+
}
16871692

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

0 commit comments

Comments
 (0)