Skip to content

Commit 7e1462d

Browse files
committed
Emit a proper error if we fail to find libEnzyme
1 parent 6e7dd2c commit 7e1462d

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend. Did you install it via rustup?
2+
13
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
24
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
3-
45
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
56
67

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3232
}
3333
}
3434

35+
#[cfg(feature = "llvm_enzyme")]
36+
#[derive(Diagnostic)]
37+
#[diag(codegen_llvm_autodiff_component_unavailable)]
38+
pub(crate) struct AutoDiffComponentUnavailable;
39+
3540
#[derive(Diagnostic)]
3641
#[diag(codegen_llvm_autodiff_without_lto)]
3742
pub(crate) struct AutoDiffWithoutLto;

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(impl_trait_in_assoc_type)]
1414
#![feature(iter_intersperse)]
1515
#![feature(macro_derive)]
16+
#![feature(once_cell_try)]
1617
#![feature(trim_prefix_suffix)]
1718
#![feature(try_blocks)]
1819
// tidy-alphabetical-end
@@ -247,7 +248,19 @@ impl CodegenBackend for LlvmCodegenBackend {
247248

248249
use crate::back::lto::enable_autodiff_settings;
249250
if sess.opts.unstable_opts.autodiff.contains(&AutoDiff::Enable) {
250-
drop(llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot));
251+
let w = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot);
252+
if let Err(_) = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
253+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
254+
}
255+
if let Ok(wrapper) = w {
256+
// As per discussion in the following issue, we have to drop this wrapper to
257+
// prevent a deadlock. Before introducing this drop, Enzyme would also report an
258+
// error saying "This analysis pass was not registered prior to being queried".
259+
// https://github.com/EnzymeAD/Enzyme/issues/2588#issuecomment-3649218580
260+
drop(wrapper);
261+
} else {
262+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
263+
}
251264
enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
252265
}
253266
}

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ pub(crate) mod Enzyme_AD {
199199
/// Safe to call multiple times - subsequent calls are no-ops due to OnceLock.
200200
pub(crate) fn get_or_init(
201201
sysroot: &rustc_session::config::Sysroot,
202-
) -> MutexGuard<'static, Self> {
203-
ENZYME_INSTANCE
204-
.get_or_init(|| {
205-
Self::call_dynamic(sysroot)
206-
.unwrap_or_else(|e| bug!("failed to load Enzyme: {e}"))
207-
.into()
208-
})
209-
.lock()
210-
.unwrap()
202+
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
203+
let mtx: &'static Mutex<EnzymeWrapper> = ENZYME_INSTANCE.get_or_try_init(|| {
204+
let w = Self::call_dynamic(sysroot)?;
205+
Ok::<_, Box<dyn std::error::Error>>(Mutex::new(w))
206+
})?;
207+
208+
Ok(mtx.lock().unwrap())
211209
}
212210

213211
/// Get the EnzymeWrapper instance. Panics if not initialized.
@@ -475,7 +473,7 @@ pub(crate) mod Fallback_AD {
475473
impl EnzymeWrapper {
476474
pub(crate) fn get_or_init(
477475
_sysroot: &rustc_session::config::Sysroot,
478-
) -> MutexGuard<'static, Self> {
476+
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
479477
unimplemented!("Enzyme not available: build with llvm_enzyme feature")
480478
}
481479

0 commit comments

Comments
 (0)