Skip to content

Commit b00b4ea

Browse files
committed
Auto merge of #143182 - xdoardo:more-addrspace, r=workingjubilee
Allow custom default address spaces and parse `p-` specifications in the datalayout string Some targets, such as CHERI, use as default an address space different from the "normal" default address space `0` (in the case of CHERI, [200 is used](https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-877.pdf)). Currently, `rustc` does not allow to specify custom address spaces and does not take into consideration [`p-` specifications in the datalayout string](https://llvm.org/docs/LangRef.html#langref-datalayout). This patch tries to mitigate these problems by allowing targets to define a custom default address space (while keeping the default value to address space `0`) and adding the code to parse the `p-` specifications in `rustc_abi`. The main changes are that `TargetDataLayout` now uses functions to refer to pointer-related informations, instead of having specific fields for the size and alignment of pointers in the default address space; furthermore, the two `pointer_size` and `pointer_align` fields in `TargetDataLayout` are replaced with an `FxHashMap` that holds info for all the possible address spaces, as parsed by the `p-` specifications. The potential performance drawbacks of not having ad-hoc fields for the default address space will be tested in this PR's CI run. r? workingjubilee
2 parents 7d6f447 + 54507d8 commit b00b4ea

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

crates/hir-ty/src/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub fn layout_of_ty_query(
261261
}
262262
// Potentially-wide pointers.
263263
TyKind::Ref(_, _, pointee) | TyKind::Raw(_, pointee) => {
264-
let mut data_ptr = scalar_unit(dl, Primitive::Pointer(AddressSpace::DATA));
264+
let mut data_ptr = scalar_unit(dl, Primitive::Pointer(AddressSpace::ZERO));
265265
if matches!(ty.kind(Interner), TyKind::Ref(..)) {
266266
data_ptr.valid_range_mut().start = 1;
267267
}
@@ -285,7 +285,7 @@ pub fn layout_of_ty_query(
285285
scalar_unit(dl, Primitive::Int(dl.ptr_sized_integer(), false))
286286
}
287287
TyKind::Dyn(..) => {
288-
let mut vtable = scalar_unit(dl, Primitive::Pointer(AddressSpace::DATA));
288+
let mut vtable = scalar_unit(dl, Primitive::Pointer(AddressSpace::ZERO));
289289
vtable.valid_range_mut().start = 1;
290290
vtable
291291
}

crates/hir-ty/src/layout/target.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use base_db::Crate;
44
use hir_def::layout::TargetDataLayout;
5-
use rustc_abi::{AlignFromBytesError, TargetDataLayoutErrors};
5+
use rustc_abi::{AlignFromBytesError, TargetDataLayoutErrors, AddressSpace};
66
use triomphe::Arc;
77

88
use crate::db::HirDatabase;
@@ -12,7 +12,7 @@ pub fn target_data_layout_query(
1212
krate: Crate,
1313
) -> Result<Arc<TargetDataLayout>, Arc<str>> {
1414
match &krate.workspace_data(db).data_layout {
15-
Ok(it) => match TargetDataLayout::parse_from_llvm_datalayout_string(it) {
15+
Ok(it) => match TargetDataLayout::parse_from_llvm_datalayout_string(it, AddressSpace::ZERO) {
1616
Ok(it) => Ok(Arc::new(it)),
1717
Err(e) => {
1818
Err(match e {
@@ -39,6 +39,7 @@ pub fn target_data_layout_query(
3939
target,
4040
} => format!(r#"inconsistent target specification: "data-layout" claims pointers are {pointer_size}-bit, while "target-pointer-width" is `{target}`"#),
4141
TargetDataLayoutErrors::InvalidBitsSize { err } => err,
42+
TargetDataLayoutErrors::UnknownPointerSpecification { err } => format!(r#"use of unknown pointer specifer in "data-layout": {err}"#),
4243
}.into())
4344
}
4445
},

crates/hir-ty/src/mir/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ impl Evaluator<'_> {
630630
Ok(target_data_layout) => target_data_layout,
631631
Err(e) => return Err(MirEvalError::TargetDataLayoutNotAvailable(e)),
632632
};
633-
let cached_ptr_size = target_data_layout.pointer_size.bytes_usize();
633+
let cached_ptr_size = target_data_layout.pointer_size().bytes_usize();
634634
Ok(Evaluator {
635635
target_data_layout,
636636
stack: vec![0],

0 commit comments

Comments
 (0)