|
|
@@ -18,14 +18,21 @@
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
+#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
+#include "clang/Frontend/CompilerInstance.h"
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
+#include "clang/Frontend/TextDiagnosticBuffer.h"
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
+#include "clang/Frontend/Utils.h"
|
|
|
+#include "clang/FrontendTool/Utils.h"
|
|
|
+#include "clang/Serialization/ObjectFilePCHContainerReader.h"
|
|
|
#include "common/filesystem.h"
|
|
|
#include "common/vlog.h"
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
|
|
+#include "llvm/ADT/Statistic.h"
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
@@ -36,31 +43,17 @@
|
|
|
#include "llvm/Support/LLVMDriver.h"
|
|
|
#include "llvm/Support/Path.h"
|
|
|
#include "llvm/Support/Program.h"
|
|
|
+#include "llvm/Support/TimeProfiler.h"
|
|
|
+#include "llvm/Support/Timer.h"
|
|
|
#include "llvm/TargetParser/Host.h"
|
|
|
#include "toolchain/base/runtime_sources.h"
|
|
|
|
|
|
-// Defined in:
|
|
|
-// https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
|
|
|
-//
|
|
|
-// While not in a header, this is the API used by llvm-driver.cpp for
|
|
|
-// busyboxing.
|
|
|
-//
|
|
|
-// NOLINTNEXTLINE(readability-identifier-naming)
|
|
|
-auto clang_main(int Argc, char** Argv, const llvm::ToolContext& ToolContext)
|
|
|
- -> int;
|
|
|
-
|
|
|
namespace Carbon {
|
|
|
|
|
|
ClangRunner::ClangRunner(const InstallPaths* install_paths,
|
|
|
- Runtimes::Cache* runtimes_cache,
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
|
|
|
- llvm::raw_ostream* vlog_stream,
|
|
|
- bool build_runtimes_on_demand)
|
|
|
- : ToolRunnerBase(install_paths, vlog_stream),
|
|
|
- runtimes_cache_(runtimes_cache),
|
|
|
- fs_(std::move(fs)),
|
|
|
- diagnostic_ids_(new clang::DiagnosticIDs()),
|
|
|
- build_runtimes_on_demand_(build_runtimes_on_demand) {}
|
|
|
+ llvm::raw_ostream* vlog_stream)
|
|
|
+ : ToolRunnerBase(install_paths, vlog_stream), fs_(std::move(fs)) {}
|
|
|
|
|
|
// Searches an argument list to a Clang execution to determine the expected
|
|
|
// target string, suitable for use with `llvm::Triple`.
|
|
|
@@ -133,31 +126,41 @@ static auto IsNonLinkCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
- Runtimes* prebuilt_runtimes) -> ErrorOr<bool> {
|
|
|
+auto ClangRunner::RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
+ Runtimes& prebuilt_runtimes)
|
|
|
+ -> ErrorOr<bool> {
|
|
|
// Check the args to see if we have a known target-independent command. If so,
|
|
|
// directly dispatch it to avoid the cost of building the target resource
|
|
|
// directory.
|
|
|
// TODO: Maybe handle response file expansion similar to the Clang CLI?
|
|
|
- if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args) ||
|
|
|
- (!build_runtimes_on_demand_ && !prebuilt_runtimes)) {
|
|
|
- return RunTargetIndependentCommand(args);
|
|
|
+ if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
|
|
|
+ return RunWithNoRuntimes(args);
|
|
|
}
|
|
|
|
|
|
std::string target = ComputeClangTarget(args);
|
|
|
|
|
|
- // If we have pre-built runtimes, use them rather than building on demand.
|
|
|
- if (prebuilt_runtimes) {
|
|
|
- CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
|
|
|
- prebuilt_runtimes->Get(Runtimes::ClangResourceDir));
|
|
|
- return RunInternal(args, target, prebuilt_resource_dir_path.native());
|
|
|
+ CARBON_ASSIGN_OR_RETURN(std::filesystem::path prebuilt_resource_dir_path,
|
|
|
+ prebuilt_runtimes.Get(Runtimes::ClangResourceDir));
|
|
|
+ return RunInternal(args, target, prebuilt_resource_dir_path.native());
|
|
|
+}
|
|
|
+
|
|
|
+auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
+ Runtimes::Cache& runtimes_cache,
|
|
|
+ llvm::ThreadPoolInterface& runtimes_build_thread_pool)
|
|
|
+ -> ErrorOr<bool> {
|
|
|
+ // Check the args to see if we have a known target-independent command. If so,
|
|
|
+ // directly dispatch it to avoid the cost of building the target resource
|
|
|
+ // directory.
|
|
|
+ // TODO: Maybe handle response file expansion similar to the Clang CLI?
|
|
|
+ if (args.empty() || args[0].starts_with("-cc1") || IsNonLinkCommand(args)) {
|
|
|
+ return RunWithNoRuntimes(args);
|
|
|
}
|
|
|
- CARBON_CHECK(build_runtimes_on_demand_);
|
|
|
|
|
|
- // Otherwise, we need to build a target resource directory.
|
|
|
+ std::string target = ComputeClangTarget(args);
|
|
|
+
|
|
|
CARBON_VLOG("Building target resource dir...\n");
|
|
|
Runtimes::Cache::Features features = {.target = target};
|
|
|
- CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache_->Lookup(features));
|
|
|
+ CARBON_ASSIGN_OR_RETURN(Runtimes runtimes, runtimes_cache.Lookup(features));
|
|
|
|
|
|
// We need to build the Clang resource directory for these runtimes. This
|
|
|
// requires a temporary directory as well as the destination directory for
|
|
|
@@ -165,11 +168,14 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
// not once we are running Clang with the built runtime.
|
|
|
std::filesystem::path resource_dir_path;
|
|
|
{
|
|
|
+ // Build the temporary directory and threadpool needed.
|
|
|
CARBON_ASSIGN_OR_RETURN(Filesystem::RemovingDir tmp_dir,
|
|
|
Filesystem::MakeTmpDir());
|
|
|
+
|
|
|
CARBON_ASSIGN_OR_RETURN(
|
|
|
resource_dir_path,
|
|
|
- BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path()));
|
|
|
+ BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path(),
|
|
|
+ runtimes_build_thread_pool));
|
|
|
}
|
|
|
|
|
|
// Note that this function always successfully runs `clang` and returns a bool
|
|
|
@@ -179,15 +185,16 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
|
return RunInternal(args, target, resource_dir_path.native());
|
|
|
}
|
|
|
|
|
|
-auto ClangRunner::RunTargetIndependentCommand(
|
|
|
- llvm::ArrayRef<llvm::StringRef> args) -> bool {
|
|
|
+auto ClangRunner::RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args)
|
|
|
+ -> bool {
|
|
|
std::string target = ComputeClangTarget(args);
|
|
|
return RunInternal(args, target, std::nullopt);
|
|
|
}
|
|
|
|
|
|
auto ClangRunner::BuildTargetResourceDir(
|
|
|
const Runtimes::Cache::Features& features, Runtimes& runtimes,
|
|
|
- const std::filesystem::path& tmp_path) -> ErrorOr<std::filesystem::path> {
|
|
|
+ const std::filesystem::path& tmp_path, llvm::ThreadPoolInterface& threads)
|
|
|
+ -> ErrorOr<std::filesystem::path> {
|
|
|
// Disable any leaking of memory while building the target resource dir, and
|
|
|
// restore the previous setting at the end.
|
|
|
auto restore_leak_flag = llvm::make_scope_exit(
|
|
|
@@ -223,21 +230,171 @@ auto ClangRunner::BuildTargetResourceDir(
|
|
|
return Error("TODO: Windows runtimes are untested and not yet supported.");
|
|
|
}
|
|
|
|
|
|
+ llvm::ThreadPoolTaskGroup task_group(threads);
|
|
|
+
|
|
|
// For Linux targets, the system libc (typically glibc) doesn't necessarily
|
|
|
// provide the CRT begin/end files, and so we need to build them.
|
|
|
if (target_triple.isOSLinux()) {
|
|
|
- BuildCrtFile(target, RuntimeSources::CrtBegin,
|
|
|
- builder.path() / lib_path / "clang_rt.crtbegin.o");
|
|
|
- BuildCrtFile(target, RuntimeSources::CrtEnd,
|
|
|
- builder.path() / lib_path / "clang_rt.crtend.o");
|
|
|
+ task_group.async(
|
|
|
+ [this, target,
|
|
|
+ path = builder.path() / lib_path / "clang_rt.crtbegin.o"] {
|
|
|
+ BuildCrtFile(target, RuntimeSources::CrtBegin, path);
|
|
|
+ });
|
|
|
+ task_group.async(
|
|
|
+ [this, target, path = builder.path() / lib_path / "clang_rt.crtend.o"] {
|
|
|
+ BuildCrtFile(target, RuntimeSources::CrtEnd, path);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
CARBON_RETURN_IF_ERROR(
|
|
|
- BuildBuiltinsLib(target, target_triple, tmp_path, lib_dir));
|
|
|
+ BuildBuiltinsLib(target, target_triple, tmp_path, lib_dir, threads));
|
|
|
+
|
|
|
+ // Now wait for all the queued builds to complete before we commit the
|
|
|
+ // runtimes into the cache.
|
|
|
+ task_group.wait();
|
|
|
|
|
|
return std::move(builder).Commit();
|
|
|
}
|
|
|
|
|
|
+auto ClangRunner::RunCC1(llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
+ llvm::BumpPtrAllocator allocator;
|
|
|
+ llvm::cl::ExpansionContext expansion_context(
|
|
|
+ allocator, llvm::cl::TokenizeGNUCommandLine);
|
|
|
+ if (llvm::Error error = expansion_context.expandResponseFiles(cc1_args)) {
|
|
|
+ llvm::errs() << toString(std::move(error)) << '\n';
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ CARBON_CHECK(cc1_args[1] == llvm::StringRef("-cc1"));
|
|
|
+
|
|
|
+ llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diag_ids =
|
|
|
+ clang::DiagnosticIDs::create();
|
|
|
+
|
|
|
+ // Register the support for object-file-wrapped Clang modules.
|
|
|
+ auto pch_ops = std::make_shared<clang::PCHContainerOperations>();
|
|
|
+ pch_ops->registerWriter(
|
|
|
+ std::make_unique<clang::ObjectFilePCHContainerWriter>());
|
|
|
+ pch_ops->registerReader(
|
|
|
+ std::make_unique<clang::ObjectFilePCHContainerReader>());
|
|
|
+
|
|
|
+ // Buffer diagnostics from argument parsing so that we can output them using a
|
|
|
+ // well formed diagnostic object.
|
|
|
+ clang::DiagnosticOptions diag_opts;
|
|
|
+ clang::TextDiagnosticBuffer diag_buffer;
|
|
|
+ clang::DiagnosticsEngine diags(diag_ids, diag_opts, &diag_buffer,
|
|
|
+ /*ShouldOwnClient=*/false);
|
|
|
+
|
|
|
+ // Setup round-trip remarks for the DiagnosticsEngine used in CreateFromArgs.
|
|
|
+ if (llvm::find(cc1_args, llvm::StringRef("-Rround-trip-cc1-args")) !=
|
|
|
+ cc1_args.end()) {
|
|
|
+ diags.setSeverity(clang::diag::remark_cc1_round_trip_generated,
|
|
|
+ clang::diag::Severity::Remark, {});
|
|
|
+ }
|
|
|
+
|
|
|
+ auto invocation = std::make_shared<clang::CompilerInvocation>();
|
|
|
+ bool success = clang::CompilerInvocation::CreateFromArgs(
|
|
|
+ *invocation, llvm::ArrayRef(cc1_args).slice(1), diags, cc1_args[0]);
|
|
|
+
|
|
|
+ // Heap allocate the compiler instance so that if we disable freeing we can
|
|
|
+ // discard the pointer without destroying or deallocating it.
|
|
|
+ auto clang_instance = std::make_unique<clang::CompilerInstance>(
|
|
|
+ std::move(invocation), std::move(pch_ops));
|
|
|
+
|
|
|
+ // Override the disabling of free when we don't want to leak memory.
|
|
|
+ if (!enable_leaking_) {
|
|
|
+ clang_instance->getFrontendOpts().DisableFree = false;
|
|
|
+ clang_instance->getCodeGenOpts().DisableFree = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!clang_instance->getFrontendOpts().TimeTracePath.empty()) {
|
|
|
+ llvm::timeTraceProfilerInitialize(
|
|
|
+ clang_instance->getFrontendOpts().TimeTraceGranularity, cc1_args[0],
|
|
|
+ clang_instance->getFrontendOpts().TimeTraceVerbose);
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: These options should take priority over the actual compilation.
|
|
|
+ // However, their implementation is currently not accessible from a library.
|
|
|
+ // We should factor the implementation into a reusable location and then use
|
|
|
+ // that here.
|
|
|
+ CARBON_CHECK(!clang_instance->getFrontendOpts().PrintSupportedCPUs &&
|
|
|
+ !clang_instance->getFrontendOpts().PrintSupportedExtensions &&
|
|
|
+ !clang_instance->getFrontendOpts().PrintEnabledExtensions);
|
|
|
+
|
|
|
+ // Infer the builtin include path if unspecified.
|
|
|
+ if (clang_instance->getHeaderSearchOpts().UseBuiltinIncludes &&
|
|
|
+ clang_instance->getHeaderSearchOpts().ResourceDir.empty()) {
|
|
|
+ clang_instance->getHeaderSearchOpts().ResourceDir =
|
|
|
+ installation_->clang_resource_path();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create the actual diagnostics engine.
|
|
|
+ clang_instance->createDiagnostics(*fs_);
|
|
|
+ if (!clang_instance->hasDiagnostics()) {
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Now flush the buffered diagnostics into the Clang instance's diagnostic
|
|
|
+ // engine. If we've already hit an error, we can exit early once that's done.
|
|
|
+ diag_buffer.FlushDiagnostics(clang_instance->getDiagnostics());
|
|
|
+ if (!success) {
|
|
|
+ clang_instance->getDiagnosticClient().finish();
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Execute the frontend actions.
|
|
|
+ {
|
|
|
+ llvm::TimeTraceScope time_scope("ExecuteCompiler");
|
|
|
+ bool time_passes = clang_instance->getCodeGenOpts().TimePasses;
|
|
|
+ if (time_passes) {
|
|
|
+ clang_instance->createFrontendTimer();
|
|
|
+ }
|
|
|
+ llvm::TimeRegion timer(time_passes ? &clang_instance->getFrontendTimer()
|
|
|
+ : nullptr);
|
|
|
+ success = clang::ExecuteCompilerInvocation(clang_instance.get());
|
|
|
+ }
|
|
|
+
|
|
|
+ // If any timers were active but haven't been destroyed yet, print their
|
|
|
+ // results now. This happens in -disable-free mode.
|
|
|
+ std::unique_ptr<llvm::raw_ostream> io_file = llvm::CreateInfoOutputFile();
|
|
|
+ if (clang_instance->getCodeGenOpts().TimePassesJson) {
|
|
|
+ *io_file << "{\n";
|
|
|
+ llvm::TimerGroup::printAllJSONValues(*io_file, "");
|
|
|
+ *io_file << "\n}\n";
|
|
|
+ } else if (!clang_instance->getCodeGenOpts().TimePassesStatsFile) {
|
|
|
+ llvm::TimerGroup::printAll(*io_file);
|
|
|
+ }
|
|
|
+ llvm::TimerGroup::clearAll();
|
|
|
+
|
|
|
+ if (llvm::timeTraceProfilerEnabled()) {
|
|
|
+ // It is possible that the compiler instance doesn't own a file manager here
|
|
|
+ // if we're compiling a module unit, since the file manager is owned by the
|
|
|
+ // AST when we're compiling a module unit. So the file manager may be
|
|
|
+ // invalid here.
|
|
|
+ //
|
|
|
+ // It should be fine to create file manager here since the file system
|
|
|
+ // options are stored in the compiler invocation and we can recreate the VFS
|
|
|
+ // from the compiler invocation.
|
|
|
+ if (!clang_instance->hasFileManager()) {
|
|
|
+ clang_instance->createFileManager(fs_);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (auto profiler_output = clang_instance->createOutputFile(
|
|
|
+ clang_instance->getFrontendOpts().TimeTracePath, /*Binary=*/false,
|
|
|
+ /*RemoveFileOnSignal=*/false,
|
|
|
+ /*useTemporary=*/false)) {
|
|
|
+ llvm::timeTraceProfilerWrite(*profiler_output);
|
|
|
+ profiler_output.reset();
|
|
|
+ llvm::timeTraceProfilerCleanup();
|
|
|
+ clang_instance->clearOutputFiles(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // When running with -disable-free, don't do any destruction or shutdown.
|
|
|
+ if (clang_instance->getFrontendOpts().DisableFree) {
|
|
|
+ llvm::BuryPointer(std::move(clang_instance));
|
|
|
+ }
|
|
|
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
+}
|
|
|
+
|
|
|
auto ClangRunner::RunInternal(
|
|
|
llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
|
|
|
std::optional<llvm::StringRef> target_resource_dir_path) -> bool {
|
|
|
@@ -250,12 +407,8 @@ auto ClangRunner::RunInternal(
|
|
|
|
|
|
// Handle special dispatch for CC1 commands as they don't use the driver.
|
|
|
if (!args.empty() && args[0].starts_with("-cc1")) {
|
|
|
- CARBON_VLOG("Calling clang_main for cc1...");
|
|
|
- // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
|
|
|
- llvm::ToolContext tool_context = {
|
|
|
- .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
|
|
|
- int exit_code = clang_main(
|
|
|
- cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
|
|
|
+ CARBON_VLOG("Calling Clang's CC1...");
|
|
|
+ int exit_code = RunCC1(cstr_args);
|
|
|
// TODO: Should this be forwarding the full exit code?
|
|
|
return exit_code == 0;
|
|
|
}
|
|
|
@@ -273,8 +426,10 @@ auto ClangRunner::RunInternal(
|
|
|
clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
|
|
|
*diagnostic_options);
|
|
|
|
|
|
- clang::DiagnosticsEngine diagnostics(diagnostic_ids_, *diagnostic_options,
|
|
|
- &diagnostic_client,
|
|
|
+ // Note that the `DiagnosticsEngine` takes ownership (via a ref count) of the
|
|
|
+ // DiagnosticIDs, unlike the other parameters.
|
|
|
+ clang::DiagnosticsEngine diagnostics(clang::DiagnosticIDs::create(),
|
|
|
+ *diagnostic_options, &diagnostic_client,
|
|
|
/*ShouldOwnClient=*/false);
|
|
|
clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
|
|
|
|
|
|
@@ -323,18 +478,8 @@ auto ClangRunner::RunInternal(
|
|
|
//
|
|
|
// Also note that we only do `-disable-free` filtering in the in-process
|
|
|
// execution here, as subprocesses leaking memory won't impact this process.
|
|
|
- auto cc1_main = [enable_leaking = enable_leaking_](
|
|
|
- llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
- if (!enable_leaking) {
|
|
|
- // Last-flag wins, so this forcibly re-enables freeing memory.
|
|
|
- cc1_args.push_back("-no-disable-free");
|
|
|
- }
|
|
|
-
|
|
|
- // cc1_args[0] will be the `clang_path` so we don't need the prepend arg.
|
|
|
- llvm::ToolContext tool_context = {
|
|
|
- .Path = cc1_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
|
|
|
- return clang_main(cc1_args.size(), const_cast<char**>(cc1_args.data()),
|
|
|
- tool_context);
|
|
|
+ auto cc1_main = [this](llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
|
|
|
+ return RunCC1(cc1_args);
|
|
|
};
|
|
|
driver.CC1Main = cc1_main;
|
|
|
|
|
|
@@ -384,7 +529,7 @@ auto ClangRunner::BuildCrtFile(llvm::StringRef target, llvm::StringRef src_file,
|
|
|
CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
|
|
|
|
|
|
std::string target_arg = llvm::formatv("--target={0}", target).str();
|
|
|
- CARBON_CHECK(RunTargetIndependentCommand({
|
|
|
+ CARBON_CHECK(RunWithNoRuntimes({
|
|
|
"-no-canonical-prefixes",
|
|
|
target_arg,
|
|
|
"-DCRT_HAS_INITFINI_ARRAY",
|
|
|
@@ -463,7 +608,7 @@ auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
|
|
|
CARBON_VLOG("Building `{0}' from `{1}`...\n", out_path, src_path);
|
|
|
|
|
|
std::string target_arg = llvm::formatv("--target={0}", target).str();
|
|
|
- CARBON_CHECK(RunTargetIndependentCommand({
|
|
|
+ CARBON_CHECK(RunWithNoRuntimes({
|
|
|
"-no-canonical-prefixes",
|
|
|
target_arg,
|
|
|
"-O3",
|
|
|
@@ -484,7 +629,8 @@ auto ClangRunner::BuildBuiltinsFile(llvm::StringRef target,
|
|
|
auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
|
|
|
const llvm::Triple& target_triple,
|
|
|
const std::filesystem::path& tmp_path,
|
|
|
- Filesystem::DirRef lib_dir)
|
|
|
+ Filesystem::DirRef lib_dir,
|
|
|
+ llvm::ThreadPoolInterface& threads)
|
|
|
-> ErrorOr<Success> {
|
|
|
llvm::SmallVector<llvm::StringRef> src_files =
|
|
|
CollectBuiltinsSrcFiles(target_triple);
|
|
|
@@ -492,25 +638,29 @@ auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
|
|
|
CARBON_ASSIGN_OR_RETURN(Filesystem::Dir tmp_dir,
|
|
|
Filesystem::Cwd().OpenDir(tmp_path));
|
|
|
|
|
|
- llvm::SmallVector<llvm::NewArchiveMember> objs;
|
|
|
- objs.reserve(src_files.size());
|
|
|
- for (llvm::StringRef src_file : src_files) {
|
|
|
+ // `NewArchiveMember` isn't default constructable unfortunately, so we first
|
|
|
+ // build the objects using an optional wrapper.
|
|
|
+ llvm::SmallVector<std::optional<llvm::NewArchiveMember>> objs;
|
|
|
+ objs.resize(src_files.size());
|
|
|
+ llvm::ThreadPoolTaskGroup member_group(threads);
|
|
|
+ for (auto [src_file, obj] : llvm::zip_equal(src_files, objs)) {
|
|
|
// Create any subdirectories needed for this file.
|
|
|
std::filesystem::path src_path = src_file.str();
|
|
|
if (src_path.has_parent_path()) {
|
|
|
CARBON_RETURN_IF_ERROR(tmp_dir.CreateDirectories(src_path.parent_path()));
|
|
|
}
|
|
|
|
|
|
- std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
|
|
|
- obj_path += ".o";
|
|
|
- BuildBuiltinsFile(target, src_file, obj_path);
|
|
|
-
|
|
|
- llvm::Expected<llvm::NewArchiveMember> obj =
|
|
|
- llvm::NewArchiveMember::getFile(obj_path.native(),
|
|
|
- /*Deterministic=*/true);
|
|
|
- CARBON_CHECK(obj, "TODO: Diagnose this: {0}",
|
|
|
- llvm::fmt_consume(obj.takeError()));
|
|
|
- objs.push_back(std::move(*obj));
|
|
|
+ member_group.async([this, target, src_file, &obj, &tmp_path] {
|
|
|
+ std::filesystem::path obj_path = tmp_path / std::string_view(src_file);
|
|
|
+ obj_path += ".o";
|
|
|
+ BuildBuiltinsFile(target, src_file, obj_path);
|
|
|
+
|
|
|
+ auto obj_result = llvm::NewArchiveMember::getFile(obj_path.native(),
|
|
|
+ /*Deterministic=*/true);
|
|
|
+ CARBON_CHECK(obj_result, "TODO: Diagnose this: {0}",
|
|
|
+ llvm::fmt_consume(obj_result.takeError()));
|
|
|
+ obj = std::move(*obj_result);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// Now build an archive out of the `.o` files for the builtins. Note that we
|
|
|
@@ -520,11 +670,22 @@ auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
|
|
|
CARBON_ASSIGN_OR_RETURN(
|
|
|
Filesystem::WriteFile builtins_a_file,
|
|
|
lib_dir.OpenWriteOnly(builtins_a_path, Filesystem::CreateAlways));
|
|
|
+
|
|
|
+ // Wait for all the object compiles to complete, and then move the objects out
|
|
|
+ // of their optional wrappers to match the API required by the archive writer.
|
|
|
+ member_group.wait();
|
|
|
+ llvm::SmallVector<llvm::NewArchiveMember> unwrapped_objs;
|
|
|
+ unwrapped_objs.reserve(objs.size());
|
|
|
+ for (auto& obj : objs) {
|
|
|
+ unwrapped_objs.push_back(*std::move(obj));
|
|
|
+ }
|
|
|
+ objs.clear();
|
|
|
+
|
|
|
+ // Write the actual archive.
|
|
|
{
|
|
|
llvm::raw_fd_ostream builtins_a_os = builtins_a_file.WriteStream();
|
|
|
-
|
|
|
llvm::Error archive_err = llvm::writeArchiveToStream(
|
|
|
- builtins_a_os, objs, llvm::SymtabWritingMode::NormalSymtab,
|
|
|
+ builtins_a_os, unwrapped_objs, llvm::SymtabWritingMode::NormalSymtab,
|
|
|
target_triple.isOSDarwin() ? llvm::object::Archive::K_DARWIN
|
|
|
: llvm::object::Archive::K_GNU,
|
|
|
/*Deterministic=*/true, /*Thin=*/false);
|
|
|
@@ -534,7 +695,6 @@ auto ClangRunner::BuildBuiltinsLib(llvm::StringRef target,
|
|
|
}
|
|
|
}
|
|
|
CARBON_RETURN_IF_ERROR(std::move(builtins_a_file).Close());
|
|
|
-
|
|
|
return Success();
|
|
|
}
|
|
|
|