Skip to content

IRGen: Teach IRGen about calling noreturn C functions #23112

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
33 changes: 32 additions & 1 deletion lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,17 @@ void CallEmission::emitToUnmappedExplosion(Explosion &out) {
result = emitObjCRetainAutoreleasedReturnValue(IGF, result);
}

auto origFnType = getCallee().getOrigFunctionType();

// Specially handle noreturn c function which would return a 'Never' SIL result
// type.
if (origFnType->getLanguage() == SILFunctionLanguage::C &&
origFnType->isNoReturnFunction()) {
auto clangResultTy = result->getType();
extractScalarResults(IGF, clangResultTy, result, out);
return;
}

// Get the natural IR type in the body of the function that makes
// the call. This may be different than the IR type returned by the
// call itself due to ABI type coercion.
Expand All @@ -1527,7 +1538,6 @@ void CallEmission::emitToUnmappedExplosion(Explosion &out) {
// for methods that have covariant ABI-compatible overrides.
auto expectedNativeResultType = nativeSchema.getExpandedType(IGF.IGM);
if (result->getType() != expectedNativeResultType) {
auto origFnType = getCallee().getOrigFunctionType();
assert(origFnType->getLanguage() == SILFunctionLanguage::C ||
origFnType->getRepresentation() == SILFunctionTypeRepresentation::Method);
result =
Expand Down Expand Up @@ -1783,9 +1793,23 @@ void CallEmission::emitToExplosion(Explosion &out, bool isOutlined) {
auto &substResultTI =
cast<LoadableTypeInfo>(IGF.getTypeInfo(substResultType));

auto origFnType = getCallee().getOrigFunctionType();
auto isNoReturnCFunction =
origFnType->getLanguage() == SILFunctionLanguage::C &&
origFnType->isNoReturnFunction();

// If the call is naturally to memory, emit it that way and then
// explode that temporary.
if (LastArgWritten == 1) {
if (isNoReturnCFunction) {
auto fnType = getCallee().getFunctionPointer().getFunctionType();
assert(fnType->getNumParams() > 0);
auto resultTy = fnType->getParamType(0)->getPointerElementType();
auto temp = IGF.createAlloca(resultTy, Alignment(0), "indirect.result");
emitToMemory(temp, substResultTI, isOutlined);
return;
}

StackAddress ctemp = substResultTI.allocateStack(IGF, substResultType,
"call.aggresult");
Address temp = ctemp.getAddress();
Expand All @@ -1802,6 +1826,13 @@ void CallEmission::emitToExplosion(Explosion &out, bool isOutlined) {
Explosion temp;
emitToUnmappedExplosion(temp);

// Specially handle noreturn c function which would return a 'Never' SIL result
// type: there is no need to cast the result.
if (isNoReturnCFunction) {
temp.transferInto(out, temp.size());
return;
}

// We might need to bitcast the results.
emitCastToSubstSchema(IGF, temp, substResultTI.getSchema(), out);
}
Expand Down
19 changes: 19 additions & 0 deletions test/IRGen/Inputs/noreturn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int scalarNoReturn() __attribute__((noreturn));

void voidNoReturn() __attribute__((noreturn));

typedef struct {
long long a;
long long b;
long long c;
long long d;
} Large;

Large largeStructNoReturn() __attribute__((noreturn));

typedef struct {
long a;
long b;
} Small;

Small smallStructNoReturn() __attribute__((noreturn));
31 changes: 31 additions & 0 deletions test/IRGen/noreturn.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-frontend -module-name A -emit-ir -primary-file %s -import-objc-header %S/Inputs/noreturn.h | %FileCheck %s


// CHECK-LABEL: define {{.*}} void @"$s1A018testDirectReturnNoC0yyF"()
// CHECK: call i32 @scalarNoReturn()
// CHECK: unreachable
public func testDirectReturnNoReturn() {
scalarNoReturn()
}

// CHECK-LABEL: define {{.*}} void @"$s1A019testDirect2ReturnNoC0yyF"()
// CHECK: call {{.*}}@smallStructNoReturn({{.*}})
// CHECK: unreachable
public func testDirect2ReturnNoReturn() {
smallStructNoReturn()
}

// CHECK-LABEL: define {{.*}} void @"$s1A020testIndirectReturnNoC0yyF"()
// CHECK: [[INDIRECT_RESULT:%.*]] = alloca %struct.Large
// CHECK: call void @largeStructNoReturn(%struct.Large* {{.*}}[[INDIRECT_RESULT]])
// CHECK: unreachable
public func testIndirectReturnNoReturn() {
largeStructNoReturn()
}

// CHECK-LABEL: define {{.*}} void @"$s1A016testVoidReturnNoC0yyF"()
// CHECK: call void @voidNoReturn()
// CHECK: unreachable
public func testVoidReturnNoReturn() {
voidNoReturn()
}