Skip to content

Commit

Permalink
[MemCpyOpt] Use dyn_cast to fix assertion failure in `processMemCpy…
Browse files Browse the repository at this point in the history
…MemCpyDependence` (#98686)

Fixes #98675.
  • Loading branch information
dtcxzyw committed Jul 12, 2024
1 parent 22b7b84 commit 99685a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,9 +1188,9 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
if (MDestOffset == MForwardOffset)
CopySource = M->getDest();
else {
NewCopySource = cast<Instruction>(Builder.CreateInBoundsPtrAdd(
CopySource, Builder.getInt64(MForwardOffset)));
CopySource = NewCopySource;
CopySource = Builder.CreateInBoundsPtrAdd(
CopySource, Builder.getInt64(MForwardOffset));
NewCopySource = dyn_cast<Instruction>(CopySource);
}
// We need to update `MCopyLoc` if an offset exists.
MCopyLoc = MCopyLoc.getWithNewPtr(CopySource);
Expand Down
17 changes: 17 additions & 0 deletions llvm/test/Transforms/MemCpyOpt/memcpy-memcpy-offset.ll
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ define void @do_not_forward_offset_and_store(ptr %src, ptr %dest) {
ret void
}

; Make sure we don't crash when the copy source is a constant.
@buf = external global [32 x i8]

define void @pr98675(ptr noalias %p1, ptr noalias %p2) {
; CHECK-LABEL: define void @pr98675(
; CHECK-SAME: ptr noalias [[P1:%.*]], ptr noalias [[P2:%.*]]) {
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P1]], ptr @buf, i64 26, i1 false)
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[P1]], i64 10
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P2]], ptr getelementptr inbounds (i8, ptr @buf, i64 10), i64 1, i1 false)
; CHECK-NEXT: ret void
;
call void @llvm.memcpy.p0.p0.i64(ptr %p1, ptr @buf, i64 26, i1 false)
%gep = getelementptr i8, ptr %p1, i64 10
call void @llvm.memmove.p0.p0.i64(ptr %p2, ptr %gep, i64 1, i1 false)
ret void
}

declare void @use(ptr)

declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture, i64, i1)
Expand Down

0 comments on commit 99685a5

Please sign in to comment.