@@ -530,14 +530,24 @@ fn get_instr_profile_output_path(config: &ModuleConfig) -> Option<CString> {
530
530
config. instrument_coverage . then ( || c"default_%m_%p.profraw" . to_owned ( ) )
531
531
}
532
532
533
+ // PreAD will run llvm opts but disable size increasing opts (vectorization, loop unrolling)
534
+ // DuringAD is the same as above, but also runs the enzyme opt and autodiff passes.
535
+ // PostAD will run all opts, including size increasing opts.
536
+ #[ derive( Debug , Eq , PartialEq ) ]
537
+ pub ( crate ) enum AutodiffStage {
538
+ PreAD ,
539
+ DuringAD ,
540
+ PostAD ,
541
+ }
542
+
533
543
pub ( crate ) unsafe fn llvm_optimize (
534
544
cgcx : & CodegenContext < LlvmCodegenBackend > ,
535
545
dcx : DiagCtxtHandle < ' _ > ,
536
546
module : & ModuleCodegen < ModuleLlvm > ,
537
547
config : & ModuleConfig ,
538
548
opt_level : config:: OptLevel ,
539
549
opt_stage : llvm:: OptStage ,
540
- skip_size_increasing_opts : bool ,
550
+ autodiff_stage : AutodiffStage ,
541
551
) -> Result < ( ) , FatalError > {
542
552
// Enzyme:
543
553
// The whole point of compiler based AD is to differentiate optimized IR instead of unoptimized
@@ -550,7 +560,7 @@ pub(crate) unsafe fn llvm_optimize(
550
560
let unroll_loops;
551
561
let vectorize_slp;
552
562
let vectorize_loop;
553
- let run_enzyme;
563
+ let run_enzyme = cfg ! ( llvm_enzyme ) && autodiff_stage == AutodiffStage :: DuringAD ;
554
564
555
565
// When we build rustc with enzyme/autodiff support, we want to postpone size-increasing
556
566
// optimizations until after differentiation. Our pipeline is thus: (opt + enzyme), (full opt).
@@ -559,17 +569,15 @@ pub(crate) unsafe fn llvm_optimize(
559
569
// FIXME(ZuseZ4): Before shipping on nightly,
560
570
// we should make this more granular, or at least check that the user has at least one autodiff
561
571
// call in their code, to justify altering the compilation pipeline.
562
- if skip_size_increasing_opts && cfg ! ( llvm_enzyme) {
572
+ if cfg ! ( llvm_enzyme) && autodiff_stage != AutodiffStage :: PostAD {
563
573
unroll_loops = false ;
564
574
vectorize_slp = false ;
565
575
vectorize_loop = false ;
566
- run_enzyme = true ;
567
576
} else {
568
577
unroll_loops =
569
578
opt_level != config:: OptLevel :: Size && opt_level != config:: OptLevel :: SizeMin ;
570
579
vectorize_slp = config. vectorize_slp ;
571
580
vectorize_loop = config. vectorize_loop ;
572
- run_enzyme = false ;
573
581
}
574
582
trace ! ( ?unroll_loops, ?vectorize_slp, ?vectorize_loop, ?run_enzyme) ;
575
583
let using_thin_buffers = opt_stage == llvm:: OptStage :: PreLinkThinLTO || config. bitcode_needed ( ) ;
@@ -691,18 +699,14 @@ pub(crate) unsafe fn optimize(
691
699
_ => llvm:: OptStage :: PreLinkNoLTO ,
692
700
} ;
693
701
694
- // If we know that we will later run AD, then we disable vectorization and loop unrolling
695
- let skip_size_increasing_opts = cfg ! ( llvm_enzyme) ;
702
+ // If we know that we will later run AD, then we disable vectorization and loop unrolling.
703
+ // Otherwise we pretend AD is already done and run the normal opt pipeline (=PostAD).
704
+ // FIXME(ZuseZ4): Make this more granular, only set PreAD if we actually have autodiff
705
+ // usages, not just if we build rustc with autodiff support.
706
+ let autodiff_stage =
707
+ if cfg ! ( llvm_enzyme) { AutodiffStage :: PreAD } else { AutodiffStage :: PostAD } ;
696
708
return unsafe {
697
- llvm_optimize (
698
- cgcx,
699
- dcx,
700
- module,
701
- config,
702
- opt_level,
703
- opt_stage,
704
- skip_size_increasing_opts,
705
- )
709
+ llvm_optimize ( cgcx, dcx, module, config, opt_level, opt_stage, autodiff_stage)
706
710
} ;
707
711
}
708
712
Ok ( ( ) )
0 commit comments