Skip to content

Rust does not perform cross language LTO for C functions forwarding to C++ #86745

@Time0o

Description

@Time0o

I believe that cross language LTO does not work correctly when calling C++ code from Rust via intermediate C functions.

Consider this example, here a C++ function foo::do_something (defined in cc/foo_cc.h) is wrapped in a C function foo_do_something (declared in c/foo_c.h) which is then called from Rust via its FFI (in rust/foo_main.rs).

For reference, when linking the static library libfoo_c.a that exposes foo_do_something to a binary created from another C file (c/foo_main.c), a call to foo_do_something is correctly reduced to a call to foo::do_something in the resulting machine code. However, this is not the case for the binary created by rustc, here the main function first jumps to foo_do_something which then does nothing but to jump to foo::do_something.

I've tested this with rustc 1.53.0 and clang 12.0.0

Activity

michaelwoerister

michaelwoerister commented on Jul 5, 2021

@michaelwoerister
Member

Thanks for the bug report, @Time0o! It would be helpful if you could provide the LLVM IR files for all files involved. The Rust compiler can generate those via --emit llvm-ir.

added
A-LTOArea: Link-time optimization (LTO)
S-needs-reproStatus: This issue has no reproduction and needs a reproduction to make progress.
and removed on Feb 28, 2024
HKalbasi

HKalbasi commented on May 23, 2025

@HKalbasi
Member

Might be related to llvm/llvm-project#141038

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LTOArea: Link-time optimization (LTO)C-bugCategory: This is a bug.S-needs-reproStatus: This issue has no reproduction and needs a reproduction to make progress.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @michaelwoerister@ChrisDenton@Time0o@jieyouxu@HKalbasi

        Issue actions

          Rust does not perform cross language LTO for C functions forwarding to C++ · Issue #86745 · rust-lang/rust