Switch compile functions to use options structs (#5742)

I've been mulling this mainly for the parameter complexity of
check/lower, but doing lex/parse for symmetry.

I'm motivated by the plan to move dumping for all of them into the
respective functions, because of discussion about llvm-verifier. That
basically would add another bool parameter (or more) to each of these.
My instinct is we're going to probably accrue a little more over time,
so I'm suggesting this as maybe adding the boundary a little simpler
and/or easier to read.

Note it may make sense to refactor a little further, e.g. maybe
Lower::Context could receive the full set of options and pick out what
it wants, but I figured creating the struct itself would be a decent
start.

I'm trying to put things into options when we can produce a reasonable
default if the user doesn't assign a value. I'm using an explicit
constructor so that values can be added without affecting every caller.

A different factoring would be to pass in everything through the param
struct, but that just felt weird when I was trying it out.

Removing `inst_namer` and `module_name` from `LowerToLLVM` params --
both of these can be inferred from `sem_ir`, and I'm not seeing a
particular reason to maintain them at the call site.
This commit is contained in:
Jon Ross-Perkins
2025-06-27 22:08:47 +00:00
committed by GitHub
parent 0722dab0ef
commit 2de746e83c
16 changed files with 149 additions and 67 deletions
+23 -16
View File
@@ -606,8 +606,11 @@ auto CompilationUnit::RunLex() -> void {
CARBON_VLOG("*** SourceBuffer ***\n```\n{0}\n```\n", source_->text());
LogCall("Lex::Lex", "lex",
[&] { tokens_ = Lex::Lex(value_stores_, *source_, *consumer_); });
LogCall("Lex::Lex", "lex", [&] {
Lex::LexOptions options;
options.consumer = consumer_;
tokens_ = Lex::Lex(value_stores_, *source_, options);
});
if (options_->dump_tokens && IncludeInDumps()) {
consumer_->Flush();
tokens_->Print(*driver_env_->output_stream,
@@ -624,7 +627,10 @@ auto CompilationUnit::RunLex() -> void {
auto CompilationUnit::RunParse() -> void {
LogCall("Parse::Parse", "parse", [&] {
parse_tree_ = Parse::Parse(*tokens_, *consumer_, vlog_stream_);
Parse::ParseOptions options;
options.consumer = consumer_;
options.vlog_stream = vlog_stream_;
parse_tree_ = Parse::Parse(*tokens_, options);
});
if (options_->dump_parse_tree && IncludeInDumps()) {
consumer_->Flush();
@@ -717,16 +723,14 @@ auto CompilationUnit::PostCheck() -> void {
auto CompilationUnit::RunLower() -> void {
LogCall("Lower::LowerToLLVM", "lower", [&] {
llvm_context_ = std::make_unique<llvm::LLVMContext>();
// TODO: Consider disabling instruction naming by default if we're not
// producing textual LLVM IR.
SemIR::InstNamer inst_namer(&*sem_ir_);
llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> subtrees =
cache_->tree_and_subtrees_getters();
module_ = Lower::LowerToLLVM(
*llvm_context_, driver_env_->fs,
options_->run_llvm_verifier ? driver_env_->error_stream : nullptr,
options_->include_debug_info, subtrees, input_filename_, *sem_ir_,
&inst_namer, vlog_stream_);
Lower::LowerToLLVMOptions options;
options.llvm_verifier_stream =
options_->run_llvm_verifier ? driver_env_->error_stream : nullptr;
options.want_debug_info = options_->include_debug_info;
options.vlog_stream = vlog_stream_;
module_ = Lower::LowerToLLVM(*llvm_context_, driver_env_->fs,
cache_->tree_and_subtrees_getters(), *sem_ir_,
options);
});
if (vlog_stream_) {
CARBON_VLOG("*** llvm::Module ***\n");
@@ -977,10 +981,13 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
// Execute the actual checking.
CARBON_VLOG_TO(driver_env.vlog_stream, "*** Check::CheckParseTrees ***\n");
Check::CheckParseTreesOptions options;
options.prelude_import = options_.prelude_import;
options.vlog_stream = driver_env.vlog_stream;
options.fuzzing = driver_env.fuzzing;
Check::CheckParseTrees(check_units, cache.tree_and_subtrees_getters(),
options_.prelude_import, driver_env.fs,
options_.codegen_options.target,
driver_env.vlog_stream, driver_env.fuzzing);
driver_env.fs, options_.codegen_options.target,
options);
CARBON_VLOG_TO(driver_env.vlog_stream,
"*** Check::CheckParseTrees done ***\n");
for (auto& unit : units) {