Skip to content

Commit de78744

Browse files
committed
prevent attribute repetition
1 parent a1865e2 commit de78744

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,22 +361,35 @@ mod llvm_enzyme {
361361
span,
362362
};
363363

364+
// We're avoid duplicating the attributes `#[rustc_autodiff]` and `#[inline(never)]`.
365+
fn same_attribute(attr: &ast::AttrKind, item: &ast::AttrKind) -> bool {
366+
match (attr, item) {
367+
(ast::AttrKind::Normal(a), ast::AttrKind::Normal(b)) => {
368+
let a = &a.item.path;
369+
let b = &b.item.path;
370+
a.segments.len() == b.segments.len()
371+
&& a.segments.iter().zip(b.segments.iter()).all(|(a, b)| a.ident == b.ident)
372+
},
373+
_ => false,
374+
}
375+
}
376+
364377
// Don't add it multiple times:
365378
let orig_annotatable: Annotatable = match item {
366379
Annotatable::Item(ref mut iitem) => {
367-
if !iitem.attrs.iter().any(|a| a.id == attr.id) {
380+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
368381
iitem.attrs.push(attr);
369382
}
370-
if !iitem.attrs.iter().any(|a| a.id == inline_never.id) {
383+
if !iitem.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind)) {
371384
iitem.attrs.push(inline_never.clone());
372385
}
373386
Annotatable::Item(iitem.clone())
374387
}
375388
Annotatable::AssocItem(ref mut assoc_item, i @ Impl) => {
376-
if !assoc_item.attrs.iter().any(|a| a.id == attr.id) {
389+
if !assoc_item.attrs.iter().any(|a| same_attribute(&a.kind, &attr.kind)) {
377390
assoc_item.attrs.push(attr);
378391
}
379-
if !assoc_item.attrs.iter().any(|a| a.id == inline_never.id) {
392+
if !assoc_item.attrs.iter().any(|a| same_attribute(&a.kind, &inline_never.kind)) {
380393
assoc_item.attrs.push(inline_never.clone());
381394
}
382395
Annotatable::AssocItem(assoc_item.clone(), i)

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,10 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
798798

799799
// check for exactly one autodiff attribute on placeholder functions.
800800
// There should only be one, since we generate a new placeholder per ad macro.
801-
// FIXME(ZuseZ4): re-enable this check. Currently we add multiple, which doesn't cause harm but
802-
// looks strange e.g. under cargo-expand.
803801
let attr = match &attrs[..] {
804802
[] => return None,
805803
[attr] => attr,
806-
// These two attributes are the same and unfortunately duplicated due to a previous bug.
807-
[attr, _attr2] => attr,
808804
_ => {
809-
//FIXME(ZuseZ4): Once we fixed our parser, we should also prohibit the two-attribute
810-
//branch above.
811805
span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
812806
}
813807
};

tests/pretty/autodiff_forward.pp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@
7676
}
7777
#[rustc_autodiff]
7878
#[inline(never)]
79-
#[rustc_autodiff]
80-
#[inline(never)]
81-
#[rustc_autodiff]
82-
#[inline(never)]
8379
pub fn f5(x: &[f64], y: f64) -> f64 {
8480
::core::panicking::panic("not implemented")
8581
}

0 commit comments

Comments
 (0)