Skip to content

Commit 8eb778d

Browse files
committed
Auto merge of #165876 - JonathanBrouwer:rollup-E1cRyMW, r=JonathanBrouwer
Rollup of 7 pull requests Successful merges: - #161922 (Tie extern "llvm-intrinsics" and llvm.* symbol names together) - #165854 (Explicitly drop job server helper thread) - #164077 (replace `TypingEnv::post_analysis()` with `mir::Body::typing_env()`) - #165305 (hexagon: pass small aggregates on the stack when registers run out) - #165417 (Mention width limit panic in fmt doc) - #165860 (Fix inverted wasm cfg in core benchmarks) - #165871 (Cleanup: Remove `name` from `AttributeExt`)
2 parents 29a5d06 + 397c1fb commit 8eb778d

30 files changed

Lines changed: 334 additions & 134 deletions

File tree

‎compiler/rustc_ast/src/attr/mod.rs‎

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,6 @@ impl Attribute {
8080
}
8181

8282
impl AttributeExt for Attribute {
83-
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
84-
fn name(&self) -> Option<Symbol> {
85-
use SyntheticAttr::*;
86-
match &self.kind {
87-
AttrKind::Normal(normal) => normal.item.name(),
88-
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace(_)) => None,
89-
AttrKind::DocComment(..) => None,
90-
AttrKind::Generated(generated) => match generated.path() {
91-
[name] => Some(*name),
92-
_ => None,
93-
},
94-
}
95-
}
96-
9783
fn path_matches(&self, name: &[Symbol]) -> bool {
9884
match &self.kind {
9985
AttrKind::Normal(normal) => {
@@ -837,10 +823,6 @@ impl MetaItemLit {
837823
}
838824

839825
pub trait AttributeExt: Debug + 'static {
840-
/// For a single-segment attribute (i.e., `#[attr]` and not `#[path::atrr]`),
841-
/// return the name of the attribute; otherwise, returns `None`.
842-
fn name(&self) -> Option<Symbol>;
843-
844826
/// Gets the value literal, as string, when using `#[attr = value]`
845827
fn value_str(&self) -> Option<Symbol>;
846828

@@ -866,13 +848,7 @@ pub trait AttributeExt: Debug + 'static {
866848
fn doc_resolution_scope(&self) -> Option<AttrStyle>;
867849
}
868850

869-
// FIXME(fn_delegation): use function delegation instead of manually forwarding
870-
871851
impl Attribute {
872-
pub fn name(&self) -> Option<Symbol> {
873-
AttributeExt::name(self)
874-
}
875-
876852
pub fn value_str(&self) -> Option<Symbol> {
877853
AttributeExt::value_str(self)
878854
}
@@ -904,6 +880,20 @@ impl Attribute {
904880
}
905881
}
906882

883+
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
884+
pub fn name(&self) -> Option<Symbol> {
885+
use SyntheticAttr::*;
886+
match &self.kind {
887+
AttrKind::Normal(normal) => normal.item.name(),
888+
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace(_)) => None,
889+
AttrKind::DocComment(..) => None,
890+
AttrKind::Generated(generated) => match generated.path() {
891+
[name] => Some(*name),
892+
_ => None,
893+
},
894+
}
895+
}
896+
907897
/// Returns true if the attribute's first *and only* path segment is equal to the passed-in
908898
/// symbol.
909899
#[inline]

‎compiler/rustc_attr_ir/src/attr.rs‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,6 @@ impl AttributeExt for Attribute {
151151
self.value_lit().and_then(|x| x.value_as_str())
152152
}
153153

154-
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
155-
#[inline]
156-
fn name(&self) -> Option<Symbol> {
157-
match &self {
158-
Attribute::DeriveHelper(n) => {
159-
if let [ident] = n.path.segments.as_ref() {
160-
Some(*ident)
161-
} else {
162-
None
163-
}
164-
}
165-
Attribute::Parsed(..) => None,
166-
// Tools are never single segment attributes
167-
Attribute::Tool(..) => None,
168-
}
169-
}
170-
171154
#[inline]
172155
fn path_matches(&self, name: &[Symbol]) -> bool {
173156
match &self {

‎compiler/rustc_data_structures/src/jobserver.rs‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub struct Proxy {
9393
/// The wrapped jobserver client.
9494
client: Client,
9595
/// Helper thread associated with the wrapped client.
96-
helper: OnceLock<HelperThread>,
96+
helper: Mutex<Option<HelperThread>>,
9797
/// The proxy's own data.
9898
data: Mutex<ProxyData>,
9999
/// Threads which are currently waiting for a token will wait on this condvar.
@@ -107,28 +107,28 @@ impl Proxy {
107107
// Assume that the main thread is actively doing work when it creates the proxy.
108108
data: Mutex::new(ProxyData { used: 1, pending: 0 }),
109109
wake_pending: Condvar::new(),
110-
helper: OnceLock::new(),
110+
helper: Default::default(),
111111
});
112-
// Use weak reference as if we will clone `Arc` then `Proxy` will never be
113-
// dropped as helper thread will have a strong reference to it, this may
114-
// result in not all tokens returned to the job server, see #165506.
115-
let weak_proxy = Arc::downgrade(&proxy);
112+
113+
// The helper thread is dropped explicitly (see `drop_helper_thread` below).
114+
// If we make this a weak reference then we still can encounter a situation
115+
// when token is not returned, as the last strong reference may be held by this
116+
// thread meaning it may not be dropped before rustc exits (see #165854).
117+
let proxy_ = Arc::clone(&proxy);
116118
let helper = proxy
117119
.client
118120
.clone()
119121
.into_helper_thread(move |token| {
120122
// Reminder: this callback runs when the helper acquires a token.
121-
if let Ok(token) = token
122-
&& let Some(proxy) = weak_proxy.upgrade()
123-
{
124-
let mut data = proxy.data.lock();
123+
if let Ok(token) = token {
124+
let mut data = proxy_.data.lock();
125125
if data.pending > 0 {
126126
// The token is still needed, give it to one of the waiting threads.
127127
token.drop_without_releasing();
128128
assert!(data.used > 0);
129129
data.used += 1;
130130
data.pending -= 1;
131-
proxy.wake_pending.notify_one();
131+
proxy_.wake_pending.notify_one();
132132
} else {
133133
// The token is no longer needed, release it by dropping.
134134
drop(data);
@@ -137,7 +137,7 @@ impl Proxy {
137137
}
138138
})
139139
.expect("failed to spawn helper thread");
140-
proxy.helper.set(helper).unwrap();
140+
*proxy.helper.lock() = Some(helper);
141141
proxy
142142
}
143143

@@ -154,7 +154,7 @@ impl Proxy {
154154
} else {
155155
// Request a token from the helper thread, this is a non-blocking operation.
156156
// Then wait until this or some other request succeeds.
157-
self.helper.get().unwrap().request_token();
157+
self.helper.lock().as_ref().unwrap().request_token();
158158
data.pending += 1;
159159
self.wake_pending.wait(&mut data);
160160
}
@@ -179,4 +179,11 @@ impl Proxy {
179179
}
180180
}
181181
}
182+
183+
/// Explicitly drops helper thread making sure that it is dropped before
184+
/// `rustc` exits and thus all tokens are returned to the job server (see #165854).
185+
pub fn drop_helper_thread(&self) {
186+
let helper_thread = self.helper.lock().take();
187+
drop(helper_thread);
188+
}
182189
}

‎compiler/rustc_interface/src/util.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
270270
let current_gcx2 = current_gcx.clone();
271271

272272
let proxy = Proxy::new();
273+
274+
// Explicitly drop and join helper thread before rustc exits,
275+
// so all job server tokens are returned.
276+
let _drop_helper_thread = defer({
277+
let proxy = Arc::clone(&proxy);
278+
move || proxy.drop_helper_thread()
279+
});
280+
273281
let proxy_ = Arc::clone(&proxy);
274282

275283
let builder = rustc_thread_pool::ThreadPoolBuilder::new()

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -414,37 +414,32 @@ impl<'tcx> Body<'tcx> {
414414
pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
415415
if tcx.use_typing_mode_post_typeck_through_borrowck() {
416416
match self.phase {
417-
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial)
418-
if let Some(def_id) = self.source.def_id().as_local() =>
419-
{
417+
// `analysis_cleanup_passes` should use `PostBorrowck`, but doing so would
418+
// complicate the code, and none of those passes seems to care.
419+
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => {
420+
let local_def_id = self
421+
.source
422+
.def_id()
423+
.as_local()
424+
.expect("We only build MIR for local bodies");
420425
TypingEnv::new(
421426
tcx.param_env(self.source.def_id()),
422-
ty::TypingMode::borrowck(tcx, def_id),
427+
ty::TypingMode::borrowck(tcx, local_def_id),
423428
)
424429
}
425-
MirPhase::Analysis(AnalysisPhase::PostCleanup)
426-
if let Some(def_id) = self.source.def_id().as_local() =>
427-
{
428-
TypingEnv::new(
429-
tcx.param_env(self.source.def_id()),
430-
ty::TypingMode::post_borrowck_analysis(tcx, def_id),
431-
)
432-
}
433-
MirPhase::Built | MirPhase::Analysis(_) => {
434-
// This branch happens for drop glue and fn ptr shims.
435-
// FIXME: why do we do any of this analysis on drop glue etc?
436-
// This should ideally all be skipped.
430+
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
437431
TypingEnv::post_analysis(tcx, self.source.def_id())
438432
}
439-
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
440433
}
441434
} else {
442435
match self.phase {
443-
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
436+
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => TypingEnv::new(
444437
tcx.param_env(self.source.def_id()),
445438
ty::TypingMode::non_body_analysis(),
446439
),
447-
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
440+
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
441+
TypingEnv::post_analysis(tcx, self.source.def_id())
442+
}
448443
}
449444
}
450445
}

‎compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::mir::*;
2-
use rustc_middle::ty::{self, TyCtxt};
2+
use rustc_middle::ty::TyCtxt;
33
use tracing::debug;
44

55
use crate::patch::MirPatch;
@@ -41,9 +41,7 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4242
debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span);
4343
let mut patch = MirPatch::new(body);
44-
// FIXME(#132279): This is used during the phase transition from analysis
45-
// to runtime, so we have to manually specify the correct typing mode.
46-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
44+
let typing_env = body.typing_env(tcx);
4745

4846
for (bb, data) in body.basic_blocks.iter_enumerated() {
4947
let loc = Location { block: bb, statement_index: data.statements.len() };

‎compiler/rustc_mir_transform/src/elaborate_drops.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
5252
#[instrument(level = "trace", skip(self, tcx, body))]
5353
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5454
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
55-
// FIXME(#132279): This is used during the phase transition from analysis
56-
// to runtime, so we have to manually specify the correct typing mode.
57-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
55+
let typing_env = body.typing_env(tcx);
5856
// For types that do not need dropping, the behaviour is trivial. So we only need to track
5957
// init/uninit for types that do need dropping.
6058
let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, typing_env));

‎compiler/rustc_mir_transform/src/known_panics_lint.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ impl<'tcx> ty::layout::HasTypingEnv<'tcx> for ConstPropagator<'_, 'tcx> {
181181
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
182182
fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
183183
let def_id = body.source.def_id();
184-
// FIXME(#132279): This is used during the phase transition from analysis
185-
// to runtime, so we have to manually specify the correct typing mode.
186-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
184+
let typing_env = body.typing_env(tcx);
187185
let can_const_prop = CanConstProp::check(tcx, typing_env, body);
188186
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), typing_env, DummyMachine);
189187

‎compiler/rustc_mir_transform/src/lib.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,17 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
649649
];
650650

651651
pm::run_passes(tcx, body, passes, Some(MirPhase::Analysis(AnalysisPhase::PostCleanup)));
652+
// `PostAnalysisNormalize` is the pass that puts a body into `TypingMode::PostAnalysis`.
653+
// It has to be run here because moving it earlier or later would cause `validate-mir`
654+
// to fail due to mismatched typing envs.
655+
pm::run_passes(tcx, body, &[&post_analysis_normalize::PostAnalysisNormalize], None);
652656
}
653657

654658
/// Returns the sequence of passes that lowers analysis to runtime MIR.
655659
fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
656660
let passes: &[&dyn MirPass<'tcx>] = &[
657661
// These next passes must be executed together.
658662
&add_call_guards::CriticalCallEdges,
659-
// Must be done before drop elaboration because we need to drop opaque types, too.
660-
&post_analysis_normalize::PostAnalysisNormalize,
661663
// Calling this after `PostAnalysisNormalize` ensures that we don't deal with opaque types.
662664
&add_subtyping_projections::Subtyper,
663665
&elaborate_drops::ElaborateDrops,

‎compiler/rustc_mir_transform/src/pass_manager.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::atomic::Ordering;
55
use rustc_attr_ir::OptimizeAttr;
66
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
77
use rustc_hir::def_id::DefId;
8-
use rustc_middle::mir::{Body, MirDumper, MirPhase, RuntimePhase};
8+
use rustc_middle::mir::{AnalysisPhase, Body, MirDumper, MirPhase, RuntimePhase};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::Session;
1111
use rustc_session::config::OptLevel;
@@ -397,9 +397,13 @@ fn run_passes_inner<'tcx>(
397397

398398
dump_mir_for_phase_change(tcx, body);
399399

400-
let validate =
401-
(validate_each & tcx.sess.opts.unstable_opts.validate_mir & !body.should_skip())
402-
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
400+
let validate = (validate_each
401+
&& tcx.sess.opts.unstable_opts.validate_mir
402+
&& !body.should_skip()
403+
// Skip validating the body in `TypingMode::PostAnalysis` because we haven't
404+
// revealed opaques yet.
405+
&& (new_phase != MirPhase::Analysis(AnalysisPhase::PostCleanup)))
406+
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
403407
let lint = tcx.sess.opts.unstable_opts.lint_mir & !body.should_skip();
404408
if validate {
405409
validate_body(tcx, body, format!("after phase change to {}", new_phase.name()));

0 commit comments

Comments
 (0)