Skip to content

Commit 817b7e0

Browse files
authored
Rollup merge of #84123 - bjorn3:compile_mono_item_dep_node, r=wesleywiser
Introduce CompileMonoItem DepNode This is likely required for allowing efficient hot code swap support in cg_clif's jit mode. My prototype currently requires re-compiling all functions, which is both slow and uses a lot of memory as there is not support for freeing the memory used by replaced functions yet. cc https://github.com/bjorn3/rustc_codegen_cranelift/issues/1087
2 parents 41f0e13 + 21f13af commit 817b7e0

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
//! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
3333
//! defines the `DepKind` enum. Each `DepKind` has its own parameters that are
3434
//! needed at runtime in order to construct a valid `DepNode` fingerprint.
35-
//! However, only `CompileCodegenUnit` is constructed explicitly (with
36-
//! `make_compile_codegen_unit`).
35+
//! However, only `CompileCodegenUnit` and `CompileMonoItem` are constructed
36+
//! explicitly (with `make_compile_codegen_unit` cq `make_compile_mono_item`).
3737
//!
3838
//! Because the macro sees what parameters a given `DepKind` requires, it can
3939
//! "infer" some properties for each kind of `DepNode`:
@@ -46,15 +46,17 @@
4646
//! `DefId` it was computed from. In other cases, too much information gets
4747
//! lost during fingerprint computation.
4848
//!
49-
//! `make_compile_codegen_unit`, together with `DepNode::new()`, ensures that only
50-
//! valid `DepNode` instances can be constructed. For example, the API does not
51-
//! allow for constructing parameterless `DepNode`s with anything other
52-
//! than a zeroed out fingerprint. More generally speaking, it relieves the
53-
//! user of the `DepNode` API of having to know how to compute the expected
54-
//! fingerprint for a given set of node parameters.
49+
//! `make_compile_codegen_unit` and `make_compile_mono_items`, together with
50+
//! `DepNode::new()`, ensures that only valid `DepNode` instances can be
51+
//! constructed. For example, the API does not allow for constructing
52+
//! parameterless `DepNode`s with anything other than a zeroed out fingerprint.
53+
//! More generally speaking, it relieves the user of the `DepNode` API of
54+
//! having to know how to compute the expected fingerprint for a given set of
55+
//! node parameters.
5556
//!
5657
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
5758
59+
use crate::mir::mono::MonoItem;
5860
use crate::ty::TyCtxt;
5961

6062
use rustc_data_structures::fingerprint::Fingerprint;
@@ -175,6 +177,14 @@ pub mod dep_kind {
175177
can_reconstruct_query_key: || false,
176178
};
177179

180+
pub const CompileMonoItem: DepKindStruct = DepKindStruct {
181+
has_params: true,
182+
is_anon: false,
183+
is_eval_always: false,
184+
185+
can_reconstruct_query_key: || false,
186+
};
187+
178188
macro_rules! define_query_dep_kinds {
179189
($(
180190
[$($attrs:tt)*]
@@ -251,6 +261,10 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
251261

252262
// WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below.
253263
[] CompileCodegenUnit(Symbol),
264+
265+
// WARNING: if `MonoItem` is changed, make sure you update `make_compile_mono_item` below.
266+
// Only used by rustc_codegen_cranelift
267+
[] CompileMonoItem(MonoItem),
254268
]);
255269

256270
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
@@ -259,6 +273,12 @@ crate fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode {
259273
DepNode::construct(tcx, DepKind::CompileCodegenUnit, &name)
260274
}
261275

276+
// WARNING: `construct` is generic and does not know that `CompileMonoItem` takes `MonoItem`s as keys.
277+
// Be very careful changing this type signature!
278+
crate fn make_compile_mono_item(tcx: TyCtxt<'tcx>, mono_item: &MonoItem<'tcx>) -> DepNode {
279+
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
280+
}
281+
262282
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
263283

264284
// We keep a lot of `DepNode`s in memory during compilation. It's not

compiler/rustc_middle/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub use rustc_query_system::dep_graph::{
1212
SerializedDepNodeIndex, WorkProduct, WorkProductId,
1313
};
1414

15-
crate use dep_node::make_compile_codegen_unit;
1615
pub use dep_node::{label_strs, DepKind, DepNode, DepNodeExt};
16+
crate use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
1717

1818
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
1919
pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;

compiler/rustc_middle/src/mir/mono.rs

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ impl<'tcx> MonoItem<'tcx> {
181181
}
182182
.map(|hir_id| tcx.hir().span(hir_id))
183183
}
184+
185+
// Only used by rustc_codegen_cranelift
186+
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
187+
crate::dep_graph::make_compile_mono_item(tcx, self)
188+
}
184189
}
185190

186191
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {

compiler/rustc_query_impl/src/plumbing.rs

+5
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ macro_rules! define_queries {
438438
try_load_from_on_disk_cache: |_, _| {},
439439
};
440440

441+
pub const CompileMonoItem: QueryStruct = QueryStruct {
442+
force_from_dep_node: |_, _| false,
443+
try_load_from_on_disk_cache: |_, _| {},
444+
};
445+
441446
$(pub const $name: QueryStruct = {
442447
const is_anon: bool = is_anon!([$($modifiers)*]);
443448

0 commit comments

Comments
 (0)