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

ABI-required target features: warn when they are missing in base CPU #136147

Merged

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jan 27, 2025

Part of #135408:
instead of adding ABI-required features to the target we build for LLVM, check that they are already there. Crucially we check this after applying -Ctarget-cpu and -Ctarget-feature, by reading sess.unstable_target_features. This means we can tweak the ABI target feature check without changing the behavior for any existing user; they will get warnings but the target features behave as before.

The test changes here show that we are un-doing the "add all required target features" part. Without the full #135408, there is no way to take a way an ABI-required target feature with -Ctarget-cpu, so we cannot yet test that part.

Cc @workingjubilee

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 27, 2025
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the required-target-features-check-not-add branch from e40d565 to 968974d Compare January 27, 2025 19:10
@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the required-target-features-check-not-add branch from 27885ad to b793ec0 Compare January 28, 2025 01:43
@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the required-target-features-check-not-add branch 2 times, most recently from 4188395 to d63382d Compare January 28, 2025 02:35
@RalfJung RalfJung force-pushed the required-target-features-check-not-add branch from d63382d to 3f6ffa1 Compare January 28, 2025 03:40
@workingjubilee workingjubilee self-assigned this Jan 28, 2025
Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct.

One question though, who are we adding new warnings to, exactly?

Comment on lines +55 to +56
/// Ensures that all target features required by the ABI are present.
/// Must be called after `unstable_target_features` has been populated!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not love ordering sensitivity, but it is tolerable in small amounts, and it seems we are localizing all our various similarly-shaped checks to this one spot, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now part of early session bringup, which is quite order-dependent. I didn't have any better idea for where to put it... I could put it somewhere in run_compiler?

Copy link
Member

@workingjubilee workingjubilee Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think we should worry about fixing this after we fix the ordering dependency of session bringup (even if just to make it more state-machine-y)

Comment on lines -120 to -143
// Ensure that the features we enable/disable are compatible with the ABI.
if enable {
if abi_incompatible_set.contains(feature) {
sess.dcx().emit_warn(ForbiddenCTargetFeature {
feature,
enabled: "enabled",
reason: "this feature is incompatible with the target ABI",
});
}
} else {
// FIXME: we have to request implied features here since
// negative features do not handle implied features above.
for &required in abi_feature_constraints.required.iter() {
let implied = sess.target.implied_target_features(std::iter::once(required));
if implied.contains(feature) {
sess.dcx().emit_warn(ForbiddenCTargetFeature {
feature,
enabled: "disabled",
reason: "this feature is required by the target ABI",
});
}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay

Comment on lines -779 to -794
// To be sure the ABI-relevant features are all in the right state, we explicitly
// (un)set them here. This means if the target spec sets those features wrong,
// we will silently correct them rather than silently producing wrong code.
// (The target sanity check tries to catch this, but we can't know which features are
// enabled in LLVM by default so we can't be fully sure about that check.)
// We add these at the beginning of the list so that `-Ctarget-features` can
// still override it... that's unsound, but more compatible with past behavior.
all_rust_features.splice(
0..0,
abi_feature_constraints
.required
.iter()
.map(|&f| (true, f))
.chain(abi_feature_constraints.incompatible.iter().map(|&f| (false, f))),
);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love to see it

@workingjubilee
Copy link
Member

and to be clear: if the answer to my question is "currently, no one" then r=me otherwise I'll think about it after I sleep on it.

@RalfJung
Copy link
Member Author

RalfJung commented Jan 28, 2025

One question though, who are we adding new warnings to, exactly?

If an ABI-required feature is missing from the base target spec (or an ABI-incompatible feature is present), this would now warn rather than just enabling the feature anyway.

All built-in targets should already be setting the right target features, so there's no new warnings here. (Ideally we'd have a test asserting this for all our targets, but that seems non-trivial.)

@@ -1,7 +0,0 @@
warning: target feature `neon` cannot be disabled with `-Ctarget-feature`: this feature is required by the target ABI
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning disappears because sve2 implies neon, so -neon,+sve2 actually doesn't have adverse ABI effects.

@oli-obk oli-obk removed their assignment Jan 28, 2025
@workingjubilee
Copy link
Member

If an ABI-required feature is missing from the base target spec (or an ABI-incompatible feature is present), this would now warn rather than just enabling the feature anyway.

Okay, so basically this is only shipping warnings to custom specs, or preventing us from shipping a broken spec? Sounds good to me, then.

@workingjubilee
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 28, 2025

📌 Commit 3f6ffa1 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 28, 2025
@RalfJung
Copy link
Member Author

The specific form in which it prevents us from shipping a broken spec is "it shows a warning and we'd see that in CI (for the major targets) / someone would surely complain". It's not great, but it's a start.

@bors p=1
(there's a follow-up PR waiting for this one)

Zalathar added a commit to Zalathar/rust that referenced this pull request Jan 29, 2025
…check-not-add, r=workingjubilee

ABI-required target features: warn when they are missing in base CPU

Part of rust-lang#135408:
instead of adding ABI-required features to the target we build for LLVM, check that they are already there. Crucially we check this after applying `-Ctarget-cpu` and `-Ctarget-feature`, by reading `sess.unstable_target_features`. This means we can tweak the ABI target feature check without changing the behavior for any existing user; they will get warnings but the target features behave as before.

The test changes here show that we are un-doing the "add all required target features" part. Without the full rust-lang#135408, there is no way to take a way an ABI-required target feature with `-Ctarget-cpu`, so we cannot yet test that part.

Cc `@workingjubilee`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 29, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang#135625 ([cfg_match] Document the use of expressions.)
 - rust-lang#135902 (Do not consider child bound assumptions for rigid alias)
 - rust-lang#135943 (Rename `Piece::String` to `Piece::Lit`)
 - rust-lang#136104 (Add mermaid graphs of NLL regions and SCCs to polonius MIR dump)
 - rust-lang#136143 (Update books)
 - rust-lang#136147 (ABI-required target features: warn when they are missing in base CPU)
 - rust-lang#136164 (Refactor FnKind variant to hold &Fn)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7e123e4 into rust-lang:master Jan 29, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 29, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 29, 2025
Rollup merge of rust-lang#136147 - RalfJung:required-target-features-check-not-add, r=workingjubilee

ABI-required target features: warn when they are missing in base CPU

Part of rust-lang#135408:
instead of adding ABI-required features to the target we build for LLVM, check that they are already there. Crucially we check this after applying `-Ctarget-cpu` and `-Ctarget-feature`, by reading `sess.unstable_target_features`. This means we can tweak the ABI target feature check without changing the behavior for any existing user; they will get warnings but the target features behave as before.

The test changes here show that we are un-doing the "add all required target features" part. Without the full rust-lang#135408, there is no way to take a way an ABI-required target feature with `-Ctarget-cpu`, so we cannot yet test that part.

Cc ``@workingjubilee``
@RalfJung RalfJung deleted the required-target-features-check-not-add branch January 29, 2025 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants