Skip to content

Commit

Permalink
Add swift.internalize_at_link feature and enable it by default
Browse files Browse the repository at this point in the history
This feature controls the `-Xfrontend -internalize-at-link` flag, which allows the linker to hide public symbols that are not being used and thus dead-strip them. Users of frameworks will need to explicitly export them with an `-exported_symbols_list` if they are not already.

PiperOrigin-RevId: 524870535
(cherry picked from commit e8838f2)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Oct 14, 2024
1 parent afded16 commit 2dad738
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ SWIFT_FEATURE_ENABLE_BARE_SLASH_REGEX = "swift.supports_bare_slash_regex"
# Clang SPIs as Swift SPIs.
SWIFT_FEATURE_DISABLE_CLANG_SPI = "swift.disable_clang_spi"

# If enabled, allow public symbols to be internalized at link time to support
# better dead-code stripping. This assumes that all clients of public types are
# part of the same link unit or that public symbols linked into frameworks are
# explicitly exported via `-exported_symbols_list`.
SWIFT_FEATURE_INTERNALIZE_AT_LINK = "swift.internalize_at_link"

# Disables Swift sandbox which prevents issues with nested sandboxing when Swift code contains system-provided macros.
# If enabled '#Preview' macro provided by SwiftUI fails to build and probably other system-provided macros.
# Enabled by default for Swift 5.10+ on macOS.
Expand Down
8 changes: 8 additions & 0 deletions swift/toolchains/config/compile_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ load(
"SWIFT_FEATURE_GLOBAL_MODULE_CACHE_USES_TMPDIR",
"SWIFT_FEATURE_INDEX_INCLUDE_LOCALS",
"SWIFT_FEATURE_INDEX_WHILE_BUILDING",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_LAYERING_CHECK",
"SWIFT_FEATURE_MODULAR_INDEXING",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
Expand Down Expand Up @@ -365,6 +366,13 @@ def compile_action_configs(
],
),

# Improve dead-code stripping.
ActionConfigInfo(
actions = [SWIFT_ACTION_COMPILE],
configurators = [add_arg("-Xfrontend", "-internalize-at-link")],
features = [SWIFT_FEATURE_INTERNALIZE_AT_LINK],
),

# Enable or disable serialization of debugging options into
# swiftmodules.
ActionConfigInfo(
Expand Down
2 changes: 2 additions & 0 deletions swift/toolchains/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ load(
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
"SWIFT_FEATURE_EMIT_SWIFTDOC",
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_NO_GENERATED_MODULE_MAP",
"SWIFT_FEATURE_OPT_USES_WMO",
Expand Down Expand Up @@ -469,6 +470,7 @@ def _swift_toolchain_impl(ctx):
SWIFT_FEATURE_DISABLE_SYSTEM_INDEX,
SWIFT_FEATURE_EMIT_SWIFTDOC,
SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO,
SWIFT_FEATURE_INTERNALIZE_AT_LINK,
SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
Expand Down
2 changes: 2 additions & 0 deletions swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ load(
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
"SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES",
"SWIFT_FEATURE_FILE_PREFIX_MAP",
"SWIFT_FEATURE_INTERNALIZE_AT_LINK",
"SWIFT_FEATURE_MODULE_MAP_HOME_IS_CWD",
"SWIFT_FEATURE_OBJC_LINK_FLAGS",
"SWIFT_FEATURE_OPT_USES_WMO",
Expand Down Expand Up @@ -680,6 +681,7 @@ def _xcode_swift_toolchain_impl(ctx):
SWIFT_FEATURE_ENABLE_BATCH_MODE,
SWIFT_FEATURE_ENABLE_SKIP_FUNCTION_BODIES,
SWIFT_FEATURE_FILE_PREFIX_MAP,
SWIFT_FEATURE_INTERNALIZE_AT_LINK,
SWIFT_FEATURE_OBJC_LINK_FLAGS,
SWIFT_FEATURE_OPT_USES_WMO,
SWIFT_FEATURE_REMAP_XCODE_PATH,
Expand Down

0 comments on commit 2dad738

Please sign in to comment.