Skip to content
Open
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
18 changes: 11 additions & 7 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,9 @@ fn codegen_cgu_content(

fn module_codegen(
tcx: TyCtxt<'_>,
(global_asm_config, cgu_name, token): (
Arc<GlobalAsmConfig>,
rustc_span::Symbol,
ConcurrencyLimiterToken,
),
global_asm_config: Arc<GlobalAsmConfig>,
cgu_name: rustc_span::Symbol,
token: ConcurrencyLimiterToken,
) -> OngoingModuleCodegen {
let mut module = make_module(tcx.sess, cgu_name.as_str().to_string());

Expand Down Expand Up @@ -513,8 +511,14 @@ pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
let (module, _) = tcx.dep_graph.with_task(
dep_node,
tcx,
(global_asm_config.clone(), cgu.name(), concurrency_limiter.acquire(tcx.dcx())),
module_codegen,
|| {
module_codegen(
tcx,
global_asm_config.clone(),
cgu.name(),
concurrency_limiter.acquire(tcx.dcx()),
)
},
Some(rustc_middle::dep_graph::hash_result),
);
IntoDynSyncSend(module)
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_codegen_gcc/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ pub fn compile_codegen_unit(
let (module, _) = tcx.dep_graph.with_task(
dep_node,
tcx,
(cgu_name, target_info, lto_supported),
module_codegen,
|| module_codegen(tcx, cgu_name, target_info, lto_supported),
Some(dep_graph::hash_result),
);
let time_to_codegen = start_time.elapsed();
Expand All @@ -96,7 +95,9 @@ pub fn compile_codegen_unit(

fn module_codegen(
tcx: TyCtxt<'_>,
(cgu_name, target_info, lto_supported): (Symbol, LockedTargetInfo, bool),
cgu_name: Symbol,
target_info: LockedTargetInfo,
lto_supported: bool,
) -> ModuleCodegen<GccContext> {
let cgu = tcx.codegen_unit(cgu_name);
// Instantiate monomorphizations without filling out definitions yet...
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ pub(crate) fn compile_codegen_unit(
let (module, _) = tcx.dep_graph.with_task(
dep_node,
tcx,
cgu_name,
module_codegen,
|| module_codegen(tcx, cgu_name),
Some(dep_graph::hash_result),
);
let time_to_codegen = start_time.elapsed();
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2474,8 +2474,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
tcx.dep_graph.with_task(
dep_node,
tcx,
path,
|tcx, path| {
|| {
with_encode_metadata_header(tcx, path, |ecx| {
// Encode all the entries and extra information in the crate,
// culminating in the `CrateRoot` which points to all of it.
Expand Down
85 changes: 27 additions & 58 deletions compiler/rustc_middle/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum QuerySideEffect {
/// the side effect dep node as a dependency.
CheckFeature { symbol: Symbol },
}

#[derive(Clone)]
pub struct DepGraph {
data: Option<Arc<DepGraphData>>,
Expand Down Expand Up @@ -215,11 +216,8 @@ impl DepGraph {
}
}

pub fn with_ignore<OP, R>(&self, op: OP) -> R
where
OP: FnOnce() -> R,
{
with_deps(TaskDepsRef::Ignore, op)
pub fn with_ignore<R>(&self, f: impl FnOnce() -> R) -> R {
with_deps(TaskDepsRef::Ignore, f)
}

/// Used to wrap the deserialization of a query result from disk,
Expand Down Expand Up @@ -268,71 +266,48 @@ impl DepGraph {
/// in the query infrastructure, and is not currently needed by the
/// decoding of any query results. Should the need arise in the future,
/// we should consider extending the query system with this functionality.
pub fn with_query_deserialization<OP, R>(&self, op: OP) -> R
where
OP: FnOnce() -> R,
{
with_deps(TaskDepsRef::Forbid, op)
pub fn with_query_deserialization<R>(&self, f: impl FnOnce() -> R) -> R {
with_deps(TaskDepsRef::Forbid, f)
}

#[inline(always)]
pub fn with_task<'tcx, A: Debug, R>(
pub fn with_task<'tcx, R>(
&self,
dep_node: DepNode,
tcx: TyCtxt<'tcx>,
task_arg: A,
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
f: impl FnOnce() -> R,
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex) {
match self.data() {
Some(data) => data.with_task(dep_node, tcx, task_arg, task_fn, hash_result),
None => (task_fn(tcx, task_arg), self.next_virtual_depnode_index()),
Some(data) => data.with_task(dep_node, tcx, f, hash_result),
None => (f(), self.next_virtual_depnode_index()),
}
}

pub fn with_anon_task<'tcx, OP, R>(
pub fn with_anon_task<'tcx, R>(
&self,
tcx: TyCtxt<'tcx>,
dep_kind: DepKind,
op: OP,
) -> (R, DepNodeIndex)
where
OP: FnOnce() -> R,
{
f: impl FnOnce() -> R,
) -> (R, DepNodeIndex) {
match self.data() {
Some(data) => {
let (result, index) = data.with_anon_task_inner(tcx, dep_kind, op);
let (result, index) = data.with_anon_task_inner(tcx, dep_kind, f);
self.read_index(index);
(result, index)
}
None => (op(), self.next_virtual_depnode_index()),
None => (f(), self.next_virtual_depnode_index()),
}
}
}

impl DepGraphData {
/// Starts a new dep-graph task. Dep-graph tasks are specified
/// using a free function (`task`) and **not** a closure -- this
/// is intentional because we want to exercise tight control over
/// what state they have access to. In particular, we want to
/// prevent implicit 'leaks' of tracked state into the task (which
/// could then be read without generating correct edges in the
/// dep-graph -- see the [rustc dev guide] for more details on
/// the dep-graph).
///
/// Therefore, the task function takes a `TyCtxt`, plus exactly one
/// additional argument, `task_arg`. The additional argument type can be
/// `()` if no argument is needed, or a tuple if multiple arguments are
/// needed.
///
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html
#[inline(always)]
pub fn with_task<'tcx, A: Debug, R>(
pub fn with_task<'tcx, R>(
&self,
dep_node: DepNode,
tcx: TyCtxt<'tcx>,
task_arg: A,
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
f: impl FnOnce() -> R,
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex) {
// If the following assertion triggers, it can have two reasons:
Expand All @@ -341,14 +316,10 @@ impl DepGraphData {
// 2. Two distinct query keys get mapped to the same `DepNode`
// (see for example #48923).
self.assert_dep_node_not_yet_allocated_in_current_session(tcx.sess, &dep_node, || {
format!(
"forcing query with already existing `DepNode`\n\
- query-key: {task_arg:?}\n\
- dep-node: {dep_node:?}"
)
format!("forcing query with already existing `DepNode`: {dep_node:?}")
});

let with_deps = |task_deps| with_deps(task_deps, || task_fn(tcx, task_arg));
let with_deps = |task_deps| with_deps(task_deps, f);
let (result, edges) = if tcx.is_eval_always(dep_node.kind) {
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new())
} else {
Expand Down Expand Up @@ -377,15 +348,12 @@ impl DepGraphData {
/// FIXME: This could perhaps return a `WithDepNode` to ensure that the
/// user of this function actually performs the read; we'll have to see
/// how to make that work with `anon` in `execute_job_incr`, though.
pub fn with_anon_task_inner<'tcx, OP, R>(
pub fn with_anon_task_inner<'tcx, R>(
&self,
tcx: TyCtxt<'tcx>,
dep_kind: DepKind,
op: OP,
) -> (R, DepNodeIndex)
where
OP: FnOnce() -> R,
{
f: impl FnOnce() -> R,
) -> (R, DepNodeIndex) {
debug_assert!(!tcx.is_eval_always(dep_kind));

// Large numbers of reads are common enough here that pre-sizing `read_set`
Expand All @@ -395,7 +363,7 @@ impl DepGraphData {
None,
128,
));
let result = with_deps(TaskDepsRef::Allow(&task_deps), op);
let result = with_deps(TaskDepsRef::Allow(&task_deps), f);
let task_deps = task_deps.into_inner();
let reads = task_deps.reads;

Expand Down Expand Up @@ -846,10 +814,11 @@ impl DepGraph {

#[cfg(debug_assertions)]
#[inline(always)]
pub(crate) fn register_dep_node_debug_str<F>(&self, dep_node: DepNode, debug_str_gen: F)
where
F: FnOnce() -> String,
{
pub(crate) fn register_dep_node_debug_str(
&self,
dep_node: DepNode,
debug_str_gen: impl FnOnce() -> String,
) {
// Early queries (e.g., `-Z query-dep-graph` on empty crates) can reach here
// before the graph is initialized. Return early to prevent an ICE.
let data = match &self.data {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_query_impl/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(
dep_graph_data.with_task(
dep_node,
tcx,
(query, key),
|tcx, (query, key)| (query.invoke_provider_fn)(tcx, key),
|| (query.invoke_provider_fn)(tcx, key),
query.hash_value_fn,
)
});
Expand Down
Loading