Skip to content

[IRGen] Don't apply direct error return to functions with indirect result #75150

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 2 commits into from
Jul 11, 2024
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
16 changes: 11 additions & 5 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void SignatureExpansion::expandResult(
const TypeInfo *directResultTypeInfo;
std::tie(ResultIRType, directResultTypeInfo) = expandDirectResult();

if (!fnConv.hasIndirectSILErrorResults()) {
if (!fnConv.hasIndirectSILResults() && !fnConv.hasIndirectSILErrorResults()) {
llvm::Type *directErrorType;
const TypeInfo *directErrorTypeInfo;
std::tie(directErrorType, directErrorTypeInfo) = expandDirectErrorType();
Expand Down Expand Up @@ -2032,7 +2032,8 @@ void SignatureExpansion::expandParameters(
auto &errorTI = IGM.getTypeInfo(errorType);
auto &nativeError = errorTI.nativeReturnValueSchema(IGM);

if (getSILFuncConventions().hasIndirectSILErrorResults() ||
if (getSILFuncConventions().hasIndirectSILResults() ||
getSILFuncConventions().hasIndirectSILErrorResults() ||
native.requiresIndirect() ||
nativeError.shouldReturnTypedErrorIndirectly()) {
ParamIRTypes.push_back(IGM.getStorageType(errorType)->getPointerTo());
Expand Down Expand Up @@ -2595,7 +2596,8 @@ class SyncCallEmission final : public CallEmission {
auto &errorSchema =
IGF.IGM.getTypeInfo(silErrorTy).nativeReturnValueSchema(IGF.IGM);

if (nativeSchema.requiresIndirect() ||
if (fnConv.hasIndirectSILResults() ||
nativeSchema.requiresIndirect() ||
errorSchema.shouldReturnTypedErrorIndirectly()) {
// Return the error indirectly.
auto buf = IGF.getCalleeTypedErrorResultSlot(silErrorTy);
Expand Down Expand Up @@ -4364,8 +4366,9 @@ bool CallEmission::mayReturnTypedErrorDirectly() const {
SILFunctionConventions fnConv(getCallee().getOrigFunctionType(),
IGF.getSILModule());
bool mayReturnErrorDirectly = false;
if (!convertDirectToIndirectReturn && !fnConv.hasIndirectSILErrorResults() &&
fnConv.funcTy->hasErrorResult() && fnConv.isTypedError()) {
if (!convertDirectToIndirectReturn && !fnConv.hasIndirectSILResults() &&
!fnConv.hasIndirectSILErrorResults() && fnConv.funcTy->hasErrorResult() &&
fnConv.isTypedError()) {
auto errorType =
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
auto &errorSchema =
Expand Down Expand Up @@ -4405,6 +4408,9 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
auto *eltTy = elt->getType();
if (nativeTy->isIntOrPtrTy() && eltTy->isIntOrPtrTy() &&
nativeTy->getPrimitiveSizeInBits() != eltTy->getPrimitiveSizeInBits()) {
if (nativeTy->isPointerTy() && eltTy == IGF.IGM.IntPtrTy) {
return IGF.Builder.CreateIntToPtr(elt, nativeTy);
}
return IGF.Builder.CreateTruncOrBitCast(elt, nativeTy);
}
return elt;
Expand Down
11 changes: 7 additions & 4 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,8 @@ static void emitEntryPointArgumentsNativeCC(IRGenSILFunction &IGF,
auto &errorTI = cast<FixedTypeInfo>(IGF.getTypeInfo(inContextErrorType));
auto &native = resultTI.nativeReturnValueSchema(IGF.IGM);
auto &nativeError = errorTI.nativeReturnValueSchema(IGF.IGM);
if (funcTy->isAsync() || native.requiresIndirect() ||
if (funcTy->isAsync() || fnConv.hasIndirectSILResults() ||
native.requiresIndirect() ||
nativeError.shouldReturnTypedErrorIndirectly()) {
IGF.setCallerTypedErrorResultSlot(
Address(emission->getCallerTypedErrorResultArgument(),
Expand Down Expand Up @@ -3911,7 +3912,8 @@ void IRGenSILFunction::visitFullApplySite(FullApplySite site) {
auto &resultSchema = resultTI.nativeReturnValueSchema(IGM);
auto &errorSchema = errorTI.nativeReturnValueSchema(IGM);

if (isAsync() || substConv.hasIndirectSILErrorResults() ||
if (isAsync() || substConv.hasIndirectSILResults() ||
substConv.hasIndirectSILErrorResults() ||
resultSchema.requiresIndirect() ||
errorSchema.shouldReturnTypedErrorIndirectly()) {
Explosion errorValue;
Expand Down Expand Up @@ -4378,7 +4380,7 @@ static void emitReturnInst(IRGenSILFunction &IGF,
funcLang == SILFunctionLanguage::C && "Need to handle all cases");
SILType errorType;
if (fnType->hasErrorResult() && conv.isTypedError() &&
!conv.hasIndirectSILErrorResults()) {
!conv.hasIndirectSILResults() && !conv.hasIndirectSILErrorResults()) {
errorType =
conv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
}
Expand Down Expand Up @@ -4433,7 +4435,8 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
auto &errorSchema = errorTI.nativeReturnValueSchema(IGM);

Builder.CreateStore(flag, getCallerErrorResultSlot());
if (resultSchema.requiresIndirect() ||
if (conv.hasIndirectSILResults() || conv.hasIndirectSILErrorResults() ||
resultSchema.requiresIndirect() ||
errorSchema.shouldReturnTypedErrorIndirectly()) {
errorTI.initialize(*this, errorResult, getCallerTypedErrorResultSlot(),
false);
Expand Down
23 changes: 23 additions & 0 deletions test/IRGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,26 @@ func mayThrow(x: Bool, y: AnyObject) throws(MyError) -> (Float, Int32, Float) {
}
return (3.0, 4, 5.0)
}

// CHECK: define hidden swiftcc { i64, i64 } @"$s12typed_throws25directErrorMergePtrAndInt1x1yyXl_SitSb_yXltAA05SmallD0VYKF"
// CHECK: [[RES:%.*]] = call swiftcc { i64, i64 } @"$s12typed_throws25directErrorMergePtrAndInt1x1yyXl_SitSb_yXltAA05SmallD0VYKF"
// CHECK: [[R0:%.*]] = extractvalue { i64, i64 } [[RES]], 0
// CHECK: inttoptr i64 [[R0]] to ptr
// CHECK: }
func directErrorMergePtrAndInt(x: Bool, y: AnyObject) throws(SmallError) -> (AnyObject, Int) {
guard x else {
throw SmallError(x: 1)
}

return try directErrorMergePtrAndInt(x: !x, y: y)
}

// This used to crash at compile time, because it was trying to use a direct
// error return in combination with an indirect result, which is illegal.
func genericThrows<T>(x: Bool, y: T) throws(SmallError) -> T {
guard x else {
throw SmallError(x: 1)
}

return y
}