Files
carbon-lang/common/init_llvm.h
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

43 lines
1.4 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
#ifndef CARBON_COMMON_INIT_LLVM_H_
#define CARBON_COMMON_INIT_LLVM_H_
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/InitLLVM.h"
namespace Carbon {
// A RAII class to handle initializing LLVM and shutting it down. An instance of
// this class should be created in the `main` function of each Carbon binary
// that interacts with LLVM, before `argc` and `argv` are first inspected.
class InitLLVM {
public:
// Initializes LLVM for use by a Carbon binary. On Windows, `argc` and `argv`
// are updated to refer to properly-encoded UTF-8 versions of the command line
// arguments.
explicit InitLLVM(int& argc, char**& argv);
// Shuts down LLVM.
~InitLLVM() = default;
private:
llvm::InitLLVM init_llvm_;
llvm::SmallVector<char*> args_;
// A pointer to the LLVM target initialization function, if :all_llvm_targets
// is linked in. Otherwise nullptr.
static auto (*InitializeTargets)() -> void;
// The initializer of this static data member populates `InitializeTargets`.
// Defined only if :all_llvm_targets is linked in. This is a member so that
// it has access to `InitializeTargets`.
static char RegisterTargets;
};
} // namespace Carbon
#endif // CARBON_COMMON_INIT_LLVM_H_