From dfac223eadef4f41422f0c3dc3fdbe626d0413bf Mon Sep 17 00:00:00 2001 From: wilbertbw Date: Thu, 27 Nov 2025 00:44:14 -0500 Subject: [PATCH 1/4] simplified autodiff handling, replaced Instance with function pointer --- .../rustc_codegen_llvm/src/builder/autodiff.rs | 16 +++++++--------- compiler/rustc_codegen_llvm/src/intrinsic.rs | 6 +++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 4b433e2b63616..ec3aff39e248e 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -4,7 +4,7 @@ use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode}; use rustc_ast::expand::typetree::FncTree; use rustc_codegen_ssa::common::TypeKind; use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods}; -use rustc_middle::ty::{Instance, PseudoCanonicalInput, TyCtxt, TypingEnv}; +use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt, TypingEnv}; use rustc_middle::{bug, ty}; use rustc_target::callconv::PassMode; use tracing::debug; @@ -16,25 +16,23 @@ use crate::llvm::{self, TRUE, Type, Value}; pub(crate) fn adjust_activity_to_abi<'tcx>( tcx: TyCtxt<'tcx>, - instance: Instance<'tcx>, + fn_ptr_ty: Ty<'tcx>, typing_env: TypingEnv<'tcx>, da: &mut Vec, ) { - let fn_ty = instance.ty(tcx, typing_env); - - if !matches!(fn_ty.kind(), ty::FnDef(..)) { - bug!("expected fn def for autodiff, got {:?}", fn_ty); + if !matches!(fn_ptr_ty.kind(), ty::FnPtr(_)) { + bug!("expected fn ptr for autodiff, got {:?}", fn_ptr_ty); } // We don't actually pass the types back into the type system. // All we do is decide how to handle the arguments. - let sig = fn_ty.fn_sig(tcx).skip_binder(); + let sig = fn_ptr_ty.fn_sig(tcx).skip_binder(); // FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions let Ok(fn_abi) = - tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty()))) + tcx.fn_abi_of_fn_ptr(fn_ptr_ty, ty::List::empty()); else { - bug!("failed to get fn_abi of instance with empty varargs"); + bug!("failed to get fn_abi of fn_ptr with empty varargs"); }; let mut new_activities = vec![]; diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 33541f7b695f8..900f6dc5a25cb 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1229,9 +1229,13 @@ fn codegen_autodiff<'ll, 'tcx>( bug!("could not find autodiff attrs") }; + let fn_ty = fn_source.ty(tcx, TypingEnv::fully_monomorphized()); + let fn_sig = fn_ty.fn_sig(tcx); + let fn_ptr_ty = tcx.mk_fn_ptr(fn_sig); + adjust_activity_to_abi( tcx, - fn_source, + fn_ptr_ty, TypingEnv::fully_monomorphized(), &mut diff_attrs.input_activity, ); From 4d2dc5ac8114fb70f56477912ae733f3a866d9a9 Mon Sep 17 00:00:00 2001 From: wilbertbw Date: Thu, 27 Nov 2025 01:15:12 -0500 Subject: [PATCH 2/4] fixed syntax with fn_ab_of_fn_ptr call --- compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index ec3aff39e248e..5a8c034586161 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -29,9 +29,7 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( let sig = fn_ptr_ty.fn_sig(tcx).skip_binder(); // FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions - let Ok(fn_abi) = - tcx.fn_abi_of_fn_ptr(fn_ptr_ty, ty::List::empty()); - else { + let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(fn_ptr_ty, ty::List::empty()) else { bug!("failed to get fn_abi of fn_ptr with empty varargs"); }; From f5632d7d1aef4b073132f890f24ef92046b942cd Mon Sep 17 00:00:00 2001 From: wilbertbw Date: Thu, 27 Nov 2025 01:36:41 -0500 Subject: [PATCH 3/4] fixed errors with arguments in autodiff.rs and function pointer creation in /compiler/rustc_codegen_llvm/src/intrinsic.rs --- compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 5 +++-- compiler/rustc_codegen_llvm/src/intrinsic.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 5a8c034586161..c50936a31d908 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -20,7 +20,7 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( typing_env: TypingEnv<'tcx>, da: &mut Vec, ) { - if !matches!(fn_ptr_ty.kind(), ty::FnPtr(_)) { + if !matches!(fn_ptr_ty.kind(), ty::FnPtr(..)) { bug!("expected fn ptr for autodiff, got {:?}", fn_ptr_ty); } @@ -29,7 +29,8 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( let sig = fn_ptr_ty.fn_sig(tcx).skip_binder(); // FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions - let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(fn_ptr_ty, ty::List::empty()) else { + let pci = PseudoCanonicalInput { typing_env, value: (sig, ty::List::empty()) }; + let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(pci) else { bug!("failed to get fn_abi of fn_ptr with empty varargs"); }; diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 900f6dc5a25cb..cfc9284595368 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1231,7 +1231,7 @@ fn codegen_autodiff<'ll, 'tcx>( let fn_ty = fn_source.ty(tcx, TypingEnv::fully_monomorphized()); let fn_sig = fn_ty.fn_sig(tcx); - let fn_ptr_ty = tcx.mk_fn_ptr(fn_sig); + let fn_ptr_ty = Ty::new_fn_ptr(tcx, fn_sig); adjust_activity_to_abi( tcx, From 9beb95db2251c7d786201d4bbac5784ddb6c6b6a Mon Sep 17 00:00:00 2001 From: wilbertbw Date: Thu, 27 Nov 2025 02:09:38 -0500 Subject: [PATCH 4/4] updated pci argument for fn_abi_of_fn_ptr call, sig does not skip_binder --- compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index c50936a31d908..00d0fbb626829 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -26,10 +26,11 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( // We don't actually pass the types back into the type system. // All we do is decide how to handle the arguments. - let sig = fn_ptr_ty.fn_sig(tcx).skip_binder(); + let poly_sig = fn_ptr_ty.fn_sig(tcx); + let sig = poly_sig.skip_binder(); // FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions - let pci = PseudoCanonicalInput { typing_env, value: (sig, ty::List::empty()) }; + let pci = PseudoCanonicalInput { typing_env, value: (poly_sig, ty::List::empty()) }; let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(pci) else { bug!("failed to get fn_abi of fn_ptr with empty varargs"); };