Skip to content

Commit

Permalink
Add linkage attributes to extern "C" blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Oct 4, 2024
1 parent 27cbd6b commit 9a7c2ad
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
8 changes: 6 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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");
Expand All @@ -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
Expand All @@ -254,7 +257,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"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.
Expand Down Expand Up @@ -296,7 +301,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/ffi_avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub unsafe fn hash_many<const N: usize>(
}

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,
Expand Down
8 changes: 8 additions & 0 deletions src/ffi_avx512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ pub unsafe fn hash_many<const N: usize>(
}

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,
Expand Down
1 change: 1 addition & 0 deletions src/ffi_neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/ffi_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub unsafe fn hash_many<const N: usize>(
}

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,
Expand Down
4 changes: 4 additions & 0 deletions src/ffi_sse41.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub unsafe fn hash_many<const N: usize>(
}

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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -320,7 +320,7 @@ impl Platform {
}
}

#[cfg(blake3_neon)]
#[cfg(blake3_neon_ffi)]
pub fn neon() -> Option<Self> {
// Assumed to be safe if the "neon" feature is on.
Some(Self::NEON)
Expand Down

0 comments on commit 9a7c2ad

Please sign in to comment.