From 4eb1f87ae637c857b41724b9980d1d2141800d8c Mon Sep 17 00:00:00 2001 From: Usamoi Date: Fri, 2 Feb 2024 18:37:07 +0800 Subject: [PATCH] fix: remove ctor (#339) Signed-off-by: usamoi --- Cargo.lock | 11 ----------- crates/c/tests/x86_64.rs | 3 +++ crates/detect/Cargo.toml | 1 - crates/detect/src/lib.rs | 17 +++++++++++++++++ crates/detect/src/linux.rs | 5 +---- crates/detect/src/x86_64.rs | 14 ++++---------- crates/detect/tests/linux.rs | 1 + crates/detect/tests/x86_64.rs | 1 + src/lib.rs | 1 + 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14041e918..0ef1ea605 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -439,16 +439,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "ctor" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d2b3721e861707777e3195b0158f950ae6dc4a27e4d02ff9f67e3eb3de199e" -dependencies = [ - "quote", - "syn 2.0.48", -] - [[package]] name = "cty" version = "0.2.2" @@ -472,7 +462,6 @@ dependencies = [ name = "detect" version = "0.0.0" dependencies = [ - "ctor", "rustix", "std_detect", ] diff --git a/crates/c/tests/x86_64.rs b/crates/c/tests/x86_64.rs index 0e981a9be..99e2a7fb5 100644 --- a/crates/c/tests/x86_64.rs +++ b/crates/c/tests/x86_64.rs @@ -2,6 +2,7 @@ #[test] fn test_v_f16_cosine() { + detect::initialize(); const EPSILON: f32 = f16::EPSILON.to_f32_const(); use half::f16; unsafe fn v_f16_cosine(a: *const u16, b: *const u16, n: usize) -> f32 { @@ -46,6 +47,7 @@ fn test_v_f16_cosine() { #[test] fn test_v_f16_dot() { + detect::initialize(); const EPSILON: f32 = 1.0f32; use half::f16; unsafe fn v_f16_dot(a: *const u16, b: *const u16, n: usize) -> f32 { @@ -86,6 +88,7 @@ fn test_v_f16_dot() { #[test] fn test_v_f16_sl2() { + detect::initialize(); const EPSILON: f32 = 1.0f32; use half::f16; unsafe fn v_f16_sl2(a: *const u16, b: *const u16, n: usize) -> f32 { diff --git a/crates/detect/Cargo.toml b/crates/detect/Cargo.toml index b3ac78202..aaae19282 100644 --- a/crates/detect/Cargo.toml +++ b/crates/detect/Cargo.toml @@ -5,5 +5,4 @@ edition.workspace = true [dependencies] std_detect = { git = "https://github.com/tensorchord/stdarch.git", branch = "avx512fp16" } -ctor = "0.2.6" rustix.workspace = true diff --git a/crates/detect/src/lib.rs b/crates/detect/src/lib.rs index 4211fc0a6..5ce11e6f7 100644 --- a/crates/detect/src/lib.rs +++ b/crates/detect/src/lib.rs @@ -1,2 +1,19 @@ +#[cfg(target_os = "linux")] pub mod linux; + +#[cfg(target_arch = "x86_64")] pub mod x86_64; + +pub fn initialize() { + #[cfg(target_os = "linux")] + { + self::linux::ctor_memfd(); + } + #[cfg(target_arch = "x86_64")] + { + self::x86_64::ctor_avx512fp16(); + self::x86_64::ctor_v2(); + self::x86_64::ctor_v3(); + self::x86_64::ctor_v4(); + } +} diff --git a/crates/detect/src/linux.rs b/crates/detect/src/linux.rs index b77a9664d..e5280bb18 100644 --- a/crates/detect/src/linux.rs +++ b/crates/detect/src/linux.rs @@ -1,5 +1,3 @@ -#![cfg(target_os = "linux")] - use std::sync::atomic::{AtomicBool, Ordering}; static ATOMIC_MEMFD: AtomicBool = AtomicBool::new(false); @@ -14,8 +12,7 @@ pub fn test_memfd() -> bool { } } -#[ctor::ctor] -fn ctor_memfd() { +pub fn ctor_memfd() { ATOMIC_MEMFD.store(test_memfd(), Ordering::Relaxed); } diff --git a/crates/detect/src/x86_64.rs b/crates/detect/src/x86_64.rs index 1c4f0d760..bfe555b2d 100644 --- a/crates/detect/src/x86_64.rs +++ b/crates/detect/src/x86_64.rs @@ -1,5 +1,3 @@ -#![cfg(target_arch = "x86_64")] - use std::sync::atomic::{AtomicBool, Ordering}; static ATOMIC_AVX512FP16: AtomicBool = AtomicBool::new(false); @@ -8,8 +6,7 @@ pub fn test_avx512fp16() -> bool { std_detect::is_x86_feature_detected!("avx512fp16") && test_v4() } -#[ctor::ctor] -fn ctor_avx512fp16() { +pub fn ctor_avx512fp16() { ATOMIC_AVX512FP16.store(test_avx512fp16(), Ordering::Relaxed); } @@ -28,8 +25,7 @@ pub fn test_v4() -> bool { && test_v3() } -#[ctor::ctor] -fn ctor_v4() { +pub fn ctor_v4() { ATOMIC_V4.store(test_v4(), Ordering::Relaxed); } @@ -52,8 +48,7 @@ pub fn test_v3() -> bool { && test_v2() } -#[ctor::ctor] -fn ctor_v3() { +pub fn ctor_v3() { ATOMIC_V3.store(test_v3(), Ordering::Relaxed); } @@ -75,8 +70,7 @@ pub fn test_v2() -> bool { && std_detect::is_x86_feature_detected!("ssse3") } -#[ctor::ctor] -fn ctor_v2() { +pub fn ctor_v2() { ATOMIC_V2.store(test_v2(), Ordering::Relaxed); } diff --git a/crates/detect/tests/linux.rs b/crates/detect/tests/linux.rs index b4a6552a2..0026ee4fa 100644 --- a/crates/detect/tests/linux.rs +++ b/crates/detect/tests/linux.rs @@ -2,5 +2,6 @@ #[test] fn print() { + detect::initialize(); assert_eq!(detect::linux::test_memfd(), detect::linux::detect_memfd()); } diff --git a/crates/detect/tests/x86_64.rs b/crates/detect/tests/x86_64.rs index 07f612ac1..41b13176c 100644 --- a/crates/detect/tests/x86_64.rs +++ b/crates/detect/tests/x86_64.rs @@ -2,6 +2,7 @@ #[test] fn print() { + detect::initialize(); assert_eq!( detect::x86_64::test_avx512fp16(), detect::x86_64::detect_avx512fp16() diff --git a/src/lib.rs b/src/lib.rs index f5725e6f4..2b23de8bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ unsafe extern "C" fn _PG_init() { SessionError::BadInit.friendly(); } unsafe { + detect::initialize(); self::gucs::init(); self::index::init(); self::ipc::init();