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

Fix unnecessary error when using neon target feature #95044

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Don't error when just giving neon as a target feature
  • Loading branch information
adamgemmell committed Mar 17, 2022
commit 3f8a774b0181a30b066ab92bd4bcde568ff4e4e3
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(

if let Some(f) = llvm_util::check_tied_features(
cx.tcx.sess,
&function_features.iter().map(|f| (*f, true)).collect(),
&mut function_features.iter().map(|f| (*f, true)).collect(),
Copy link
Member

Choose a reason for hiding this comment

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

Doing &mut collection.iter().stuff().collect() seems like an anti-pattern to me; you're passing in a collection to be mutated, but immediately throwing it away? Its certainly not clear on the callee side that that is what you'll be doing.

I skimmed the source; it seems like all (two?) callsites are creating the hashmap and throwing it away after the call to llvm_util::check_tied_features.

If I'm correct and all call sites are indeed freshly creating the hashmap and immediately throwing it away, then I would either change check_tied_features to take the hashmap by value, or I'd revise the code for check_tied_features so that it doesn't need to modify the hashmap at all.

) {
let span = cx
.tcx
Expand Down
13 changes: 10 additions & 3 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,15 @@ pub fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]
// ensure only valid combinations are allowed.
pub fn check_tied_features(
sess: &Session,
features: &FxHashMap<&str, bool>,
features: &mut FxHashMap<&str, bool>,
) -> Option<&'static [&'static str]> {
// neon implies fp, so we don't need to error in this case.
if sess.target.arch == "aarch64" && !features.contains_key("fp") {
if let Some(neon) = features.get("neon").copied() {
features.insert("fp", neon);
}
}

for tied in tied_target_features(sess) {
// Tied features must be set to the same value, or not set at all
let mut tied_iter = tied.iter();
Expand Down Expand Up @@ -487,8 +494,8 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str

if diagnostics {
// FIXME(nagisa): figure out how to not allocate a full hashset here.
let featmap = feats.iter().map(|&(flag, feat)| (feat, flag == '+')).collect();
if let Some(f) = check_tied_features(sess, &featmap) {
let mut featmap = feats.iter().map(|&(flag, feat)| (feat, flag == '+')).collect();
if let Some(f) = check_tied_features(sess, &mut featmap) {
sess.err(&format!(
"target features {} must all be enabled or disabled together",
f.join(", ")
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/target-feature/tied-features-cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// revisions: one two three
// revisions: one two three four five
// compile-flags: --crate-type=rlib --target=aarch64-unknown-linux-gnu
// needs-llvm-components: aarch64
//
Expand All @@ -11,6 +11,8 @@
// [three] compile-flags: -C target-feature=+paca,+pacg,-paca
// [four] build-pass
// [four] compile-flags: -C target-feature=-paca,+pacg -C target-feature=+paca
// [five] build-pass
// [five] compile-flags: -C target-feature=+neon
#![feature(no_core, lang_items)]
#![no_core]

Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/target-feature/tied-features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ pub fn main() {
bar();
baz();
inner();
implicit_fp();
}
}

#[target_feature(enable = "paca")]
//~^ ERROR must all be either enabled or disabled together
unsafe fn foo() {}


#[target_feature(enable = "paca,pacg")]
unsafe fn bar() {}

#[target_feature(enable = "paca")]
#[target_feature(enable = "pacg")]
unsafe fn baz() {}

#[target_feature(enable = "neon")]
unsafe fn implicit_fp() {}
2 changes: 1 addition & 1 deletion src/test/ui/target-feature/tied-features.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | #[target_feature(enable = "pacg")]
= help: add the missing features in a `target_feature` attribute

error: the target features paca, pacg must all be either enabled or disabled together
--> $DIR/tied-features.rs:25:1
--> $DIR/tied-features.rs:26:1
|
LL | #[target_feature(enable = "paca")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down