Skip to content

linking with C library changes the behavior of _Float16 #118813

Closed
@usamoi

Description

@usamoi

I tried this code:

[build-dependencies]
cc = "1.0.83"
// /build.rs
fn main() {
    cc::Build::new()
        .compiler("/usr/bin/clang-16")
        .file("./src/abc.c")
        .opt_level(0)
        .compile("abc");
}
// /src/abc.c
float test() {
  _Float16 y = 114;
  float z = y;
  return z;
}
// /src/main.rs
extern "C" {
    fn test() -> f32;
}

fn main() {
    println!("{}", unsafe { test() });
}

I expected to see this happen: It prints 114.

Instead, this happened: It prints an unknown value, 0.00017929077, -0.045898438 or other.

After debugging, I found both compiler_builtins and libgcc provide the symbol __extendhfsf2. __extendhfsf2 is a compiler-rt intrinsics for casting a _Float16 to float. In compiler_builtins, the only argument of __extendhfsf2 is passed in a general-proposed register, However, in libgcc, the only argument of __extendhfsf2 is passed in xmm.

related: llvm/llvm-project#56854

Meta

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-floating-pointArea: Floating point numbers and arithmeticA-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions