Skip to content

Commit df260af

Browse files
committed
Mark main() as used
…except when the integrated REPL is in use. This prevents dead code stripping from removing the main() function used in widgets. Fixes rdar://65862827.
1 parent 62e6834 commit df260af

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,9 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,
18641864
// Exclude "main", because it should naturally be used, and because adding it
18651865
// to llvm.used leaves a dangling use when the REPL attempts to discard
18661866
// intermediate mains.
1867-
if (LinkInfo::isUsed(IRL) && global->getName() != SWIFT_ENTRY_POINT_FUNCTION)
1867+
if (LinkInfo::isUsed(IRL)
1868+
&& (!IGM.getOptions().IntegratedREPL
1869+
|| global->getName() != SWIFT_ENTRY_POINT_FUNCTION))
18681870
IGM.addUsedGlobal(global);
18691871
}
18701872

@@ -1959,7 +1961,9 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
19591961
// Exclude "main", because it should naturally be used, and because adding it
19601962
// to llvm.used leaves a dangling use when the REPL attempts to discard
19611963
// intermediate mains.
1962-
if (linkInfo.isUsed() && name != SWIFT_ENTRY_POINT_FUNCTION) {
1964+
if (linkInfo.isUsed()
1965+
&& (!IGM.getOptions().IntegratedREPL
1966+
|| name != SWIFT_ENTRY_POINT_FUNCTION)) {
19631967
IGM.addUsedGlobal(fn);
19641968
}
19651969

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)