Skip to content

Commit

Permalink
[InstCombine] Dont throw away noalias/alias scope metadata when inlin…
Browse files Browse the repository at this point in the history
…ing memcpys (#74805)

This was found in julia when we changed some operations from explicit
loads + stores to memcpys. While applying it to both the src and the
dest seems weird, thats what we do for normal TBAA.
  • Loading branch information
gbaraldi committed Jan 4, 2024
1 parent b336ab4 commit a87fa7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 7 additions & 9 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {

// If the memcpy has metadata describing the members, see if we can get the
// TBAA tag describing our copy.
MDNode *CopyMD = nullptr;
if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa)) {
CopyMD = M;
} else if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
AAMDNodes AACopyMD = MI->getAAMetadata();

if (MDNode *M = AACopyMD.TBAAStruct) {
AACopyMD.TBAAStruct = nullptr;
if (M->getNumOperands() == 3 && M->getOperand(0) &&
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
Expand All @@ -184,16 +184,15 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
Size &&
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
CopyMD = cast<MDNode>(M->getOperand(2));
AACopyMD.TBAA = cast<MDNode>(M->getOperand(2));
}

Value *Src = MI->getArgOperand(1);
Value *Dest = MI->getArgOperand(0);
LoadInst *L = Builder.CreateLoad(IntType, Src);
// Alignment from the mem intrinsic will be better, so use it.
L->setAlignment(*CopySrcAlign);
if (CopyMD)
L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
L->setAAMetadata(AACopyMD);
MDNode *LoopMemParallelMD =
MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
if (LoopMemParallelMD)
Expand All @@ -205,8 +204,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
StoreInst *S = Builder.CreateStore(L, Dest);
// Alignment from the mem intrinsic will be better, so use it.
S->setAlignment(*CopyDstAlign);
if (CopyMD)
S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
S->setAAMetadata(AACopyMD);
if (LoopMemParallelMD)
S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
if (AccessGroupMD)
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Transforms/InstCombine/memcpy-to-load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ define void @copy_16_bytes(ptr %d, ptr %s) {
ret void
}

define void @copy_8_bytes_noalias(ptr %d, ptr %s) {
; CHECK-LABEL: @copy_8_bytes_noalias(
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[S:%.*]], align 1, !alias.scope [[META0:![0-9]+]], !noalias [[META3:![0-9]+]]
; CHECK-NEXT: store i64 [[TMP1]], ptr [[D:%.*]], align 1, !alias.scope [[META0]], !noalias [[META3]]
; CHECK-NEXT: ret void
;
call void @llvm.memcpy.p0.p0.i32(ptr %d, ptr %s, i32 8, i1 false), !alias.scope !4, !noalias !5
ret void
}

!0 = distinct !{!0, !"The domain"}
!1 = distinct !{!1}
!2 = !{!2, !0}
!3 = !{!3, !1}
!4 = !{!2}
!5 = !{!3}

0 comments on commit a87fa7f

Please sign in to comment.