Skip to content

Commit e16de5d

Browse files
committed
add new flag to print the module post-AD, before opts
1 parent 6e3ca78 commit e16de5d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ fn enable_autodiff_settings(ad: &[config::AutoDiff], module: &mut ModuleCodegen<
610610
}
611611
// We handle this below
612612
config::AutoDiff::PrintModAfter => {}
613+
// We handle this below
614+
config::AutoDiff::PrintModFinal => {}
613615
// This is required and already checked
614616
config::AutoDiff::Enable => {}
615617
}
@@ -657,14 +659,20 @@ pub(crate) fn run_pass_manager(
657659
}
658660

659661
if cfg!(llvm_enzyme) && enable_ad {
662+
// This is the post-autodiff IR, mainly used for testing and educational purposes.
663+
if config.autodiff.contains(&config::AutoDiff::PrintModAfter) {
664+
unsafe { llvm::LLVMDumpModule(module.module_llvm.llmod()) };
665+
}
666+
660667
let opt_stage = llvm::OptStage::FatLTO;
661668
let stage = write::AutodiffStage::PostAD;
662669
unsafe {
663670
write::llvm_optimize(cgcx, dcx, module, None, config, opt_level, opt_stage, stage)?;
664671
}
665672

666-
// This is the final IR, so people should be able to inspect the optimized autodiff output.
667-
if config.autodiff.contains(&config::AutoDiff::PrintModAfter) {
673+
// This is the final IR, so people should be able to inspect the optimized autodiff output,
674+
// for manual inspection.
675+
if config.autodiff.contains(&config::AutoDiff::PrintModFinal) {
668676
unsafe { llvm::LLVMDumpModule(module.module_llvm.llmod()) };
669677
}
670678
}

compiler/rustc_session/src/config.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,12 @@ pub enum AutoDiff {
235235
PrintPerf,
236236
/// Print intermediate IR generation steps
237237
PrintSteps,
238-
/// Print the whole module, before running opts.
238+
/// Print the module, before running autodiff.
239239
PrintModBefore,
240-
/// Print the module after Enzyme differentiated everything.
240+
/// Print the module after running autodiff.
241241
PrintModAfter,
242+
/// Print the module after running autodiff and optimizations.
243+
PrintModFinal,
242244

243245
/// Enzyme's loose type debug helper (can cause incorrect gradients!!)
244246
/// Usable in cases where Enzyme errors with `can not deduce type of X`.

compiler/rustc_session/src/options.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ mod desc {
707707
pub(crate) const parse_list: &str = "a space-separated list of strings";
708708
pub(crate) const parse_list_with_polarity: &str =
709709
"a comma-separated list of strings, with elements beginning with + or -";
710-
pub(crate) const parse_autodiff: &str = "a comma separated list of settings: `Enable`, `PrintSteps`, `PrintTA`, `PrintAA`, `PrintPerf`, `PrintModBefore`, `PrintModAfter`, `LooseTypes`, `Inline`";
710+
pub(crate) const parse_autodiff: &str = "a comma separated list of settings: `Enable`, `PrintSteps`, `PrintTA`, `PrintAA`, `PrintPerf`, `PrintModBefore`, `PrintModAfter`, `PrintModFinal`, `LooseTypes`, `Inline`";
711711
pub(crate) const parse_comma_list: &str = "a comma-separated list of strings";
712712
pub(crate) const parse_opt_comma_list: &str = parse_comma_list;
713713
pub(crate) const parse_number: &str = "a number";
@@ -1355,6 +1355,7 @@ pub mod parse {
13551355
"PrintSteps" => AutoDiff::PrintSteps,
13561356
"PrintModBefore" => AutoDiff::PrintModBefore,
13571357
"PrintModAfter" => AutoDiff::PrintModAfter,
1358+
"PrintModFinal" => AutoDiff::PrintModFinal,
13581359
"LooseTypes" => AutoDiff::LooseTypes,
13591360
"Inline" => AutoDiff::Inline,
13601361
_ => {
@@ -2088,6 +2089,7 @@ options! {
20882089
`=PrintSteps`
20892090
`=PrintModBefore`
20902091
`=PrintModAfter`
2092+
`=PrintModFinal`
20912093
`=LooseTypes`
20922094
`=Inline`
20932095
Multiple options can be combined with commas."),

0 commit comments

Comments
 (0)