Skip to content

Commit 7af551c

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 c39901d commit 7af551c

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
@@ -1778,6 +1778,18 @@ void IRGenModule::emitSwiftAsyncExtendedFrameInfoWeakRef() {
17781778
extendedFramePointerFlagsWeakRef = new llvm::GlobalVariable(Module, Int8PtrTy, false,
17791779
llvm::GlobalValue::ExternalWeakLinkage, nullptr,
17801780
symbolName);
1781+
1782+
// The weak imported extendedFramePointerFlagsWeakRef gets optimized out
1783+
// before being added back as a strong import.
1784+
// Declarations can't be added to the used list, so we create a little
1785+
// global that can't be used from the program, but can be in the used list to
1786+
// avoid optimizations.
1787+
llvm::GlobalVariable *usage = new llvm::GlobalVariable(
1788+
Module, extendedFramePointerFlagsWeakRef->getType(), false,
1789+
llvm::GlobalValue::PrivateLinkage,
1790+
static_cast<llvm::GlobalVariable *>(extendedFramePointerFlagsWeakRef),
1791+
"_swift_async_extendedFramePointerFlagsUser");
1792+
addUsedGlobal(usage);
17811793
}
17821794

17831795
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)