Repository navigation
Conversation
|
cc @bjorn3
cc @rust-lang/miri
|
|
r? @chenyukang rustbot has assigned @chenyukang. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
||
| fn init(&mut self, _sess: &EarlySession) -> CodegenBackendInit { | ||
| Default::default() | ||
| fn init(&mut self, _sess: &EarlySession) -> (CodegenBackendInit, TargetConfig) { |
There was a problem hiding this comment.
The 2nd commit refactors backend initialization by merging init and target_config. This is because the LLVM backend can avoid recomputing some data if it can take intermediate results from init and use them to construct target_config. This makes it so that base_llvm_features is actually finally called only once ever, which makes me very happy.
Maybe that means the two types should be merged as well, but I'd prefer to leave that to a future PR as it seems mostly orthogonal, logic-wise.
4262fd4 to
bc2cf69
Compare
|
r? @folkertdev |
|
|
This comment has been minimized.
This comment has been minimized.
bc2cf69 to
7721e8d
Compare
This comment has been minimized.
This comment has been minimized.
7721e8d to
01422ce
Compare
This comment has been minimized.
This comment has been minimized.
01422ce to
e00304e
Compare
This comment has been minimized.
This comment has been minimized.
| None, | ||
| &sess.target, | ||
| ); | ||
| let _ = native_codegen_backend.init(sess); |
There was a problem hiding this comment.
Why is removing the call to init here correct? That function takes &mut self so it might update the session.
There was a problem hiding this comment.
The call has been moved into MiriCodegenBackend::init, because it felt more natural to have it there.
There was a problem hiding this comment.
It's not clear locally how MiriCodegenBackend::init gets called (I guess in compiler/rustc_interface/src/interface.rs?), but I'll happily believe you wrt Miri code.
There was a problem hiding this comment.
MiriCodegenBackend is basically wrapping another codegen backend, so forwarding these calls makes most sense to me. I'm not sure why we previously went for eager initialization...
| // Typically we'll want more explicit settings to override the implicit ones, so: | ||
| // | ||
| // * Features from -Ctarget-cpu=*; are overridden by [^1] | ||
| // * Features implied by --target; are overridden by |
There was a problem hiding this comment.
does this mean "overridden by empty string"? it reads really weird?
There was a problem hiding this comment.
No, it means "overriden by the things listed later in this enumeration"
e00304e to
c9e3b90
Compare
| None, | ||
| &sess.target, | ||
| ); | ||
| let _ = native_codegen_backend.init(sess); |
There was a problem hiding this comment.
It's not clear locally how MiriCodegenBackend::init gets called (I guess in compiler/rustc_interface/src/interface.rs?), but I'll happily believe you wrt Miri code.
| /// LLVM. Does NOT include the features in `-Ctarget-feature`! | ||
| pub(crate) fn base_llvm_features(sess: &EarlySession) -> Vec<String> { | ||
| // Features that come earlier are overridden by conflicting features later in the string. | ||
| // Typically we'll want more explicit settings to override the implicit ones, so: |
There was a problem hiding this comment.
| // Typically we'll want more explicit settings to override the implicit ones, so: | |
| // Typically we'll want more explicit settings to override the implicit ones, so the ordering we use is: |
Maybe?
Also the GCC backend has what is basically a copy of this function, are the comments there still accurate now? I think they are because the changes are only relevant to LLVM.
There was a problem hiding this comment.
Also the GCC backend has what is basically a copy of this function, are the comments there still accurate now? I think they are because the changes are only relevant to LLVM.
Yeah... I have added a commit for that I tried to preserve the existing logic even when it strangely does not match what LLVM does, and left FIXMEs.
Cc @GuillaumeGomez @antoyo I'd appreciate if you could take a look.
There was a problem hiding this comment.
Changes look good to me but I didn't work on this part of the code so better wait for @antoyo's confirmation.
There was a problem hiding this comment.
What happens on the LLVM side is roughly the following
- compute "backend" target features, does not include
-Ctarget-feature - construct a temporary LLVM machine instance for that
- query which Rust target features that machine has
- add
-Ctarget-features - store the result for
cfg(target_feature) - separately translate
-Ctarget-featuresto LLVM feature and add them to the "backend" target features - use that list when actually compiling the code
We are doing this dance because we do not want LLVM feature implications to influence how we interpret -Ctarget-features. So we cannot add -Ctarget-features to the temporary LLVM machine and then back-translate that to Rust features.
With GCC, my best guess is that the BackendInfo constructed in init plays the role of that temporary LLVM machine... but that does not look temporary, it is stored in the backend and later used in compile_codegen_unit. So it seems to me that the GCC backend is not doing the "two separate backend machine instances" dance correctly.
There was a problem hiding this comment.
With GCC, my best guess is that the
BackendInfoconstructed ininitplays the role of that temporary LLVM machine... but that does not look temporary, it is stored in the backend and later used incompile_codegen_unit. So it seems to me that the GCC backend is not doing the "two separate backend machine instances" dance correctly.
Only the TargetInfo is saved (to know if the types like f16/f128 are supported), not the context, so I believe this is doing the same as LLVM by creating 2 libgccjit Contexts here.
There was a problem hiding this comment.
That TargetInfo is saved though, unlike LLVM where it exists just temporarily. So something is still different.
There was a problem hiding this comment.
to know if the types like f16/f128 are supported
With this PR, that happens inside init, so this does not need saving the TargetInfo any more.
However, target_info is also used here, and that has no equivalent in LLVM as far as I can tell. It looks like it is doing codegen, but it is using the TargetInfo that was created for cfg queries. That does not seem right.
There was a problem hiding this comment.
However,
target_infois also used here, and that has no equivalent in LLVM as far as I can tell. It looks like it is doing codegen, but it is using the TargetInfo that was created forcfgqueries
That's what I meant by checking support for the f16/f128 types: it is only used for that checking in the called function.
This comment has been minimized.
This comment has been minimized.
c9e3b90 to
687e034
Compare
687e034 to
ba6921e
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
b5fbf06 to
abea8b4
Compare
abea8b4 to
ddbf0a3
Compare
0c84573 to
d87e107
Compare
1ad9660 to
fe4e465
Compare
fe4e465 to
669da09
Compare
View all comments
Currently, -Zllvm-target-feature takes precedence for populating
internal_target_features, but -Ctarget-feature takes precedence for what we actually tell LLVM. That's pretty bad, so this PR makes them consistent.I first wanted -Zllvm-target-feature to take precedence everywhere, as that was the intent in #160457. However, I don't see a good way to implement that: internal_target_features are computed by instantiating an actual LLVM data structure without -Ctarget-feature, querying which Rust target features that supports, and then applying -Ctarget-feature and its implications "manually". This is done to prevent backend implications from affecting
cfg(target_feature). But this means we can't really apply -Zllvm-target-feature after having done that as that would take us back down to the LLVM level. So instead, the precedence order is that all LLVM-level stuff comes first, and then -Ctarget-feature comes all the way at the end.Cc @folkertdev (reviewer of #160457), @nnethercote (recently did some refactoring of the backend init logic that is being further adjusted here)
Tracking issue: #157753