Skip to content

Commit adc1890

Browse files
create and use GlobalAlloc::address_space
1 parent 009192b commit adc1890

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

compiler/rustc_codegen_gcc/src/consts.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}
77
use rustc_middle::mir::mono::MonoItem;
88
use rustc_middle::ty::{self, Instance, Ty};
99
use rustc_middle::ty::layout::LayoutOf;
10-
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar as InterpScalar, read_target_uint};
10+
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
1111
use rustc_span::def_id::DefId;
12-
use rustc_target::abi::{self, AddressSpace, Align, HasDataLayout, Primitive, Size, WrappingRange};
12+
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
1313

1414
use crate::base;
1515
use crate::context::CodegenCx;
@@ -323,12 +323,7 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAl
323323
.expect("const_alloc_to_llvm: could not read relocation pointer")
324324
as u64;
325325

326-
let address_space = match cx.tcx.global_alloc(alloc_id) {
327-
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
328-
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
329-
AddressSpace::DATA
330-
}
331-
};
326+
let address_space = cx.tcx.global_alloc(alloc_id).address_space(cx);
332327

333328
llvals.push(cx.scalar_to_backend(
334329
InterpScalar::from_pointer(

compiler/rustc_codegen_llvm/src/consts.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ use rustc_codegen_ssa::traits::*;
1313
use rustc_hir::def_id::DefId;
1414
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1515
use rustc_middle::mir::interpret::{
16-
read_target_uint, Allocation, ConstAllocation, ErrorHandled, GlobalAlloc, InitChunk, Pointer,
16+
read_target_uint, Allocation, ConstAllocation, ErrorHandled, InitChunk, Pointer,
1717
Scalar as InterpScalar,
1818
};
1919
use rustc_middle::mir::mono::MonoItem;
2020
use rustc_middle::ty::layout::LayoutOf;
2121
use rustc_middle::ty::{self, Instance, Ty};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_session::config::Lto;
24-
use rustc_target::abi::{
25-
AddressSpace, Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
26-
};
24+
use rustc_target::abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange};
2725
use std::ops::Range;
2826

2927
pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value {
@@ -98,12 +96,7 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<
9896
.expect("const_alloc_to_llvm: could not read relocation pointer")
9997
as u64;
10098

101-
let address_space = match cx.tcx.global_alloc(alloc_id) {
102-
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
103-
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
104-
AddressSpace::DATA
105-
}
106-
};
99+
let address_space = cx.tcx.global_alloc(alloc_id).address_space(cx);
107100

108101
llvals.push(cx.scalar_to_backend(
109102
InterpScalar::from_pointer(

compiler/rustc_middle/src/mir/interpret/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ use rustc_hir::def_id::DefId;
110110
use rustc_macros::HashStable;
111111
use rustc_middle::ty::print::with_no_trimmed_paths;
112112
use rustc_serialize::{Decodable, Encodable};
113-
use rustc_target::abi::Endian;
113+
use rustc_target::abi::{AddressSpace, Endian, HasDataLayout};
114114

115115
use crate::mir;
116116
use crate::ty::codec::{TyDecoder, TyEncoder};
@@ -438,6 +438,17 @@ impl<'tcx> GlobalAlloc<'tcx> {
438438
_ => bug!("expected vtable, got {:?}", self),
439439
}
440440
}
441+
442+
/// The address space that this `GlobalAlloc` should be placed in.
443+
#[inline]
444+
pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
445+
match self {
446+
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
447+
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
448+
AddressSpace::DATA
449+
}
450+
}
451+
}
441452
}
442453

443454
pub(crate) struct AllocMap<'tcx> {

0 commit comments

Comments
 (0)