Skip to content

Commit f9f5959

Browse files
committed
more iffy improvements
1 parent 0597d21 commit f9f5959

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,33 @@ impl WriteBackendMethods for LlvmCodegenBackend {
237237
diff_fncs: Vec<AutoDiffItem>,
238238
config: &ModuleConfig,
239239
) -> Result<(), FatalError> {
240-
if cgcx.lto != Lto::Fat {
241-
let dcx = cgcx.create_dcx();
242-
return Err(dcx.handle().emit_almost_fatal(AutoDiffWithoutLTO));
243-
}
240+
//if cgcx.lto != Lto::Fat {
241+
// let dcx = cgcx.create_dcx();
242+
// return Err(dcx.handle().emit_almost_fatal(AutoDiffWithoutLTO));
243+
//}
244244
let module_llvm = &module.module_llvm;
245245
builder::autodiff::differentiate(module_llvm, cgcx, diff_fncs, config)
246246
}
247+
fn autodiff_thin(
248+
cgcx: &CodegenContext<Self>,
249+
thin_module: &ThinModule<Self>,
250+
diff_fncs: Vec<AutoDiffItem>,
251+
config: &ModuleConfig,
252+
) -> Result<(), FatalError> {
253+
let dcx = cgcx.create_dcx();
254+
let dcx = dcx.handle();
255+
256+
let module_name = &thin_module.shared.module_names[thin_module.idx];
257+
258+
// Right now the implementation we've got only works over serialized
259+
// modules, so we create a fresh new LLVM context and parse the module
260+
// into that context. One day, however, we may do this for upstream
261+
// crates but for locally codegened modules we may be able to reuse
262+
// that LLVM Context and Module.
263+
let module_llvm = ModuleLlvm::parse(cgcx, module_name, thin_module.data(), dcx)?;
264+
265+
builder::autodiff::differentiate(&module_llvm, cgcx, diff_fncs, config)
266+
}
247267
}
248268

249269
impl LlvmCodegenBackend {

compiler/rustc_codegen_ssa/src/back/lto.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
9494
match &self {
9595
LtoModuleCodegen::Fat(module) => {
9696
B::autodiff(cgcx, &module, diff_fncs, config)?;
97+
},
98+
LtoModuleCodegen::Thin(module) => {
99+
B::autodiff_thin(cgcx, module, diff_fncs, config)?;
97100
}
98-
_ => panic!("autodiff called with non-fat LTO module"),
99101
}
100-
101102
Ok(self)
102103
}
103104
}

compiler/rustc_codegen_ssa/src/back/write.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,18 @@ fn generate_lto_work<B: ExtraBackendMethods>(
412412
vec![(WorkItem::LTO(module), 0)]
413413
} else {
414414
if !autodiff.is_empty() {
415-
let dcx = cgcx.create_dcx();
416-
dcx.handle().emit_fatal(AutodiffWithoutLto {});
415+
//let dcx = cgcx.create_dcx();
416+
//dcx.handle().emit_fatal(AutodiffWithoutLto {});
417417
}
418+
let config = cgcx.config(ModuleKind::Regular);
418419
assert!(needs_fat_lto.is_empty());
419420
let (lto_modules, copy_jobs) = B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules)
420421
.unwrap_or_else(|e| e.raise());
421422
lto_modules
422423
.into_iter()
423424
.map(|module| {
425+
let mut module =
426+
unsafe { module.autodiff(cgcx, autodiff.clone(), config).unwrap_or_else(|e| e.raise()) };
424427
let cost = module.cost();
425428
(WorkItem::LTO(module), cost)
426429
})

compiler/rustc_codegen_ssa/src/traits/write.rs

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
6868
diff_fncs: Vec<AutoDiffItem>,
6969
config: &ModuleConfig,
7070
) -> Result<(), FatalError>;
71+
fn autodiff_thin(
72+
cgcx: &CodegenContext<Self>,
73+
thin: &ThinModule<Self>,
74+
diff_fncs: Vec<AutoDiffItem>,
75+
config: &ModuleConfig,
76+
) -> Result<(), FatalError>;
7177
}
7278

7379
pub trait ThinBufferMethods: Send + Sync {

0 commit comments

Comments
 (0)