From fd9200ebc356d43f22803432a18e1cf9330d012e Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Thu, 1 Oct 2026 00:10:11 +0000 Subject: [PATCH] Disable `InstNamer` in lowering unless we're emitting textual llvm IR (#7878) `InstNamer` takes O(N^2) time to resolve N colliding inst names, so it should be limited to textual dumps (where we can presumably prioritize readability over performance). Without this change, compiling 200,000 destructor calls took hours. With this change, compiling 1,000,000 destructor calls takes about 30 seconds. --- toolchain/driver/compile_driver.cpp | 2 ++ toolchain/lower/lower.cpp | 11 +++++++---- toolchain/lower/options.h | 3 +++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/toolchain/driver/compile_driver.cpp b/toolchain/driver/compile_driver.cpp index d87eac7eaf36..fccb6d6ab22e 100644 --- a/toolchain/driver/compile_driver.cpp +++ b/toolchain/driver/compile_driver.cpp @@ -191,6 +191,8 @@ auto CompilationUnit::RunLower() -> void { options.vlog_stream = vlog_stream_; options.opt_level = options_->opt_level; options.mangle_string_fingerprint = options_->mangle_string_fingerprint; + // Only generate inst names if the llvm IR needs to be human-readable. + options.generate_inst_names = options_->dump_llvm_ir; module_ = Lower::LowerToLLVM(*llvm_context_, driver_env_->fs, cache_->tree_and_subtrees_getters(), *sem_ir_, total_ir_count_, options); diff --git a/toolchain/lower/lower.cpp b/toolchain/lower/lower.cpp index 83d24b2c2243..57062b92a9f1 100644 --- a/toolchain/lower/lower.cpp +++ b/toolchain/lower/lower.cpp @@ -27,10 +27,13 @@ auto LowerToLLVM( 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::optional inst_namer; + if (options.generate_inst_names) { + inst_namer.emplace(&sem_ir, total_ir_count); + } + context + .GetFileContext(&sem_ir, inst_namer.has_value() ? &*inst_namer : nullptr) + .LowerDefinitions(); std::unique_ptr module = std::move(context).Finalize(); diff --git a/toolchain/lower/options.h b/toolchain/lower/options.h index e5097a1db356..914067625326 100644 --- a/toolchain/lower/options.h +++ b/toolchain/lower/options.h @@ -40,6 +40,9 @@ struct LowerToLLVMOptions { // Whether to use the string form of the fingerprint for mangling. bool mangle_string_fingerprint = false; + + // Whether to generate names for LLVM instructions. + bool generate_inst_names = false; }; } // namespace Carbon::Lower