Skip to content

Unreachable overflow checks not optimized out of compiler-builtins i128 conversions #116832

Open
@saethlin

Description

@saethlin

If I drop this code into a src/main.rs:

#[no_mangle]
fn i128tof32(i: u128) -> f32 {
    i as _
}

#[no_mangle]
fn i128tof64(i: u128) -> f64 {
    i as _
}

#[no_mangle]
fn f32toi128(i: f32) -> i128 {
    i as _
}

#[no_mangle]
fn f64toi128(i: f64) -> i128 {
    i as _
}

Then try to compile with

RUSTFLAGS="-Coverflow-checks=yes -Cembed-bitcode=yes -Clto=fat" cargo +nightly r --release -Zbuild-std --target=x86_64-unknown-linux-gnu

I get this pile of linker errors:

  = note: /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o): in function `compiler_builtins::float::conv::__floatuntisf':
          compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv13__floatuntisf17hfc1a50ce7fce74e8E+0xa4): undefined reference to `core::panicking::panic'
          /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o): in function `compiler_builtins::float::conv::__floatuntidf':
          compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv13__floatuntidf17h52065f1075830490E+0xa1): undefined reference to `core::panicking::panic'
          /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o): in function `compiler_builtins::float::conv::__floattisf':
          compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv11__floattisf17hb01f6c3fe609b91dE+0xc6): undefined reference to `core::panicking::panic'
          /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o): in function `compiler_builtins::float::conv::__floattidf':
          compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv11__floattidf17h80c8820ef68b8afeE+0xc5): undefined reference to `core::panicking::panic'
          /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o): in function `compiler_builtins::float::conv::__fixsfti':
          compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv9__fixsfti17hfbc031ecb7549268E+0xb1): undefined reference to `core::panicking::panic'
          /usr/bin/ld: /tmp/scratch/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-dbbd29384eda2292.rlib(compiler_builtins-dbbd29384eda2292.compiler_builtins.76a90f18db901457-cgu.1.rcgu.o):compiler_builtins.76a90f18db901457-cgu.1:(.text._ZN17compiler_builtins5float4conv9__fixdfti17h76053a313caec3efE+0xcc): more undefined references to `core::panicking::panic' follow

I looked at a few of the functions in question into alive2 and it seems sure that eliminating the overflow check is correct: https://alive2.llvm.org/ce/z/e68yAu

I don't really know why this manages to link without LTO. There's one panic path in many of the compiler-builtins functions with or without LTO, the linker error just only surfaces with LTO enabled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions