@@ -26,7 +26,7 @@ use std::mem::ManuallyDrop;
26
26
use back:: owned_target_machine:: OwnedTargetMachine ;
27
27
use back:: write:: { create_informational_target_machine, create_target_machine} ;
28
28
use context:: SimpleCx ;
29
- use errors:: { AutoDiffWithoutLTO , ParseTargetMachineConfig } ;
29
+ use errors:: ParseTargetMachineConfig ;
30
30
use llvm_util:: target_config;
31
31
use rustc_ast:: expand:: allocator:: AllocatorKind ;
32
32
use rustc_ast:: expand:: autodiff_attrs:: AutoDiffItem ;
@@ -43,7 +43,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
43
43
use rustc_middle:: ty:: TyCtxt ;
44
44
use rustc_middle:: util:: Providers ;
45
45
use rustc_session:: Session ;
46
- use rustc_session:: config:: { Lto , OptLevel , OutputFilenames , PrintKind , PrintRequest } ;
46
+ use rustc_session:: config:: { OptLevel , OutputFilenames , PrintKind , PrintRequest } ;
47
47
use rustc_span:: Symbol ;
48
48
49
49
mod back {
@@ -174,12 +174,23 @@ impl WriteBackendMethods for LlvmCodegenBackend {
174
174
) -> Result < ModuleCodegen < Self :: Module > , FatalError > {
175
175
back:: write:: link ( cgcx, dcx, modules)
176
176
}
177
- fn run_fat_lto (
177
+ fn run_and_optimize_fat_lto (
178
178
cgcx : & CodegenContext < Self > ,
179
179
modules : Vec < FatLtoInput < Self > > ,
180
180
cached_modules : Vec < ( SerializedModule < Self :: ModuleBuffer > , WorkProduct ) > ,
181
+ diff_fncs : Vec < AutoDiffItem > ,
181
182
) -> Result < ModuleCodegen < Self :: Module > , FatalError > {
182
- back:: lto:: run_fat ( cgcx, modules, cached_modules)
183
+ let mut module = back:: lto:: run_fat ( cgcx, modules, cached_modules) ?;
184
+
185
+ if !diff_fncs. is_empty ( ) {
186
+ builder:: autodiff:: differentiate ( & module, cgcx, diff_fncs) ?;
187
+ }
188
+
189
+ let dcx = cgcx. create_dcx ( ) ;
190
+ let dcx = dcx. handle ( ) ;
191
+ back:: lto:: run_pass_manager ( cgcx, dcx, & mut module, false ) ?;
192
+
193
+ Ok ( module)
183
194
}
184
195
fn run_thin_lto (
185
196
cgcx : & CodegenContext < Self > ,
@@ -196,14 +207,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
196
207
) -> Result < ( ) , FatalError > {
197
208
back:: write:: optimize ( cgcx, dcx, module, config)
198
209
}
199
- fn optimize_fat (
200
- cgcx : & CodegenContext < Self > ,
201
- module : & mut ModuleCodegen < Self :: Module > ,
202
- ) -> Result < ( ) , FatalError > {
203
- let dcx = cgcx. create_dcx ( ) ;
204
- let dcx = dcx. handle ( ) ;
205
- back:: lto:: run_pass_manager ( cgcx, dcx, module, false )
206
- }
207
210
fn optimize_thin (
208
211
cgcx : & CodegenContext < Self > ,
209
212
thin : ThinModule < Self > ,
@@ -226,18 +229,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
226
229
fn serialize_module ( module : ModuleCodegen < Self :: Module > ) -> ( String , Self :: ModuleBuffer ) {
227
230
( module. name , back:: lto:: ModuleBuffer :: new ( module. module_llvm . llmod ( ) ) )
228
231
}
229
- /// Generate autodiff rules
230
- fn autodiff (
231
- cgcx : & CodegenContext < Self > ,
232
- module : & ModuleCodegen < Self :: Module > ,
233
- diff_fncs : Vec < AutoDiffItem > ,
234
- ) -> Result < ( ) , FatalError > {
235
- if cgcx. lto != Lto :: Fat {
236
- let dcx = cgcx. create_dcx ( ) ;
237
- return Err ( dcx. handle ( ) . emit_almost_fatal ( AutoDiffWithoutLTO ) ) ;
238
- }
239
- builder:: autodiff:: differentiate ( module, cgcx, diff_fncs)
240
- }
241
232
}
242
233
243
234
impl LlvmCodegenBackend {
0 commit comments