Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cross language LTO #3162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ParkMyCar
Copy link
Contributor

If you're using Clang for your C toolchain, it's possible to do cross language optimizations between Rust and C, see Rust's Linker-plugin-based LTO docs for more info.

This PR adds a new @rules_rust//rust/settings:experimental_cross_language_lto (off by default) which when building a Rust binary will set -Clinker-plugin-lto. If LTO is enabled for both Rust and C will then delay optimizations to the linker, resulting in cross language optimizations.

This feature is highly experimental. To have it work correctly you need to be using a Clang toolchain with lld as the linker that is recent enough to understand the bitcode emitted by rustc. There is much more we can do here to make the feature more stable, but this seemed like a good starting point?

@ParkMyCar
Copy link
Contributor Author

Will take a look at the CI failures. FWIW I tested this locally with clang-18 and Rust 1.83 and it indeed works. When LTO is enabled for both Rust and C I see in-lining occurring across the language boundary

args = []

# proc-macros do not benefit from LTO, and cannot be dynamically linked with LTO.
if mode in ["thin", "fat", "off"] and not is_exec_configuration(ctx) and crate_info.type != "proc-macro":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you revert this intentionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was experimenting, but I switched this back to how it was before!

@ParkMyCar ParkMyCar force-pushed the lto/x-lang branch 3 times, most recently from cfdef01 to 307b3c0 Compare January 14, 2025 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants