Skip to content

Commit cb1e54c

Browse files
Merge pull request #39415 from aschwaighofer/weak_swift_async_extendedFramePointerFlags
IRGen: Emit a weak reference to swift_async_extendedFramePointerFlags when neccessary
2 parents 1521b75 + 4d7854f commit cb1e54c

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,7 @@ bool IRGenModule::finalize() {
16441644
if (!ClangCodeGen->GetModule())
16451645
return false;
16461646

1647+
emitSwiftAsyncExtendedFrameInfoWeakRef();
16471648
emitAutolinkInfo();
16481649
emitGlobalLists();
16491650
if (DebugInfo)
@@ -1776,3 +1777,30 @@ TypeExpansionContext IRGenModule::getMaximalTypeExpansionContext() const {
17761777
const TypeLayoutEntry &IRGenModule::getTypeLayoutEntry(SILType T) {
17771778
return Types.getTypeLayoutEntry(T);
17781779
}
1780+
1781+
1782+
void IRGenModule::emitSwiftAsyncExtendedFrameInfoWeakRef() {
1783+
if (!hasSwiftAsyncFunctionDef || extendedFramePointerFlagsWeakRef)
1784+
return;
1785+
if (IRGen.Opts.SwiftAsyncFramePointer !=
1786+
SwiftAsyncFramePointerKind::Auto)
1787+
return;
1788+
if (isConcurrencyAvailable())
1789+
return;
1790+
1791+
// Emit a weak reference to the `swift_async_extendedFramePointerFlags` symbol
1792+
// needed by Swift async functions.
1793+
auto symbolName = "swift_async_extendedFramePointerFlags";
1794+
if ((extendedFramePointerFlagsWeakRef = Module.getGlobalVariable(symbolName)))
1795+
return;
1796+
extendedFramePointerFlagsWeakRef = new llvm::GlobalVariable(Module, Int8PtrTy, false,
1797+
llvm::GlobalValue::ExternalWeakLinkage, nullptr,
1798+
symbolName);
1799+
}
1800+
1801+
bool IRGenModule::isConcurrencyAvailable() {
1802+
auto &ctx = getSwiftModule()->getASTContext();
1803+
auto deploymentAvailability =
1804+
AvailabilityContext::forDeploymentTarget(ctx);
1805+
return deploymentAvailability.isContainedIn(ctx.getConcurrencyAvailability());
1806+
}

lib/IRGen/IRGenModule.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,17 @@ private: \
16911691
void addLazyConformances(const IterableDeclContext *idc);
16921692

16931693
//--- Global context emission --------------------------------------------------
1694+
bool hasSwiftAsyncFunctionDef = false;
1695+
llvm::Value *extendedFramePointerFlagsWeakRef = nullptr;
1696+
1697+
/// Emit a weak reference to the `swift_async_extendedFramePointerFlags`
1698+
/// symbol needed by Swift async functions.
1699+
void emitSwiftAsyncExtendedFrameInfoWeakRef();
16941700
public:
1701+
bool isConcurrencyAvailable();
1702+
void noteSwiftAsyncFunctionDef() {
1703+
hasSwiftAsyncFunctionDef = true;
1704+
}
16951705
void emitRuntimeRegistration();
16961706
void emitVTableStubs();
16971707
void emitTypeVerifier();

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,12 +2181,16 @@ void IRGenSILFunction::emitSILFunction() {
21812181
IGM.IRGen.addDynamicReplacement(CurSILFn);
21822182

21832183
auto funcTy = CurSILFn->getLoweredFunctionType();
2184-
if (funcTy->isAsync() && funcTy->getLanguage() == SILFunctionLanguage::Swift) {
2184+
bool isAsyncFn = funcTy->isAsync();
2185+
if (isAsyncFn && funcTy->getLanguage() == SILFunctionLanguage::Swift) {
21852186
emitAsyncFunctionPointer(IGM,
21862187
CurFn,
21872188
LinkEntity::forSILFunction(CurSILFn),
21882189
getAsyncContextLayout(*this).getSize());
21892190
}
2191+
if (isAsyncFn) {
2192+
IGM.noteSwiftAsyncFunctionDef();
2193+
}
21902194

21912195
// Configure the dominance resolver.
21922196
// TODO: consider re-using a dom analysis from the PassManager

lib/IRGen/MetadataRequest.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,13 +1230,6 @@ static MetadataResponse emitTupleTypeMetadataRef(IRGenFunction &IGF,
12301230
}
12311231
}
12321232

1233-
/// Determine whether concurrency support is available in the runtime.
1234-
static bool isConcurrencyAvailable(ASTContext &ctx) {
1235-
auto deploymentAvailability =
1236-
AvailabilityContext::forDeploymentTarget(ctx);
1237-
return deploymentAvailability.isContainedIn(ctx.getConcurrencyAvailability());
1238-
}
1239-
12401233
namespace {
12411234
/// A visitor class for emitting a reference to a metatype object.
12421235
/// This implements a "raw" access, useful for implementing cache
@@ -1598,7 +1591,7 @@ namespace {
15981591
}
15991592

16001593
auto *getMetadataFn = type->getGlobalActor()
1601-
? (isConcurrencyAvailable(IGF.getSwiftModule()->getASTContext())
1594+
? (IGF.IGM.isConcurrencyAvailable()
16021595
? IGF.IGM.getGetFunctionMetadataGlobalActorFn()
16031596
: IGF.IGM.getGetFunctionMetadataGlobalActorBackDeployFn())
16041597
: type->isDifferentiable()

test/IRGen/swift_async_extended_frame_info.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ public func someAsyncFunction() async {
2121
// NEVER: s31swift_async_extended_frame_info17someAsyncFunctionyyYaF:
2222
// NEVER-NOT: _swift_async_extendedFramePointerFlags
2323
// NEVER-NOT: btsq $60
24+
25+
// AUTO: .weak_reference _swift_async_extendedFramePointerFlags
26+
// NEVER-NOT: .weak_reference _swift_async_extendedFramePointerFlags
27+
// ALWAYS-NOT: .weak_reference _swift_async_extendedFramePointerFlags

0 commit comments

Comments
 (0)