Skip to content

Commit 9db63b9

Browse files
Merge pull request #2367 from swiftwasm/katei/embed-swms
Enable stdlib thin CMO
2 parents f1a66a4 + 7835c57 commit 9db63b9

File tree

11 files changed

+70
-2
lines changed

11 files changed

+70
-2
lines changed

cmake/caches/Runtime-WASI-wasm32.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY NO CACHE BOOL "")
2222
# TODO(katei): This should get turned off, as this is not an ABI stable platform.
2323
# But current CMake build system doesn't support SWIFT_STDLIB_STABLE_ABI=NO
2424
set(SWIFT_STDLIB_STABLE_ABI YES CACHE BOOL "")
25+
set(SWIFT_STDLIB_ENABLE_THINCMO YES CACHE BOOL "")
2526

2627
# build with the host compiler
2728
set(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER YES CACHE BOOL "")

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ class IRGenOptions {
244244
/// Frameworks that we should not autolink against.
245245
SmallVector<std::string, 1> DisableAutolinkFrameworks;
246246

247+
/// Disable autolinking swiftCore for stdlib thin CMO
248+
unsigned DisableAutolinkStdlib : 1;
249+
247250
/// Print the LLVM inline tree at the end of the LLVM pass pipeline.
248251
unsigned PrintInlineTree : 1;
249252

@@ -358,6 +361,7 @@ class IRGenOptions {
358361
DisableLLVMOptzns(false), DisableSwiftSpecificLLVMOptzns(false),
359362
DisableLLVMSLPVectorizer(false), Playground(false),
360363
EmitStackPromotionChecks(false), FunctionSections(false),
364+
DisableAutolinkStdlib(false),
361365
PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
362366
LLVMLTOKind(IRGenLLVMLTOKind::None), HasValueNamesSetting(false),
363367
ValueNames(false), EnableReflectionMetadata(true),

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ def no_stdlib_rpath: Flag<["-"], "no-stdlib-rpath">,
713713
Flags<[HelpHidden,DoesNotAffectIncrementalBuild]>,
714714
HelpText<"Don't add any rpath entries.">;
715715

716+
def no_stdlib_link: Flag<["-"], "no-stdlib-link">,
717+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
718+
HelpText<"Don't link stdlib.">;
719+
716720
def static_executable : Flag<["-"], "static-executable">,
717721
HelpText<"Statically link the executable">;
718722
def no_static_executable : Flag<["-"], "no-static-executable">,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
306306
}
307307

308308
addLTOArgs(OI, arguments);
309+
inputArgs.AddLastArg(arguments, options::OPT_no_stdlib_link);
309310

310311
// -g implies -enable-anonymous-context-mangled-names, because the extra
311312
// metadata aids debugging.

lib/Driver/WebAssemblyToolChains.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
163163
llvm::report_fatal_error(linkFile + " not found");
164164
}
165165

166+
if (context.Args.hasArg(options::OPT_no_stdlib_link)) {
167+
Arguments.push_back("-lswiftStandaloneRuntime");
168+
} else {
169+
Arguments.push_back("-lswiftCore");
170+
}
171+
166172
// Explicitly pass the target to the linker
167173
Arguments.push_back(
168174
context.Args.MakeArgString("--target=" + getTriple().str()));

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
15591559
A->getAsString(Args), A->getValue());
15601560
}
15611561

1562+
if (Args.hasArg(OPT_no_stdlib_link)) {
1563+
Opts.DisableAutolinkStdlib = true;
1564+
}
1565+
15621566
if (const Arg *A = Args.getLastArg(options::OPT_sanitize_coverage_EQ)) {
15631567
Opts.SanitizeCoverage =
15641568
parseSanitizerCoverageArgValue(A, Triple, Diags, Opts.Sanitizers);

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,8 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
12201220

12211221
switch (linkLib.getKind()) {
12221222
case LibraryKind::Library: {
1223+
if (linkLib.getName() == "swiftCore" && IRGen.Opts.DisableAutolinkStdlib)
1224+
return;
12231225
AutolinkEntries.emplace_back(linkLib);
12241226
break;
12251227
}

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ option(SWIFT_ENABLE_MODULE_INTERFACES
5959
"Generate .swiftinterface files alongside .swiftmodule files"
6060
"${SWIFT_STDLIB_STABLE_ABI}")
6161

62+
option(SWIFT_STDLIB_ENABLE_THINCMO
63+
"Generate .swiftmodulesummary and .sib files alongside .swiftmodule files for thin cross module optimization"
64+
FALSE)
65+
6266
option(SWIFT_ENABLE_COMPATIBILITY_OVERRIDES
6367
"Support back-deploying compatibility fixes for newer apps running on older runtimes."
6468
TRUE)

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,28 @@ function(_compile_swift_files
806806
${copy_legacy_layouts_dep}
807807
COMMENT "Generating ${module_file}")
808808

809+
set(modulesummary_file "${module_base}.swiftmodulesummary")
810+
set(modulesummary_file_static "${module_base_static}.swiftmodulesummary")
811+
set(thincmo_sib_file "${module_base}.sib")
812+
set(thincmo_sib_file_static "${module_base_static}.sib")
813+
if(SWIFT_STDLIB_ENABLE_THINCMO)
814+
add_custom_command_target(
815+
thincmo_intermediate_target
816+
COMMAND
817+
"$<TARGET_FILE:Python3::Interpreter>" "${line_directive_tool}" "@${file_path}" --
818+
"${swift_compiler_tool}" "-emit-sib" "-o" "${thincmo_sib_file}"
819+
${swift_flags} "-emit-module-summary" "@${file_path}"
820+
OUTPUT "${modulesummary_file}" "${thincmo_sib_file}"
821+
DEPENDS
822+
${swift_compiler_tool_dep}
823+
${source_files} ${SWIFTFILE_DEPENDS}
824+
${swift_ide_test_dependency}
825+
${create_dirs_dependency_target}
826+
${copy_legacy_layouts_dep}
827+
${module_dependency_target}
828+
COMMENT "Generating ${modulesummary_file}")
829+
endif()
830+
809831
if(SWIFTFILE_STATIC)
810832
add_custom_command_target(
811833
module_dependency_target_static
@@ -823,8 +845,22 @@ function(_compile_swift_files
823845
"${module_dependency_target}"
824846
COMMENT "Generating ${module_file}")
825847
set("${dependency_module_target_out_var_name}" "${module_dependency_target_static}" PARENT_SCOPE)
848+
849+
if(SWIFT_STDLIB_ENABLE_THINCMO)
850+
add_custom_command_target(
851+
thincmo_intermediate_target_static
852+
COMMAND
853+
"${CMAKE_COMMAND}" "-E" "copy" ${modulesummary_file} ${modulesummary_file_static}
854+
COMMAND
855+
"${CMAKE_COMMAND}" "-E" "copy" ${thincmo_sib_file} ${thincmo_sib_file_static}
856+
OUTPUT ${modulesummary_file_static}
857+
DEPENDS
858+
"${thincmo_intermediate_target}" "${module_dependency_target_static}"
859+
COMMENT "Generating ${modulesummary_file_static}")
860+
set("${dependency_module_target_out_var_name}" "${module_dependency_target_static}" "${thincmo_intermediate_target_static}" PARENT_SCOPE)
861+
endif()
826862
else()
827-
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" PARENT_SCOPE)
863+
set("${dependency_module_target_out_var_name}" "${module_dependency_target}" "${thincmo_intermediate_target}" PARENT_SCOPE)
828864
endif()
829865

830866
# macCatalyst zippered swiftmodule

stdlib/public/runtime/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY
153153
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
154154
INSTALL_IN_COMPONENT never_install)
155155

156+
157+
add_swift_target_library(swiftStandaloneRuntime STATIC IS_STDLIB
158+
INCORPORATE_OBJECT_LIBRARIES
159+
swiftRuntime swiftLLVMSupport swiftDemangling swiftStdlibStubs
160+
INSTALL_IN_COMPONENT
161+
stdlib)
162+
156163
set(ELFISH_SDKS)
157164
set(COFF_SDKS)
158165
set(WASM_SDKS)

0 commit comments

Comments
 (0)