-
Notifications
You must be signed in to change notification settings - Fork 13.8k
rustc_codegen_llvm: traitification of LLVM-specific CodegenCx and Builder methods #55627
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
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
6538a47
to
cffad3f
Compare
r? @eddyb |
This comment has been minimized.
This comment has been minimized.
Thanks for fixing the bug ! It was a silly mistake from my part... |
rustc_target: pass contexts by reference, not value. `LayoutOf` now takes `&self` instead of `self`, and so does every method generic over a context that implements `LayoutOf` and/or other traits, like `HasDataLayout`, `HasTyCtxt`, etc. Originally using by-value `Copy` types was relevant because `TyCtxt` was one of those types, but now `TyCtxt::layout_of` is separate from `LayoutOf`, and `TyCtxt` is not an often used layout context. Passing these context by reference is a lot nicer for miri, which has `self: &mut EvalContext`, and needed `f(&self)` (that is, creating `&&mut EvalContext` references) for layout purposes. Now, the `&mut EvalContext` can be passed to a function expecting `&C`, directly. This should help with #54012 / #55627 (to not need `where &'a T::Cx: LayoutOf` bounds). r? @nikomatsakis or @oli-obk or @nagisa cc @sunfishcode
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.
First batch of comments (my browser hates me already!)
src/librustc_codegen_llvm/abi.rs
Outdated
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.
dst
should probably be on its own line.
src/librustc_codegen_llvm/abi.rs
Outdated
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 think this is a LLVM-ism, does Cranelift support getting at the function params from a builder? What I'd do instead is also pass a slice of values, alongside the index.
src/librustc_codegen_llvm/abi.rs
Outdated
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.
Maybe have a crate-wide type-alias so that we can continue to write CodegenCx<'ll, '_>
instead of CodegenCx<'ll, '_, &'ll Value>
in rustc_codegen_llvm
?
src/librustc_codegen_llvm/abi.rs
Outdated
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.
Hmm, I think it might be better if instead of the "apply attrs" LLVM-ism, the call
/invoke
methods of Builder
took an FnType
and internally called the LLVM-only apply_attrs_callsite
.
src/librustc_codegen_llvm/abi.rs
Outdated
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.
Do any of these depend on anything LLVM-specific? IMO these could be moved into rustc_codegen_ssa
or even rustc
itself!
src/librustc_codegen_llvm/builder.rs
Outdated
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.
For this to be a safe Rust function, btw, *const c_char
needs to be replaced with &CStr
.
src/librustc_codegen_llvm/builder.rs
Outdated
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.
This makes me think From
should be implemented instead. Then, the LLVM-specific AtomicOrdering
do not need to be imported, removing rustc_codegen_ssa::common::
noise in the signature and we can just do order.into()
and whatnot.
src/librustc_codegen_llvm/builder.rs
Outdated
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.
This probably needs to be generalized to storage_{live,dead}
(which is what the MIR contains).
src/librustc_codegen_llvm/builder.rs
Outdated
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.
This is unsafe! The trait method should be marked as such. Maybe we can find a way to avoid doing it at all.
src/librustc_codegen_llvm/builder.rs
Outdated
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.
Applying attributes via the return value is a LLVM-ism, we should clean it up.
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.
Second round of comments, got through rustc_codegen_llvm
and rustc_codegen_ssa::back
, things seems pretty good so far!
src/librustc_codegen_llvm/callee.rs
Outdated
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.
Any reason this can't be a method in an extension trait on TyLayout
still?
That is, I'd prefer to write cx.layout_of(fn_ptr_ty).codegen_type(cx)
.
OTOH, another thing we can do is remove the &
there.
TyLayout
is a "cheap" type, a pair of references IIRC, by design!
src/librustc_codegen_llvm/common.rs
Outdated
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.
This should probably be moved to the consts.rs
file or something.
src/librustc_codegen_llvm/common.rs
Outdated
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'd maybe call this codegen_scalar
or, better, const_scalar
.
src/librustc_codegen_llvm/consts.rs
Outdated
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.
This seems like would require this method to be marked as unsafe
(as references to old_g
might still be around, outside of the LLVM module itself).
src/librustc_codegen_llvm/context.rs
Outdated
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, this is local struct. You can do V = &'ll Value
here!
src/librustc_codegen_llvm/type_.rs
Outdated
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.
These should be inverted - that is, implemented on TyLayout
, in rustc_codegen_ssa
. I guess you still need this trait, hmm.
src/librustc_codegen_ssa/README.md
Outdated
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 think you could have Value
in a trait that's only associated types, and is implemented for builders to point to their backend's associated types.
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.
This reminds me the name Codegen
in the context of the "back" module, is suboptimal.
Including in "optimize_and_codegen". I think we could call this step "emitting machine code/a binary", so maybe optimize_and_emit
? OptimizeAndEmitContext
is not great, but more descriptive.
cc @michaelwoerister @rkruppe @alexcrichton
Also, :
should be :
.
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.
Not sure what to do about all of these LLVM-specific options.
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.
Maybe rename bitcode
to ir
, to generalize? or llvm_bc
, to make it clear it's LLVM-specific.
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.
Maybe someone should search for llvm
(case-insensitive) in rustc_codegen_ssa
.
There's a general issue, that |
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.
Third batch, and that's all the comments for now! Let me know if I can help resolve everything.
src/librustc_codegen_ssa/base.rs
Outdated
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.
Outdated comment - could even be removed, base
ended up as a dumping ground (and common
similarly), they should be properly redistributed around IMO.
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.
But only the comment is a concern for now.
src/librustc_codegen_ssa/base.rs
Outdated
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.
So, &'a Cx : LayoutOf + HasTyCtxt
bounds should not longer be needed, since my PR landed.
Also, we either need to 1. avoid putting bounds on structs 2. enable the implied bounds feature to avoid writing the bounds anywhere else. cc @nikomatsakis
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.
Also, are any of these "stats" useful anymore? It feels like any new infrastructure should be designed with MIR in mind...
src/librustc_codegen_ssa/base.rs
Outdated
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.
IMO Real
in the type name should be changed to Float
. I have no idea why LLVM chose that (wrong) name in the first place.
src/librustc_codegen_ssa/base.rs
Outdated
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.
As I mentioned earlier, I think it would be cleaner if builders implemented the same trait holding associated types, so this was just Bx::Value
.
Also, I'm seeing 'll
in this crate - that shouldn't be needed!
After all, we're abstracting over types like &'ll Value
- so the parametrization should only exist in rustc_codegen_llvm
.
src/librustc_codegen_ssa/base.rs
Outdated
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'm impressed all of this entrypoint stuff could be reused!
src/librustc_codegen_utils/lib.rs
Outdated
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.
Why is this needed? It should, at all, be placed on individual items!
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 we, instead, make Bx
an associated type on Cx
? That would get rid of some of the explicit parametrization.
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 think this should be on an extension trait on TyCtxt
in rustc_codegen_ssa
.
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.
These packing methods should be relegated to the backend.
AFAIK, Cranelift uses multiple returns instead of packing two values into one.
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.
This should be a builder method.
cffad3f
to
1764f2c
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☔ The latest upstream changes (presumably #55410) made this pull request unmergeable. Please resolve the merge conflicts. |
1764f2c
to
09235bb
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☔ The latest upstream changes (presumably #55746) made this pull request unmergeable. Please resolve the merge conflicts. |
@sunfishcode Can you take a look at fixing the merge conflicts and resolving the CI failure here? |
@Mark-Simulacrum I'm rebasing it and getting close to finishing. |
⌛ Testing commit 756f84d with merge c5b7d98c594251886e5f58bf7197a8aa59053ad3... |
💔 Test failed - status-appveyor |
@alexcrichton @retep998 Is this spurious, or a real failure? @bors retry (just in case) |
rustc_codegen_llvm: traitification of LLVM-specific CodegenCx and Builder methods This PR is the continuation of #54012 and earlier PRs, in the grand plan of #45274 to allow for multiple codegen backends. High-level summary: interpose a set of traits between Rust's codegen logic and the LLVM APIs, allowing another backend to implement the traits and share most of the codegen logic. These traits are currently somewhat LLVM-specific, but once this refactoring is in place, they can evolve to be more general. See [this README](https://github.com/rust-lang/rust/blob/756f84d7cef90b7364ae88ca707e59670dde4c92/src/librustc_codegen_ssa/README.md) for a writeup on the current trait organization.
☀️ Test successful - status-appveyor, status-travis |
Add rustc_codegen_ssa to sysroot Outside of rustc you are currently unable to use it. r? @nikomatsakis (because you r+'ed rust-lang#55627)
Enable tests on rustc_codegen_ssa This enables unittests in rustc_codegen_ssa. There are some tests, primarily in [`back/rpath/tests.rs`](https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs) that haven't ever been running since the unittests are disabled. From what I can tell, this was just a consequence of how things evolved. When testing was initially added in rust-lang#33282, `librustc_trans` had test=false because it didn't have any tests. `rustc_codegen_ssa` eventually split off from that (rust-lang#55627), and the rpath module eventually got merged in too (from `librustc_back` where it used to live). That migration didn't enable the tests. This also includes some fluent diagnostic tests, though I'm not sure what exactly they are testing.
Enable tests on rustc_codegen_ssa This enables unittests in rustc_codegen_ssa. There are some tests, primarily in [`back/rpath/tests.rs`](https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs) that haven't ever been running since the unittests are disabled. From what I can tell, this was just a consequence of how things evolved. When testing was initially added in rust-lang#33282, `librustc_trans` had test=false because it didn't have any tests. `rustc_codegen_ssa` eventually split off from that (rust-lang#55627), and the rpath module eventually got merged in too (from `librustc_back` where it used to live). That migration didn't enable the tests. This also includes some fluent diagnostic tests, though I'm not sure what exactly they are testing.
This PR is the continuation of #54012 and earlier PRs, in the grand plan of #45274 to allow for multiple codegen backends.
High-level summary: interpose a set of traits between Rust's codegen logic and the LLVM APIs, allowing another backend to implement the traits and share most of the codegen logic. These traits are currently somewhat LLVM-specific, but once this refactoring is in place, they can evolve to be more general.
See this README for a writeup on the current trait organization.