Skip to content

Commit 34abdf2

Browse files
committed
rlib handling
1 parent 66bc5a4 commit 34abdf2

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use rustc_target::callconv::PassMode;
2121
use rustc_target::spec::Os;
2222
use tracing::debug;
2323

24+
use rustc_session::config::CrateType;
25+
2426
use crate::abi::FnAbiLlvmExt;
2527
use crate::builder::Builder;
2628
use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call};
@@ -1136,8 +1138,17 @@ fn codegen_autodiff<'ll, 'tcx>(
11361138
if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) {
11371139
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable);
11381140
}
1139-
if tcx.sess.lto() != rustc_session::config::Lto::Fat {
1140-
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1141+
1142+
let ct = tcx.crate_types();
1143+
let lto = tcx.sess.lto();
1144+
if ct.len() == 1 && ct.contains(&CrateType::Executable) {
1145+
if lto != rustc_session::config::Lto::Fat {
1146+
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1147+
}
1148+
} else {
1149+
if lto != rustc_session::config::Lto::Fat && !tcx.sess.opts.cg.linker_plugin_lto.enabled(){
1150+
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1151+
}
11411152
}
11421153

11431154
let fn_args = instance.args;

compiler/rustc_mir_transform/src/cross_crate_inline.rs

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

37+
// FIXME(autodiff): replace this as per discussion in https://github.com/rust-lang/rust/pull/149033#discussion_r2535465880
38+
if tcx.has_attr(def_id, sym::autodiff_forward) || tcx.has_attr(def_id, sym::autodiff_reverse) || tcx.has_attr(def_id, sym::rustc_autodiff) {
39+
return true;
40+
}
41+
3742
if tcx.has_attr(def_id, sym::rustc_intrinsic) {
3843
// Intrinsic fallback bodies are always cross-crate inlineable.
3944
// To ensure that the MIR inliner doesn't cluelessly try to inline fallback

compiler/rustc_monomorphize/src/collector/autodiff.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::collector::{MonoItems, create_fn_mono_item};
77
// mono so this does not interfere in `autodiff` intrinsics
88
// codegen process. If they are unused, LLVM will remove them when
99
// compiling with O3.
10+
// FIXME(autodiff): Remove this whole file, as per discussion in
11+
// https://github.com/rust-lang/rust/pull/149033#discussion_r2535465880
1012
pub(crate) fn collect_autodiff_fn<'tcx>(
1113
tcx: TyCtxt<'tcx>,
1214
instance: ty::Instance<'tcx>,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(autodiff)]
2+
#![crate_type = "rlib"]
3+
//@ needs-enzyme
4+
//@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat
5+
//@ build-fail
6+
7+
// We test that we fail to compile if a user applied an autodiff_ macro in src/lib.rs,
8+
// since autodiff doesn't work in libraries yet. In the past we used to just return zeros in the
9+
// autodiffed functions, which is obviously confusing and wrong, so erroring is an improvement.
10+
11+
use std::autodiff::autodiff_reverse;
12+
//~? ERROR: using the autodiff feature with library builds is not yet supported
13+
14+
#[autodiff_reverse(d_square, Duplicated, Active)]
15+
pub fn square(x: &f64) -> f64 {
16+
*x * *x
17+
}

0 commit comments

Comments
 (0)