Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3da1435

Browse files
committedJan 29, 2025
Auto merge of #136201 - davidv1992:eliminate-field-offset-alt, r=<try>
Removed dependency on the field-offset crate, alternate approach This is an alternate approach to reach the same goals as #136003. As it touches the core of the query system, this too probably should be evaluated for performance. r? `@Mark-Simulacrum`
2 parents ccc9ba5 + ef6ee83 commit 3da1435

File tree

7 files changed

+18
-35
lines changed

7 files changed

+18
-35
lines changed
 

‎Cargo.lock

-21
Original file line numberDiff line numberDiff line change
@@ -1195,16 +1195,6 @@ version = "2.3.0"
11951195
source = "registry+https://github.com/rust-lang/crates.io-index"
11961196
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
11971197

1198-
[[package]]
1199-
name = "field-offset"
1200-
version = "0.3.6"
1201-
source = "registry+https://github.com/rust-lang/crates.io-index"
1202-
checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f"
1203-
dependencies = [
1204-
"memoffset",
1205-
"rustc_version",
1206-
]
1207-
12081198
[[package]]
12091199
name = "filetime"
12101200
version = "0.2.25"
@@ -2269,15 +2259,6 @@ dependencies = [
22692259
"libc",
22702260
]
22712261

2272-
[[package]]
2273-
name = "memoffset"
2274-
version = "0.9.1"
2275-
source = "registry+https://github.com/rust-lang/crates.io-index"
2276-
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
2277-
dependencies = [
2278-
"autocfg",
2279-
]
2280-
22812262
[[package]]
22822263
name = "mime"
22832264
version = "0.3.17"
@@ -4119,7 +4100,6 @@ version = "0.0.0"
41194100
dependencies = [
41204101
"bitflags",
41214102
"either",
4122-
"field-offset",
41234103
"gsgdt",
41244104
"polonius-engine",
41254105
"rustc-rayon-core",
@@ -4365,7 +4345,6 @@ dependencies = [
43654345
name = "rustc_query_impl"
43664346
version = "0.0.0"
43674347
dependencies = [
4368-
"field-offset",
43694348
"measureme",
43704349
"rustc_data_structures",
43714350
"rustc_errors",

‎compiler/rustc_middle/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ edition = "2021"
77
# tidy-alphabetical-start
88
bitflags = "2.4.1"
99
either = "1.5.0"
10-
field-offset = "0.3.5"
1110
gsgdt = "0.1.2"
1211
polonius-engine = "0.13.0"
1312
rustc-rayon-core = { version = "0.5.0" }

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::ops::Deref;
22

3-
use field_offset::FieldOffset;
43
use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
54
use rustc_hir::def_id::{DefId, LocalDefId};
65
use rustc_hir::hir_id::OwnerId;
@@ -24,8 +23,8 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
2423
pub eval_always: bool,
2524
pub dep_kind: DepKind,
2625
pub handle_cycle_error: HandleCycleError,
27-
pub query_state: FieldOffset<QueryStates<'tcx>, QueryState<C::Key>>,
28-
pub query_cache: FieldOffset<QueryCaches<'tcx>, C>,
26+
pub query_state: usize,
27+
pub query_cache: usize,
2928
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
3029
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
3130
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,

‎compiler/rustc_query_impl/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
field-offset = "0.3.5"
98
measureme = "11"
109
rustc_data_structures = { path = "../rustc_data_structures" }
1110
rustc_errors = { path = "../rustc_errors" }

‎compiler/rustc_query_impl/src/lib.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![warn(unreachable_pub)]
1212
// tidy-alphabetical-end
1313

14-
use field_offset::offset_of;
1514
use rustc_data_structures::stable_hasher::HashStable;
1615
use rustc_data_structures::sync::AtomicU64;
1716
use rustc_middle::arena::Arena;
@@ -89,15 +88,27 @@ where
8988
where
9089
QueryCtxt<'tcx>: 'a,
9190
{
92-
self.dynamic.query_state.apply(&qcx.tcx.query_system.states)
91+
// Safety:
92+
// This is just manually doing the subfield referencing through pointer math.
93+
unsafe {
94+
&*(&qcx.tcx.query_system.states as *const QueryStates<'tcx>)
95+
.byte_add(self.dynamic.query_state)
96+
.cast::<QueryState<Self::Key>>()
97+
}
9398
}
9499

95100
#[inline(always)]
96101
fn query_cache<'a>(self, qcx: QueryCtxt<'tcx>) -> &'a Self::Cache
97102
where
98103
'tcx: 'a,
99104
{
100-
self.dynamic.query_cache.apply(&qcx.tcx.query_system.caches)
105+
// Safety:
106+
// This is just manually doing the subfield referencing through pointer math.
107+
unsafe {
108+
&*(&qcx.tcx.query_system.caches as *const QueryCaches<'tcx>)
109+
.byte_add(self.dynamic.query_cache)
110+
.cast::<Self::Cache>()
111+
}
101112
}
102113

103114
#[inline(always)]

‎compiler/rustc_query_impl/src/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ macro_rules! define_queries {
605605
eval_always: is_eval_always!([$($modifiers)*]),
606606
dep_kind: dep_graph::dep_kinds::$name,
607607
handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
608-
query_state: offset_of!(QueryStates<'tcx> => $name),
609-
query_cache: offset_of!(QueryCaches<'tcx> => $name),
608+
query_state: std::mem::offset_of!(QueryStates<'tcx>, $name),
609+
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
610610
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
611611
execute_query: |tcx, key| erase(tcx.$name(key)),
612612
compute: |tcx, key| {

‎src/tools/tidy/src/deps.rs

-4
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
285285
"expect-test",
286286
"fallible-iterator", // dependency of `thorin`
287287
"fastrand",
288-
"field-offset",
289288
"flate2",
290289
"fluent-bundle",
291290
"fluent-langneg",
@@ -327,7 +326,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
327326
"measureme",
328327
"memchr",
329328
"memmap2",
330-
"memoffset",
331329
"miniz_oxide",
332330
"nix",
333331
"nu-ansi-term",
@@ -367,14 +365,12 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
367365
"rustc-rayon-core",
368366
"rustc-stable-hash",
369367
"rustc_apfloat",
370-
"rustc_version",
371368
"rustix",
372369
"ruzstd", // via object in thorin-dwp
373370
"ryu",
374371
"scoped-tls",
375372
"scopeguard",
376373
"self_cell",
377-
"semver",
378374
"serde",
379375
"serde_derive",
380376
"serde_json",

0 commit comments

Comments
 (0)
Please sign in to comment.