Skip to content

Commit 4ccc3c0

Browse files
committed
rustdoc: Output target feature information
`#[target_feature]` attributes refer to a target-specific list of features. Enabling certain features can imply enabling other features. Certain features are always enabled on certain targets, since they are required by the target's ABI. Features can also be enabled indirectly based on other compiler flags. Feature information is ultimately known to `rustc`. Rather than force external tools to track it -- which may be wildly impractical due to `-C target-cpu` -- have `rustdoc` output `rustc`'s feature data.
1 parent 5337252 commit 4ccc3c0

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

src/librustdoc/json/mod.rs

+53
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::io::{BufWriter, Write, stdout};
1414
use std::path::PathBuf;
1515
use std::rc::Rc;
1616

17+
use rustc_data_structures::fx::FxHashSet;
1718
use rustc_hir::def_id::{DefId, DefIdSet};
1819
use rustc_middle::ty::TyCtxt;
1920
use rustc_session::Session;
@@ -123,6 +124,57 @@ impl<'tcx> JsonRenderer<'tcx> {
123124
}
124125
}
125126

127+
fn target(sess: &rustc_session::Session) -> types::Target {
128+
let globally_enabled_features: FxHashSet<&str> =
129+
sess.unstable_target_features.iter().map(|name| name.as_str()).collect();
130+
131+
// Build a map of target feature stability by feature name
132+
use rustc_target::target_features::Stability;
133+
let feature_stability: FxHashMap<&str, Stability> = sess
134+
.target
135+
.rust_target_features()
136+
.into_iter()
137+
.copied()
138+
.map(|(name, stability, _)| (name, stability))
139+
.collect();
140+
141+
types::Target {
142+
triple: sess.opts.target_triple.tuple().into(),
143+
target_features: sess
144+
.target
145+
.rust_target_features()
146+
.into_iter()
147+
.copied()
148+
.filter(|(_, stability, _)| {
149+
// Describe only target features which the user can toggle
150+
stability.toggle_allowed().is_ok()
151+
})
152+
.map(|(name, stability, implied_features)| {
153+
types::TargetFeature {
154+
name: name.into(),
155+
unstable_feature_gate: match stability {
156+
Stability::Unstable(feature_gate) => Some(feature_gate.as_str().into()),
157+
_ => None,
158+
},
159+
implies_features: implied_features
160+
.into_iter()
161+
.copied()
162+
.filter(|name| {
163+
// Imply only target features which the user can toggle
164+
feature_stability
165+
.get(name)
166+
.map(|stability| stability.toggle_allowed().is_ok())
167+
.unwrap_or(false)
168+
})
169+
.map(String::from)
170+
.collect(),
171+
globally_enabled: globally_enabled_features.contains(name),
172+
}
173+
})
174+
.collect(),
175+
}
176+
}
177+
126178
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
127179
fn descr() -> &'static str {
128180
"json"
@@ -288,6 +340,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
288340
)
289341
})
290342
.collect(),
343+
target: target(self.tcx.sess),
291344
format_version: types::FORMAT_VERSION,
292345
};
293346
if let Some(ref out_dir) = self.out_dir {

src/rustdoc-json-types/lib.rs

+57-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3030
/// This integer is incremented with every breaking change to the API,
3131
/// and is returned along with the JSON blob as [`Crate::format_version`].
3232
/// Consuming code should assert that this value matches the format version(s) that it supports.
33-
pub const FORMAT_VERSION: u32 = 43;
33+
pub const FORMAT_VERSION: u32 = 44;
3434

3535
/// The root of the emitted JSON blob.
3636
///
@@ -52,11 +52,67 @@ pub struct Crate {
5252
pub paths: HashMap<Id, ItemSummary>,
5353
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
5454
pub external_crates: HashMap<u32, ExternalCrate>,
55+
/// Information about the target for which this documentation was generated
56+
pub target: Target,
5557
/// A single version number to be used in the future when making backwards incompatible changes
5658
/// to the JSON output.
5759
pub format_version: u32,
5860
}
5961

62+
/// Information about a target
63+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
64+
pub struct Target {
65+
/// The target triple for which this documentation was generated
66+
pub triple: String,
67+
/// A list of `#[target_feature]` which exist for this target, along with their status in this
68+
/// compiler session
69+
pub target_features: Vec<TargetFeature>,
70+
}
71+
72+
/// Information about a target feature.
73+
///
74+
/// Rust target features are used to influence code generation, especially around selecting
75+
/// instructions which are not universally supported by the target architecture.
76+
///
77+
/// Target features are commonly enabled by the [`#[target_feature]` attribute][1] to influence code
78+
/// generation for a particular function, and less commonly enabled by compiler options like
79+
/// `-Ctarget-feature` or `-Ctarget-cpu`. Targets themselves automatically enable certain target
80+
/// features by default, for example because the target's ABI specification requires saving specific
81+
/// registers which only exist in an architectural extension.
82+
///
83+
/// Target features can imply other target features: for example, x86-64 `avx2` implies `avx`, and
84+
/// aarch64 `sve2` implies `sve`, since both of these architectural extensions depend on their
85+
/// predecessors.
86+
///
87+
/// Target features can be probed at compile time by [`#[cfg(target_feature)]`][2] or `cfg!(…)`
88+
/// conditional compilation to determine whether a target feature is enabled in a particular
89+
/// context.
90+
///
91+
/// [1]: https://doc.rust-lang.org/stable/reference/attributes/codegen.html#the-target_feature-attribute
92+
/// [2]: https://doc.rust-lang.org/reference/conditional-compilation.html#target_feature
93+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
94+
pub struct TargetFeature {
95+
/// The name of this target feature.
96+
pub name: String,
97+
/// Other target features which are implied by this target feature, if any.
98+
pub implies_features: Vec<String>,
99+
/// If this target feature is unstable, the name of the associated language feature gate.
100+
pub unstable_feature_gate: Option<String>,
101+
/// Whether this feature is globally enabled for this compilation session.
102+
///
103+
/// Target features can be globally enabled implicitly as a result of the target's definition.
104+
/// For example, x86-64 hardware floating point ABIs require saving x87 and SSE2 registers,
105+
/// which in turn requires globally enabling the `x87` and `sse2` target features so that the
106+
/// generated machine code conforms to the target's ABI.
107+
///
108+
/// Target features can also be globally enabled explicitly as a result of compiler flags like
109+
/// [`-Ctarget-feature`][1] or [`-Ctarget-cpu`][2].
110+
///
111+
/// [1]: https://doc.rust-lang.org/beta/rustc/codegen-options/index.html#target-feature
112+
/// [2]: https://doc.rust-lang.org/beta/rustc/codegen-options/index.html#target-cpu
113+
pub globally_enabled: bool,
114+
}
115+
60116
/// Metadata of a crate, either the same crate on which `rustdoc` was invoked, or its dependency.
61117
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
62118
pub struct ExternalCrate {

src/tools/jsondoclint/src/validator/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn errors_on_missing_links() {
4242
)]),
4343
paths: FxHashMap::default(),
4444
external_crates: FxHashMap::default(),
45+
target: rustdoc_json_types::Target { target_features: vec![] },
4546
format_version: rustdoc_json_types::FORMAT_VERSION,
4647
};
4748

@@ -112,6 +113,7 @@ fn errors_on_local_in_paths_and_not_index() {
112113
},
113114
)]),
114115
external_crates: FxHashMap::default(),
116+
target: rustdoc_json_types::Target { target_features: vec![] },
115117
format_version: rustdoc_json_types::FORMAT_VERSION,
116118
};
117119

@@ -216,6 +218,7 @@ fn errors_on_missing_path() {
216218
ItemSummary { crate_id: 0, path: vec!["foo".to_owned()], kind: ItemKind::Module },
217219
)]),
218220
external_crates: FxHashMap::default(),
221+
target: rustdoc_json_types::Target { target_features: vec![] },
219222
format_version: rustdoc_json_types::FORMAT_VERSION,
220223
};
221224

@@ -259,6 +262,7 @@ fn checks_local_crate_id_is_correct() {
259262
)]),
260263
paths: FxHashMap::default(),
261264
external_crates: FxHashMap::default(),
265+
target: rustdoc_json_types::Target { target_features: vec![] },
262266
format_version: FORMAT_VERSION,
263267
};
264268
check(&krate, &[]);

0 commit comments

Comments
 (0)