Skip to content

Commit 076205a

Browse files
authored
Unrolled build for #150747
Rollup merge of #150747 - fix/liloading-enzyme-err, r=lqd tests/ui/runtime/on-broken-pipe/with-rustc_main.rs: Not needed so remove related: #145899 (comment) print error from EnzymeWrapper::get_or_init(sysroot) as a note r? @ZuseZ4 e.g. 1. when libEnzyme not found ```shell $ rustc +stage1 -Z autodiff=Enable -C lto=fat src/main.rs error: autodiff backend not found in the sysroot: failed to find a `libEnzyme-21` folder in the sysroot candidates: * /Volumes/WD_BLACK_SN850X_HS_1TB/rust-lang/rust/build/aarch64-apple-darwin/stage1/lib | = note: it will be distributed via rustup in the future ``` 2. when could not load libEnzyme successfully ```shell rustc +stage1 -Z autodiff=Enable -C lto=fat src/main.rs error: failed to load our autodiff backend: DlOpen { source: "dlopen(/Volumes/WD_BLACK_SN850X_HS_1TB/rust-lang/rust/build/aarch64-apple-darwin/stage1/lib/rustlib/aarch64-apple-darwin/lib/libEnzyme-21.dylib, 0x0005): tried: \'/Volumes/WD_BLACK_SN850X_HS_1TB/rust-lang/rust/build/aarch64-apple-darwin/stage1/lib/rustlib/aarch64-apple-darwin/lib/libEnzyme-21.dylib\' (slice is not valid mach-o file), \'/System/Volumes/Preboot/Cryptexes/OS/Volumes/WD_BLACK_SN850X_HS_1TB/rust-lang/rust/build/aarch64-apple-darwin/stage1/lib/rustlib/aarch64-apple-darwin/lib/libEnzyme-21.dylib\' (no such file), \'/Volumes/WD_BLACK_SN850X_HS_1TB/rust-lang/rust/build/aarch64-apple-darwin/stage1/lib/rustlib/aarch64-apple-darwin/lib/libEnzyme-21.dylib\' (slice is not valid mach-o file)" } ```
2 parents 598c7bd + 14ac6a1 commit 076205a

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend. Did you install it via rustup?
1+
codegen_llvm_autodiff_component_missing = autodiff backend not found in the sysroot: {$err}
2+
.note = it will be distributed via rustup in the future
3+
4+
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend: {$err}
25
36
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
47
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3434

3535
#[derive(Diagnostic)]
3636
#[diag(codegen_llvm_autodiff_component_unavailable)]
37-
pub(crate) struct AutoDiffComponentUnavailable;
37+
pub(crate) struct AutoDiffComponentUnavailable {
38+
pub err: String,
39+
}
40+
41+
#[derive(Diagnostic)]
42+
#[diag(codegen_llvm_autodiff_component_missing)]
43+
#[note]
44+
pub(crate) struct AutoDiffComponentMissing {
45+
pub err: String,
46+
}
3847

3948
#[derive(Diagnostic)]
4049
#[diag(codegen_llvm_autodiff_without_lto)]

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,14 @@ impl CodegenBackend for LlvmCodegenBackend {
249249

250250
use crate::back::lto::enable_autodiff_settings;
251251
if sess.opts.unstable_opts.autodiff.contains(&AutoDiff::Enable) {
252-
if let Err(_) = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
253-
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
252+
match llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
253+
Ok(_) => {}
254+
Err(llvm::EnzymeLibraryError::NotFound { err }) => {
255+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentMissing { err });
256+
}
257+
Err(llvm::EnzymeLibraryError::LoadFailed { err }) => {
258+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable { err });
259+
}
254260
}
255261
enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
256262
}

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(crate) mod Enzyme_AD {
153153
fn load_ptr_by_symbol_mut_void(
154154
lib: &libloading::Library,
155155
bytes: &[u8],
156-
) -> Result<*mut c_void, Box<dyn std::error::Error>> {
156+
) -> Result<*mut c_void, libloading::Error> {
157157
unsafe {
158158
let s: libloading::Symbol<'_, *mut c_void> = lib.get(bytes)?;
159159
// libloading = 0.9.0: try_as_raw_ptr always succeeds and returns Some
@@ -192,15 +192,27 @@ pub(crate) mod Enzyme_AD {
192192

193193
static ENZYME_INSTANCE: OnceLock<Mutex<EnzymeWrapper>> = OnceLock::new();
194194

195+
#[derive(Debug)]
196+
pub(crate) enum EnzymeLibraryError {
197+
NotFound { err: String },
198+
LoadFailed { err: String },
199+
}
200+
201+
impl From<libloading::Error> for EnzymeLibraryError {
202+
fn from(err: libloading::Error) -> Self {
203+
Self::LoadFailed { err: format!("{err:?}") }
204+
}
205+
}
206+
195207
impl EnzymeWrapper {
196208
/// Initialize EnzymeWrapper with the given sysroot if not already initialized.
197209
/// Safe to call multiple times - subsequent calls are no-ops due to OnceLock.
198210
pub(crate) fn get_or_init(
199211
sysroot: &rustc_session::config::Sysroot,
200-
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
212+
) -> Result<MutexGuard<'static, Self>, EnzymeLibraryError> {
201213
let mtx: &'static Mutex<EnzymeWrapper> = ENZYME_INSTANCE.get_or_try_init(|| {
202214
let w = Self::call_dynamic(sysroot)?;
203-
Ok::<_, Box<dyn std::error::Error>>(Mutex::new(w))
215+
Ok::<_, EnzymeLibraryError>(Mutex::new(w))
204216
})?;
205217

206218
Ok(mtx.lock().unwrap())
@@ -351,7 +363,7 @@ pub(crate) mod Enzyme_AD {
351363
#[allow(non_snake_case)]
352364
fn call_dynamic(
353365
sysroot: &rustc_session::config::Sysroot,
354-
) -> Result<Self, Box<dyn std::error::Error>> {
366+
) -> Result<Self, EnzymeLibraryError> {
355367
let enzyme_path = Self::get_enzyme_path(sysroot)?;
356368
let lib = unsafe { libloading::Library::new(enzyme_path)? };
357369

@@ -416,7 +428,7 @@ pub(crate) mod Enzyme_AD {
416428
})
417429
}
418430

419-
fn get_enzyme_path(sysroot: &Sysroot) -> Result<String, String> {
431+
fn get_enzyme_path(sysroot: &Sysroot) -> Result<String, EnzymeLibraryError> {
420432
let llvm_version_major = unsafe { LLVMRustVersionMajor() };
421433

422434
let path_buf = sysroot
@@ -434,15 +446,19 @@ pub(crate) mod Enzyme_AD {
434446
.map(|p| p.join("lib").display().to_string())
435447
.collect::<Vec<String>>()
436448
.join("\n* ");
437-
format!(
438-
"failed to find a `libEnzyme-{llvm_version_major}` folder \
449+
EnzymeLibraryError::NotFound {
450+
err: format!(
451+
"failed to find a `libEnzyme-{llvm_version_major}` folder \
439452
in the sysroot candidates:\n* {candidates}"
440-
)
453+
),
454+
}
441455
})?;
442456

443457
Ok(path_buf
444458
.to_str()
445-
.ok_or_else(|| format!("invalid UTF-8 in path: {}", path_buf.display()))?
459+
.ok_or_else(|| EnzymeLibraryError::LoadFailed {
460+
err: format!("invalid UTF-8 in path: {}", path_buf.display()),
461+
})?
446462
.to_string())
447463
}
448464
}

0 commit comments

Comments
 (0)