-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
add --crate-type metadata #37681
add --crate-type metadata #37681
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me, thanks @nrc!
The only major thing I see is whether the format here is an rlib with a metadata file or just the metadata file itself (I'd sorta prefer the latter).
Can you also add a test with some native libraries? That is, despite whatever #[link]
directives are found, they're all ignored when generating a metadata crate type.
Also, can you add tests which emit both an rlib and a metadata crate? Just to make sure nothing explodes too badly there.
@@ -1632,6 +1634,7 @@ impl fmt::Display for CrateType { | |||
CrateTypeStaticlib => "staticlib".fmt(f), | |||
CrateTypeCdylib => "cdylib".fmt(f), | |||
CrateTypeProcMacro => "proc-macro".fmt(f), | |||
CrateTypeMetadata => "rmeta".fmt(f), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/rmeta/metadata/
@@ -750,7 +753,7 @@ impl<'a> CrateLoader<'a> { | |||
}; | |||
info!("panic runtime not found -- loading {}", name); | |||
|
|||
let (cnum, data, _) = self.resolve_crate(&None, name, name, None, | |||
let (cnum, data) = self.resolve_crate(&None, name, name, None, | |||
syntax_pos::DUMMY_SP, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(left indent)
@@ -53,6 +53,13 @@ | |||
//! is a platform-defined dynamic library. Each library has a metadata somewhere | |||
//! inside of it. | |||
//! | |||
//! A third kind of dependency is an rmeta file. These are rlibs, which contain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having read through this file, everything looks good. I might expect though that libfoo.rmeta
is not an archive but rather just the literal metadata file in the rlib normally itself. This'd avoid going through LLVM's archive support which may speed things up a bit, but probably won't have that much effect on perf.
I'm not sure if overall it'd be less code as well, just a thought!
@@ -190,6 +189,7 @@ pub fn link_binary(sess: &Session, | |||
let mut out_filenames = Vec::new(); | |||
for &crate_type in sess.crate_types.borrow().iter() { | |||
// Ignore executable crates if we have -Z no-trans, as they will error. | |||
// TODO do we need to check for CrateTypeMetadata here? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably CrateTypeMetadata
halts before the linking stage, right? So we should just skip it here?
@@ -344,7 +350,7 @@ fn link_binary_output(sess: &Session, | |||
}; | |||
|
|||
match crate_type { | |||
config::CrateTypeRlib => { | |||
config::CrateTypeRlib | config::CrateTypeMetadata => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm so this is where I start to get a little uneasy, link_rlib
has tons of logic for object files, native libraries, etc. Perhaps it'd be clearer and/or easier to just emit the metadata here to a dedicated file?
sess.opts.output_types.contains_key(&OutputType::Exe); | ||
(sess.crate_types.borrow().contains(&config::CrateTypeRlib) && | ||
sess.opts.output_types.contains_key(&OutputType::Exe)) || | ||
sess.crate_types.borrow().contains(&config::CrateTypeMetadata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want bitcode for metadata, right? Just the metadata file?
let needs_crate_object = | ||
sess.opts.output_types.contains_key(&OutputType::Exe); | ||
sess.opts.output_types.contains_key(&OutputType::Exe) || | ||
sess.crate_types.borrow().contains(&config::CrateTypeMetadata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same as above)
|
||
// aux-build:rmeta_rlib.rs | ||
// no-prefer-dynamic | ||
// must-compile-successfully |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoa, I had no idea this existed!
cc @rust-lang/tools as well, a new crate type! @eddyb did you want to hold off on merging until the compiler team has a chance to talk about this as well? |
Yeah, For example, OTOH, the metadata file is part of an rlib like an object file, and you can get the latter through |
Another question would be whether to include all MIR of the crate or none at all, maybe? Right now, this will omit MIR for those functions that would have been translated to machine code if this was an rlib, which does not make so much sense any more in the new setting. |
@eddyb yeah I'm not sure if this is most appropriate as an I'm not sure I follow the bits about MIR/miri personally, but so long as it serves the problem of "fast compilation which doesn't require linkers" seems plausible to me! @michaelwoerister I'd personally expect MIR to be omitted if we can, in the sense that I'm thinking metadata crates are purely used for static analysis and/or checking compiles. If MIR is needed for that it's included but if not then it's omitted. |
You need at least MIR for constants, now and |
If we only include the minimal set of MIR needed for static analysis, we would be able to completely skip MIR optimization passes, translation item collection, and codegen unit partitioning. That might be a worthwhile goal for the future. |
Indeed! I'd imagine that we optimize metadata-generation to be as fast as possible and skip as much as possible in the compiler |
I'd like to raise the question of stability here - to what extent can we change the contents of the output of a stable option like this? For obvious reasons I'd like to land this asap, but it sounds like there a things we could improve in the future around metadata, etc. On --emit vs --crate-type. I think this is a user-facing change if the user is using the compiler directly. I agree the two options are not at all orthogonal. I believe in the current model, --crate-type makes sense because we are changing the kind of crate emitted, but not really changing anything else. |
@nrc You probably want to enforce that no codegen happens in dependent crates, i.e. metadata crates can only be used to build more metadata crates, and only expand this in the future if we want to. |
@eddyb is that not a Cargo issue, rather than a rustc one? We don't do any codegen for dep crates in any case do we? |
@nrc I feel like the stability here is the same as rlibs, which is to say that you're required to treat it as a black box from the outside and it's only guaranteed to work with one compiler. Put another way, there's zero stability but it hopefully doesn't have any bugs :) |
After the compiler meeting, I'm pretty much on board with this approach, even if it overlaps with both As long as it cannot be misused from dependent crates and there's no stabilization schedule, SGTM. |
@eddyb it would be insta-stable (at least the option would be, sounds like the contents would be perma-unstable). |
Updated to use a raw metadata file, rather than a degenerate rlib. And added a test |
@@ -845,6 +860,15 @@ fn get_metadata_section_imp(target: &Target, | |||
Ok(blob) | |||
} | |||
}; | |||
} else if flavor == CrateFlavor::Rmeta { | |||
let mut file = File::open(filename).map_err(|_| | |||
format!("could not open file: '{}'", filename.display()))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this include the reason it failed to open as well? (e.g. the argument to map_err
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was basically following the errors from the rlib branch directly above here. Those follow the same pattern (ignoring the actual error), although in that case because the error gets converted into an Option
in the meantime. So I could (c.f., rlibs) add the extra info here, but it would be inconsistent with the rlib version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok, I guess whatever is fine. We shouldn't ever throw away the error context, but it's not really that critical.
Looks good to me! r=me assuming compiler team's OK w/ this as well |
☔ The latest upstream changes (presumably #37545) made this pull request unmergeable. Please resolve the merge conflicts. |
Just a quick question as I'm quite interested in this crate type: Will it contain all MIR as rlibs exclude some parts afaik (e.g https://github.com/rust-lang/rust/blob/master/src/librustc_metadata/encoder.rs#L826-L834)? |
No, it contains exactly the same metadata that an rlib would contain. |
With the same semantics as -Zno-trans
@bors: r= @alexcrichton |
📌 Commit d038ebc has been approved by `` |
📌 Commit d038ebc has been approved by |
⌛ Testing commit d038ebc with merge 084ed2a... |
💔 Test failed - auto-win-gnu-32-opt-rustbuild |
Travis failed tidy. "/checkout/src/librustc_metadata/locator.rs:735: line longer than 100 chars". |
📌 Commit af1b195 has been approved by |
add --crate-type metadata r? @alexcrichton
This looks like it is insta-stable. Why is it not feature gated? |
|
cargo check ~~This is not finished - the big omission is no tests, and it needs some more testing too. It also requires rust-lang/rust#37681 However, this is my first non-trivial Cargo patch, so I'd like to get some early feedback. r? @alexcrichton and cc @rust-lang/tools since this adds a new Cargo sub-command (although we have previously discussed and approved the idea in a tools meeting).
r? @alexcrichton