Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub(crate) use self::Enzyme_AD::*;

pub(crate) mod Enzyme_AD {
use std::ffi::{c_char, c_void};
use std::path::PathBuf;
use std::sync::{Mutex, MutexGuard, OnceLock};

use rustc_session::config::{Sysroot, host_tuple};
Expand Down Expand Up @@ -433,38 +434,36 @@ pub(crate) mod Enzyme_AD {
})
}

fn get_enzyme_path(sysroot: &Sysroot) -> Result<String, EnzymeLibraryError> {
fn get_enzyme_path(sysroot: &Sysroot) -> Result<PathBuf, EnzymeLibraryError> {
let llvm_version_major = llvm::LLVMRustVersionMajor();
let mut searched = Vec::new();

let path_buf = sysroot
.all_paths()
.map(|sysroot_path| {
filesearch::make_target_lib_path(sysroot_path, host_tuple())
.join("lib")
.with_file_name(format!("libEnzyme-{llvm_version_major}"))
.with_extension(std::env::consts::DLL_EXTENSION)
})
.find(|f| f.exists())
.ok_or_else(|| {
let candidates = sysroot
.all_paths()
.map(|p| p.join("lib").display().to_string())
.collect::<Vec<String>>()
.join("\n* ");
EnzymeLibraryError::NotFound {
err: format!(
"failed to find a `libEnzyme-{llvm_version_major}` folder \
in the sysroot candidates:\n* {candidates}"
),
}
})?;

Ok(path_buf
.to_str()
.ok_or_else(|| EnzymeLibraryError::LoadFailed {
err: format!("invalid UTF-8 in path: {}", path_buf.display()),
})?
.to_string())
for p in sysroot.all_paths() {
let libdir = filesearch::make_target_lib_path(p, host_tuple());

let enzyme_path = libdir
.join(format!("libEnzyme-{llvm_version_major}"))
.with_extension(std::env::consts::DLL_EXTENSION);

if enzyme_path.exists() {
return Ok(enzyme_path);
}

searched.push(libdir);
}

Err(EnzymeLibraryError::NotFound {
err: format!(
"failed to find a `libEnzyme-{llvm_version_major}.{}` \
in the sysroot candidates:\n* {}",
std::env::consts::DLL_EXTENSION,
searched
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n* ")
),
})
}
}
}
Expand Down
Loading