Skip to content

Commit c4bbeb2

Browse files
committed
Mark main() as used
This prevents dead code stripping from removing the main() function used in widgets. This special case was added to support the integrated REPL, which was removed after Swift 5.3, and so it is no longer necessary. Fixes rdar://65862827.
1 parent 0c684f0 commit c4bbeb2

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,11 +1908,7 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,
19081908

19091909
// Everything externally visible is considered used in Swift.
19101910
// That mostly means we need to be good at not marking things external.
1911-
//
1912-
// Exclude "main", because it should naturally be used, and because adding it
1913-
// to llvm.used leaves a dangling use when the REPL attempts to discard
1914-
// intermediate mains.
1915-
if (LinkInfo::isUsed(IRL) && global->getName() != SWIFT_ENTRY_POINT_FUNCTION)
1911+
if (LinkInfo::isUsed(IRL))
19161912
IGM.addUsedGlobal(global);
19171913
}
19181914

@@ -2003,11 +1999,7 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
20031999

20042000
// Everything externally visible is considered used in Swift.
20052001
// That mostly means we need to be good at not marking things external.
2006-
//
2007-
// Exclude "main", because it should naturally be used, and because adding it
2008-
// to llvm.used leaves a dangling use when the REPL attempts to discard
2009-
// intermediate mains.
2010-
if (linkInfo.isUsed() && name != SWIFT_ENTRY_POINT_FUNCTION) {
2002+
if (linkInfo.isUsed()) {
20112003
IGM.addUsedGlobal(fn);
20122004
}
20132005

test/IRGen/unused.sil

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// REQUIRES: CPU=x86_64
44

55
sil_stage canonical
6+
import Builtin
7+
import Swift
68

79
sil private @foo : $@convention(thin) () -> () {
810
bb0:
@@ -43,12 +45,20 @@ bb0:
4345
return %1 : $()
4446
}
4547

46-
// CHECK-macho: @llvm.used = appending global [2 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata"
47-
// CHECK-elf: @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*), i8* getelementptr inbounds ([0 x i8], [0 x i8]* @_swift1_autolink_entries, i32 0, i32 0)], section "llvm.metadata"
48+
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
49+
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
50+
%2 = integer_literal $Builtin.Int32, 0 // user: %3
51+
%3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4
52+
return %3 : $Int32 // id: %4
53+
}
54+
55+
// CHECK-macho: @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata"
56+
// CHECK-elf: @llvm.used = appending global [4 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*), i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* @_swift1_autolink_entries, i32 0, i32 0)], section "llvm.metadata"
4857

4958
// CHECK: define linkonce_odr hidden swiftcc void @qux()
5059
// CHECK: define hidden swiftcc void @fred()
5160
// CHECK: define{{( dllexport)?}}{{( protected)?}} swiftcc void @frieda()
61+
// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main
5262

5363
// NEGATIVE-NOT: @foo
5464
// NEGATIVE-NOT: @bar

0 commit comments

Comments
 (0)