Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify LLVM version finding logic #136962

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,9 +1092,10 @@ pub fn rustc_cargo(

// We want to link against registerEnzyme and in the future we want to use additional
// functionality from Enzyme core. For that we need to link against Enzyme.
// FIXME(ZuseZ4): Get the LLVM version number automatically instead of hardcoding it.
if builder.config.llvm_enzyme {
cargo.rustflag("-l").rustflag("Enzyme-19");
let llvm_config = builder.llvm_config(builder.config.build).unwrap();
let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
}

// Building with protected visibility reduces the number of dynamic relocations needed, giving
Expand Down
17 changes: 12 additions & 5 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,7 @@ impl Step for Llvm {

// Helper to find the name of LLVM's shared library on darwin and linux.
let find_llvm_lib_name = |extension| {
let version =
command(&res.llvm_config).arg("--version").run_capture_stdout(builder).stdout();
let major = version.split('.').next().unwrap();

let major = get_llvm_version_major(builder, &res.llvm_config);
match &llvm_version_suffix {
Some(version_suffix) => format!("libLLVM-{major}{version_suffix}.{extension}"),
None => format!("libLLVM-{major}.{extension}"),
Expand Down Expand Up @@ -624,12 +621,22 @@ impl Step for Llvm {
}
}

pub fn get_llvm_version(builder: &Builder<'_>, llvm_config: &Path) -> String {
command(llvm_config).arg("--version").run_capture_stdout(builder).stdout().trim().to_owned()
}

pub fn get_llvm_version_major(builder: &Builder<'_>, llvm_config: &Path) -> u8 {
let version = get_llvm_version(builder, llvm_config);
let major_str = version.split_once('.').expect("Failed to parse LLVM version").0;
major_str.parse().unwrap()
}

fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
if builder.config.dry_run() {
return;
}

let version = command(llvm_config).arg("--version").run_capture_stdout(builder).stdout();
let version = get_llvm_version(builder, llvm_config);
let mut parts = version.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
if let (Some(major), Some(_minor)) = (parts.next(), parts.next()) {
if major >= 18 {
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap_complete::shells;

use crate::core::build_steps::compile::run_cargo;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::build_steps::llvm::get_llvm_version;
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
use crate::core::build_steps::tool::{self, SourceType, Tool};
use crate::core::build_steps::toolstate::ToolState;
Expand Down Expand Up @@ -1945,8 +1946,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
let llvm::LlvmResult { llvm_config, .. } =
builder.ensure(llvm::Llvm { target: builder.config.build });
if !builder.config.dry_run() {
let llvm_version =
command(&llvm_config).arg("--version").run_capture_stdout(builder).stdout();
let llvm_version = get_llvm_version(builder, &llvm_config);
let llvm_components =
command(&llvm_config).arg("--components").run_capture_stdout(builder).stdout();
// Remove trailing newline from llvm-config output.
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ impl<'a> Builder<'a> {
///
/// Note that this returns `None` if LLVM is disabled, or if we're in a
/// check build or dry-run, where there's no need to build all of LLVM.
fn llvm_config(&self, target: TargetSelection) -> Option<PathBuf> {
pub fn llvm_config(&self, target: TargetSelection) -> Option<PathBuf> {
if self.config.llvm_enabled(target) && self.kind != Kind::Check && !self.config.dry_run() {
let llvm::LlvmResult { llvm_config, .. } = self.ensure(llvm::Llvm { target });
if llvm_config.is_file() {
Expand Down
Loading