Skip to content

Fix for a crash due to performLocalReleaseMotion and performLocalRetainMotion #38150

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
Jun 30, 2021
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
13 changes: 12 additions & 1 deletion lib/LLVMPasses/LLVMARCOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
++NumNoopDeleted;
continue;
}
if (!CI.use_empty()) {
// Do not get RC identical value here, could end up with a
// crash in replaceAllUsesWith as the type maybe different.
CI.replaceAllUsesWith(CI.getArgOperand(0));
Changed = true;
}
// Rewrite unknown retains into swift_retains.
NativeRefs.insert(ArgVal);
for (auto &X : UnknownObjectRetains[ArgVal]) {
Expand All @@ -138,7 +144,12 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
++NumNoopDeleted;
continue;
}

if (!CI.use_empty()) {
// Do not get RC identical value here, could end up with a
// crash in replaceAllUsesWith as the type maybe different.
CI.replaceAllUsesWith(CI.getArgOperand(0));
Changed = true;
}
// Have not encountered a strong retain/release. keep it in the
// unknown retain/release list for now. It might get replaced
// later.
Expand Down
53 changes: 53 additions & 0 deletions test/LLVMPasses/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,59 @@ bb1:
ret void
}

; CHECK-LABEL: @releasemotion_forwarding
; CHECK-NOT: swift_retain
; CHECK-NOT: swift_release
; CHECK: call void @user(%swift.refcounted* %P)
; CHECK: ret
define void @releasemotion_forwarding(%swift.refcounted* %P, i8* %O, %swift.bridge* %B) {
entry:
%res = tail call %swift.refcounted* @swift_retain(%swift.refcounted* %P)
tail call void @swift_release(%swift.refcounted* %res) nounwind
call void @user(%swift.refcounted* %res) nounwind
ret void
}

; CHECK-LABEL: @retainmotion_forwarding
; CHECK: store %swift.refcounted* %P, %swift.refcounted** %R, align 4
; CHECK-NOT: swift_retain
; CHECK-NOT: swift_release
; CHECK: ret
define void @retainmotion_forwarding(%swift.refcounted* %P, %swift.refcounted** %R, %swift.bridge* %B) {
entry:
%res = tail call %swift.refcounted* @swift_retain(%swift.refcounted* %P)
store %swift.refcounted* %res, %swift.refcounted** %R, align 4
call void @swift_bridgeObjectRelease(%swift.bridge* %B)
tail call void @swift_release(%swift.refcounted* %res) nounwind
ret void
}

; CHECK-LABEL: @unknownreleasemotion_forwarding
; CHECK-NOT: swift_unknownObjectRetain
; CHECK-NOT: swift_unknownObjectRelease
; CHECK: call void @user(%swift.refcounted* %P)
; CHECK: ret
define void @unknownreleasemotion_forwarding(%swift.refcounted* %P, i8* %O, %swift.bridge* %B) {
entry:
%res = tail call %swift.refcounted* @swift_unknownObjectRetain(%swift.refcounted* %P)
tail call void @swift_unknownObjectRelease(%swift.refcounted* %res) nounwind
call void @user(%swift.refcounted* %res) nounwind
ret void
}

; CHECK-LABEL: @unknownretainmotion_forwarding
; CHECK: store %swift.refcounted* %P, %swift.refcounted** %R, align 4
; CHECK-NOT: swift_unknownObjectRetain
; CHECK-NOT: swift_unknownObjectRelease
; CHECK: ret
define void @unknownretainmotion_forwarding(%swift.refcounted* %P, %swift.refcounted** %R, %swift.bridge* %B) {
entry:
%res = tail call %swift.refcounted* @swift_unknownObjectRetain(%swift.refcounted* %P)
store %swift.refcounted* %res, %swift.refcounted** %R, align 4
call void @swift_bridgeObjectRelease(%swift.bridge* %B)
tail call void @swift_unknownObjectRelease(%swift.refcounted* %res) nounwind
ret void
}

!llvm.dbg.cu = !{!1}
!llvm.module.flags = !{!4}
Expand Down