Skip to content

Commit 1e7ce90

Browse files
committed
Fixing the strong imported async frame pointer flags
The weakly-imported symbol was getting optimized out, then put back in as a strongly-imported symbol. This is no good. The symbol is a declaration though, so it can't be "used" directly. Instead, we assign it to another global and "use" it. That avoids the optimizations and should be fine. Even if that symbol is a nullptr because it doesn't exist, we are taking the pointer to it, which should be fine for all situations.
1 parent dd984b1 commit 1e7ce90

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,18 @@ void IRGenModule::emitSwiftAsyncExtendedFrameInfoWeakRef() {
18341834
extendedFramePointerFlagsWeakRef = new llvm::GlobalVariable(Module, Int8PtrTy, false,
18351835
llvm::GlobalValue::ExternalWeakLinkage, nullptr,
18361836
symbolName);
1837+
1838+
// The weak imported extendedFramePointerFlagsWeakRef gets optimized out
1839+
// before being added back as a strong import.
1840+
// Declarations can't be added to the used list, so we create a little
1841+
// global that can't be used from the program, but can be in the used list to
1842+
// avoid optimizations.
1843+
llvm::GlobalVariable *usage = new llvm::GlobalVariable(
1844+
Module, extendedFramePointerFlagsWeakRef->getType(), false,
1845+
llvm::GlobalValue::PrivateLinkage,
1846+
static_cast<llvm::GlobalVariable *>(extendedFramePointerFlagsWeakRef),
1847+
"_swift_async_extendedFramePointerFlagsUser");
1848+
addUsedGlobal(usage);
18371849
}
18381850

18391851
bool IRGenModule::isConcurrencyAvailable() {

test/Concurrency/Backdeploy/weak_linking.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %FileCheck %s --check-prefix=NEW < %t/new.ir
44
// RUN: %target-swift-frontend %s -target x86_64-apple-macosx10.15 -module-name main -emit-ir -o %t/old.ir -disable-availability-checking
55
// RUN: %FileCheck %s --check-prefix=OLD < %t/old.ir
6+
// RUN: %target-swift-frontend %s -target x86_64-apple-macosx10.15 -O -module-name main -emit-ir -o %t/optimized.ir -disable-availability-checking
7+
// RUN: %FileCheck %s --check-prefix=OPTIMIZED < %t/optimized.ir
8+
69

710
// REQUIRES: OS=macosx
811

@@ -12,6 +15,11 @@
1215
// OLD: declare extern_weak swiftcc %swift.metadata_response @"$sScPMa"
1316
// OLD: declare extern_weak swiftcc i8 @"$sScP8rawValues5UInt8Vvg"
1417

18+
// OPTIMIZED: @swift_async_extendedFramePointerFlags = extern_weak global i8*
19+
// OPTIMIZED: @_swift_async_extendedFramePointerFlagsUser = private global i8** @swift_async_extendedFramePointerFlags
20+
// OPTIMIZED: @llvm.used =
21+
// OPTIMIZED-SAME: (i8*** @_swift_async_extendedFramePointerFlagsUser to i8*)
22+
1523
@available(macOS 12.0, *)
1624
public func g() async -> String { "hello" }
1725

0 commit comments

Comments
 (0)