Skip to content

Commit b06e840

Browse files
committed
Add comments about address spaces
1 parent 436e4fb commit b06e840

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
12261226
fn get_static(&mut self, def_id: DefId) -> &'ll Value {
12271227
// Forward to the `get_static` method of `CodegenCx`
12281228
let s = self.cx().get_static(def_id);
1229-
// Cast to default address space if statics are in a different addrspace
1229+
// Cast to default address space if globals are in a different addrspace
12301230
self.cx().const_pointercast(s, self.type_ptr())
12311231
}
12321232
}

compiler/rustc_codegen_llvm/src/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
221221
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
222222
}
223223
llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
224+
// Cast to default address space if globals are in a different addrspace
224225
let g = self.const_pointercast(g, self.type_ptr());
225226
(s.to_owned(), g)
226227
})
@@ -324,6 +325,7 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
324325
let llval = unsafe {
325326
llvm::LLVMConstInBoundsGEP2(
326327
self.type_i8(),
328+
// Cast to the required address space if necessary
327329
self.const_pointercast(base_addr, self.type_ptr_ext(base_addr_space)),
328330
&self.const_usize(offset.bytes()),
329331
1,

compiler/rustc_codegen_llvm/src/consts.rs

+13
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ impl<'ll> CodegenCx<'ll, '_> {
214214
unsafe { llvm::LLVMConstPointerCast(val, ty) }
215215
}
216216

217+
/// Create a global variable.
218+
///
219+
/// The returned global variable is a pointer in the default address space for globals.
220+
/// Fails if a symbol with the given name already exists.
217221
pub(crate) fn static_addr_of_mut(
218222
&self,
219223
cv: &'ll Value,
@@ -237,6 +241,9 @@ impl<'ll> CodegenCx<'ll, '_> {
237241
gv
238242
}
239243

244+
/// Create a global constant.
245+
///
246+
/// The returned global variable is a pointer in the default address space for globals.
240247
pub(crate) fn static_addr_of_impl(
241248
&self,
242249
cv: &'ll Value,
@@ -534,8 +541,14 @@ impl<'ll> CodegenCx<'ll, '_> {
534541
}
535542

536543
impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
544+
/// Get a pointer to a global variable.
545+
///
546+
/// The pointer will always be in the default address space. If global variables default to a
547+
/// different address space, an addrspacecast is inserted.
537548
fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
538549
let gv = self.static_addr_of_impl(cv, align, kind);
550+
// static_addr_of_impl returns the bare global variable, which might not be in the default
551+
// address space. Cast to the default address space if necessary.
539552
self.const_pointercast(gv, self.type_ptr())
540553
}
541554

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,14 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
15001500
.di_node
15011501
}
15021502

1503+
/// Get the global variable for the vtable.
1504+
///
1505+
/// When using global variables, we may have created an addrspacecast to get a pointer to the
1506+
/// default address space if global variables are created in a different address space.
1507+
/// For modifying the vtable, we need the real global variable. This function accepts either a
1508+
/// global variable (which is simply returned), or an addrspacecast constant expression.
1509+
/// If the given value is an addrspacecast, the cast is removed and the global variable behind
1510+
/// the cast is returned.
15031511
fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
15041512
// The vtable is a global variable, which may be behind an addrspacecast.
15051513
unsafe {
@@ -1532,6 +1540,7 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
15321540

15331541
let Some(trait_ref) = trait_ref else { return };
15341542

1543+
// Unwrap potential addrspacecast
15351544
let vtable = find_vtable_behind_cast(vtable);
15361545
let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
15371546
let trait_ref_self = cx.tcx.erase_regions(trait_ref_self);
@@ -1606,6 +1615,7 @@ pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
16061615
return;
16071616
}
16081617

1618+
// Unwrap potential addrspacecast
16091619
let vtable = find_vtable_behind_cast(vtable);
16101620

16111621
// When full debuginfo is enabled, we want to try and prevent vtables from being

0 commit comments

Comments
 (0)