Skip to content

Commit

Permalink
feat: emit warnings if f16 kernels not built (#2077)
Browse files Browse the repository at this point in the history
Also, fixes an issue that if the f16 kernels did not build due to an
error, it crashed the build (instead of skipping the kernels) due to a
missing return.

---------

Co-authored-by: Will Jones <willjones127@gmail.com>
  • Loading branch information
westonpace and wjones127 authored Mar 15, 2024
1 parent da25b32 commit 28eb22a
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions rust/lance-linalg/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::env;

fn main() {
fn main() -> Result<(), String> {
let rust_toolchain = env::var("RUSTUP_TOOLCHAIN")
.or_else(|e| match e {
env::VarError::NotPresent => Ok("stable".into()),
Expand All @@ -28,10 +28,17 @@ fn main() {

println!("cargo:rerun-if-changed=src/simd/f16.c");

if cfg!(any(not(feature = "fp16kernels"), target_os = "windows")) {
// We only compile the f16 kernels if the feature is enabled
// MSVC does not support the f16 type, so we also skip it on Windows.
return;
if cfg!(not(feature = "fp16kernels")) {
println!(
"cargo:warning=fp16kernels feature is not enabled, skipping build of fp16 kernels"
);
return Ok(());
}

if cfg!(target_os = "windows") {
return Err(
"cargo:warning=fp16 kernels are not supported on Windows. Please remove fp16kernels feature".to_string()
);
}

if cfg!(all(target_arch = "aarch64", target_os = "macos")) {
Expand All @@ -40,15 +47,16 @@ fn main() {
} else if cfg!(all(target_arch = "aarch64", target_os = "linux")) {
// Build a version with NEON
build_f16_with_flags("neon", &["-march=armv8.2-a+fp16"]).unwrap();
}

if cfg!(target_arch = "x86_64") {
} else if cfg!(target_arch = "x86_64") {
// Build a version with AVX512
if let Err(err) = build_f16_with_flags("avx512", &["-march=sapphirerapids", "-mavx512fp16"])
{
// It's likely the compiler doesn't support the sapphirerapids architecture
// Clang 12 and GCC 11 are the first versions with sapphire rapids support
eprintln!("Skipping Sapphire Rapids build due to error: {}", err);
println!(
"cargo:warning=Skipping build of AVX-512 fp16 kernels. Clang/GCC too old or compiler does not support sapphirerapids architecture. Error: {}",
err
);
} else {
// We create a special cfg so that we can detect we have in fact
// generated the AVX512 version of the f16 kernels.
Expand All @@ -58,9 +66,14 @@ fn main() {
// While GCC doesn't have support for _Float16 until GCC 12, clang
// has support for __fp16 going back to at least clang 6.
// We use haswell since it's the oldest CPUs on AWS.
build_f16_with_flags("avx2", &["-march=haswell"]).unwrap();
if let Err(err) = build_f16_with_flags("avx2", &["-march=haswell"]) {
return Err(format!("Unable to build AVX2 f16 kernels. Please use Clang >= 6 or GCC >= 12 or remove the fp16kernels feature. Received error: {}", err));
};
// There is no SSE instruction set for f16 -> f32 float conversion
} else {
return Err("Unable to build f16 kernels on given target_arch. Please use x86_64 or aarch64 or remove the fp16kernels feature".to_string());
}
Ok(())
}

fn build_f16_with_flags(suffix: &str, flags: &[&str]) -> Result<(), cc::Error> {
Expand Down

0 comments on commit 28eb22a

Please sign in to comment.