@@ -586,6 +586,29 @@ fn thin_lto(
586586 }
587587}
588588
589+ fn enable_autodiff_settings ( ad : & [ config:: AutoDiff ] , module : & mut ModuleCodegen < ModuleLlvm > ) {
590+
591+ for & val in ad {
592+ match val {
593+ config:: AutoDiff :: PrintModBefore => { unsafe { llvm:: LLVMDumpModule ( module. module_llvm . llmod ( ) ) } ; } ,
594+ config:: AutoDiff :: PrintPerf => { llvm:: set_print_perf ( true ) ; } ,
595+ config:: AutoDiff :: PrintAA => { llvm:: set_print_activity ( true ) ; } ,
596+ config:: AutoDiff :: PrintTA => { llvm:: set_print_type ( true ) ; } ,
597+ config:: AutoDiff :: Inline => { llvm:: set_inline ( true ) ; } ,
598+ config:: AutoDiff :: LooseTypes => { llvm:: set_loose_types ( false ) ; } ,
599+ config:: AutoDiff :: PrintSteps => { llvm:: set_print ( true ) ; } ,
600+ // We handle this below
601+ config:: AutoDiff :: PrintModAfter => { }
602+ // This is required and already checked
603+ config:: AutoDiff :: Enable => { }
604+ }
605+ }
606+ // This helps with handling enums for now.
607+ llvm:: set_strict_aliasing ( false ) ;
608+ // FIXME(ZuseZ4): Test this, since it was added a long time ago.
609+ llvm:: set_rust_rules ( true ) ;
610+ }
611+
589612pub ( crate ) fn run_pass_manager (
590613 cgcx : & CodegenContext < LlvmCodegenBackend > ,
591614 dcx : DiagCtxtHandle < ' _ > ,
@@ -604,10 +627,6 @@ pub(crate) fn run_pass_manager(
604627 let opt_stage = if thin { llvm:: OptStage :: ThinLTO } else { llvm:: OptStage :: FatLTO } ;
605628 let opt_level = config. opt_level . unwrap_or ( config:: OptLevel :: No ) ;
606629
607- // If this rustc version was build with enzyme/autodiff enabled, and if users applied the
608- // `#[autodiff]` macro at least once, then we will later call llvm_optimize a second time.
609- debug ! ( "running llvm pm opt pipeline" ) ;
610-
611630 // The PostAD behavior is the same that we would have if no autodiff was used.
612631 // It will run the default optimization pipeline. If AD is enabled we select
613632 // the DuringAD stage, which will disable vectorization and loop unrolling, and
@@ -618,25 +637,27 @@ pub(crate) fn run_pass_manager(
618637 let stage =
619638 if enable_ad { write:: AutodiffStage :: DuringAD } else { write:: AutodiffStage :: PostAD } ;
620639
621- // If Enzyme fails to differentiate the code, then this module will have everything that's
622- // needed to reproduce the bug.
623- if config. autodiff . contains ( & config:: AutoDiff :: PrintModBefore ) {
624- unsafe { llvm:: LLVMDumpModule ( module. module_llvm . llmod ( ) ) } ;
640+ if enable_ad {
641+ enable_autodiff_settings ( & config. autodiff , module) ;
625642 }
643+
626644 unsafe {
627645 write:: llvm_optimize ( cgcx, dcx, module, config, opt_level, opt_stage, stage) ?;
628646 }
647+
629648 if cfg ! ( llvm_enzyme) && enable_ad {
630649 let opt_stage = llvm:: OptStage :: FatLTO ;
631650 let stage = write:: AutodiffStage :: PostAD ;
632651 unsafe {
633652 write:: llvm_optimize ( cgcx, dcx, module, config, opt_level, opt_stage, stage) ?;
634653 }
654+
655+ // This is the final IR, so people should be able to inspect the optimized autodiff output.
656+ if config. autodiff . contains ( & config:: AutoDiff :: PrintModAfter ) {
657+ unsafe { llvm:: LLVMDumpModule ( module. module_llvm . llmod ( ) ) } ;
658+ }
635659 }
636- // This is the final IR, so people should be able to inspect the optimized autodiff output.
637- if config. autodiff . contains ( & config:: AutoDiff :: PrintModAfter ) {
638- unsafe { llvm:: LLVMDumpModule ( module. module_llvm . llmod ( ) ) } ;
639- }
660+
640661 debug ! ( "lto done" ) ;
641662 Ok ( ( ) )
642663}
0 commit comments