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