From 9a7c2ad5b9756bfa84391868770112394fb82986 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 3 Oct 2024 23:53:37 -0400 Subject: [PATCH] Add linkage attributes to extern "C" blocks --- build.rs | 8 ++++++-- src/ffi_avx2.rs | 4 ++++ src/ffi_avx512.rs | 8 ++++++++ src/ffi_neon.rs | 1 + src/ffi_sse2.rs | 4 ++++ src/ffi_sse41.rs | 4 ++++ src/lib.rs | 2 +- src/platform.rs | 18 +++++++++--------- 8 files changed, 37 insertions(+), 12 deletions(-) diff --git a/build.rs b/build.rs index af5805d9c..2436113c7 100644 --- a/build.rs +++ b/build.rs @@ -189,6 +189,7 @@ fn build_avx512_c_intrinsics() { // This is required on 32-bit x86 targets, since the assembly // implementation doesn't support those. println!("cargo:rustc-cfg=blake3_avx512_ffi"); + println!("cargo:rustc-cfg=blake3_avx512_ffi_intrinsics"); let mut build = new_build(); build.file("c/blake3_avx512.c"); if is_windows_msvc() { @@ -209,6 +210,7 @@ fn build_avx512_assembly() { // only supports x86_64. assert!(is_x86_64()); println!("cargo:rustc-cfg=blake3_avx512_ffi"); + println!("cargo:rustc-cfg=blake3_avx512_ffi_assembly"); let mut build = new_build(); if is_windows_msvc() { build.file("c/blake3_avx512_x86-64_windows_msvc.asm"); @@ -230,6 +232,7 @@ fn build_avx512_assembly() { } fn build_neon_c_intrinsics() { + println!("cargo:rustc-cfg=blake3_neon_ffi"); let mut build = new_build(); // Note that blake3_neon.c normally depends on the blake3_portable.c // for the single-instance compression function, but we expose @@ -254,7 +257,9 @@ fn main() -> Result<(), Box> { "blake3_avx2_ffi", "blake3_avx2_rust", "blake3_avx512_ffi", - "blake3_neon", + "blake3_avx512_ffi_assembly", + "blake3_avx512_ffi_intrinsics", + "blake3_neon_ffi", ]; for cfg_name in all_cfgs { // TODO: Switch this whole file to the new :: syntax when our MSRV reaches 1.77. @@ -296,7 +301,6 @@ fn main() -> Result<(), Box> { if (is_arm() && is_neon()) || (!is_no_neon() && !is_pure() && is_aarch64() && is_little_endian()) { - println!("cargo:rustc-cfg=blake3_neon"); build_neon_c_intrinsics(); } diff --git a/src/ffi_avx2.rs b/src/ffi_avx2.rs index 33961e9d4..0fc466487 100644 --- a/src/ffi_avx2.rs +++ b/src/ffi_avx2.rs @@ -33,6 +33,10 @@ pub unsafe fn hash_many( } pub mod ffi { + #[cfg_attr( + blake3_avx2_ffi, + link(name = "blake3_sse2_sse41_avx2_assembly", kind = "static") + )] extern "C" { pub fn blake3_hash_many_avx2( inputs: *const *const u8, diff --git a/src/ffi_avx512.rs b/src/ffi_avx512.rs index 884f48135..ae76d8821 100644 --- a/src/ffi_avx512.rs +++ b/src/ffi_avx512.rs @@ -61,6 +61,14 @@ pub unsafe fn hash_many( } pub mod ffi { + #[cfg_attr( + blake3_avx512_ffi_assembly, + link(name = "blake3_avx512_assembly", kind = "static") + )] + #[cfg_attr( + blake3_avx512_ffi_intrinsics, + link(name = "blake3_avx512_intrinsics", kind = "static") + )] extern "C" { pub fn blake3_compress_in_place_avx512( cv: *mut u32, diff --git a/src/ffi_neon.rs b/src/ffi_neon.rs index 54d07a4de..b8493dae0 100644 --- a/src/ffi_neon.rs +++ b/src/ffi_neon.rs @@ -53,6 +53,7 @@ pub extern "C" fn blake3_compress_in_place_portable( } pub mod ffi { + #[cfg_attr(blake3_neon_ffi, link(name = "blake3_neon", kind = "static"))] extern "C" { pub fn blake3_hash_many_neon( inputs: *const *const u8, diff --git a/src/ffi_sse2.rs b/src/ffi_sse2.rs index 1c5da81f9..353bac0ae 100644 --- a/src/ffi_sse2.rs +++ b/src/ffi_sse2.rs @@ -61,6 +61,10 @@ pub unsafe fn hash_many( } pub mod ffi { + #[cfg_attr( + blake3_sse2_ffi, + link(name = "blake3_sse2_sse41_avx2_assembly", kind = "static") + )] extern "C" { pub fn blake3_compress_in_place_sse2( cv: *mut u32, diff --git a/src/ffi_sse41.rs b/src/ffi_sse41.rs index 62989c5ec..ff7b62c6e 100644 --- a/src/ffi_sse41.rs +++ b/src/ffi_sse41.rs @@ -61,6 +61,10 @@ pub unsafe fn hash_many( } pub mod ffi { + #[cfg_attr( + blake3_sse41_ffi, + link(name = "blake3_sse2_sse41_avx2_assembly", kind = "static") + )] extern "C" { pub fn blake3_compress_in_place_sse41( cv: *mut u32, diff --git a/src/lib.rs b/src/lib.rs index e6a9d6771..4e3886963 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,7 @@ mod avx2; #[cfg(blake3_avx512_ffi)] #[path = "ffi_avx512.rs"] mod avx512; -#[cfg(blake3_neon)] +#[cfg(blake3_neon_ffi)] #[path = "ffi_neon.rs"] mod neon; mod portable; diff --git a/src/platform.rs b/src/platform.rs index 79bc9a3fb..63e9657d9 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -10,7 +10,7 @@ cfg_if::cfg_if! { pub const MAX_SIMD_DEGREE: usize = 8; } } - } else if #[cfg(blake3_neon)] { + } else if #[cfg(blake3_neon_ffi)] { pub const MAX_SIMD_DEGREE: usize = 4; } else { pub const MAX_SIMD_DEGREE: usize = 1; @@ -30,7 +30,7 @@ cfg_if::cfg_if! { pub const MAX_SIMD_DEGREE_OR_2: usize = 8; } } - } else if #[cfg(blake3_neon)] { + } else if #[cfg(blake3_neon_ffi)] { pub const MAX_SIMD_DEGREE_OR_2: usize = 4; } else { pub const MAX_SIMD_DEGREE_OR_2: usize = 2; @@ -49,7 +49,7 @@ pub enum Platform { #[cfg(blake3_avx512_ffi)] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] AVX512, - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] NEON, } @@ -81,7 +81,7 @@ impl Platform { } // We don't use dynamic feature detection for NEON. If the "neon" // feature is on, NEON is assumed to be supported. - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] { return Platform::NEON; } @@ -100,7 +100,7 @@ impl Platform { #[cfg(blake3_avx512_ffi)] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] Platform::AVX512 => 16, - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] Platform::NEON => 4, }; debug_assert!(degree <= MAX_SIMD_DEGREE); @@ -134,7 +134,7 @@ impl Platform { crate::avx512::compress_in_place(cv, block, block_len, counter, flags) }, // No NEON compress_in_place() implementation yet. - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] Platform::NEON => portable::compress_in_place(cv, block, block_len, counter, flags), } } @@ -166,7 +166,7 @@ impl Platform { crate::avx512::compress_xof(cv, block, block_len, counter, flags) }, // No NEON compress_xof() implementation yet. - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] Platform::NEON => portable::compress_xof(cv, block, block_len, counter, flags), } } @@ -261,7 +261,7 @@ impl Platform { ) }, // Assumed to be safe if the "neon" feature is on. - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] Platform::NEON => unsafe { crate::neon::hash_many( inputs, @@ -320,7 +320,7 @@ impl Platform { } } - #[cfg(blake3_neon)] + #[cfg(blake3_neon_ffi)] pub fn neon() -> Option { // Assumed to be safe if the "neon" feature is on. Some(Self::NEON)