From c4bbeb21a5e3e7335f799301c75b3d0484319550 Mon Sep 17 00:00:00 2001 From: Brent Royal-Gordon Date: Wed, 29 Jul 2020 15:16:02 -0700 Subject: [PATCH] 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. --- lib/IRGen/GenDecl.cpp | 12 ++---------- test/IRGen/unused.sil | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 6bc087829a991..93e9c6c2e7d57 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -1908,11 +1908,7 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM, // Everything externally visible is considered used in Swift. // That mostly means we need to be good at not marking things external. - // - // Exclude "main", because it should naturally be used, and because adding it - // to llvm.used leaves a dangling use when the REPL attempts to discard - // intermediate mains. - if (LinkInfo::isUsed(IRL) && global->getName() != SWIFT_ENTRY_POINT_FUNCTION) + if (LinkInfo::isUsed(IRL)) IGM.addUsedGlobal(global); } @@ -2003,11 +1999,7 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM, // Everything externally visible is considered used in Swift. // That mostly means we need to be good at not marking things external. - // - // Exclude "main", because it should naturally be used, and because adding it - // to llvm.used leaves a dangling use when the REPL attempts to discard - // intermediate mains. - if (linkInfo.isUsed() && name != SWIFT_ENTRY_POINT_FUNCTION) { + if (linkInfo.isUsed()) { IGM.addUsedGlobal(fn); } diff --git a/test/IRGen/unused.sil b/test/IRGen/unused.sil index 65f1f12b587f4..71bc4dad851f6 100644 --- a/test/IRGen/unused.sil +++ b/test/IRGen/unused.sil @@ -3,6 +3,8 @@ // REQUIRES: CPU=x86_64 sil_stage canonical +import Builtin +import Swift sil private @foo : $@convention(thin) () -> () { bb0: @@ -43,12 +45,20 @@ bb0: return %1 : $() } -// 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" -// 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" +sil @main : $@convention(c) (Int32, UnsafeMutablePointer>>) -> Int32 { +bb0(%0 : $Int32, %1 : $UnsafeMutablePointer>>): + %2 = integer_literal $Builtin.Int32, 0 // user: %3 + %3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4 + return %3 : $Int32 // id: %4 +} + +// 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" +// 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" // CHECK: define linkonce_odr hidden swiftcc void @qux() // CHECK: define hidden swiftcc void @fred() // CHECK: define{{( dllexport)?}}{{( protected)?}} swiftcc void @frieda() +// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main // NEGATIVE-NOT: @foo // NEGATIVE-NOT: @bar