refactor handling of target features in Session - #160530
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
02d834e to
db9655e
Compare
311dc25 to
36489cd
Compare
| if sess.target_features.contains(&sym::zca) { | ||
| if sess.internal_target_features.contains(&sym::zca) { | ||
| e_flags |= elf::EF_RISCV_RVC; | ||
| } | ||
|
|
||
| // Check if RVTSO is enabled | ||
| if sess.target_features.contains(&sym::ztso) { | ||
| if sess.internal_target_features.contains(&sym::ztso) { | ||
| e_flags |= elf::EF_RISCV_TSO; | ||
| } |
There was a problem hiding this comment.
This used to check whether the feature is in cfg(target_feature) rather than checking whether the feature is enabled. I don't think that makes a ton of sense... it means that with -Ctarget-feature=+zca on stable, we do enable that target feature (with a warning), but we don't set the flags here.
There was a problem hiding this comment.
Actually these features are stable so this diff is a non-functional change.
|
Some changes occurred in cfg and check-cfg configuration cc @Urgau
cc @rust-lang/miri These commits modify compiler targets.
cc @bjorn3 |
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
36489cd to
1555262
Compare
| let features_map = sess.target.rust_target_features_map(); | ||
|
|
||
| // Compute which of the known target features are enabled in the 'base' target machine. We only | ||
| // consider "supported" features; "forbidden" features are not reflected in `cfg` as of now. |
There was a problem hiding this comment.
The old comment here was outdated, we are not actually excluding "forbidden" features here.
This comment has been minimized.
This comment has been minimized.
71ce3cd to
3ccab9c
Compare
| "atomics-32", | ||
| Stability::Forbidden { | ||
| // Not implied by anything. (FIXME: is that true?) | ||
| Stability::InternalOnly { | ||
| reason: "unsound because it changes the ABI of atomic operations", |
There was a problem hiding this comment.
@taiki-e is it true that nothing (no CPU model and no other target feature) implies "atomics-32" on ARM?
There was a problem hiding this comment.
Yes, this is a feature regarding ABI, not ISA. (It's the same as riscv's forced-atomics feature.)
2cfdc5e to
aac414a
Compare
| "forced-atomics", | ||
| Stability::Forbidden { | ||
| // Not implied by any CPU model or other feature. (FIXME: is that true?) | ||
| Stability::InternalOnly { | ||
| reason: "unsound because it changes the ABI of atomic operations", |
There was a problem hiding this comment.
@kito-cheng @michaelmaitland @robin-randhawa-sifive @topperc is it true that the "forced-atomics" riscv target feature is not implied by any other target feature or -Ctarget-cpu?
There was a problem hiding this comment.
Yes, it indeed will cause ABI incompatible and no implied by any target-cpu for RISC-V
More comments in LLVM source tree: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/RISCVFeatures.td#L1986-L1993
| "windowed", | ||
| Forbidden { reason: "windowed changes the Xtensa calling convention", hard_error: false }, | ||
| // Not implied by any CPU model or other feature. (FIXME: is that true?) | ||
| InternalOnly { | ||
| reason: "windowed changes the Xtensa calling convention", | ||
| hard_error: false, |
There was a problem hiding this comment.
@MabezDev is it true that the "windows" xtensa target feature is not implied by any other target feature or -Ctarget-cpu?
There was a problem hiding this comment.
Yes, it's true. It's a standalone dependency free option. Only other ISA features depend on it.
We have two examples of such chips, esp8266 without windowed and esp32 family have windowed enabled, if you want to look further.
There was a problem hiding this comment.
It seems to imply "exception" so apparently there is a dependency?
Any dependency in either direction is a problem. The way things stand now, we have to make "exception" also an internal-only target feature, since it affects the ABI via its connection with "windowed".
There was a problem hiding this comment.
Okay, I think I understand a bit better what the context of this is. So the window feature is dependency-free no other ISA feature forces it to be enabled by implication.
What I didn't understand on the first run is the CPU side of it. The CPU will enable the windowed feature for certain chips (anything that's not esp8266 basically, which is every modern esp32).
Therefore the comment is incorrect, and needs to be changed to something along the lines of
// Not implied by any other ISA feature, but can be implied by the value of CPU.There was a problem hiding this comment.
So if I mix code with different -Ctarget-cpu values I can get UB because the ABI is different?
Uh, that's not okay. That's a soundness bug.
There was a problem hiding this comment.
That's exactly why the esp8266 is retired, and why there are no upstream targets for it. All current targets are windowed, and through the diff highlighted here, it's not possible to set windowed via -Ctarget-feature.
So essentially, yes there could be possible UB, but the current rust targets are all windowed. It would require doing something wacky, like setting cpu to esp8266 on for example, the esp32s3 rust target. If you think we need a more concrete fix, I'm all ears.
There was a problem hiding this comment.
I can also ask our LLVM maintainers to drop the esp8266 cpu completely, and therefore making this path uncrossable (unless someone else ads an non windowed target in LLVM).
There was a problem hiding this comment.
Oh so Rust should only support the case where "windowed" is enabled? That's easy, that just needs to be configured in abi_required_features. :)
Since "windowed" implies "exception", that means "exception" is also always enabled?
| reason: "windowed changes the Xtensa calling convention", | ||
| hard_error: false, | ||
| }, | ||
| &["exception"], |
There was a problem hiding this comment.
This implication means that -Ctarget-feature=-exception will turn off "windowed". How does it make sense to reject -Ctarget-feature=-windows but allow -Ctarget-feature=-exception?
aac414a to
00dcb30
Compare
|
Does this still ensure for example Details |
6c2a21f to
1611f60
Compare
|
It's probably worth a grep to check that all @rustbot author |
1611f60 to
ea26f7c
Compare
ea26f7c to
561be60
Compare
Good point, done. I also found a @bors r=nnethercote |
561be60 to
e5004d0
Compare
|
This pull request was unapproved. |
|
I removed the xtensa ABI checks here as those will be handled by #160643. |
|
@bors r=nnethercote |
View all comments
Sessioncurrently contains two lists of target features:target_features, which is also exposed incfg, andunstable_target_features, which is used internally to communicate between various parts of the compiler which target features are actually available, including some that we don't have plans to put incfg, namely "forbidden" target features. Theunstable_target_featureslist is not equivalent to what nightly code sees incfg(target_features)as the latter excludes "forbidden" target features. Both lists are computed byfn cfg_target_featureseven though one of them is never used forcfg. It's all kind of messy.This PR refactors that:
fn cfg_target_featuresis replaced byfn internal_target_featureswhich computes all enabled Rust target features (including "forbidden" ones -- which are really more like "internal-only" ones so the 2nd commit renames them). We then computecfg(target_features)from that. The session only stores one list,internal_target_features, which corresponds to the previousunstable_target_features.To simplify computing
internal_target_featuresI also refactoredparse_rust_feature_listto better distinguish actual Rust target features from unknown target features that we are just grandfathering in. I also madeimplied_target_featuresnot rebuild the same hash map over and over again. And I got rid of a bunch of silly temporary vectors and iterations over all Rust target features.