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

add --crate-type metadata #37681

Merged
merged 4 commits into from
Nov 23, 2016
Merged

add --crate-type metadata #37681

merged 4 commits into from
Nov 23, 2016

Conversation

nrc
Copy link
Member

@nrc nrc commented Nov 10, 2016

@nrc nrc mentioned this pull request Nov 10, 2016
@eddyb eddyb added I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 10, 2016
Copy link
Member

@alexcrichton alexcrichton left a 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),
Copy link
Member

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,
Copy link
Member

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
Copy link
Member

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?
Copy link
Member

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 => {
Copy link
Member

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);
Copy link
Member

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);
Copy link
Member

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
Copy link
Member

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!

@alexcrichton
Copy link
Member

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?

@eddyb
Copy link
Member

eddyb commented Nov 10, 2016

Yeah, although we might not be having enough of a meeting today.
My main concern is that crate types are usually user-visible while this is a "mode".
That said, there is no real difference between an rlib and a dylib as far as metadata goes, so "metadata-only library" works for me. It'd be interesting to see how tools could use a version of this with all MIR.

For example, miri could evaluate everything (possibly emulating FFI) and other potential backends don't really have a concept of "library" or "linking" and rather do codegen only once, for the final "executable".

OTOH, the metadata file is part of an rlib like an object file, and you can get the latter through --emit.
I'm not fully decided either way, partly because --emit and "crate types" are not fully orthogonal.

@michaelwoerister
Copy link
Member

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.

@alexcrichton
Copy link
Member

@eddyb yeah I'm not sure if this is most appropriate as an --emit or just a new crate type, but a new crate type seems fine to me (it'll compile down to the same thing essentially).

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.

@eddyb
Copy link
Member

eddyb commented Nov 10, 2016

You need at least MIR for constants, now and const fn in the future, even without any codegen.

@michaelwoerister
Copy link
Member

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.

@alexcrichton
Copy link
Member

Indeed! I'd imagine that we optimize metadata-generation to be as fast as possible and skip as much as possible in the compiler

@nrc
Copy link
Member Author

nrc commented Nov 10, 2016

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.

@eddyb
Copy link
Member

eddyb commented Nov 10, 2016

@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.

@nrc
Copy link
Member Author

nrc commented Nov 10, 2016

@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?

@alexcrichton
Copy link
Member

@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 :)

@eddyb
Copy link
Member

eddyb commented Nov 10, 2016

After the compiler meeting, I'm pretty much on board with this approach, even if it overlaps with both --emit and future incremental recompilation workdir structure, it's still relevant in its own right.

As long as it cannot be misused from dependent crates and there's no stabilization schedule, SGTM.

@nrc
Copy link
Member Author

nrc commented Nov 10, 2016

@eddyb it would be insta-stable (at least the option would be, sounds like the contents would be perma-unstable).

@eddyb eddyb added the relnotes Marks issues that should be documented in the release notes of the next release. label Nov 11, 2016
@nrc
Copy link
Member Author

nrc commented Nov 15, 2016

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()))?;
Copy link
Member

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)

Copy link
Member Author

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.

Copy link
Member

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.

@alexcrichton
Copy link
Member

Looks good to me! r=me assuming compiler team's OK w/ this as well

@bors
Copy link
Contributor

bors commented Nov 16, 2016

☔ The latest upstream changes (presumably #37545) made this pull request unmergeable. Please resolve the merge conflicts.

@nrc nrc mentioned this pull request Nov 16, 2016
@msiglreith
Copy link
Contributor

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)?

@nrc
Copy link
Member Author

nrc commented Nov 18, 2016

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
@nrc
Copy link
Member Author

nrc commented Nov 20, 2016

@bors: r= @alexcrichton

@bors
Copy link
Contributor

bors commented Nov 20, 2016

📌 Commit d038ebc has been approved by ``

@nrc
Copy link
Member Author

nrc commented Nov 21, 2016

@bors: r=@alexcrichton

@bors
Copy link
Contributor

bors commented Nov 21, 2016

📌 Commit d038ebc has been approved by @alexcrichton

@bors
Copy link
Contributor

bors commented Nov 22, 2016

⌛ Testing commit d038ebc with merge 084ed2a...

@bors
Copy link
Contributor

bors commented Nov 22, 2016

💔 Test failed - auto-win-gnu-32-opt-rustbuild

@sanxiyn
Copy link
Member

sanxiyn commented Nov 22, 2016

Travis failed tidy. "/checkout/src/librustc_metadata/locator.rs:735: line longer than 100 chars".

@nrc
Copy link
Member Author

nrc commented Nov 22, 2016

@bors: r=@alexcrichton

@bors
Copy link
Contributor

bors commented Nov 22, 2016

📌 Commit af1b195 has been approved by @alexcrichton

@bors
Copy link
Contributor

bors commented Nov 23, 2016

⌛ Testing commit af1b195 with merge 5196ca8...

bors added a commit that referenced this pull request Nov 23, 2016
@bors bors merged commit af1b195 into rust-lang:master Nov 23, 2016
@brson
Copy link
Contributor

brson commented Nov 29, 2016

This looks like it is insta-stable. Why is it not feature gated?

@nrc
Copy link
Member Author

nrc commented Nov 29, 2016

This looks like it is insta-stable. Why is it not feature gated?

  • it does the same thing as -Zno-trans which has been around forever
  • the data emitted is not stable and can be changed at any time
  • the only point of this (c.f., just using -Zno-trans) is for it to be stable
  • it was proposed and accepted at the tools team meeting as a stable feature (apologies if the insta-stable-ness of it was not communicated well enough)
  • the motivation here is to have something Cargo can use, and Cargo only uses stable features.

bors added a commit to rust-lang/cargo that referenced this pull request Dec 14, 2016
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants