Skip to content

Commit 266c99b

Browse files
If the type of a global is not the same, we remove the global and replace it with a new one
1 parent eca5cb5 commit 266c99b

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

src/consts.rs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#[cfg(feature = "master")]
22
use gccjit::{FnAttribute, VarAttribute, Visibility};
3-
use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue};
3+
use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue, Type};
44
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
5+
use rustc_hir::def::DefKind;
6+
use rustc_middle::bug;
57
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
68
use rustc_middle::mir::interpret::{
79
self, read_target_uint, ConstAllocation, ErrorHandled, Scalar as InterpScalar,
810
};
911
use rustc_middle::mir::mono::MonoItem;
1012
use rustc_middle::span_bug;
1113
use rustc_middle::ty::layout::LayoutOf;
12-
use rustc_middle::ty::{self, Instance, Ty};
14+
use rustc_middle::ty::{self, Instance};
1315
use rustc_span::def_id::DefId;
1416
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
1517

@@ -66,13 +68,11 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
6668
fn codegen_static(&self, def_id: DefId) {
6769
let attrs = self.tcx.codegen_fn_attrs(def_id);
6870

69-
let value = match codegen_static_initializer(self, def_id) {
70-
Ok((value, _)) => value,
71+
let Ok((value, alloc)) = codegen_static_initializer(self, def_id) else {
7172
// Error has already been reported
72-
Err(_) => return,
73+
return;
7374
};
74-
75-
let global = self.get_static(def_id);
75+
let alloc = alloc.inner();
7676

7777
// boolean SSA values are i1, but they have to be stored in i8 slots,
7878
// otherwise some LLVM optimization passes don't work as expected
@@ -81,23 +81,30 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
8181
unimplemented!();
8282
};
8383

84-
let instance = Instance::mono(self.tcx, def_id);
85-
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
86-
let gcc_type = self.layout_of(ty).gcc_type(self);
84+
let is_thread_local = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
85+
let mut global = self.get_static_inner(def_id, val_llty);
8786

88-
set_global_alignment(self, global, self.align_of(ty));
87+
if global.to_rvalue().get_type() != val_llty {
88+
let instance = Instance::mono(self.tcx, def_id);
89+
self.instances.borrow_mut().remove(&instance);
90+
91+
global.remove();
92+
let name = self.tcx.symbol_name(instance).name;
93+
self.globals.borrow_mut().remove(name);
94+
global = self.get_static_inner(def_id, val_llty);
95+
}
96+
set_global_alignment(self, global, alloc.align);
8997

90-
let value = self.bitcast_if_needed(value, gcc_type);
9198
global.global_set_initializer_rvalue(value);
9299

93100
// As an optimization, all shared statics which do not have interior
94101
// mutability are placed into read-only memory.
95-
if !self.tcx.static_mutability(def_id).unwrap().is_mut() && self.type_is_freeze(ty) {
102+
if alloc.mutability.is_not() {
96103
#[cfg(feature = "master")]
97104
global.global_set_readonly();
98105
}
99106

100-
if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
107+
if is_thread_local {
101108
// Do not allow LLVM to change the alignment of a TLS on macOS.
102109
//
103110
// By default a global's alignment can be freely increased.
@@ -205,34 +212,48 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
205212

206213
pub fn get_static(&self, def_id: DefId) -> LValue<'gcc> {
207214
let instance = Instance::mono(self.tcx, def_id);
208-
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
215+
let DefKind::Static { nested, .. } = self.tcx.def_kind(def_id) else { bug!() };
216+
// Nested statics do not have a type, so pick a random type and let `define_static` figure out
217+
// the gcc type from the actual evaluated initializer.
218+
let gcc_type = if nested {
219+
self.type_i8()
220+
} else {
221+
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
222+
self.layout_of(ty).gcc_type(self)
223+
};
224+
225+
self.get_static_inner(def_id, gcc_type)
226+
}
227+
228+
pub(crate) fn get_static_inner(&self, def_id: DefId, gcc_type: Type<'gcc>) -> LValue<'gcc> {
229+
let instance = Instance::mono(self.tcx, def_id);
209230
if let Some(&global) = self.instances.borrow().get(&instance) {
231+
trace!("used cached value");
210232
return global;
211233
}
212234

213-
let defined_in_current_codegen_unit =
214-
self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
215-
assert!(
216-
!defined_in_current_codegen_unit,
217-
"consts::get_static() should always hit the cache for \
218-
statics defined in the same CGU, but did not for `{:?}`",
219-
def_id
220-
);
221-
222-
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
235+
// FIXME: Once we stop removing globals in `codegen_static`, we can uncomment this code.
236+
// let defined_in_current_codegen_unit =
237+
// self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
238+
// assert!(
239+
// !defined_in_current_codegen_unit,
240+
// "consts::get_static() should always hit the cache for \
241+
// statics defined in the same CGU, but did not for `{:?}`",
242+
// def_id
243+
// );
223244
let sym = self.tcx.symbol_name(instance).name;
245+
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
224246

225247
let global = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
226-
let llty = self.layout_of(ty).gcc_type(self);
227248
if let Some(global) = self.get_declared_value(sym) {
228-
if self.val_ty(global) != self.type_ptr_to(llty) {
249+
if self.val_ty(global) != self.type_ptr_to(gcc_type) {
229250
span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
230251
}
231252
}
232253

233254
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
234255
let global =
235-
self.declare_global(sym, llty, GlobalKind::Exported, is_tls, fn_attrs.link_section);
256+
self.declare_global(sym, gcc_type, GlobalKind::Exported, is_tls, fn_attrs.link_section);
236257

237258
if !self.tcx.is_reachable_non_generic(def_id) {
238259
#[cfg(feature = "master")]
@@ -241,7 +262,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
241262

242263
global
243264
} else {
244-
check_and_apply_linkage(self, fn_attrs, ty, sym)
265+
check_and_apply_linkage(self, fn_attrs, gcc_type, sym)
245266
};
246267

247268
if !def_id.is_local() {
@@ -355,11 +376,10 @@ fn codegen_static_initializer<'gcc, 'tcx>(
355376
fn check_and_apply_linkage<'gcc, 'tcx>(
356377
cx: &CodegenCx<'gcc, 'tcx>,
357378
attrs: &CodegenFnAttrs,
358-
ty: Ty<'tcx>,
379+
gcc_type: Type<'gcc>,
359380
sym: &str,
360381
) -> LValue<'gcc> {
361382
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
362-
let gcc_type = cx.layout_of(ty).gcc_type(cx);
363383
if let Some(linkage) = attrs.import_linkage {
364384
// Declare a symbol `foo` with the desired linkage.
365385
let global1 =

0 commit comments

Comments
 (0)