Skip to content

Commit 4e6e476

Browse files
authored
[MemCpyOpt] Merge alias metadatas when replacing arguments (llvm#67539)
Alias metadata may no longer be valid after replacing the call argument. Fix this by merging it with the memcpy alias metadata. This fixes a miscompilation encountered in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Failing.20tests.20when.20rustc.20is.20compiled.20with.201.20CGU.
1 parent cc62782 commit 4e6e476

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,17 @@ static bool writtenBetween(MemorySSA *MSSA, BatchAAResults &AA,
336336
return !MSSA->dominates(Clobber, Start);
337337
}
338338

339+
// Update AA metadata
340+
static void combineAAMetadata(Instruction *ReplInst, Instruction *I) {
341+
// FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
342+
// handled here, but combineMetadata doesn't support them yet
343+
unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
344+
LLVMContext::MD_noalias,
345+
LLVMContext::MD_invariant_group,
346+
LLVMContext::MD_access_group};
347+
combineMetadata(ReplInst, I, KnownIDs, true);
348+
}
349+
339350
/// When scanning forward over instructions, we look for some other patterns to
340351
/// fold away. In particular, this looks for stores to neighboring locations of
341352
/// memory. If it sees enough consecutive ones, it attempts to merge them
@@ -1096,16 +1107,9 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
10961107
MSSA->getMemoryAccess(C));
10971108
}
10981109

1099-
// Update AA metadata
1100-
// FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
1101-
// handled here, but combineMetadata doesn't support them yet
1102-
unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1103-
LLVMContext::MD_noalias,
1104-
LLVMContext::MD_invariant_group,
1105-
LLVMContext::MD_access_group};
1106-
combineMetadata(C, cpyLoad, KnownIDs, true);
1110+
combineAAMetadata(C, cpyLoad);
11071111
if (cpyLoad != cpyStore)
1108-
combineMetadata(C, cpyStore, KnownIDs, true);
1112+
combineAAMetadata(C, cpyStore);
11091113

11101114
++NumCallSlot;
11111115
return true;
@@ -1961,6 +1965,7 @@ bool MemCpyOptPass::processImmutArgument(CallBase &CB, unsigned ArgNo) {
19611965
<< " " << CB << "\n");
19621966

19631967
// Otherwise we're good! Update the immut argument.
1968+
combineAAMetadata(&CB, MDep);
19641969
CB.setArgOperand(ArgNo, MDep->getSource());
19651970
++NumMemCpyInstr;
19661971
return true;

llvm/test/Transforms/MemCpyOpt/memcpy.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,11 @@ define void @immut_valid_align_branched(i1 %c, ptr noalias align 4 %val) {
695695
ret void
696696
}
697697

698-
; FIXME: This is a miscompile.
698+
; Merge/drop noalias metadata when replacing parameter.
699699
define void @immut_param_noalias_metadata(ptr align 4 byval(i32) %ptr) {
700700
; CHECK-LABEL: @immut_param_noalias_metadata(
701-
; CHECK-NEXT: call void @f(ptr noalias nocapture readonly [[PTR:%.*]]), !alias.scope !0
701+
; CHECK-NEXT: store i32 1, ptr [[PTR:%.*]], align 4, !noalias !0
702+
; CHECK-NEXT: call void @f(ptr noalias nocapture readonly [[PTR]])
702703
; CHECK-NEXT: ret void
703704
;
704705
%tmp = alloca i32, align 4

0 commit comments

Comments
 (0)