Skip to content

[MemCpyOpt] Merge alias metadatas when replacing arguments #67539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ static bool writtenBetween(MemorySSA *MSSA, BatchAAResults &AA,
return !MSSA->dominates(Clobber, Start);
}

// Update AA metadata
static void combineAAMetadata(Instruction *ReplInst, Instruction *I) {
// FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
// handled here, but combineMetadata doesn't support them yet
unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
LLVMContext::MD_noalias,
LLVMContext::MD_invariant_group,
LLVMContext::MD_access_group};
combineMetadata(ReplInst, I, KnownIDs, true);
}

/// When scanning forward over instructions, we look for some other patterns to
/// fold away. In particular, this looks for stores to neighboring locations of
/// memory. If it sees enough consecutive ones, it attempts to merge them
Expand Down Expand Up @@ -1096,16 +1107,9 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
MSSA->getMemoryAccess(C));
}

// Update AA metadata
// FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
// handled here, but combineMetadata doesn't support them yet
unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
LLVMContext::MD_noalias,
LLVMContext::MD_invariant_group,
LLVMContext::MD_access_group};
combineMetadata(C, cpyLoad, KnownIDs, true);
combineAAMetadata(C, cpyLoad);
if (cpyLoad != cpyStore)
combineMetadata(C, cpyStore, KnownIDs, true);
combineAAMetadata(C, cpyStore);

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

// Otherwise we're good! Update the immut argument.
combineAAMetadata(&CB, MDep);
CB.setArgOperand(ArgNo, MDep->getSource());
++NumMemCpyInstr;
return true;
Expand Down
5 changes: 3 additions & 2 deletions llvm/test/Transforms/MemCpyOpt/memcpy.ll
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,11 @@ define void @immut_valid_align_branched(i1 %c, ptr noalias align 4 %val) {
ret void
}

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