mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Instead of creating a CodeGenerator per CppDomain, and then crashing in lowering when we try to consume the same llvm Module multiple times, create a CodeGenerator for each CppFile within the domain. For now, we mulitplex all of Clang's ASTConsumer output to all code generators, which means that any strong external definitions within a Carbon file (for example, in an inline `Cpp` fragment) will be emitted to all output files in the same `CppDomain`, resulting in link errors due to symbol redefinitions. This will be addressed later. But this should be sufficient for Carbon compilations in which such symbols are not defined. We also don't yet attempt to classify which compilations will need C++ code generation, and instead create a clang `CodeGenerator` for every Carbon file that has C++ imports. For `carbom compile`, only one Carbon file will need code generation, and yet we still build multiple `CodeGenerator` objects in general. Fixing this requires more plumbing from the driver, and this will also be handled in a follow-up. Assisted-by: Gemini via Antigravity
52 lines
1.7 KiB
C++
52 lines
1.7 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 "toolchain/lower/lower.h"
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
|
|
#include "common/vlog.h"
|
|
#include "llvm/IR/Verifier.h"
|
|
#include "toolchain/lower/context.h"
|
|
#include "toolchain/lower/file_context.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
auto LowerToLLVM(
|
|
llvm::LLVMContext& llvm_context,
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
|
|
const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters,
|
|
const SemIR::File& sem_ir, int total_ir_count,
|
|
const LowerToLLVMOptions& options) -> std::unique_ptr<llvm::Module> {
|
|
Context context(
|
|
&llvm_context, std::move(fs), options.want_debug_info,
|
|
&tree_and_subtrees_getters,
|
|
sem_ir.cpp_file() ? sem_ir.cpp_file()->code_generator() : nullptr,
|
|
sem_ir.filename(), total_ir_count, options.opt_level,
|
|
options.mangle_string_fingerprint, options.vlog_stream);
|
|
|
|
// TODO: Consider disabling instruction naming by default if we're not
|
|
// producing textual LLVM IR.
|
|
SemIR::InstNamer inst_namer(&sem_ir, total_ir_count);
|
|
context.GetFileContext(&sem_ir, &inst_namer).LowerDefinitions();
|
|
|
|
std::unique_ptr<llvm::Module> module = std::move(context).Finalize();
|
|
|
|
if (options.vlog_stream) {
|
|
CARBON_VLOG_TO(options.vlog_stream, "*** llvm::Module ***\n");
|
|
module->print(*options.vlog_stream, /*AAW=*/nullptr,
|
|
/*ShouldPreserveUseListOrder=*/false,
|
|
/*IsForDebug=*/true);
|
|
}
|
|
|
|
if (options.llvm_verifier_stream) {
|
|
CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
|
|
}
|
|
|
|
return module;
|
|
}
|
|
|
|
} // namespace Carbon::Lower
|