Skip to content

Commit c6a43f6

Browse files
Rollup merge of #159795 - dingxiangfei2009:coro-layout-flag, r=tmandry
Introduce `-Z pack-coroutine-layout` flag This is still mostly cosmetic, but more stuff is coming over from #135527. Only the tip of the branch matters, the other commit will be shipped from another PR. It will go away after a rebase, hopefully. r? @cjgillot
2 parents 1e251c2 + f499038 commit c6a43f6

14 files changed

Lines changed: 96 additions & 9 deletions

File tree

‎compiler/rustc_abi/src/layout.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::num::NonZero;
44
use std::ops::Deref;
55
use std::range::{RangeFrom, RangeToInclusive};
66

7+
pub use coroutine::PackCoroutineLayout;
78
use rustc_hashes::Hash64;
89
use rustc_index::Idx;
910
use rustc_index::bit_set::BitMatrix;

‎compiler/rustc_abi/src/layout/coroutine.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ use crate::{
3030
StructKind, TagEncoding, VariantLayout, Variants, WrappingRange,
3131
};
3232

33+
/// This option controls how coroutine saved locals are packed
34+
/// into the coroutine state data
35+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
36+
#[cfg_attr(
37+
feature = "nightly",
38+
derive(
39+
rustc_macros::Encodable_NoContext,
40+
rustc_macros::Decodable_NoContext,
41+
rustc_macros::StableHash,
42+
)
43+
)]
44+
pub enum PackCoroutineLayout {
45+
/// The classic layout where captures are always promoted to coroutine state prefix
46+
#[default]
47+
Classic,
48+
/// Captures are first saved into the `UNRESUMED` state and promoted
49+
/// when they are used across more than one suspension
50+
CapturesOnly,
51+
}
52+
3353
/// Overlap eligibility and variant assignment for each CoroutineSavedLocal.
3454
#[derive(Clone, Debug, PartialEq)]
3555
enum SavedLocalEligibility<VariantIdx, FieldIdx> {

‎compiler/rustc_abi/src/lib.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ pub use canon_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
7272
#[cfg(feature = "nightly")]
7373
pub use extern_abi::CVariadicStatus;
7474
pub use extern_abi::{ExternAbi, all_names};
75-
pub use layout::{FIRST_VARIANT, FieldIdx, LayoutCalculator, LayoutCalculatorError, VariantIdx};
75+
pub use layout::{
76+
FIRST_VARIANT, FieldIdx, LayoutCalculator, LayoutCalculatorError, PackCoroutineLayout,
77+
VariantIdx,
78+
};
7679
#[cfg(feature = "nightly")]
7780
pub use layout::{Layout, TyAbiInterface, TyAndLayout};
7881
pub use wrapping_range::WrappingRange;

‎compiler/rustc_middle/src/mir/pretty.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,9 @@ fn write_coroutine_layout<'tcx>(
573573
w: &mut dyn io::Write,
574574
options: PrettyPrintMirOptions,
575575
) -> io::Result<()> {
576-
let CoroutineLayout { field_tys, variant_fields, variant_source_info, storage_conflicts } =
577-
layout;
576+
let CoroutineLayout {
577+
field_tys, variant_fields, variant_source_info, storage_conflicts, ..
578+
} = layout;
578579

579580
writeln!(w, "{INDENT}coroutine layout {{")?;
580581

‎compiler/rustc_middle/src/mir/query.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt::{self, Debug};
44

5-
use rustc_abi::{FieldIdx, VariantIdx};
5+
use rustc_abi::{FieldIdx, PackCoroutineLayout, VariantIdx};
66
use rustc_errors::ErrorGuaranteed;
77
use rustc_index::IndexVec;
88
use rustc_index::bit_set::BitMatrix;
@@ -53,6 +53,11 @@ pub struct CoroutineLayout<'tcx> {
5353
#[type_foldable(identity)]
5454
#[type_visitable(ignore)]
5555
pub storage_conflicts: BitMatrix<CoroutineSavedLocal, CoroutineSavedLocal>,
56+
57+
/// Coroutine layout packing
58+
#[type_foldable(identity)]
59+
#[type_visitable(ignore)]
60+
pub pack: PackCoroutineLayout,
5661
}
5762

5863
impl Debug for CoroutineLayout<'_> {

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,8 @@ impl<'tcx> TyCtxt<'tcx> {
20612061
variant_fields,
20622062
variant_source_info,
20632063
storage_conflicts: BitMatrix::new(0, 0),
2064+
// FIXME(-Zpack-coroutine-layout): Stop ignoring the flag here.
2065+
pack: rustc_abi::PackCoroutineLayout::Classic,
20642066
};
20652067
return Ok(self.arena.alloc(proxy_layout));
20662068
} else {

‎compiler/rustc_mir_transform/src/coroutine/layout.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use std::ops;
2424

2525
use itertools::izip;
26-
use rustc_abi::{FieldIdx, VariantIdx};
26+
use rustc_abi::{FieldIdx, PackCoroutineLayout, VariantIdx};
2727
use rustc_attr_ir::find_attr;
2828
use rustc_attr_ir::lang_items::LangItem;
2929
use rustc_data_structures::fx::FxHashSet;
@@ -443,8 +443,14 @@ pub(super) fn compute_layout<'tcx>(
443443
tys[saved_local].debuginfo_name.get_or_insert(var.name);
444444
}
445445

446-
let layout =
447-
CoroutineLayout { field_tys: tys, variant_fields, variant_source_info, storage_conflicts };
446+
let layout = CoroutineLayout {
447+
field_tys: tys,
448+
variant_fields,
449+
variant_source_info,
450+
storage_conflicts,
451+
// FIXME(-Zpack-coroutine-layout): Stop ignoring the flag here.
452+
pack: PackCoroutineLayout::Classic,
453+
};
448454
debug!(?remap);
449455
debug!(?layout);
450456
debug!(?storage_liveness);

‎compiler/rustc_session/src/config.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3504,7 +3504,7 @@ pub(crate) mod dep_tracking {
35043504
use std::num::NonZero;
35053505
use std::path::PathBuf;
35063506

3507-
use rustc_abi::Align;
3507+
use rustc_abi::{Align, PackCoroutineLayout};
35083508
use rustc_ast::attr::version::RustcVersion;
35093509
use rustc_data_structures::fx::FxIndexMap;
35103510
use rustc_data_structures::stable_hash::{DummyStableHashCtxt, StableHash, StableHasher};
@@ -3610,6 +3610,7 @@ pub(crate) mod dep_tracking {
36103610
OptLevel,
36113611
OutFileName,
36123612
OutputType,
3613+
PackCoroutineLayout,
36133614
PanicStrategy,
36143615
PatchableFunctionEntry,
36153616
PointerAuthOption,

‎compiler/rustc_session/src/diagnostics.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ pub(crate) struct ThinLtoNotSupportedByBackend;
721721
#[diag("`-Zpacked-stack` is only supported on s390x")]
722722
pub(crate) struct UnsupportedPackedStack;
723723

724+
#[derive(Diagnostic)]
725+
#[diag("`-Z pack-coroutine-layout=captures-only` is not yet implemented and will be ignored")]
726+
pub(crate) struct PackCoroutineLayoutNotImplemented;
727+
724728
#[derive(Diagnostic)]
725729
#[diag("`-Ctarget-cpu=native` is not allowed for target `{$target_triple}`")]
726730
#[note("this target requires consistent `-Ctarget-cpu` values across all crates")]

‎compiler/rustc_session/src/options.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::num::{IntErrorKind, NonZero};
33
use std::path::PathBuf;
44
use std::str;
55

6-
use rustc_abi::Align;
6+
use rustc_abi::{Align, PackCoroutineLayout};
77
use rustc_ast::attr::version::RustcVersion;
88
use rustc_data_structures::profiling::TimePassesFormat;
99
use rustc_data_structures::stable_hash::StableHasher;
@@ -916,6 +916,7 @@ mod desc {
916916
pub(crate) const parse_panic_strategy: &str = "either `unwind`, `abort`, or `immediate-abort`";
917917
pub(crate) const parse_on_broken_pipe: &str = "either `kill`, `error`, or `inherit`";
918918
pub(crate) const parse_patchable_function_entry: &str = "a comma separated list of (prefix_nops,total_nops,section_name), (prefix_nops,total_nops), or (total_nops). Where prefix_nops <= total_nops where 0 < total_nops <= 255 and prefix_nops <= total_nops";
919+
pub(crate) const parse_pack_coroutine_layout: &str = "either `classic` or `captures-only`";
919920
pub(crate) const parse_opt_panic_strategy: &str = parse_panic_strategy;
920921
pub(crate) const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
921922
pub(crate) const parse_sanitizers: &str = "comma separated list of sanitizers: `address`, `cfi`, `dataflow`, `hwaddress`, `kcfi`, `kernel-address`, `kernel-hwaddress`, `leak`, `memory`, `memtag`, `safestack`, `shadow-call-stack`, `thread`, or 'realtime'";
@@ -2177,6 +2178,18 @@ pub mod parse {
21772178
true
21782179
}
21792180

2181+
pub(crate) fn parse_pack_coroutine_layout(
2182+
slot: &mut PackCoroutineLayout,
2183+
v: Option<&str>,
2184+
) -> bool {
2185+
*slot = match v {
2186+
Some("classic") => PackCoroutineLayout::Classic,
2187+
Some("captures-only") => PackCoroutineLayout::CapturesOnly,
2188+
_ => return false,
2189+
};
2190+
true
2191+
}
2192+
21802193
pub(crate) fn parse_inlining_threshold(slot: &mut InliningThreshold, v: Option<&str>) -> bool {
21812194
match v {
21822195
Some("always" | "yes") => {
@@ -2822,6 +2835,8 @@ options! {
28222835
"behavior of std::io::ErrorKind::BrokenPipe (SIGPIPE)"),
28232836
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
28242837
"pass `-install_name @rpath/...` to the macOS linker (default: no)"),
2838+
pack_coroutine_layout: PackCoroutineLayout = (PackCoroutineLayout::default(), parse_pack_coroutine_layout, [TRACKED],
2839+
"set strategy to pack coroutine state layout (default: classic)"),
28252840
packed_bundled_libs: bool = (false, parse_bool, [TRACKED],
28262841
"change rlib format to store native libraries as archives"),
28272842
packed_stack: bool = (false, parse_bool, [TRACKED],

0 commit comments

Comments
 (0)