Skip to content

Commit dd99221

Browse files
committed
refactor(autodiff): Simplify rlib dep handling; use fn_ptr_ty in adjust_activity_to_abi, drop mono-collection & cross-crate-inline workarounds
1 parent d933cf4 commit dd99221

5 files changed

Lines changed: 11 additions & 71 deletions

File tree

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_codegen_ssa::common::TypeKind;
66
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods};
77
use rustc_data_structures::thin_vec::ThinVec;
88
use rustc_hir::attrs::RustcAutodiff;
9-
use rustc_middle::ty::{Instance, PseudoCanonicalInput, TyCtxt, TypingEnv};
9+
use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt, TypingEnv};
1010
use rustc_middle::{bug, ty};
1111
use rustc_target::callconv::PassMode;
1212
use tracing::debug;
@@ -18,25 +18,23 @@ use crate::llvm::{self, TRUE, Type, Value};
1818

1919
pub(crate) fn adjust_activity_to_abi<'tcx>(
2020
tcx: TyCtxt<'tcx>,
21-
instance: Instance<'tcx>,
21+
fn_ptr_ty: Ty<'tcx>,
2222
typing_env: TypingEnv<'tcx>,
2323
da: &mut ThinVec<DiffActivity>,
2424
) {
25-
let fn_ty = instance.ty(tcx, typing_env);
26-
27-
if !matches!(fn_ty.kind(), ty::FnDef(..)) {
28-
bug!("expected fn def for autodiff, got {:?}", fn_ty);
25+
if !matches!(fn_ptr_ty.kind(), ty::FnPtr(..)) {
26+
bug!("expected fn ptr for autodiff, got {:?}", fn_ptr_ty);
2927
}
3028

3129
// We don't actually pass the types back into the type system.
3230
// All we do is decide how to handle the arguments.
33-
let sig = fn_ty.fn_sig(tcx).skip_binder();
31+
let fn_sig = fn_ptr_ty.fn_sig(tcx);
32+
let sig = fn_sig.skip_binder();
3433

3534
// FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions
36-
let Ok(fn_abi) =
37-
tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
35+
let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((fn_sig, ty::List::empty())))
3836
else {
39-
bug!("failed to get fn_abi of instance with empty varargs");
37+
bug!("failed to get fn_abi of fn_ptr with empty varargs");
4038
};
4139

4240
let mut new_activities = vec![];

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,11 @@ fn codegen_autodiff<'ll, 'tcx>(
13741374
bug!("could not find autodiff attrs")
13751375
};
13761376

1377+
let fn_ptr_ty =
1378+
Ty::new_fn_ptr(tcx, fn_source.ty(tcx, TypingEnv::fully_monomorphized()).fn_sig(tcx));
13771379
adjust_activity_to_abi(
13781380
tcx,
1379-
fn_source,
1381+
fn_ptr_ty,
13801382
TypingEnv::fully_monomorphized(),
13811383
&mut diff_attrs.input_activity,
13821384
);

compiler/rustc_mir_transform/src/cross_crate_inline.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3535
return true;
3636
}
3737

38-
// FIXME(autodiff): replace this as per discussion in https://github.com/rust-lang/rust/pull/149033#discussion_r2535465880
39-
if find_attr!(tcx, def_id, RustcAutodiff(..)) {
40-
return true;
41-
}
42-
4338
if find_attr!(tcx, def_id, RustcIntrinsic) {
4439
// Intrinsic fallback bodies are always cross-crate inlineable.
4540
// To ensure that the MIR inliner doesn't cluelessly try to inline fallback

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@
205205
//! this is not implemented however: a mono item will be produced
206206
//! regardless of whether it is actually needed or not.
207207
208-
mod autodiff;
209-
210208
use std::cell::OnceCell;
211209
use std::ops::ControlFlow;
212210

@@ -240,7 +238,6 @@ use rustc_span::source_map::{Spanned, dummy_spanned, respan};
240238
use rustc_span::{DUMMY_SP, Span};
241239
use tracing::{debug, instrument, trace};
242240

243-
use crate::collector::autodiff::collect_autodiff_fn;
244241
use crate::errors::{
245242
self, EncounteredErrorWhileInstantiating, EncounteredErrorWhileInstantiatingGlobalAsm,
246243
NoOptimizedMir, RecursionLimit,
@@ -990,8 +987,6 @@ fn visit_instance_use<'tcx>(
990987
return;
991988
}
992989
if let Some(intrinsic) = tcx.intrinsic(instance.def_id()) {
993-
collect_autodiff_fn(tcx, instance, intrinsic, output);
994-
995990
if let Some(_requirement) = ValidityRequirement::from_intrinsic(intrinsic.name) {
996991
// The intrinsics assert_inhabited, assert_zero_valid, and assert_mem_uninitialized_valid will
997992
// be lowered in codegen to nothing or a call to panic_nounwind. So if we encounter any

compiler/rustc_monomorphize/src/collector/autodiff.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)