Skip to content

Commit 41e7476

Browse files
committed
fix: compiler built-in function calling convention workaround
Signed-off-by: usamoi <[email protected]>
1 parent 0646a92 commit 41e7476

File tree

8 files changed

+37
-21
lines changed

8 files changed

+37
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/c/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.s
2-
*.o
2+
*.o
3+
*.out

crates/c/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ name = "c"
33
version.workspace = true
44
edition.workspace = true
55

6+
[dependencies]
7+
half = { version = "~2.3", features = ["use-intrinsics"] }
8+
69
[build-dependencies]
710
cc = "1.0"

crates/c/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
fn main() {
2-
println!("rerun-if-changed:src/c.h");
3-
println!("rerun-if-changed:src/c.c");
2+
println!("cargo:rerun-if-changed=src/c.h");
3+
println!("cargo:rerun-if-changed=src/c.c");
44
cc::Build::new()
55
.compiler("/usr/bin/clang-16")
66
.file("./src/c.c")
77
.opt_level(3)
8+
.debug(true)
89
.compile("pgvectorsc");
910
}

crates/c/src/c.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#if defined(__x86_64__)
99

1010
__attribute__((target("avx512fp16,bmi2"))) extern float
11-
v_f16_cosine_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
12-
size_t n) {
11+
v_f16_cosine_axv512(_Float16 *a, _Float16 *b, size_t n) {
1312
__m512h xy = _mm512_set1_ph(0);
1413
__m512h xx = _mm512_set1_ph(0);
1514
__m512h yy = _mm512_set1_ph(0);
@@ -35,8 +34,7 @@ v_f16_cosine_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
3534
}
3635

3736
__attribute__((target("avx512fp16,bmi2"))) extern float
38-
v_f16_dot_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
39-
size_t n) {
37+
v_f16_dot_axv512(_Float16 *a, _Float16 *b, size_t n) {
4038
__m512h xy = _mm512_set1_ph(0);
4139

4240
while (n >= 32) {
@@ -55,8 +53,7 @@ v_f16_dot_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
5553
}
5654

5755
__attribute__((target("avx512fp16,bmi2"))) extern float
58-
v_f16_sl2_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
59-
size_t n) {
56+
v_f16_sl2_axv512(_Float16 *a, _Float16 *b, size_t n) {
6057
__m512h dd = _mm512_set1_ph(0);
6158

6259
while (n >= 32) {
@@ -78,8 +75,7 @@ v_f16_sl2_axv512(_Float16 const *restrict a, _Float16 const *restrict b,
7875
}
7976

8077
__attribute__((target("avx2"))) extern float
81-
v_f16_cosine_axv2(_Float16 const *restrict a, _Float16 const *restrict b,
82-
size_t n) {
78+
v_f16_cosine_axv2(_Float16 *a, _Float16 *b, size_t n) {
8379
float xy = 0;
8480
float xx = 0;
8581
float yy = 0;
@@ -95,8 +91,7 @@ v_f16_cosine_axv2(_Float16 const *restrict a, _Float16 const *restrict b,
9591
}
9692

9793
__attribute__((target("avx2"))) extern float
98-
v_f16_dot_axv2(_Float16 const *restrict a, _Float16 const *restrict b,
99-
size_t n) {
94+
v_f16_dot_axv2(_Float16 *a, _Float16 *b, size_t n) {
10095
float xy = 0;
10196
#pragma clang loop vectorize_width(8)
10297
for (size_t i = 0; i < n; i++) {
@@ -108,8 +103,7 @@ v_f16_dot_axv2(_Float16 const *restrict a, _Float16 const *restrict b,
108103
}
109104

110105
__attribute__((target("avx2"))) extern float
111-
v_f16_sl2_axv2(_Float16 const *restrict a, _Float16 const *restrict b,
112-
size_t n) {
106+
v_f16_sl2_axv2(_Float16 *a, _Float16 *b, size_t n) {
113107
float dd = 0;
114108
#pragma clang loop vectorize_width(8)
115109
for (size_t i = 0; i < n; i++) {

crates/c/src/c.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#if defined(__x86_64__)
55

6-
extern float v_f16_cosine_axv512(_Float16 const *, _Float16 const *, size_t n);
7-
extern float v_f16_dot_axv512(_Float16 const *, _Float16 const *, size_t n);
8-
extern float v_f16_sl2_axv512(_Float16 const *, _Float16 const *, size_t n);
9-
extern float v_f16_cosine_axv2(_Float16 const *, _Float16 const *, size_t n);
10-
extern float v_f16_dot_axv2(_Float16 const *, _Float16 const *, size_t n);
11-
extern float v_f16_sl2_axv2(_Float16 const *, _Float16 const *, size_t n);
6+
extern float v_f16_cosine_axv512(_Float16 *, _Float16 *, size_t n);
7+
extern float v_f16_dot_axv512(_Float16 *, _Float16 *, size_t n);
8+
extern float v_f16_sl2_axv512(_Float16 *, _Float16 *, size_t n);
9+
extern float v_f16_cosine_axv2(_Float16 *, _Float16 *, size_t n);
10+
extern float v_f16_dot_axv2(_Float16 *, _Float16 *, size_t n);
11+
extern float v_f16_sl2_axv2(_Float16 *, _Float16 *, size_t n);
1212

1313
#endif

crates/c/src/c.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(target_arch = "x86_64")]
12
#[link(name = "pgvectorsc", kind = "static")]
23
extern "C" {
34
pub fn v_f16_cosine_axv512(a: *const u16, b: *const u16, n: usize) -> f32;
@@ -7,3 +8,16 @@ extern "C" {
78
pub fn v_f16_dot_axv2(a: *const u16, b: *const u16, n: usize) -> f32;
89
pub fn v_f16_sl2_axv2(a: *const u16, b: *const u16, n: usize) -> f32;
910
}
11+
12+
// `compiler_builtin` defines `__extendhfsf2` with integer calling convention.
13+
// However C compilers links `__extendhfsf2` with floating calling convention.
14+
// The code should be removed once Rust offically supports `f16`.
15+
16+
#[no_mangle]
17+
#[linkage = "external"]
18+
extern "C" fn __extendhfsf2(f: f64) -> f32 {
19+
unsafe {
20+
let f: half::f16 = std::mem::transmute_copy(&f);
21+
f.to_f32()
22+
}
23+
}

crates/c/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(linkage)]
2+
13
mod c;
24

35
pub use self::c::*;

0 commit comments

Comments
 (0)