Skip to content

[6.0][SwiftSyntax] Enable Package CMO if possible #75355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,18 @@ include(CheckSymbolExists)
include(CMakeDependentOption)
include(CheckLanguage)
include(GNUInstallDirs)
include(SwiftImplicitImport)
include(SwiftCompilerCapability)
include(FetchContent)

# Enable Swift for the host compiler build if we have the language. It is
# optional until we have a bootstrap story.
check_language(Swift)
if(CMAKE_Swift_COMPILER)
# we are not interested in logging any Swift module used
# when configuring the build system -- those are not useful
# since they will not contribute to the build of the compiler itself
unset(ENV{SWIFT_LOADED_MODULE_TRACE_FILE})

enable_language(Swift)
set(DEFAULT_SWIFT_MIN_RUNTIME_VERSION "${CMAKE_Swift_COMPILER_VERSION}")
else()
Expand Down Expand Up @@ -845,6 +850,10 @@ if (CMAKE_Swift_COMPILER)
swift_supports_implicit_module("backtracing"
SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
message(STATUS " Implicit 'backtracing' import: ${SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT}")

swift_get_package_cmo_support(
Swift_COMPILER_PACKAGE_CMO_SUPPORT)
message(STATUS " Package CMO: ${Swift_COMPILER_PACKAGE_CMO_SUPPORT}")
else()
message(STATUS "Swift Compiler (None).")
endif()
Expand Down
51 changes: 51 additions & 0 deletions cmake/modules/SwiftCompilerCapability.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Test if the Swift compiler returns success for supplied compiler arguments....
function(swift_supports_compiler_arguments out_var)
file(WRITE "${CMAKE_BINARY_DIR}/tmp/dummy.swift" "")
execute_process(
COMMAND "${CMAKE_Swift_COMPILER}" -parse ${ARGN} -
INPUT_FILE "${CMAKE_BINARY_DIR}/tmp/dummy.swift"
OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE result
)
if(NOT result)
set("${out_var}" "TRUE" PARENT_SCOPE)
else()
set("${out_var}" "FALSE" PARENT_SCOPE)
endif()
endfunction()

# Test if the Swift compiler supports -disable-implicit-<module>-module-import.
macro(swift_supports_implicit_module module_name out_var)
swift_supports_compiler_arguments(${out_var}
-Xfrontend -disable-implicit-${module_name}-module-import
)
endmacro()

# Get "package cross-module-optimization" compiler arguments suitable for the compiler.
function(swift_get_package_cmo_support out_var)
# > 6.0 : Fixed feature.
swift_supports_compiler_arguments(result
-package-name my-package
-Xfrontend -package-cmo
-Xfrontend -allow-non-resilient-access
)
if(result)
set(${out_var} IMPLEMENTED PARENT_SCOPE)
return()
endif()

# == 6.0 : Experimental.
swift_supports_compiler_arguments(result
-package-name my-package
-Xfrontend -experimental-package-cmo
-Xfrontend -experimental-allow-non-resilient-access
-Xfrontend -experimental-package-bypass-resilience
)
if(result)
set(${out_var} EXPERIMENTAL PARENT_SCOPE)
return()
endif()

# < 6.0 : Not supported.
set(${out_var} NO PARENT_SCOPE)
endfunction()
23 changes: 0 additions & 23 deletions cmake/modules/SwiftImplicitImport.cmake

This file was deleted.

6 changes: 6 additions & 0 deletions lib/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ else()
list(APPEND compile_options "-cxx-interoperability-mode=default")
endif()

# FXIME: Importing package-cmo enabled '.swiftmodule' causes compiler crash :(
if(Swift_COMPILER_PACKAGE_CMO_SUPPORT)
list(APPEND compile_options
"SHELL:-Xfrontend -module-load-mode -Xfrontend prefer-interface")
endif()

if(SWIFT_BUILD_SWIFT_SYNTAX)
foreach(target swiftASTGen swiftIDEUtilsBridging)
target_compile_options(${target} PRIVATE ${compile_options})
Expand Down
6 changes: 6 additions & 0 deletions lib/Macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function(add_swift_macro_library name)
return()
endif()

# FXIME: Importing package-cmo enabled '.swiftmodule' causes compiler crash :(
if(Swift_COMPILER_PACKAGE_CMO_SUPPORT)
target_compile_options(${name} PRIVATE
"SHELL:-Xfrontend -module-load-mode -Xfrontend prefer-interface")
endif()

# Add rpath to 'lib/{platform}'
file(RELATIVE_PATH relpath_to_lib
"${SWIFT_HOST_PLUGINS_DEST_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ swift_create_stdlib_targets("swift-test-libexec" "" FALSE)

# Check whether the Swift compiler we're using supports
# -disable-implicit-backtracing-module-import
include(SwiftImplicitImport)
include(SwiftCompilerCapability)

swift_supports_implicit_module("backtracing" SWIFT_COMPILER_SUPPORTS_BACKTRACING)

Expand Down
6 changes: 6 additions & 0 deletions tools/swift-plugin-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ if (SWIFT_BUILD_SWIFT_SYNTAX)
SwiftCompilerPluginMessageHandling
SwiftLibraryPluginProvider
)

# FXIME: Importing package-cmo enabled '.swiftmodule' causes compiler crash :(
if(Swift_COMPILER_PACKAGE_CMO_SUPPORT)
target_compile_options(swift-plugin-server PRIVATE
"SHELL:-Xfrontend -module-load-mode -Xfrontend prefer-interface")
endif()
endif()