diff --git a/swift/internal/compiling.bzl b/swift/internal/compiling.bzl index fba2971d4..5690a599e 100644 --- a/swift/internal/compiling.bzl +++ b/swift/internal/compiling.bzl @@ -60,6 +60,7 @@ load( "SWIFT_FEATURE_USE_C_MODULES", "SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE", "SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE", + "SWIFT_FEATURE_USE_HOST_NUM_THREADS", "SWIFT_FEATURE_VFSOVERLAY", "SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS", "SWIFT_FEATURE__WMO_IN_SWIFTCOPTS", @@ -767,6 +768,16 @@ def compile_action_configs( ], not_features = [SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS], ), + swift_toolchain_config.action_config( + actions = [ + swift_action_names.COMPILE, + swift_action_names.DERIVE_FILES, + ], + configurators = [_wmo_host_thread_count_configurator], + features = [ + [SWIFT_FEATURE_USE_HOST_NUM_THREADS, SWIFT_FEATURE__WMO_IN_SWIFTCOPTS], + ], + ), swift_toolchain_config.action_config( actions = [ swift_action_names.COMPILE, @@ -1385,6 +1396,11 @@ def _wmo_thread_count_configurator(should_check_flags, prerequisites, args): # Force threaded mode for WMO builds. args.add("-num-threads", str(_DEFAULT_WMO_THREAD_COUNT)) +def _wmo_host_thread_count_configurator(prerequisites, args): + """Sets num-threads to number of threads on the host. + """ + args.add("-Xwrapped-swift=-use-host-num-threads") + def _is_wmo_manually_requested(user_compile_flags): """Returns `True` if a WMO flag is in the given list of compiler flags. diff --git a/swift/internal/feature_names.bzl b/swift/internal/feature_names.bzl index 318235d0c..c3e964126 100644 --- a/swift/internal/feature_names.bzl +++ b/swift/internal/feature_names.bzl @@ -266,3 +266,6 @@ SWIFT_FEATURE__WMO_IN_SWIFTCOPTS = "swift._wmo_in_swiftcopts" # were passed on the command line using `--swiftcopt`. Users should never # manually enable, disable, or query this feature. SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS = "swift._num_threads_0_in_swiftcopts" + +# If enabled, it passes the number of threads that the host has +SWIFT_FEATURE_USE_HOST_NUM_THREADS = "swift.use_host_num_threads" diff --git a/tools/worker/swift_runner.cc b/tools/worker/swift_runner.cc index 41068581c..6dadbc5c1 100644 --- a/tools/worker/swift_runner.cc +++ b/tools/worker/swift_runner.cc @@ -14,6 +14,9 @@ #include "tools/worker/swift_runner.h" +#include +#include + #include #include "tools/common/bazel_substitutions.h" @@ -102,7 +105,7 @@ static bool StripPrefix(const std::string &prefix, std::string &str) { SwiftRunner::SwiftRunner(const std::vector &args, bool force_response_file) - : force_response_file_(force_response_file) { + : force_response_file_(force_response_file), use_host_num_threads_(false) { args_ = ProcessArguments(args); } @@ -257,6 +260,26 @@ bool SwiftRunner::ProcessPossibleResponseFile( return changed; } +static int GetNumCPU() { + int sysctlarg[4]; + int numCPU; + std::size_t len = sizeof(numCPU); + + // set the sysctlarg for hw.ncpu + sysctlarg[0] = CTL_HW; + sysctlarg[1] = HW_AVAILCPU; + + // Gets NCPU from sysctl + sysctl(sysctlarg, 2, &numCPU, &len, NULL, 0); + + if (numCPU < 1) { + sysctlarg[1] = HW_NCPU; + sysctl(sysctlarg, 2, &numCPU, &len, NULL, 0); + if (numCPU < 1) numCPU = 1; + } + return numCPU; +} + template bool SwiftRunner::ProcessArgument( Iterator &itr, const std::string &arg, @@ -318,6 +341,14 @@ bool SwiftRunner::ProcessArgument( ++itr; new_arg = output_file_map_path_; changed = true; + } else if (use_host_num_threads_ && arg == "-num-threads") { + consumer("-num-threads"); + + // If we're using the number of threads on the host, set this value to + // the NumCPU. + ++itr; + new_arg = std::to_string(GetNumCPU()); + changed = true; } // Apply any other text substitutions needed in the argument (i.e., for @@ -347,6 +378,8 @@ std::vector SwiftRunner::ParseArguments(Iterator itr) { global_index_store_import_path_ = arg; } else if (StripPrefix("-generated-header-rewriter=", arg)) { generated_header_rewriter_path_ = arg; + } else if (StripPrefix("-use-host-num-threads", arg)) { + use_host_num_threads_ = true; } } else { if (arg == "-output-file-map") { diff --git a/tools/worker/swift_runner.h b/tools/worker/swift_runner.h index ad5389576..231ca231c 100644 --- a/tools/worker/swift_runner.h +++ b/tools/worker/swift_runner.h @@ -146,6 +146,9 @@ class SwiftRunner { // `-index-store-path`. After running `swiftc` `index-import` copies relevant // index outputs into the `index_store_path` to integrate outputs with Bazel. std::string global_index_store_import_path_; + + // If the worker should set num-threads to number of threads on the host. + bool use_host_num_threads_; }; #endif // BUILD_BAZEL_RULES_SWIFT_TOOLS_WORKER_SWIFT_RUNNER_H_