Skip to content

Commit 21026ca

Browse files
committed
Merge run_fat_lto, optimize_fat and autodiff into run_and_optimize_fat_lto
1 parent 8d63c7a commit 21026ca

File tree

6 files changed

+26
-69
lines changed

6 files changed

+26
-69
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,16 @@ impl WriteBackendMethods for GccCodegenBackend {
357357
type ThinData = ThinData;
358358
type ThinBuffer = ThinBuffer;
359359

360-
fn run_fat_lto(
360+
fn run_and_optimize_fat_lto(
361361
cgcx: &CodegenContext<Self>,
362362
modules: Vec<FatLtoInput<Self>>,
363363
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
364+
diff_fncs: Vec<AutoDiffItem>,
364365
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
366+
if !diff_fncs.is_empty() {
367+
unimplemented!();
368+
}
369+
365370
back::lto::run_fat(cgcx, modules, cached_modules)
366371
}
367372

@@ -391,14 +396,6 @@ impl WriteBackendMethods for GccCodegenBackend {
391396
Ok(())
392397
}
393398

394-
fn optimize_fat(
395-
_cgcx: &CodegenContext<Self>,
396-
_module: &mut ModuleCodegen<Self::Module>,
397-
) -> Result<(), FatalError> {
398-
// TODO(antoyo)
399-
Ok(())
400-
}
401-
402399
fn optimize_thin(
403400
cgcx: &CodegenContext<Self>,
404401
thin: ThinModule<Self>,
@@ -432,14 +429,6 @@ impl WriteBackendMethods for GccCodegenBackend {
432429
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
433430
back::write::link(cgcx, dcx, modules)
434431
}
435-
436-
fn autodiff(
437-
_cgcx: &CodegenContext<Self>,
438-
_module: &ModuleCodegen<Self::Module>,
439-
_diff_functions: Vec<AutoDiffItem>,
440-
) -> Result<(), FatalError> {
441-
unimplemented!()
442-
}
443432
}
444433

445434
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
2-
codegen_llvm_autodiff_without_lto = using the autodiff feature requires using fat-lto
32
43
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
54

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3737
}
3838
}
3939

40-
#[derive(Diagnostic)]
41-
#[diag(codegen_llvm_autodiff_without_lto)]
42-
pub(crate) struct AutoDiffWithoutLTO;
43-
4440
#[derive(Diagnostic)]
4541
#[diag(codegen_llvm_autodiff_without_enable)]
4642
pub(crate) struct AutoDiffWithoutEnable;

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::mem::ManuallyDrop;
2626
use back::owned_target_machine::OwnedTargetMachine;
2727
use back::write::{create_informational_target_machine, create_target_machine};
2828
use context::SimpleCx;
29-
use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
29+
use errors::ParseTargetMachineConfig;
3030
use llvm_util::target_config;
3131
use rustc_ast::expand::allocator::AllocatorKind;
3232
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
@@ -43,7 +43,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4343
use rustc_middle::ty::TyCtxt;
4444
use rustc_middle::util::Providers;
4545
use rustc_session::Session;
46-
use rustc_session::config::{Lto, OptLevel, OutputFilenames, PrintKind, PrintRequest};
46+
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
4747
use rustc_span::Symbol;
4848

4949
mod back {
@@ -174,12 +174,23 @@ impl WriteBackendMethods for LlvmCodegenBackend {
174174
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
175175
back::write::link(cgcx, dcx, modules)
176176
}
177-
fn run_fat_lto(
177+
fn run_and_optimize_fat_lto(
178178
cgcx: &CodegenContext<Self>,
179179
modules: Vec<FatLtoInput<Self>>,
180180
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
181+
diff_fncs: Vec<AutoDiffItem>,
181182
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
182-
back::lto::run_fat(cgcx, modules, cached_modules)
183+
let mut module = back::lto::run_fat(cgcx, modules, cached_modules)?;
184+
185+
if !diff_fncs.is_empty() {
186+
builder::autodiff::differentiate(&module, cgcx, diff_fncs)?;
187+
}
188+
189+
let dcx = cgcx.create_dcx();
190+
let dcx = dcx.handle();
191+
back::lto::run_pass_manager(cgcx, dcx, &mut module, false)?;
192+
193+
Ok(module)
183194
}
184195
fn run_thin_lto(
185196
cgcx: &CodegenContext<Self>,
@@ -196,14 +207,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
196207
) -> Result<(), FatalError> {
197208
back::write::optimize(cgcx, dcx, module, config)
198209
}
199-
fn optimize_fat(
200-
cgcx: &CodegenContext<Self>,
201-
module: &mut ModuleCodegen<Self::Module>,
202-
) -> Result<(), FatalError> {
203-
let dcx = cgcx.create_dcx();
204-
let dcx = dcx.handle();
205-
back::lto::run_pass_manager(cgcx, dcx, module, false)
206-
}
207210
fn optimize_thin(
208211
cgcx: &CodegenContext<Self>,
209212
thin: ThinModule<Self>,
@@ -226,18 +229,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
226229
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
227230
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
228231
}
229-
/// Generate autodiff rules
230-
fn autodiff(
231-
cgcx: &CodegenContext<Self>,
232-
module: &ModuleCodegen<Self::Module>,
233-
diff_fncs: Vec<AutoDiffItem>,
234-
) -> Result<(), FatalError> {
235-
if cgcx.lto != Lto::Fat {
236-
let dcx = cgcx.create_dcx();
237-
return Err(dcx.handle().emit_almost_fatal(AutoDiffWithoutLTO));
238-
}
239-
builder::autodiff::differentiate(module, cgcx, diff_fncs)
240-
}
241232
}
242233

243234
impl LlvmCodegenBackend {

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -995,17 +995,7 @@ fn execute_fat_lto_work_item<B: ExtraBackendMethods>(
995995
autodiff: Vec<AutoDiffItem>,
996996
module_config: &ModuleConfig,
997997
) -> Result<WorkItemResult<B>, FatalError> {
998-
let mut module =
999-
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
1000-
1001-
if !autodiff.is_empty() {
1002-
if let Err(err) = B::autodiff(cgcx, &module, autodiff) {
1003-
err.raise();
1004-
}
1005-
}
1006-
1007-
B::optimize_fat(cgcx, &mut module)?;
1008-
998+
let module = B::run_and_optimize_fat_lto(cgcx, needs_fat_lto, import_only_modules, autodiff)?;
1009999
let module = B::codegen(cgcx, module, module_config)?;
10101000
Ok(WorkItemResult::Finished(module))
10111001
}

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ pub trait WriteBackendMethods: Clone + 'static {
2020
dcx: DiagCtxtHandle<'_>,
2121
modules: Vec<ModuleCodegen<Self::Module>>,
2222
) -> Result<ModuleCodegen<Self::Module>, FatalError>;
23-
/// Performs fat LTO by merging all modules into a single one and returning it
24-
/// for further optimization.
25-
fn run_fat_lto(
23+
/// Performs fat LTO by merging all modules into a single one, running autodiff
24+
/// if necessary and running any further optimizations
25+
fn run_and_optimize_fat_lto(
2626
cgcx: &CodegenContext<Self>,
2727
modules: Vec<FatLtoInput<Self>>,
2828
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
29+
diff_fncs: Vec<AutoDiffItem>,
2930
) -> Result<ModuleCodegen<Self::Module>, FatalError>;
3031
/// Performs thin LTO by performing necessary global analysis and returning two
3132
/// lists, one of the modules that need optimization and another for modules that
@@ -43,10 +44,6 @@ pub trait WriteBackendMethods: Clone + 'static {
4344
module: &mut ModuleCodegen<Self::Module>,
4445
config: &ModuleConfig,
4546
) -> Result<(), FatalError>;
46-
fn optimize_fat(
47-
cgcx: &CodegenContext<Self>,
48-
llmod: &mut ModuleCodegen<Self::Module>,
49-
) -> Result<(), FatalError>;
5047
fn optimize_thin(
5148
cgcx: &CodegenContext<Self>,
5249
thin: ThinModule<Self>,
@@ -61,11 +58,6 @@ pub trait WriteBackendMethods: Clone + 'static {
6158
want_summary: bool,
6259
) -> (String, Self::ThinBuffer);
6360
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
64-
fn autodiff(
65-
cgcx: &CodegenContext<Self>,
66-
module: &ModuleCodegen<Self::Module>,
67-
diff_fncs: Vec<AutoDiffItem>,
68-
) -> Result<(), FatalError>;
6961
}
7062

7163
pub trait ThinBufferMethods: Send + Sync {

0 commit comments

Comments
 (0)