Skip to content

Commit 793d990

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

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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
35
46
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
57
6-
78
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
89
910
codegen_llvm_from_llvm_diag = {$message}

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: 4 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,9 @@ 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+
if let Err(_) = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
252+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
253+
}
251254
enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
252255
}
253256
}

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)