Skip to content

Move workaround for swifterror miscompile earlier #69819

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
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: 7 additions & 6 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,13 @@ void IRGenSILFunction::visitFullApplySite(FullApplySite site) {

// If the error value is non-null, branch to the error destination.
auto hasError = Builder.CreateICmpNE(errorValue, nullError);

// Create a dummy use of 'errorValue' in the catch BB to workaround an
// LLVM miscompile that ends up taking the wrong branch if there are no
// uses of 'errorValue' in the catch block.
// FIXME: Remove this when the following radar is fixed: rdar://116636601
Builder.CreatePtrToInt(errorValue, IGM.IntPtrTy);

Builder.CreateCondBr(hasError,
typedErrorLoadBB ? typedErrorLoadBB : errorDest.bb,
normalDest.bb);
Expand All @@ -3806,12 +3813,6 @@ void IRGenSILFunction::visitFullApplySite(FullApplySite site) {
} else {
Builder.emitBlock(typedErrorLoadBB);

// Create a dummy use of 'errorValue' in the catch BB to workaround an
// LLVM miscompile that ends up taking the wrong branch if there are no
// uses of 'errorValue' in the catch block.
// FIXME: Remove this when the following radar is fixed: rdar://116636601
Builder.CreatePtrToInt(errorValue, IGM.IntPtrTy);

auto &ti = cast<LoadableTypeInfo>(IGM.getTypeInfo(errorType));
Explosion errorValue;
ti.loadAsTake(*this, getCalleeTypedErrorResultSlot(errorType), errorValue);
Expand Down
1 change: 1 addition & 0 deletions test/IRGen/errors.sil
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ entry(%0 : $AnyObject):
// CHECK-native-NEXT: [[RESULT:%.*]] = call swiftcc ptr @try_apply_helper(ptr %0, ptr swiftself undef, ptr noalias nocapture [[SWIFTERROR]]{{( )?}}dereferenceable({{.}}) [[ERRORSLOT]])
// CHECK-NEXT: [[ERR:%.*]] = load ptr, ptr [[ERRORSLOT]], align
// CHECK-NEXT: [[T0:%.*]] = icmp ne ptr [[ERR]], null
// CHECK-NEXT: ptrtoint ptr [[ERR]] to i
// CHECK-NEXT: br i1 [[T0]],
%1 = function_ref @try_apply_helper : $@convention(thin) (@owned AnyObject) -> (@owned AnyObject, @error Error)
try_apply %1(%0) : $@convention(thin) (@owned AnyObject) -> (@owned AnyObject, @error Error),
Expand Down
5 changes: 3 additions & 2 deletions test/IRGen/unmanaged_objc_throw_func.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import Foundation
// CHECK-NEXT: call void @llvm.objc.release(ptr %[[T4]])
// CHECK-NEXT: ret ptr %[[T4]]
let arr = [1] as CFArray
return Unmanaged.passUnretained(arr)
}
return Unmanaged.passUnretained(arr)
}
}

// CHECK: %[[T0:.+]] = call swiftcc ptr @"$s25unmanaged_objc_throw_func1CC22returnUnmanagedCFArrays0F0VySo0G3RefaGyKF"
// CHECK-NEXT: %[[T2:.+]] = load ptr, ptr %swifterror, align {{[0-9]+}}
// CHECK-NEXT: %[[T3:.+]] = icmp ne ptr %[[T2]], null
// CHECK-NEXT: ptrtoint ptr %[[T2]] to i
// CHECK-NEXT: br i1 %[[T3]], label %[[L1:.+]], label %[[L2:.+]]

// CHECK: [[L2]]: ; preds = %entry
Expand Down