Skip to content

Commit fb623bc

Browse files
debuginfo: Don't emit dwarfAddressSpace attribute for pointer types.
1 parent 0407030 commit fb623bc

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_session::config::{self, DebugInfo};
3838
use rustc_span::symbol::Symbol;
3939
use rustc_span::FileNameDisplayPreference;
4040
use rustc_span::{self, SourceFile, SourceFileHash};
41+
use rustc_target::abi::AddressSpace;
4142
use rustc_target::abi::{Align, Size};
4243
use smallvec::smallvec;
4344
use tracing::debug;
@@ -174,6 +175,9 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
174175
cx.size_and_align_of(cx.tcx.mk_imm_ptr(cx.tcx.types.unit));
175176
let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
176177

178+
let ptr_ty_layout = cx.layout_of(ptr_type);
179+
let address_space = address_space_of(cx, ptr_ty_layout);
180+
177181
match fat_pointer_kind(cx, pointee_type) {
178182
None => {
179183
// This is a thin pointer. Create a regular pointer type and give it the correct name.
@@ -191,7 +195,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
191195
pointee_type_di_node,
192196
thin_pointer_size.bits(),
193197
thin_pointer_align.bits() as u32,
194-
0, // Ignore DWARF address space.
198+
address_space.as_ref(),
195199
ptr_type_debuginfo_name.as_ptr().cast(),
196200
ptr_type_debuginfo_name.len(),
197201
)
@@ -212,9 +216,8 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
212216
DIFlags::FlagZero,
213217
),
214218
|cx, owner| {
215-
let layout = cx.layout_of(ptr_type);
216-
let addr_field = layout.field(cx, abi::FAT_PTR_ADDR);
217-
let extra_field = layout.field(cx, abi::FAT_PTR_EXTRA);
219+
let addr_field = ptr_ty_layout.field(cx, abi::FAT_PTR_ADDR);
220+
let extra_field = ptr_ty_layout.field(cx, abi::FAT_PTR_EXTRA);
218221

219222
let (addr_field_name, extra_field_name) = match fat_pointer_kind {
220223
FatPtrKind::Dyn => ("pointer", "vtable"),
@@ -232,7 +235,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
232235
pointee_type_di_node,
233236
addr_field.size.bits(),
234237
addr_field.align.abi.bits() as u32,
235-
0, // Ignore DWARF address space.
238+
address_space.as_ref(),
236239
std::ptr::null(),
237240
0,
238241
)
@@ -244,7 +247,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
244247
owner,
245248
addr_field_name,
246249
(addr_field.size, addr_field.align.abi),
247-
layout.fields.offset(abi::FAT_PTR_ADDR),
250+
ptr_ty_layout.fields.offset(abi::FAT_PTR_ADDR),
248251
DIFlags::FlagZero,
249252
data_ptr_type_di_node,
250253
),
@@ -253,7 +256,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
253256
owner,
254257
extra_field_name,
255258
(extra_field.size, extra_field.align.abi),
256-
layout.fields.offset(abi::FAT_PTR_EXTRA),
259+
ptr_ty_layout.fields.offset(abi::FAT_PTR_EXTRA),
257260
DIFlags::FlagZero,
258261
type_di_node(cx, extra_field.ty),
259262
),
@@ -318,6 +321,8 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
318321
)
319322
};
320323

324+
let address_space = address_space_of(cx, cx.layout_of(fn_ty));
325+
321326
// This is actually a function pointer, so wrap it in pointer DI.
322327
let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
323328
let di_node = unsafe {
@@ -326,7 +331,7 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
326331
fn_di_node,
327332
cx.tcx.data_layout.pointer_size.bits(),
328333
cx.tcx.data_layout.pointer_align.abi.bits() as u32,
329-
0, // Ignore DWARF address space.
334+
address_space.as_ref(),
330335
name.as_ptr().cast(),
331336
name.len(),
332337
)
@@ -1479,3 +1484,19 @@ pub fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
14791484
.map(|s| Cow::from(*s))
14801485
.unwrap_or_else(|| Cow::from(format!("__{}", field_index)))
14811486
}
1487+
1488+
fn address_space_of<'ll, 'tcx>(
1489+
cx: &CodegenCx<'ll, 'tcx>,
1490+
ptr_ty_layout: TyAndLayout<'tcx>,
1491+
) -> Option<c_uint> {
1492+
ptr_ty_layout.pointee_info_at(cx, Size::ZERO).map(|pi| pi.address_space).and_then(
1493+
|address_space| {
1494+
if address_space == AddressSpace::DATA {
1495+
// Don't emit the DW_AT_address_class attribute for things with AddressSpace::DATA
1496+
None
1497+
} else {
1498+
Some(address_space.0 as c_uint)
1499+
}
1500+
},
1501+
)
1502+
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ extern "C" {
20212021
PointeeTy: &'a DIType,
20222022
SizeInBits: u64,
20232023
AlignInBits: u32,
2024-
AddressSpace: c_uint,
2024+
AddressSpace: Option<&c_uint>,
20252025
Name: *const c_char,
20262026
NameLen: size_t,
20272027
) -> &'a DIDerivedType;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,14 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateTypedef(
763763

764764
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreatePointerType(
765765
LLVMRustDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
766-
uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
766+
uint64_t SizeInBits, uint32_t AlignInBits, const unsigned* AddressSpace,
767767
const char *Name, size_t NameLen) {
768+
769+
auto OptAddressSpace = AddressSpace ? Optional<unsigned>(*AddressSpace) : None;
770+
768771
return wrap(Builder->createPointerType(unwrapDI<DIType>(PointeeTy),
769772
SizeInBits, AlignInBits,
770-
AddressSpace,
773+
OptAddressSpace,
771774
StringRef(Name, NameLen)));
772775
}
773776

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// compile-flags: -Cdebuginfo=2
2+
#![crate_type = "lib"]
3+
4+
// The program below results in debuginfo being generated for at least
5+
// five different pointer types. Check that they don't have a
6+
// dwarfAddressSpace attribute.
7+
8+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
9+
// CHECK-NOT: dwarfAddressSpace
10+
11+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
12+
// CHECK-NOT: dwarfAddressSpace
13+
14+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
15+
// CHECK-NOT: dwarfAddressSpace
16+
17+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
18+
// CHECK-NOT: dwarfAddressSpace
19+
20+
// CHECK: !DIDerivedType(tag: DW_TAG_pointer_type
21+
// CHECK-NOT: dwarfAddressSpace
22+
23+
pub fn foo(
24+
shared_ref: &usize,
25+
mut_ref: &mut usize,
26+
const_ptr: *const usize,
27+
mut_ptr: *mut usize,
28+
boxed: Box<usize>,
29+
) -> usize {
30+
*shared_ref + *mut_ref + const_ptr as usize + mut_ptr as usize + *boxed
31+
}

0 commit comments

Comments
 (0)