Skip to content

Commit 3b97f13

Browse files
committed
Auto merge of #142770 - tgross35:rollup-w74w39t, r=tgross35
Rollup of 8 pull requests Successful merges: - #138291 (rewrite `optimize` attribute to use new attribute parsing infrastructure) - #140920 (Extract some shared code from codegen backend target feature handling) - #141990 (Implement send_signal for unix child processes) - #142668 (vec_deque/fmt/vec tests: remove static mut) - #142687 (Reduce uses of `hir_crate`.) - #142699 (Update books) - #142714 (add comment to `src/bootstrap/build.rs`) - #142753 (Update library dependencies) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 18491d5 + bb72cc7 commit 3b97f13

File tree

77 files changed

+870
-1082
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+870
-1082
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub enum InstructionSetAttr {
3838
ArmT32,
3939
}
4040

41-
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic, Default)]
41+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, PrintAttribute)]
42+
#[derive(Encodable, Decodable, HashStable_Generic)]
4243
pub enum OptimizeAttr {
4344
/// No `#[optimize(..)]` attribute
4445
#[default]
@@ -229,7 +230,8 @@ pub enum AttributeKind {
229230

230231
/// Represents `#[rustc_macro_transparency]`.
231232
MacroTransparency(Transparency),
232-
233+
/// Represents `#[optimize(size|speed)]`
234+
Optimize(OptimizeAttr, Span),
233235
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
234236
Repr(ThinVec<(ReprAttr, Span)>),
235237

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rustc_attr_data_structures::{AttributeKind, OptimizeAttr};
2+
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_span::sym;
4+
5+
use super::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::context::{AcceptContext, Stage};
7+
use crate::parser::ArgParser;
8+
9+
pub(crate) struct OptimizeParser;
10+
11+
impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
12+
const PATH: &[rustc_span::Symbol] = &[sym::optimize];
13+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
15+
const TEMPLATE: AttributeTemplate = template!(List: "size|speed|none");
16+
17+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
let Some(list) = args.list() else {
19+
cx.expected_list(cx.attr_span);
20+
return None;
21+
};
22+
23+
let Some(single) = list.single() else {
24+
cx.expected_single_argument(list.span);
25+
return None;
26+
};
27+
28+
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
29+
Some(sym::size) => OptimizeAttr::Size,
30+
Some(sym::speed) => OptimizeAttr::Speed,
31+
Some(sym::none) => OptimizeAttr::DoNotOptimize,
32+
_ => {
33+
cx.expected_specific_argument(single.span(), vec!["size", "speed", "none"]);
34+
OptimizeAttr::Default
35+
}
36+
};
37+
38+
Some(AttributeKind::Optimize(res, cx.attr_span))
39+
}
40+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::session_diagnostics::UnusedMultiple;
2828

2929
pub(crate) mod allow_unstable;
3030
pub(crate) mod cfg;
31+
pub(crate) mod codegen_attrs;
3132
pub(crate) mod confusables;
3233
pub(crate) mod deprecation;
3334
pub(crate) mod inline;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_session::Session;
1515
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
18+
use crate::attributes::codegen_attrs::OptimizeParser;
1819
use crate::attributes::confusables::ConfusablesParser;
1920
use crate::attributes::deprecation::DeprecationParser;
2021
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -108,6 +109,7 @@ attribute_parsers!(
108109
Single<ConstStabilityIndirectParser>,
109110
Single<DeprecationParser>,
110111
Single<InlineParser>,
112+
Single<OptimizeParser>,
111113
Single<RustcForceInlineParser>,
112114
Single<TransparencyParser>,
113115
// tidy-alphabetical-end
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
codegen_gcc_unknown_ctarget_feature_prefix =
2-
unknown feature specified for `-Ctarget-feature`: `{$feature}`
3-
.note = features must begin with a `+` to enable or `-` to disable it
4-
51
codegen_gcc_unwinding_inline_asm =
62
GCC backend does not support unwinding from inline asm
73
@@ -16,15 +12,3 @@ codegen_gcc_lto_disallowed = lto can only be run for executables, cdylibs and st
1612
codegen_gcc_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdylib-lto`
1713
1814
codegen_gcc_lto_bitcode_from_rlib = failed to get bitcode from object file for LTO ({$gcc_err})
19-
20-
codegen_gcc_unknown_ctarget_feature =
21-
unknown and unstable feature specified for `-Ctarget-feature`: `{$feature}`
22-
.note = it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
23-
.possible_feature = you might have meant: `{$rust_feature}`
24-
.consider_filing_feature_request = consider filing a feature request
25-
26-
codegen_gcc_missing_features =
27-
add the missing features in a `target_feature` attribute
28-
29-
codegen_gcc_target_feature_disable_or_enable =
30-
the target features {$features} must all be either enabled or disabled together

compiler/rustc_codegen_gcc/src/errors.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
1-
use rustc_macros::{Diagnostic, Subdiagnostic};
1+
use rustc_macros::Diagnostic;
22
use rustc_span::Span;
33

4-
#[derive(Diagnostic)]
5-
#[diag(codegen_gcc_unknown_ctarget_feature_prefix)]
6-
#[note]
7-
pub(crate) struct UnknownCTargetFeaturePrefix<'a> {
8-
pub feature: &'a str,
9-
}
10-
11-
#[derive(Diagnostic)]
12-
#[diag(codegen_gcc_unknown_ctarget_feature)]
13-
#[note]
14-
pub(crate) struct UnknownCTargetFeature<'a> {
15-
pub feature: &'a str,
16-
#[subdiagnostic]
17-
pub rust_feature: PossibleFeature<'a>,
18-
}
19-
20-
#[derive(Subdiagnostic)]
21-
pub(crate) enum PossibleFeature<'a> {
22-
#[help(codegen_gcc_possible_feature)]
23-
Some { rust_feature: &'a str },
24-
#[help(codegen_gcc_consider_filing_feature_request)]
25-
None,
26-
}
27-
284
#[derive(Diagnostic)]
295
#[diag(codegen_gcc_unwinding_inline_asm)]
306
pub(crate) struct UnwindingInlineAsm {

compiler/rustc_codegen_gcc/src/gcc_util.rs

Lines changed: 23 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
#[cfg(feature = "master")]
22
use gccjit::Context;
3-
use rustc_codegen_ssa::codegen_attrs::check_tied_features;
4-
use rustc_codegen_ssa::errors::TargetFeatureDisableOrEnable;
5-
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::unord::UnordSet;
3+
use rustc_codegen_ssa::target_features;
74
use rustc_session::Session;
8-
use rustc_session::features::{StabilityExt, retpoline_features_by_flags};
9-
use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES;
105
use smallvec::{SmallVec, smallvec};
116

12-
use crate::errors::{PossibleFeature, UnknownCTargetFeature, UnknownCTargetFeaturePrefix};
13-
14-
fn gcc_features_by_flags(sess: &Session) -> Vec<&str> {
15-
let mut features: Vec<&str> = Vec::new();
16-
retpoline_features_by_flags(sess, &mut features);
17-
features
7+
fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
8+
target_features::retpoline_features_by_flags(sess, features);
9+
// FIXME: LLVM also sets +reserve-x18 here under some conditions.
1810
}
1911

2012
/// The list of GCC features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
@@ -44,98 +36,29 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
4436
features.extend(sess.target.features.split(',').filter(|v| !v.is_empty()).map(String::from));
4537

4638
// -Ctarget-features
47-
let known_features = sess.target.rust_target_features();
48-
let mut featsmap = FxHashMap::default();
49-
50-
// Compute implied features
51-
let mut all_rust_features = vec![];
52-
for feature in sess.opts.cg.target_feature.split(',').chain(gcc_features_by_flags(sess)) {
53-
if let Some(feature) = feature.strip_prefix('+') {
54-
all_rust_features.extend(
55-
UnordSet::from(sess.target.implied_target_features(feature))
56-
.to_sorted_stable_ord()
57-
.iter()
58-
.map(|&&s| (true, s)),
59-
)
60-
} else if let Some(feature) = feature.strip_prefix('-') {
61-
// FIXME: Why do we not remove implied features on "-" here?
62-
// We do the equivalent above in `target_config`.
63-
// See <https://github.com/rust-lang/rust/issues/134792>.
64-
all_rust_features.push((false, feature));
65-
} else if !feature.is_empty() && diagnostics {
66-
sess.dcx().emit_warn(UnknownCTargetFeaturePrefix { feature });
67-
}
68-
}
69-
// Remove features that are meant for rustc, not codegen.
70-
all_rust_features.retain(|&(_, feature)| {
71-
// Retain if it is not a rustc feature
72-
!RUSTC_SPECIFIC_FEATURES.contains(&feature)
73-
});
74-
75-
// Check feature validity.
76-
if diagnostics {
77-
for &(enable, feature) in &all_rust_features {
78-
let feature_state = known_features.iter().find(|&&(v, _, _)| v == feature);
79-
match feature_state {
80-
None => {
81-
let rust_feature = known_features.iter().find_map(|&(rust_feature, _, _)| {
82-
let gcc_features = to_gcc_features(sess, rust_feature);
83-
if gcc_features.contains(&feature) && !gcc_features.contains(&rust_feature)
84-
{
85-
Some(rust_feature)
86-
} else {
87-
None
88-
}
89-
});
90-
let unknown_feature = if let Some(rust_feature) = rust_feature {
91-
UnknownCTargetFeature {
92-
feature,
93-
rust_feature: PossibleFeature::Some { rust_feature },
94-
}
95-
} else {
96-
UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
97-
};
98-
sess.dcx().emit_warn(unknown_feature);
99-
}
100-
Some(&(_, stability, _)) => {
101-
stability.verify_feature_enabled_by_flag(sess, enable, feature);
102-
}
103-
}
104-
105-
// FIXME(nagisa): figure out how to not allocate a full hashset here.
106-
featsmap.insert(feature, enable);
107-
}
108-
}
109-
110-
// Translate this into GCC features.
111-
let feats =
112-
all_rust_features.iter().flat_map(|&(enable, feature)| {
113-
let enable_disable = if enable { '+' } else { '-' };
39+
target_features::flag_to_backend_features(
40+
sess,
41+
diagnostics,
42+
|feature| to_gcc_features(sess, feature),
43+
|feature, enable| {
11444
// We run through `to_gcc_features` when
11545
// passing requests down to GCC. This means that all in-language
11646
// features also work on the command line instead of having two
11747
// different names when the GCC name and the Rust name differ.
118-
to_gcc_features(sess, feature)
119-
.iter()
120-
.flat_map(|feat| to_gcc_features(sess, feat).into_iter())
121-
.map(|feature| {
122-
if enable_disable == '-' {
123-
format!("-{}", feature)
124-
} else {
125-
feature.to_string()
126-
}
127-
})
128-
.collect::<Vec<_>>()
129-
});
130-
features.extend(feats);
131-
132-
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
133-
sess.dcx().emit_err(TargetFeatureDisableOrEnable {
134-
features: f,
135-
span: None,
136-
missing_features: None,
137-
});
138-
}
48+
features.extend(
49+
to_gcc_features(sess, feature)
50+
.iter()
51+
.flat_map(|feat| to_gcc_features(sess, feat).into_iter())
52+
.map(
53+
|feature| {
54+
if !enable { format!("-{}", feature) } else { feature.to_string() }
55+
},
56+
),
57+
);
58+
},
59+
);
60+
61+
gcc_features_by_flags(sess, &mut features);
13962

14063
features
14164
}

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ use rustc_codegen_ssa::back::write::{
102102
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
103103
};
104104
use rustc_codegen_ssa::base::codegen_crate;
105+
use rustc_codegen_ssa::target_features::cfg_target_feature;
105106
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
106107
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
107108
use rustc_data_structures::fx::FxIndexMap;
@@ -476,42 +477,21 @@ fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
476477

477478
/// Returns the features that should be set in `cfg(target_feature)`.
478479
fn target_config(sess: &Session, target_info: &LockedTargetInfo) -> TargetConfig {
479-
// TODO(antoyo): use global_gcc_features.
480-
let f = |allow_unstable| {
481-
sess.target
482-
.rust_target_features()
483-
.iter()
484-
.filter_map(|&(feature, gate, _)| {
485-
if allow_unstable
486-
|| (gate.in_cfg()
487-
&& (sess.is_nightly_build() || gate.requires_nightly().is_none()))
488-
{
489-
Some(feature)
490-
} else {
491-
None
492-
}
493-
})
494-
.filter(|feature| {
495-
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
496-
if *feature == "neon" {
497-
return false;
498-
}
499-
target_info.cpu_supports(feature)
500-
// cSpell:disable
501-
/*
502-
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
503-
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
504-
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
505-
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
506-
*/
507-
// cSpell:enable
508-
})
509-
.map(Symbol::intern)
510-
.collect()
511-
};
512-
513-
let target_features = f(false);
514-
let unstable_target_features = f(true);
480+
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
481+
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
482+
if feature == "neon" {
483+
return false;
484+
}
485+
target_info.cpu_supports(feature)
486+
// cSpell:disable
487+
/*
488+
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
489+
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
490+
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
491+
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
492+
*/
493+
// cSpell:enable
494+
});
515495

516496
let has_reliable_f16 = target_info.supports_target_dependent_type(CType::Float16);
517497
let has_reliable_f128 = target_info.supports_target_dependent_type(CType::Float128);

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ codegen_llvm_symbol_already_defined =
5959
codegen_llvm_target_machine = could not create LLVM TargetMachine for triple: {$triple}
6060
codegen_llvm_target_machine_with_llvm_err = could not create LLVM TargetMachine for triple: {$triple}: {$llvm_err}
6161
62-
codegen_llvm_unknown_ctarget_feature =
63-
unknown and unstable feature specified for `-Ctarget-feature`: `{$feature}`
64-
.note = it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
65-
.possible_feature = you might have meant: `{$rust_feature}`
66-
.consider_filing_feature_request = consider filing a feature request
67-
68-
codegen_llvm_unknown_ctarget_feature_prefix =
69-
unknown feature specified for `-Ctarget-feature`: `{$feature}`
70-
.note = features must begin with a `+` to enable or `-` to disable it
71-
7262
codegen_llvm_unknown_debuginfo_compression = unknown debuginfo compression algorithm {$algorithm} - will fall back to uncompressed debuginfo
7363
7464
codegen_llvm_write_bytecode = failed to write bytecode to {$path}: {$err}

0 commit comments

Comments
 (0)