Skip to content

Commit e04567c

Browse files
committed
Check ZST via PassMode
1 parent 0bf85d3 commit e04567c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::ptr;
33
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
44
use rustc_codegen_ssa::common::TypeKind;
55
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods};
6-
use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt, TypingEnv};
6+
use rustc_middle::ty::{Instance, PseudoCanonicalInput, TyCtxt, TypingEnv};
77
use rustc_middle::{bug, ty};
8+
use rustc_target::callconv::PassMode;
89
use tracing::debug;
910

1011
use crate::builder::{Builder, PlaceRef, UNNAMED};
@@ -16,9 +17,12 @@ use crate::value::Value;
1617

1718
pub(crate) fn adjust_activity_to_abi<'tcx>(
1819
tcx: TyCtxt<'tcx>,
19-
fn_ty: Ty<'tcx>,
20+
instance: Instance<'tcx>,
21+
typing_env: TypingEnv<'tcx>,
2022
da: &mut Vec<DiffActivity>,
2123
) {
24+
let fn_ty = instance.ty(tcx, typing_env);
25+
2226
if !matches!(fn_ty.kind(), ty::FnDef(..)) {
2327
bug!("expected fn def for autodiff, got {:?}", fn_ty);
2428
}
@@ -27,6 +31,13 @@ pub(crate) fn adjust_activity_to_abi<'tcx>(
2731
// All we do is decide how to handle the arguments.
2832
let sig = fn_ty.fn_sig(tcx).skip_binder();
2933

34+
// FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions
35+
let Ok(fn_abi) =
36+
tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
37+
else {
38+
bug!("failed to get fn_abi of instance with empty varargs");
39+
};
40+
3041
let mut new_activities = vec![];
3142
let mut new_positions = vec![];
3243
let mut del_activities = 0;
@@ -91,10 +102,13 @@ pub(crate) fn adjust_activity_to_abi<'tcx>(
91102
}
92103
};
93104

105+
let pass_mode = &fn_abi.args[i].mode;
106+
94107
// For ZST, just ignore and don't add its activity, as this arg won't be present
95108
// in the LLVM passed to Enzyme.
109+
// Some targets pass ZST indirectly in the C ABI, in that case, handle it as a normal arg
96110
// FIXME(Sa4dUs): Enforce ZST corresponding diff activity be `Const`
97-
if layout.is_zst() {
111+
if *pass_mode == PassMode::Ignore {
98112
del_activities += 1;
99113
da.remove(i);
100114
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,8 @@ fn codegen_autodiff<'ll, 'tcx>(
11981198

11991199
adjust_activity_to_abi(
12001200
tcx,
1201-
fn_source.ty(tcx, TypingEnv::fully_monomorphized()),
1201+
fn_source,
1202+
TypingEnv::fully_monomorphized(),
12021203
&mut diff_attrs.input_activity,
12031204
);
12041205

0 commit comments

Comments
 (0)