Files
carbon-lang/common/init_llvm.cpp
T
Richard SmithandJon Ross-Perkins bf8697113a 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>
2023-12-07 01:45:47 +00:00

40 lines
1.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/init_llvm.h"
#include "llvm/Support/TargetSelect.h"
namespace Carbon {
InitLLVM::InitLLVM(int& argc, char**& argv)
: init_llvm_(argc, argv),
// LLVM assumes that argc and argv won't change, and registers them with
// an `llvm::PrettyStackTraceProgram` that will crash if an argv element
// gets nulled out, which for example `testing::InitGoogleTest` does. So
// make a copy of the argv that LLVM produces in order to support
// mutation.
args_(argv, argv + argc) {
// Return our mutable copy of argv for the program to use.
argc = args_.size();
argv = args_.data();
// `argv[argc]` is expected to be a null pointer.
args_.push_back(0);
llvm::setBugReportMsg(
"Please report issues to "
"https://github.com/carbon-language/carbon-lang/issues and include the "
"crash backtrace.\n");
// Initialize LLVM targets if //common:all_llvm_targets was linked in.
if (InitializeTargets) {
InitializeTargets();
}
}
auto (*InitLLVM::InitializeTargets)() -> void = nullptr;
} // namespace Carbon