Skip to content

Commit ac8749d

Browse files
committed
Support ZST args
1 parent 910d813 commit ac8749d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) fn adjust_activity_to_abi<'tcx>(
2929

3030
let mut new_activities = vec![];
3131
let mut new_positions = vec![];
32+
let mut del_activities = 0;
3233
for (i, ty) in sig.inputs().iter().enumerate() {
3334
if let Some(inner_ty) = ty.builtin_deref(true) {
3435
if inner_ty.is_slice() {
@@ -90,12 +91,20 @@ pub(crate) fn adjust_activity_to_abi<'tcx>(
9091
}
9192
};
9293

94+
// For ZST, just ignore and don't add its activity, as this arg won't be present
95+
// in the LLVM passed to Enzyme.
96+
// FIXME(Sa4dUs): Enforce ZST corresponding diff activity be `Const`
97+
if layout.is_zst() {
98+
del_activities += 1;
99+
da.remove(i);
100+
}
101+
93102
// If the argument is lowered as a `ScalarPair`, we need to duplicate its activity.
94103
// Otherwise, the number of activities won't match the number of LLVM arguments and
95104
// this will lead to errors when verifying the Enzyme call.
96105
if let rustc_abi::BackendRepr::ScalarPair(_, _) = layout.backend_repr() {
97106
new_activities.push(da[i].clone());
98-
new_positions.push(i + 1);
107+
new_positions.push(i + 1 - del_activities);
99108
}
100109
}
101110
// now add the extra activities coming from slices

tests/ui/autodiff/zst.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat
2+
//@ no-prefer-dynamic
3+
//@ needs-enzyme
4+
//@ build-pass
5+
6+
// Check that differentiating functions with ZST args does not break
7+
8+
#![feature(autodiff)]
9+
10+
#[core::autodiff::autodiff_forward(fd_inner, Const, Dual)]
11+
fn f(_zst: (), _x: &mut f64) {}
12+
13+
#[unsafe(no_mangle)]
14+
pub extern "C" fn fd(x: &mut f64, xd: &mut f64) {
15+
fd_inner((), x, xd);
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)