Files
carbon-lang/toolchain/lower/lower.cpp
T
Geoff Romer 92534a0e07 Cache and reuse InstNamers (#8105)
This is intended to enable a future change, where a file's SemIR will be
able to directly refer to insts from another file. In order to format
such references, we will need to access the `InstNamer` for the foreign
IR while formatting the local IR, so that we can keep the inst name
consistent across files.
2026-11-13 01:17:20 +00:00

54 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,
SemIR::GetInstNamerFn get_inst_namer, 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);
const SemIR::InstNamer* inst_namer = nullptr;
if (options.generate_inst_names) {
inst_namer = &get_inst_namer();
}
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