Move llvm::Initialize* calls to main. (#3449)

Per their documentation, the `llvm::Initialize*` functions are only
supposed to be called by the main program, not by a library like
toolchain/codegen. Fixes a hang due to a data race in multithreaded
autoupdate.

Add a utility class `Carbon::InitLLVM` to do the common LLVM
initialization shared by all Carbon tools, optionally including
initializing the LLVM targets. Because the LLVM targets add a lot of
binary size, only initialize them for binaries that opt in by depending
on a new target `//common:all_llvm_targets`.

Also fix `//explorer:file_test` and `//explorer:file_test.trace` to
share a binary rather than linking an identical binary twice.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2023-12-07 01:45:47 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent e9fc07feee
commit bf8697113a
16 changed files with 319 additions and 54 deletions
-8
View File
@@ -8,7 +8,6 @@
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/TargetParser/Host.h"
@@ -17,13 +16,6 @@ namespace Carbon {
auto CodeGen::Create(llvm::Module& module, llvm::StringRef target_triple,
llvm::raw_pwrite_stream& errors)
-> std::optional<CodeGen> {
// Initialize the target registry etc.
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
std::string error;
const llvm::Target* target =
llvm::TargetRegistry::lookupTarget(target_triple, error);