Skip to content

Commit 35ccb26

Browse files
committed
Remove const_data_from_alloc to make cg_ssa more amenable for cg_clif
1 parent 32f7e78 commit 35ccb26

File tree

8 files changed

+47
-69
lines changed

8 files changed

+47
-69
lines changed

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use gccjit::{LValue, RValue, ToRValue, Type};
22
use rustc_abi as abi;
3-
use rustc_abi::{Align, HasDataLayout};
3+
use rustc_abi::HasDataLayout;
44
use rustc_codegen_ssa::traits::{
55
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
66
};
@@ -40,6 +40,22 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
4040
// SIMD builtins require a constant value.
4141
self.bitcast_if_needed(value, typ)
4242
}
43+
44+
pub fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> RValue<'gcc> {
45+
// We ignore the alignment for the purpose of deduping RValues
46+
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
47+
// so it is OK to overwrite it here.
48+
let mut mock_alloc = alloc.inner().clone();
49+
mock_alloc.align = rustc_abi::Align::MAX;
50+
// Check if the rvalue is already in the cache - if so, just return it directly.
51+
if let Some(res) = self.const_cache.borrow().get(&mock_alloc) {
52+
return *res;
53+
}
54+
// Rvalue not in the cache - convert and add it.
55+
let res = crate::consts::const_alloc_to_gcc_uncached(self, alloc);
56+
self.const_cache.borrow_mut().insert(mock_alloc, res);
57+
res
58+
}
4359
}
4460

4561
pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> {
@@ -227,22 +243,6 @@ impl<'gcc, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
227243
None
228244
}
229245

230-
fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
231-
// We ignore the alignment for the purpose of deduping RValues
232-
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
233-
// so it is OK to overwrite it here.
234-
let mut mock_alloc = alloc.inner().clone();
235-
mock_alloc.align = rustc_abi::Align::MAX;
236-
// Check if the rvalue is already in the cache - if so, just return it directly.
237-
if let Some(res) = self.const_cache.borrow().get(&mock_alloc) {
238-
return *res;
239-
}
240-
// Rvalue not in the cache - convert and add it.
241-
let res = crate::consts::const_alloc_to_gcc_uncached(self, alloc);
242-
self.const_cache.borrow_mut().insert(mock_alloc, res);
243-
res
244-
}
245-
246246
fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
247247
self.context
248248
.new_array_access(None, base_addr, self.const_usize(offset.bytes()))
@@ -261,17 +261,13 @@ impl<'gcc, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
261261
self.context.new_cast(None, val, ty)
262262
}
263263

264-
fn static_addr_of_const(
265-
&self,
266-
cv: Self::Value,
267-
align: Align,
268-
kind: Option<&str>,
269-
) -> Self::Value {
270-
self.static_addr_of(cv, align, kind)
264+
fn static_addr_of_const(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value {
265+
self.static_addr_of(alloc, kind)
271266
}
272267

273-
fn static_addr_of_mut(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value {
274-
self.static_addr_of_mut(cv, align, kind)
268+
fn static_addr_of_mut(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value {
269+
let cv = self.const_data_from_alloc(alloc);
270+
self.static_addr_of_mut(cv, alloc.inner().align, kind)
275271
}
276272
}
277273

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ fn set_global_alignment<'gcc, 'tcx>(
3535
}
3636

3737
impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
38-
fn static_addr_of(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
38+
fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> RValue<'gcc> {
39+
let align = alloc.inner().align;
40+
let cv = self.const_data_from_alloc(alloc);
3941
if let Some(variable) = self.const_globals.borrow().get(&cv) {
4042
if let Some(global_variable) = self.global_lvalues.borrow().get(variable) {
4143
let alignment = align.bits() as i32;

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::borrow::Borrow;
44

55
use libc::{c_char, c_uint};
66
use rustc_abi as abi;
7-
use rustc_abi::{Align, HasDataLayout};
7+
use rustc_abi::HasDataLayout;
88
use rustc_codegen_ssa::common::TypeKind;
99
use rustc_codegen_ssa::traits::*;
1010
use rustc_hir::def_id::DefId;
@@ -251,10 +251,6 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
251251
})
252252
}
253253

254-
fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
255-
const_alloc_to_llvm(self, alloc, /*static*/ false)
256-
}
257-
258254
fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
259255
unsafe {
260256
llvm::LLVMConstInBoundsGEP2(
@@ -279,17 +275,14 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
279275
unsafe { llvm::LLVMConstPtrToInt(val, ty) }
280276
}
281277

282-
fn static_addr_of_const(
283-
&self,
284-
cv: Self::Value,
285-
align: Align,
286-
kind: Option<&str>,
287-
) -> Self::Value {
288-
self.static_addr_of_const(cv, align, kind)
278+
fn static_addr_of_const(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value {
279+
let cv = const_alloc_to_llvm(self, alloc, /*static*/ false);
280+
self.static_addr_of_const(cv, alloc.inner().align, kind)
289281
}
290282

291-
fn static_addr_of_mut(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value {
292-
self.static_addr_of_mut(cv, align, kind)
283+
fn static_addr_of_mut(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value {
284+
let cv = const_alloc_to_llvm(self, alloc, /*static*/ false);
285+
self.static_addr_of_mut(cv, alloc.inner().align, kind)
293286
}
294287
}
295288

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,9 @@ impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
551551
///
552552
/// The pointer will always be in the default address space. If global variables default to a
553553
/// different address space, an addrspacecast is inserted.
554-
fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
555-
let gv = self.static_addr_of_const(cv, align, kind);
554+
fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> &'ll Value {
555+
let cv = const_alloc_to_llvm(self, alloc, /*static*/ false);
556+
let gv = self.static_addr_of_const(cv, alloc.inner().align, kind);
556557
// static_addr_of_const returns the bare global variable, which might not be in the default
557558
// address space. Cast to the default address space if necessary.
558559
self.const_pointercast(gv, self.type_ptr())

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ pub(crate) fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
114114

115115
let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
116116
let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
117-
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
118-
let align = cx.data_layout().pointer_align.abi;
119-
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
117+
let vtable = cx.static_addr_of(vtable_allocation, Some("vtable"));
120118

121119
cx.apply_vcall_visibility_metadata(ty, trait_ref, vtable);
122120
cx.create_vtable_debuginfo(ty, trait_ref, vtable);

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
237237
// Neither a scalar nor scalar pair. Load from a place
238238
// FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
239239
// same `ConstAllocation`?
240-
let init = bx.const_data_from_alloc(alloc);
241-
let base_addr = bx.static_addr_of(init, alloc_align, None);
240+
let base_addr = bx.static_addr_of(alloc, None);
242241

243242
let llval = bx.const_ptr_byte_offset(base_addr, offset);
244243
bx.load_operand(PlaceRef::new_sized(llval, layout))

compiler/rustc_codegen_ssa/src/traits/consts.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_abi::{self as abi, Align, HasDataLayout, Primitive};
1+
use rustc_abi::{self as abi, HasDataLayout, Primitive};
22
use rustc_ast::Mutability;
33
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
44
use rustc_hashes::Hash128;
@@ -49,24 +49,17 @@ pub trait ConstCodegenMethods<'tcx>:
4949
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;
5050
fn const_to_opt_u128(&self, v: Self::Value, sign_ext: bool) -> Option<u128>;
5151

52-
fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value;
53-
5452
fn const_bitcast(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
5553
fn const_pointercast(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
5654
fn const_int_to_ptr(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
5755
fn const_ptr_to_int(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
5856
/// Create a global constant.
5957
///
6058
/// The returned global variable is a pointer in the default address space for globals.
61-
fn static_addr_of_const(
62-
&self,
63-
cv: Self::Value,
64-
align: Align,
65-
kind: Option<&str>,
66-
) -> Self::Value;
59+
fn static_addr_of_const(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value;
6760

6861
/// Same as `static_addr_of_const`, but does not mark the static as immutable
69-
fn static_addr_of_mut(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
62+
fn static_addr_of_mut(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value;
7063

7164
fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: Self::Type) -> Self::Value {
7265
let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
@@ -97,18 +90,16 @@ pub trait ConstCodegenMethods<'tcx>:
9790
self.const_bitcast(llval, llty)
9891
};
9992
} else {
100-
let init = self.const_data_from_alloc(alloc);
101-
let alloc = alloc.inner();
102-
let value = match alloc.mutability {
103-
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
104-
_ => self.static_addr_of_const(init, alloc.align, None),
93+
let value = match alloc.inner().mutability {
94+
Mutability::Mut => self.static_addr_of_mut(alloc, None),
95+
_ => self.static_addr_of_const(alloc, None),
10596
};
10697
if !self.tcx().sess.fewer_names()
10798
&& self.get_value_name(value).is_empty()
10899
{
109100
let hash = self.tcx().with_stable_hashing_context(|mut hcx| {
110101
let mut hasher = StableHasher::new();
111-
alloc.hash_stable(&mut hcx, &mut hasher);
102+
alloc.inner().hash_stable(&mut hcx, &mut hasher);
112103
hasher.finish::<Hash128>()
113104
});
114105
self.set_value_name(value, format!("alloc_{hash:032x}").as_bytes());
@@ -127,9 +118,7 @@ pub trait ConstCodegenMethods<'tcx>:
127118
}),
128119
)))
129120
.unwrap_memory();
130-
let init = self.const_data_from_alloc(alloc);
131-
let value = self.static_addr_of_const(init, alloc.inner().align, None);
132-
value
121+
self.static_addr_of_const(alloc, None)
133122
}
134123
GlobalAlloc::Static(def_id) => {
135124
assert!(self.tcx().is_static(def_id));

compiler/rustc_codegen_ssa/src/traits/statics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use rustc_abi::Align;
21
use rustc_hir::def_id::DefId;
2+
use rustc_middle::mir::interpret::ConstAllocation;
33

44
use super::BackendTypes;
55

66
pub trait StaticCodegenMethods: BackendTypes {
7-
fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
7+
fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> Self::Value;
88
fn get_value_name(&self, val: Self::Value) -> &[u8];
99
fn set_value_name(&self, val: Self::Value, name: &[u8]);
1010
fn codegen_static(&mut self, def_id: DefId);

0 commit comments

Comments
 (0)