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

Use the object crate for metadata reading #83640

Merged
merged 8 commits into from
May 14, 2021

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Mar 29, 2021

This allows sharing the metadata reader between cg_llvm, cg_clif and other codegen backends.

This is not currently useful for rlib reading with cg_spirv (rust-gpu) as it uses tar rather than ar as .rlib format, but it is useful for dylib reading required for loading proc macros. (cc @eddyb)

The object crate is already trusted as dependency of libstd through backtrace. As far as I know it supports reading all object file formats used by targets for which we support rust dylibs with crate metadata, but I am not certain. If this happens to not be the case, I could keep using LLVM for reading dylib metadata.

Marked as WIP for a perf run and as it is based on #83637.

@rust-highfive
Copy link
Collaborator

Some changes occured to rustc_codegen_cranelift

cc @bjorn3

@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 29, 2021
@bjorn3
Copy link
Member Author

bjorn3 commented Mar 29, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 29, 2021
@bors
Copy link
Contributor

bors commented Mar 29, 2021

⌛ Trying commit aeee8d87bffdd7370b2c2bf149bf1bfaf7bc18e0 with merge 652060f31a2669f8467ef860c7b663be75b0bd44...

@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member Author

bjorn3 commented Mar 29, 2021

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2021
@bjorn3
Copy link
Member Author

bjorn3 commented Mar 29, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Contributor

bors commented Mar 29, 2021

⌛ Trying commit d2b63d8cd784c90b2c96853d5acea0d6c8ab9976 with merge a7a4d9f098502325534a9e8e22b59dcec45582bc...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member Author

bjorn3 commented Mar 29, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Contributor

bors commented Mar 29, 2021

⌛ Trying commit 29f195cc7bb5fbcaa7abfab7a1a9fff63ebcbcb0 with merge dfb3f56d49b7f86c1163b25c8f7d06ca511df6e1...

@bors
Copy link
Contributor

bors commented Mar 29, 2021

☀️ Try build successful - checks-actions
Build commit: dfb3f56d49b7f86c1163b25c8f7d06ca511df6e1 (dfb3f56d49b7f86c1163b25c8f7d06ca511df6e1)

@rust-timer
Copy link
Collaborator

Queued dfb3f56d49b7f86c1163b25c8f7d06ca511df6e1 with parent 40334da, future comparison URL.

The version 1 resolver unifies enabled features across the
whole workspace. This includes libstd which isn't allowed
to depend on wasmparser.
@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member Author

bjorn3 commented May 7, 2021

Spurious error. Re-running PR checks.

let archive = object::read::archive::ArchiveFile::parse(&*data)
.map_err(|e| format!("{:?}", e))?;

for entry_result in archive.members() {
Copy link
Member

Choose a reason for hiding this comment

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

So, I think this (and use of mmap) may be one plausible reason for the slight performance regression. If my memory of the ar format serves me well, obtaining the list of members in an ar has to process the file effectively as if it was a ump list. I suspect that such a read pattern may be a pathological for mmap based I/O: kernel would try loading more data (page?) into memory only for us to inspect the file name and length before we jump to the next entry(-ies), discarding the rest of the data that kernel spent time loading in.

Without digging into LLVM's ArchiveRO implementation I can imagine that more precise reads could be more effective here.

Copy link
Member Author

Choose a reason for hiding this comment

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

ArchiveRO::open also uses mmap for header reading I think:

ErrorOr<std::unique_ptr<MemoryBuffer>> BufOr =
MemoryBufferRef doesn't export any method allowing read calls on the mapped file.

Cargo.lock Outdated Show resolved Hide resolved
@nagisa
Copy link
Member

nagisa commented May 8, 2021

The implementation is very reasonable and a huge code quality improvement over the LLVM based version IMO. I'm especially happy with the unification of metadata reading code between backends. The largest regressions introduce a instruction count hit of 0.7% in benchmarks in-line with those I'd expect in terms of the workflows that would be affected by this change most (check, debug).

While the hit is not trivially ignorable, its also small enough, I think, that the maintainability improvements would justify it. And it sounds like there may be a number of low-hanging fruit in archive parsing implementation too.

@alexcrichton
Copy link
Member

There's a segfault in #84449 and what I think is memory corruption (probably the same thing), and I would personally love to not have to track it down to some weird interaction with the LLVM C API here. I suspect it will "magically go away" if this were all Rust-based!

@nagisa
Copy link
Member

nagisa commented May 11, 2021

Note that I and @philipc found a couple of obvious places to optimize the archive reading code. Some of that has landed and will eventually make its way over to rustc during natural passage of time. Some of that would need changes on the rustc side AFAICT.

AFAICT the code with ReadCache would also be somewhat simpler, too. Care to try that out? r=me regardless.

@bjorn3
Copy link
Member Author

bjorn3 commented May 11, 2021

AFAICT the code with ReadCache would also be somewhat simpler, too. Care to try that out?

I am not sure what you are referring to.

@nagisa
Copy link
Member

nagisa commented May 14, 2021

@bors r+ Thanks.

I am not sure what you are referring to.

I was referring to the experiment made in this branch (linked to by one of the comments I linked above), and in particular this commit. But its fine anyway.

@bors
Copy link
Contributor

bors commented May 14, 2021

📌 Commit 6381aaf has been approved by nagisa

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 14, 2021
@bors
Copy link
Contributor

bors commented May 14, 2021

⌛ Testing commit 6381aaf with merge 75da570...

@bors
Copy link
Contributor

bors commented May 14, 2021

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing 75da570 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 14, 2021
@bors bors merged commit 75da570 into rust-lang:master May 14, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 14, 2021
@bjorn3 bjorn3 deleted the shared_metadata_reader branch May 14, 2021 16:11
bjorn3 pushed a commit to bjorn3/rust that referenced this pull request May 27, 2021
Use the object crate for metadata reading

This allows sharing the metadata reader between cg_llvm, cg_clif and other codegen backends.

This is not currently useful for rlib reading with cg_spirv ([rust-gpu](https://github.com/EmbarkStudios/rust-gpu/)) as it uses tar rather than ar as .rlib format, but it is useful for dylib reading required for loading proc macros. (cc `@eddyb)`

The object crate is already trusted as dependency of libstd through backtrace. As far as I know it supports reading all object file formats used by targets for which we support rust dylibs with crate metadata, but I am not certain. If this happens to not be the case, I could keep using LLVM for reading dylib metadata.

Marked as WIP for a perf run and as it is based on rust-lang#83637.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet