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

Support -Zno-link #9019

Open
bjorn3 opened this issue Dec 27, 2020 · 3 comments
Open

Support -Zno-link #9019

bjorn3 opened this issue Dec 27, 2020 · 3 comments
Labels
A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Performance Gotta go fast! S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@bjorn3
Copy link
Member

bjorn3 commented Dec 27, 2020

Describe the problem you are trying to solve
Compilation pipelining can currently compile dependent crates as soon as the crate metadata is written in most cases. This improves cpu utilization for from-scratch builds. It is however not possible to apply pipelining on crates that need to be linked like executables and dylibs as all object files need to be available to link something.

Describe the solution you'd like
Using the -Zno-link option of rustc it is possible to separate the compilation and linking step. This makes it possible to pipeline the compilation and only wait for all crates to be fully compiled once we want to link the final executable. This option was introduced in rust-lang/rust#68487 and fixed in rust-lang/rust#80187 (now with a test to prevent regressions).

Notes
cc rust-lang/rust#64191

To implement this rustc will first need to be invoked with -Zno-link as argument. This will produce all object files and a .rlink file. Once all dependencies are compiled you need to invoke rustc a second time with -Zlink-only as argument and the .rlink file instead of the source file.

@bjorn3 bjorn3 added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Dec 27, 2020
@ehuss ehuss added A-linkage Area: linker issues, dylib, cdylib, shared libraries, so Performance Gotta go fast! labels Jan 7, 2021
@weihanglo
Copy link
Member

I am interested in this issue and plan to implement it. Just some questions need to make sure I didn't get it wrong.

Below is my understanding of a possible flow. @bjorn3, could you help me check its correctness?

  1. The first rustc invocation is for all dependencies with -Zno-link.
  2. And the next one is also for all dependencies with -Zlink-only.
  3. Then compile and link the Target (crate root).
  4. If there is any dependency attempting to produce a staticlib/dylib/proc-macro which requires upstream objects, the compilation pipeline must invoke rustc with -Zlink-only for all its upstream dependencies before proceeding.

@alexcrichton, since you are the person who implemented pipeline compilation and opened rust-lang/rust#64191, it would be very helpful if you can provide some advice and tips for prototyping. Thanks 😄

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 11, 2021

The first step is to compile all crates, with the crates that need to be linked getting an extra -Zno-link argument. Non-linked crates must not get -Zno-link as argument. This step can be fully pipelined, so once the metadata file of a dependency is written, all dependent crates can immediately be compiled without having to wait on codegen. This pipelining is unchanged from master except for also pipelining crates that need to be linked. The second step is after all crates are done compiling completely, only running rustc on the crates that need to be linked with -Zlink-only and with the .rmeta file from -Zno-link instead of the source file. If I am right, all crate types except for rlib need to be linked.

@alexcrichton
Copy link
Member

This is likely to be a somewhat nontrivial change in Cargo. My best guess for the way to implement this would be to introduce a Unit for -Zno-link and a second unit for -Zlink-only for compilations that today are a single Unit which invoke the linker (e.g. everything other than an rlib basically). Cargo's unit of execution of a process is a Unit, and today there's a Unit-per-rustc-invocation basically. A Unit also tracks things like execution of a build script.

Pipelining with rlib dependencies is different since we don't invoke rustc twice. Instead we invoke rustc once and only once we've received the message that the rmeta is available do we start executing other units. There's special-casing that handles this (see JobQueue::enqueue and Artifact::Metadata), but if we're executing rustc twice then that probably doesn't need to happen.

I suspect functions like requires_upstream_objects and only_requires_rmeta may not need to be refactored, but you'll want to eye them carefully in case new support here impacts them.

@weihanglo weihanglo added the S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. label Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Performance Gotta go fast! S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

4 participants