@@ -8,6 +8,7 @@ use std::{fs, io, mem, str, thread};
88
99use jobserver:: { Acquired , Client } ;
1010use rustc_ast:: attr;
11+ use rustc_ast:: expand:: autodiff_attrs:: AutoDiffItem ;
1112use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
1213use rustc_data_structures:: memmap:: Mmap ;
1314use rustc_data_structures:: profiling:: { SelfProfilerRef , VerboseTimingGuard } ;
@@ -41,7 +42,7 @@ use tracing::debug;
4142use super :: link:: { self , ensure_removed} ;
4243use super :: lto:: { self , SerializedModule } ;
4344use super :: symbol_export:: symbol_name_for_instance_in_crate;
44- use crate :: errors:: ErrorCreatingRemarkDir ;
45+ use crate :: errors:: { AutodiffWithoutLto , ErrorCreatingRemarkDir } ;
4546use crate :: traits:: * ;
4647use crate :: {
4748 CachedModuleCodegen , CodegenResults , CompiledModule , CrateInfo , ModuleCodegen , ModuleKind ,
@@ -120,6 +121,7 @@ pub struct ModuleConfig {
120121 pub merge_functions : bool ,
121122 pub emit_lifetime_markers : bool ,
122123 pub llvm_plugins : Vec < String > ,
124+ pub autodiff : Vec < config:: AutoDiff > ,
123125}
124126
125127impl ModuleConfig {
@@ -280,6 +282,7 @@ impl ModuleConfig {
280282
281283 emit_lifetime_markers : sess. emit_lifetime_markers ( ) ,
282284 llvm_plugins : if_regular ! ( sess. opts. unstable_opts. llvm_plugins. clone( ) , vec![ ] ) ,
285+ autodiff : if_regular ! ( sess. opts. unstable_opts. autodiff. clone( ) , vec![ ] ) ,
283286 }
284287 }
285288
@@ -401,6 +404,7 @@ impl<B: WriteBackendMethods> CodegenContext<B> {
401404
402405fn generate_lto_work < B : ExtraBackendMethods > (
403406 cgcx : & CodegenContext < B > ,
407+ autodiff : Vec < AutoDiffItem > ,
404408 needs_fat_lto : Vec < FatLtoInput < B > > ,
405409 needs_thin_lto : Vec < ( String , B :: ThinBuffer ) > ,
406410 import_only_modules : Vec < ( SerializedModule < B :: ModuleBuffer > , WorkProduct ) > ,
@@ -411,9 +415,18 @@ fn generate_lto_work<B: ExtraBackendMethods>(
411415 assert ! ( needs_thin_lto. is_empty( ) ) ;
412416 let module =
413417 B :: run_fat_lto ( cgcx, needs_fat_lto, import_only_modules) . unwrap_or_else ( |e| e. raise ( ) ) ;
418+ if cgcx. lto == Lto :: Fat {
419+ let _config = cgcx. config ( ModuleKind :: Regular ) ;
420+ todo ! ( "fat LTO with autodiff is not yet implemented" ) ;
421+ //module = unsafe { module.autodiff(cgcx, autodiff, config).unwrap() };
422+ }
414423 // We are adding a single work item, so the cost doesn't matter.
415424 vec ! [ ( WorkItem :: LTO ( module) , 0 ) ]
416425 } else {
426+ if !autodiff. is_empty ( ) {
427+ let dcx = cgcx. create_dcx ( ) ;
428+ dcx. handle ( ) . emit_fatal ( AutodiffWithoutLto { } ) ;
429+ }
417430 assert ! ( needs_fat_lto. is_empty( ) ) ;
418431 let ( lto_modules, copy_jobs) = B :: run_thin_lto ( cgcx, needs_thin_lto, import_only_modules)
419432 . unwrap_or_else ( |e| e. raise ( ) ) ;
@@ -1041,6 +1054,9 @@ pub(crate) enum Message<B: WriteBackendMethods> {
10411054 /// Sent from a backend worker thread.
10421055 WorkItem { result : Result < WorkItemResult < B > , Option < WorkerFatalError > > , worker_id : usize } ,
10431056
1057+ /// A vector containing all the AutoDiff tasks that we have to pass to Enzyme.
1058+ AddAutoDiffItems ( Vec < AutoDiffItem > ) ,
1059+
10441060 /// The frontend has finished generating something (backend IR or a
10451061 /// post-LTO artifact) for a codegen unit, and it should be passed to the
10461062 /// backend. Sent from the main thread.
@@ -1367,6 +1383,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
13671383
13681384 // This is where we collect codegen units that have gone all the way
13691385 // through codegen and LLVM.
1386+ let mut autodiff_items = Vec :: new ( ) ;
13701387 let mut compiled_modules = vec ! [ ] ;
13711388 let mut compiled_allocator_module = None ;
13721389 let mut needs_link = Vec :: new ( ) ;
@@ -1478,9 +1495,13 @@ fn start_executing_work<B: ExtraBackendMethods>(
14781495 let needs_thin_lto = mem:: take ( & mut needs_thin_lto) ;
14791496 let import_only_modules = mem:: take ( & mut lto_import_only_modules) ;
14801497
1481- for ( work, cost) in
1482- generate_lto_work ( & cgcx, needs_fat_lto, needs_thin_lto, import_only_modules)
1483- {
1498+ for ( work, cost) in generate_lto_work (
1499+ & cgcx,
1500+ autodiff_items. clone ( ) ,
1501+ needs_fat_lto,
1502+ needs_thin_lto,
1503+ import_only_modules,
1504+ ) {
14841505 let insertion_index = work_items
14851506 . binary_search_by_key ( & cost, |& ( _, cost) | cost)
14861507 . unwrap_or_else ( |e| e) ;
@@ -1615,6 +1636,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
16151636 main_thread_state = MainThreadState :: Idle ;
16161637 }
16171638
1639+ Message :: AddAutoDiffItems ( mut items) => {
1640+ autodiff_items. append ( & mut items) ;
1641+ }
1642+
16181643 Message :: CodegenComplete => {
16191644 if codegen_state != Aborted {
16201645 codegen_state = Completed ;
@@ -2092,6 +2117,10 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
20922117 drop ( self . coordinator . sender . send ( Box :: new ( Message :: CodegenComplete :: < B > ) ) ) ;
20932118 }
20942119
2120+ pub ( crate ) fn submit_autodiff_items ( & self , items : Vec < AutoDiffItem > ) {
2121+ drop ( self . coordinator . sender . send ( Box :: new ( Message :: < B > :: AddAutoDiffItems ( items) ) ) ) ;
2122+ }
2123+
20952124 pub ( crate ) fn check_for_errors ( & self , sess : & Session ) {
20962125 self . shared_emitter_main . check ( sess, false ) ;
20972126 }
0 commit comments