Skip to content
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

[MemCpyOpt] Use dyn_cast to fix assertion failure in processMemCpyMemCpyDependence #98686

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,9 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
if (MDestOffset == MForwardOffset)
CopySource = M->getDest();
else {
NewCopySource = cast<Instruction>(Builder.CreateInBoundsPtrAdd(
CopySource, Builder.getInt64(MForwardOffset)));
NewCopySource = GetElementPtrInst::CreateInBounds(
Builder.getInt8Ty(), CopySource, Builder.getInt64(MForwardOffset), "",
Builder.GetInsertPoint());
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
CopySource = NewCopySource;
}
// We need to update `MCopyLoc` if an offset exists.
Expand Down
18 changes: 18 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,24 @@ 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: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @buf, i64 10
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P2]], ptr [[TMP1]], 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
Loading