Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,6 @@ pub(crate) unsafe fn llvm_optimize(
config.verify_llvm_ir,
config.lint_llvm_ir,
thin_lto_buffer,
config.emit_thin_lto,
config.emit_thin_lto_summary,
merge_functions,
unroll_loops,
Expand Down Expand Up @@ -1033,7 +1032,7 @@ pub(crate) fn codegen(
"LLVM_module_codegen_make_bitcode",
&*module.name,
);
ThinBuffer::new(llmod, config.emit_thin_lto)
ThinBuffer::new(llmod, cgcx.lto != Lto::Fat)
};
let data = thin.data();
let _timer = prof
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,6 @@ unsafe extern "C" {
VerifyIR: bool,
LintIR: bool,
ThinLTOBuffer: Option<&mut *mut ThinLTOBuffer>,
EmitThinLTO: bool,
EmitThinLTOSummary: bool,
MergeFunctions: bool,
UnrollLoops: bool,
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ pub struct ModuleConfig {
pub emit_ir: bool,
pub emit_asm: bool,
pub emit_obj: EmitObj,
pub emit_thin_lto: bool,
pub emit_thin_lto_summary: bool,

// Miscellaneous flags. These are mostly copied from command-line
Expand Down Expand Up @@ -212,9 +211,6 @@ impl ModuleConfig {
false
),
emit_obj,
// thin lto summaries prevent fat lto, so do not emit them if fat
// lto is requested. See PR #136840 for background information.
emit_thin_lto: sess.opts.unstable_opts.emit_thin_lto && sess.lto() != Lto::Fat,
emit_thin_lto_summary: if_regular!(
sess.opts.output_types.contains_key(&OutputType::ThinLinkBitcode),
false
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,6 @@ fn test_unstable_options_tracking_hash() {
tracked!(dwarf_version, Some(5));
tracked!(embed_metadata, false);
tracked!(embed_source, true);
tracked!(emit_thin_lto, false);
tracked!(emscripten_wasm_eh, false);
tracked!(export_executable_symbols, true);
tracked!(fewer_names, Some(true));
Expand Down
34 changes: 14 additions & 20 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ extern "C" LLVMRustResult LLVMRustOptimize(
LLVMModuleRef ModuleRef, LLVMTargetMachineRef TMRef,
LLVMRustPassBuilderOptLevel OptLevelRust, LLVMRustOptStage OptStage,
bool IsLinkerPluginLTO, bool NoPrepopulatePasses, bool VerifyIR,
bool LintIR, LLVMRustThinLTOBuffer **ThinLTOBufferRef, bool EmitThinLTO,
bool LintIR, LLVMRustThinLTOBuffer **ThinLTOBufferRef,
bool EmitThinLTOSummary, bool MergeFunctions, bool UnrollLoops,
bool SLPVectorize, bool LoopVectorize, bool DisableSimplifyLibCalls,
bool EmitLifetimeMarkers, registerEnzymeAndPassPipelineFn EnzymePtr,
Expand Down Expand Up @@ -808,44 +808,35 @@ extern "C" LLVMRustResult LLVMRustOptimize(
}

ModulePassManager MPM;
bool NeedThinLTOBufferPasses = EmitThinLTO;
bool NeedThinLTOBufferPasses = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to be true when fat LTO is not used, so I ensured all fat LTO phases do NeedThinLTOBufferPasses = false below.

auto ThinLTOBuffer = std::make_unique<LLVMRustThinLTOBuffer>();
raw_string_ostream ThinLTODataOS(ThinLTOBuffer->data);
raw_string_ostream ThinLinkDataOS(ThinLTOBuffer->thin_link_data);
bool IsLTO = OptStage == LLVMRustOptStage::ThinLTO ||
OptStage == LLVMRustOptStage::FatLTO;
if (!NoPrepopulatePasses) {
for (const auto &C : PipelineStartEPCallbacks)
PB.registerPipelineStartEPCallback(C);
for (const auto &C : OptimizerLastEPCallbacks)
PB.registerOptimizerLastEPCallback(C);

// The pre-link pipelines don't support O0 and require using
// buildO0DefaultPipeline() instead. At the same time, the LTO pipelines do
// support O0 and using them is required.
if (OptLevel == OptimizationLevel::O0 && !IsLTO) {
for (const auto &C : PipelineStartEPCallbacks)
PB.registerPipelineStartEPCallback(C);
for (const auto &C : OptimizerLastEPCallbacks)
PB.registerOptimizerLastEPCallback(C);

// We manually schedule ThinLTOBufferPasses below, so don't pass the value
// to enable it here.
MPM = PB.buildO0DefaultPipeline(OptLevel);
} else {
for (const auto &C : PipelineStartEPCallbacks)
PB.registerPipelineStartEPCallback(C);
for (const auto &C : OptimizerLastEPCallbacks)
PB.registerOptimizerLastEPCallback(C);

switch (OptStage) {
case LLVMRustOptStage::PreLinkNoLTO:
if (ThinLTOBufferRef) {
// This is similar to LLVM's `buildFatLTODefaultPipeline`, where the
// bitcode for embedding is obtained after performing
// `ThinLTOPreLinkDefaultPipeline`.
MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(OptLevel));
if (EmitThinLTO) {
MPM.addPass(ThinLTOBitcodeWriterPass(
ThinLTODataOS, EmitThinLTOSummary ? &ThinLinkDataOS : nullptr));
} else {
MPM.addPass(BitcodeWriterPass(ThinLTODataOS));
}
MPM.addPass(ThinLTOBitcodeWriterPass(
ThinLTODataOS, EmitThinLTOSummary ? &ThinLinkDataOS : nullptr));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreLinkNoLTO is never used when fat LTO is enabled.

*ThinLTOBufferRef = ThinLTOBuffer.release();
MPM.addPass(PB.buildModuleOptimizationPipeline(
OptLevel, ThinOrFullLTOPhase::None));
Expand All @@ -870,6 +861,7 @@ extern "C" LLVMRustResult LLVMRustOptimize(
break;
case LLVMRustOptStage::FatLTO:
MPM = PB.buildLTODefaultPipeline(OptLevel, nullptr);
NeedThinLTOBufferPasses = false;
break;
}
}
Expand All @@ -895,9 +887,11 @@ extern "C" LLVMRustResult LLVMRustOptimize(
MPM.addPass(CanonicalizeAliasesPass());
MPM.addPass(NameAnonGlobalPass());
}
// For `-Copt-level=0`, ThinLTO, or LTO.
// For `-Copt-level=0`, and the pre-link fat/thin LTO stages.
if (ThinLTOBufferRef && *ThinLTOBufferRef == nullptr) {
if (EmitThinLTO) {
// thin lto summaries prevent fat lto, so do not emit them if fat
// lto is requested. See PR #136840 for background information.
if (OptStage != LLVMRustOptStage::PreLinkFatLTO) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to check LLVMRustOptStage::FatLTO as ThinLTOBufferRef is NULL in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, how is this block "For ... LTO" at all?
(per the existing comment, assuming unqualified means full)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LTO runs this function twice. Once on each individual module (PreLinkFatLTO or PreLinkThinLTO). In this case ThinBufferRef is a valid pointer to write the bitcode to after optimizing. And a second time after doing module linking (FatLTO) or cross-module function importing (ThinLTO). In this case ThinBufferRef is NULL as we will emit object file(s) rather thaj bitcode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then perhaps the "For" comment can mention that this is only expected in the prelink phase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

MPM.addPass(ThinLTOBitcodeWriterPass(
ThinLTODataOS, EmitThinLTOSummary ? &ThinLinkDataOS : nullptr));
} else {
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,8 +2335,6 @@ options! {
"embed source text in DWARF debug sections (default: no)"),
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
"emit a section containing stack size metadata (default: no)"),
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
"emit the bc module with thin LTO info (default: yes)"),
emscripten_wasm_eh: bool = (true, parse_bool, [TRACKED],
"Use WebAssembly error handling for wasm32-unknown-emscripten"),
enforce_type_length_limit: bool = (false, parse_bool, [TRACKED],
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/issue-84395-lto-embed-bitcode/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
.arg("-Clinker-plugin-lto")
.arg(format!("-Clinker={}", env_var("CLANG")))
.arg("-Clink-arg=-Wl,--plugin-opt=-lto-embed-bitcode=optimized")
.arg("-Zemit-thin-lto=no")
.arg("-Clto=fat")
.run();

llvm_objcopy().dump_section(".llvmbc", "test.bc").arg("test").run();
Expand Down
Loading