2022-08-27 15:39:23 -04:00
|
|
|
#[cfg(feature = "master")]
|
|
|
|
use gccjit::FnAttribute;
|
|
|
|
use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue, Type};
|
2020-05-10 10:54:30 -04:00
|
|
|
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
|
2022-12-13 17:26:17 +01:00
|
|
|
use rustc_middle::span_bug;
|
2020-05-10 10:54:30 -04:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
|
|
|
use rustc_middle::mir::mono::MonoItem;
|
|
|
|
use rustc_middle::ty::{self, Instance, Ty};
|
2021-09-18 00:19:25 +03:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2023-01-25 01:46:19 -05:00
|
|
|
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
|
2020-05-10 10:54:30 -04:00
|
|
|
use rustc_span::def_id::DefId;
|
2023-01-25 01:46:19 -05:00
|
|
|
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
|
2020-05-10 10:54:30 -04:00
|
|
|
|
|
|
|
use crate::base;
|
|
|
|
use crate::context::CodegenCx;
|
2023-03-04 14:26:00 -05:00
|
|
|
use crate::errors::InvalidMinimumAlignment;
|
2020-05-10 10:54:30 -04:00
|
|
|
use crate::type_of::LayoutGccExt;
|
|
|
|
|
|
|
|
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
|
|
|
pub fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
|
|
|
|
if value.get_type() == self.bool_type.make_pointer() {
|
|
|
|
if let Some(pointee) = typ.get_pointee() {
|
2021-12-31 16:26:32 +01:00
|
|
|
if pointee.dyncast_vector().is_some() {
|
2020-05-10 10:54:30 -04:00
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 17:04:24 -05:00
|
|
|
// NOTE: since bitcast makes a value non-constant, don't bitcast if not necessary as some
|
|
|
|
// SIMD builtins require a constant value.
|
2022-04-29 23:14:26 -04:00
|
|
|
self.bitcast_if_needed(value, typ)
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 14:26:00 -05:00
|
|
|
fn set_global_alignment<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, gv: LValue<'gcc>, mut align: Align) {
|
|
|
|
// The target may require greater alignment for globals than the type does.
|
|
|
|
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
|
|
|
|
// which can force it to be smaller. Rust doesn't support this yet.
|
|
|
|
if let Some(min) = cx.sess().target.min_global_align {
|
|
|
|
match Align::from_bits(min) {
|
|
|
|
Ok(min) => align = align.max(min),
|
|
|
|
Err(err) => {
|
|
|
|
cx.sess().emit_err(InvalidMinimumAlignment { err });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gv.set_alignment(align.bytes() as i32);
|
|
|
|
}
|
|
|
|
|
2020-05-10 10:54:30 -04:00
|
|
|
impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
|
|
|
|
fn static_addr_of(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
|
2021-12-31 16:26:32 +01:00
|
|
|
// TODO(antoyo): implement a proper rvalue comparison in libgccjit instead of doing the
|
|
|
|
// following:
|
|
|
|
for (value, variable) in &*self.const_globals.borrow() {
|
|
|
|
if format!("{:?}", value) == format!("{:?}", cv) {
|
2022-03-26 18:29:37 +01:00
|
|
|
if let Some(global_variable) = self.global_lvalues.borrow().get(variable) {
|
|
|
|
let alignment = align.bits() as i32;
|
|
|
|
if alignment > global_variable.get_alignment() {
|
|
|
|
global_variable.set_alignment(alignment);
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 16:26:32 +01:00
|
|
|
return *variable;
|
|
|
|
}
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
let global_value = self.static_addr_of_mut(cv, align, kind);
|
2022-05-26 00:08:50 +09:00
|
|
|
#[cfg(feature = "master")]
|
|
|
|
self.global_lvalues.borrow().get(&global_value)
|
|
|
|
.expect("`static_addr_of_mut` did not add the global to `self.global_lvalues`")
|
|
|
|
.global_set_readonly();
|
2020-05-10 10:54:30 -04:00
|
|
|
self.const_globals.borrow_mut().insert(cv, global_value);
|
|
|
|
global_value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
|
|
|
|
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
|
|
|
|
2021-09-26 12:20:02 -04:00
|
|
|
let value =
|
2020-05-10 10:54:30 -04:00
|
|
|
match codegen_static_initializer(&self, def_id) {
|
2021-09-26 12:20:02 -04:00
|
|
|
Ok((value, _)) => value,
|
2020-05-10 10:54:30 -04:00
|
|
|
// Error has already been reported
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let global = self.get_static(def_id);
|
|
|
|
|
|
|
|
// boolean SSA values are i1, but they have to be stored in i8 slots,
|
|
|
|
// otherwise some LLVM optimization passes don't work as expected
|
|
|
|
let val_llty = self.val_ty(value);
|
|
|
|
let value =
|
|
|
|
if val_llty == self.type_i1() {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value
|
|
|
|
};
|
|
|
|
|
|
|
|
let instance = Instance::mono(self.tcx, def_id);
|
|
|
|
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
2022-07-23 19:12:35 -04:00
|
|
|
let gcc_type = self.layout_of(ty).gcc_type(self);
|
2020-05-10 10:54:30 -04:00
|
|
|
|
2023-03-04 14:26:00 -05:00
|
|
|
set_global_alignment(self, global, self.align_of(ty));
|
2021-09-26 12:20:02 -04:00
|
|
|
|
2022-04-29 23:14:26 -04:00
|
|
|
let value = self.bitcast_if_needed(value, gcc_type);
|
2021-12-31 16:26:32 +01:00
|
|
|
global.global_set_initializer_rvalue(value);
|
2020-05-10 10:54:30 -04:00
|
|
|
|
|
|
|
// As an optimization, all shared statics which do not have interior
|
|
|
|
// mutability are placed into read-only memory.
|
|
|
|
if !is_mutable {
|
|
|
|
if self.type_is_freeze(ty) {
|
2022-05-26 00:08:50 +09:00
|
|
|
#[cfg(feature = "master")]
|
|
|
|
global.global_set_readonly();
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
|
|
|
|
// Do not allow LLVM to change the alignment of a TLS on macOS.
|
|
|
|
//
|
|
|
|
// By default a global's alignment can be freely increased.
|
|
|
|
// This allows LLVM to generate more performant instructions
|
|
|
|
// e.g., using load-aligned into a SIMD register.
|
|
|
|
//
|
|
|
|
// However, on macOS 10.10 or below, the dynamic linker does not
|
|
|
|
// respect any alignment given on the TLS (radar 24221680).
|
|
|
|
// This will violate the alignment assumption, and causing segfault at runtime.
|
|
|
|
//
|
|
|
|
// This bug is very easy to trigger. In `println!` and `panic!`,
|
|
|
|
// the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
|
|
|
|
// which the values would be `mem::replace`d on initialization.
|
|
|
|
// The implementation of `mem::replace` will use SIMD
|
|
|
|
// whenever the size is 32 bytes or higher. LLVM notices SIMD is used
|
|
|
|
// and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
|
|
|
|
// which macOS's dyld disregarded and causing crashes
|
|
|
|
// (see issues #51794, #51758, #50867, #48866 and #44056).
|
|
|
|
//
|
|
|
|
// To workaround the bug, we trick LLVM into not increasing
|
|
|
|
// the global's alignment by explicitly assigning a section to it
|
|
|
|
// (equivalent to automatically generating a `#[link_section]` attribute).
|
|
|
|
// See the comment in the `GlobalValue::canIncreaseAlignment()` function
|
|
|
|
// of `lib/IR/Globals.cpp` for why this works.
|
|
|
|
//
|
|
|
|
// When the alignment is not increased, the optimized `mem::replace`
|
|
|
|
// will use load-unaligned instructions instead, and thus avoiding the crash.
|
|
|
|
//
|
|
|
|
// We could remove this hack whenever we decide to drop macOS 10.10 support.
|
|
|
|
if self.tcx.sess.target.options.is_like_osx {
|
2022-08-27 14:11:19 -04:00
|
|
|
// The `inspect` method is okay here because we checked for provenance, and
|
2020-05-10 10:54:30 -04:00
|
|
|
// because we are doing this access to inspect the final interpreter state
|
|
|
|
// (not as part of the interpreter execution).
|
|
|
|
//
|
|
|
|
// FIXME: This check requires that the (arbitrary) value of undefined bytes
|
|
|
|
// happens to be zero. Instead, we should only check the value of defined bytes
|
|
|
|
// and set all undefined bytes to zero if this allocation is headed for the
|
|
|
|
// BSS.
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wasm statics with custom link sections get special treatment as they
|
|
|
|
// go into custom sections of the wasm executable.
|
|
|
|
if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
|
|
|
|
if let Some(_section) = attrs.link_section {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo): set link section.
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
|
2021-11-22 13:14:54 +01:00
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::USED) || attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) {
|
2021-09-26 12:20:02 -04:00
|
|
|
self.add_used_global(global.to_rvalue());
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
|
|
|
|
fn add_used_global(&self, _global: RValue<'gcc>) {
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo)
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
2021-09-18 00:19:25 +03:00
|
|
|
|
2022-08-27 15:39:23 -04:00
|
|
|
fn add_compiler_used_global(&self, global: RValue<'gcc>) {
|
|
|
|
// NOTE: seems like GCC does not make the distinction between compiler.used and used.
|
|
|
|
self.add_used_global(global);
|
2021-09-18 00:19:25 +03:00
|
|
|
}
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
2022-08-27 15:39:23 -04:00
|
|
|
#[cfg_attr(not(feature="master"), allow(unused_variables))]
|
|
|
|
pub fn add_used_function(&self, function: Function<'gcc>) {
|
|
|
|
#[cfg(feature = "master")]
|
|
|
|
function.add_attribute(FnAttribute::Used);
|
|
|
|
}
|
|
|
|
|
2020-05-10 10:54:30 -04:00
|
|
|
pub fn static_addr_of_mut(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
|
2021-09-26 12:20:02 -04:00
|
|
|
let global =
|
2020-05-10 10:54:30 -04:00
|
|
|
match kind {
|
|
|
|
Some(kind) if !self.tcx.sess.fewer_names() => {
|
|
|
|
let name = self.generate_local_symbol_name(kind);
|
2022-03-26 18:29:37 +01:00
|
|
|
// TODO(antoyo): check if it's okay that no link_section is set.
|
2022-02-06 17:04:24 -05:00
|
|
|
|
|
|
|
let typ = self.val_ty(cv).get_aligned(align.bytes());
|
|
|
|
let global = self.declare_private_global(&name[..], typ);
|
2021-09-26 12:20:02 -04:00
|
|
|
global
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let typ = self.val_ty(cv).get_aligned(align.bytes());
|
2021-09-26 12:20:02 -04:00
|
|
|
let global = self.declare_unnamed_global(typ);
|
|
|
|
global
|
2020-05-10 10:54:30 -04:00
|
|
|
},
|
|
|
|
};
|
2021-12-31 16:26:32 +01:00
|
|
|
global.global_set_initializer_rvalue(cv);
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo): set unnamed address.
|
2022-03-26 18:29:37 +01:00
|
|
|
let rvalue = global.get_address(None);
|
|
|
|
self.global_lvalues.borrow_mut().insert(rvalue, global);
|
|
|
|
rvalue
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-26 12:20:02 -04:00
|
|
|
pub fn get_static(&self, def_id: DefId) -> LValue<'gcc> {
|
2020-05-10 10:54:30 -04:00
|
|
|
let instance = Instance::mono(self.tcx, def_id);
|
|
|
|
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
|
|
|
|
if let Some(&global) = self.instances.borrow().get(&instance) {
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
let defined_in_current_codegen_unit =
|
|
|
|
self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
|
|
|
|
assert!(
|
|
|
|
!defined_in_current_codegen_unit,
|
|
|
|
"consts::get_static() should always hit the cache for \
|
|
|
|
statics defined in the same CGU, but did not for `{:?}`",
|
|
|
|
def_id
|
|
|
|
);
|
|
|
|
|
|
|
|
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
|
|
|
let sym = self.tcx.symbol_name(instance).name;
|
|
|
|
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
let global =
|
|
|
|
if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
|
2022-12-13 17:26:17 +01:00
|
|
|
let llty = self.layout_of(ty).gcc_type(self);
|
|
|
|
if let Some(global) = self.get_declared_value(sym) {
|
|
|
|
if self.val_ty(global) != self.type_ptr_to(llty) {
|
|
|
|
span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 10:54:30 -04:00
|
|
|
|
2022-12-13 17:26:17 +01:00
|
|
|
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
|
|
|
let global = self.declare_global(
|
|
|
|
&sym,
|
|
|
|
llty,
|
|
|
|
GlobalKind::Exported,
|
|
|
|
is_tls,
|
|
|
|
fn_attrs.link_section,
|
|
|
|
);
|
|
|
|
|
|
|
|
if !self.tcx.is_reachable_non_generic(def_id) {
|
|
|
|
// TODO(antoyo): set visibility.
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
2022-12-13 17:26:17 +01:00
|
|
|
|
|
|
|
global
|
|
|
|
} else {
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
check_and_apply_linkage(&self, &fn_attrs, ty, sym)
|
2022-12-13 17:26:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if !def_id.is_local() {
|
|
|
|
let needs_dll_storage_attr = false; // TODO(antoyo)
|
|
|
|
|
|
|
|
// If this assertion triggers, there's something wrong with commandline
|
|
|
|
// argument validation.
|
|
|
|
debug_assert!(
|
|
|
|
!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
|
|
|
|
&& self.tcx.sess.target.options.is_like_msvc
|
|
|
|
&& self.tcx.sess.opts.cg.prefer_dynamic)
|
|
|
|
);
|
|
|
|
|
|
|
|
if needs_dll_storage_attr {
|
|
|
|
// This item is external but not foreign, i.e., it originates from an external Rust
|
|
|
|
// crate. Since we don't know whether this crate will be linked dynamically or
|
|
|
|
// statically in the final application, we always mark such symbols as 'dllimport'.
|
|
|
|
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
|
|
|
|
// to make things work.
|
|
|
|
//
|
|
|
|
// However, in some scenarios we defer emission of statics to downstream
|
|
|
|
// crates, so there are cases where a static with an upstream DefId
|
|
|
|
// is actually present in the current crate. We can find out via the
|
|
|
|
// is_codegened_item query.
|
|
|
|
if !self.tcx.is_codegened_item(def_id) {
|
|
|
|
unimplemented!();
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
2022-12-13 17:26:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 10:54:30 -04:00
|
|
|
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo): set dll storage class.
|
2020-05-10 10:54:30 -04:00
|
|
|
|
|
|
|
self.instances.borrow_mut().insert(instance, global);
|
|
|
|
global
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAllocation<'tcx>) -> RValue<'gcc> {
|
|
|
|
let alloc = alloc.inner();
|
2022-11-06 14:15:20 +01:00
|
|
|
let mut llvals = Vec::with_capacity(alloc.provenance().ptrs().len() + 1);
|
2020-05-10 10:54:30 -04:00
|
|
|
let dl = cx.data_layout();
|
|
|
|
let pointer_size = dl.pointer_size.bytes() as usize;
|
|
|
|
|
|
|
|
let mut next_offset = 0;
|
2022-11-06 14:15:20 +01:00
|
|
|
for &(offset, alloc_id) in alloc.provenance().ptrs().iter() {
|
2020-05-10 10:54:30 -04:00
|
|
|
let offset = offset.bytes();
|
|
|
|
assert_eq!(offset as usize as u64, offset);
|
|
|
|
let offset = offset as usize;
|
|
|
|
if offset > next_offset {
|
2022-08-27 14:11:19 -04:00
|
|
|
// This `inspect` is okay since we have checked that it is not within a pointer with provenance, it
|
2020-05-10 10:54:30 -04:00
|
|
|
// is within the bounds of the allocation, and it doesn't affect interpreter execution
|
|
|
|
// (we inspect the result after interpreter execution). Any undef byte is replaced with
|
|
|
|
// some arbitrary byte value.
|
|
|
|
//
|
|
|
|
// FIXME: relay undef bytes to codegen as undef const bytes
|
|
|
|
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(next_offset..offset);
|
|
|
|
llvals.push(cx.const_bytes(bytes));
|
|
|
|
}
|
|
|
|
let ptr_offset =
|
|
|
|
read_target_uint( dl.endian,
|
|
|
|
// This `inspect` is okay since it is within the bounds of the allocation, it doesn't
|
|
|
|
// affect interpreter execution (we inspect the result after interpreter execution),
|
2022-08-27 14:11:19 -04:00
|
|
|
// and we properly interpret the provenance as a relocation pointer offset.
|
2020-05-10 10:54:30 -04:00
|
|
|
alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
|
|
|
|
)
|
|
|
|
.expect("const_alloc_to_llvm: could not read relocation pointer")
|
|
|
|
as u64;
|
2023-01-22 23:03:58 -05:00
|
|
|
|
2023-01-25 01:46:19 -05:00
|
|
|
let address_space = cx.tcx.global_alloc(alloc_id).address_space(cx);
|
2023-01-22 23:03:58 -05:00
|
|
|
|
2020-05-10 10:54:30 -04:00
|
|
|
llvals.push(cx.scalar_to_backend(
|
|
|
|
InterpScalar::from_pointer(
|
|
|
|
interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
|
|
|
|
&cx.tcx,
|
|
|
|
),
|
2023-01-22 23:03:58 -05:00
|
|
|
abi::Scalar::Initialized { value: Primitive::Pointer(address_space), valid_range: WrappingRange::full(dl.pointer_size) },
|
|
|
|
cx.type_i8p_ext(address_space),
|
2020-05-10 10:54:30 -04:00
|
|
|
));
|
|
|
|
next_offset = offset + pointer_size;
|
|
|
|
}
|
|
|
|
if alloc.len() >= next_offset {
|
|
|
|
let range = next_offset..alloc.len();
|
2022-08-27 14:11:19 -04:00
|
|
|
// This `inspect` is okay since we have check that it is after all provenance, it is
|
2020-05-10 10:54:30 -04:00
|
|
|
// within the bounds of the allocation, and it doesn't affect interpreter execution (we
|
|
|
|
// inspect the result after interpreter execution). Any undef byte is replaced with some
|
|
|
|
// arbitrary byte value.
|
|
|
|
//
|
|
|
|
// FIXME: relay undef bytes to codegen as undef const bytes
|
|
|
|
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
|
|
|
|
llvals.push(cx.const_bytes(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.const_struct(&llvals, true)
|
|
|
|
}
|
|
|
|
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, ConstAllocation<'tcx>), ErrorHandled> {
|
2020-05-10 10:54:30 -04:00
|
|
|
let alloc = cx.tcx.eval_static_initializer(def_id)?;
|
|
|
|
Ok((const_alloc_to_gcc(cx, alloc), alloc))
|
|
|
|
}
|
|
|
|
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &CodegenFnAttrs, ty: Ty<'tcx>, sym: &str) -> LValue<'gcc> {
|
2020-05-10 10:54:30 -04:00
|
|
|
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
2023-03-02 17:15:57 -05:00
|
|
|
let gcc_type = cx.layout_of(ty).gcc_type(cx);
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
if let Some(linkage) = attrs.import_linkage {
|
2020-05-10 10:54:30 -04:00
|
|
|
// Declare a symbol `foo` with the desired linkage.
|
Move linkage type check to HIR analysis and fix semantics issues.
This ensures that the error is printed even for unused variables,
as well as unifying the handling between the LLVM and GCC backends.
This also fixes unusual behavior around exported Rust-defined variables
with linkage attributes. With the previous behavior, it appears to be
impossible to define such a variable such that it can actually be imported
and used by another crate. This is because on the importing side, the
variable is required to be a pointer, but on the exporting side, the
type checker rejects static variables of pointer type because they do
not implement `Sync`. Even if it were possible to import such a type, it
appears that code generation on the importing side would add an unexpected
additional level of pointer indirection, which would break type safety.
This highlighted that the semantics of linkage on Rust-defined variables
is different to linkage on foreign items. As such, we now model the
difference with two different codegen attributes: linkage for Rust-defined
variables, and import_linkage for foreign items.
This change gives semantics to the test
src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs which was
previously expected to fail to compile. Therefore, convert it into a
test that is expected to successfully compile.
The update to the GCC backend is speculative and untested.
2022-11-23 18:13:30 -08:00
|
|
|
let global1 = cx.declare_global_with_linkage(&sym, cx.type_i8(), base::global_linkage_to_gcc(linkage));
|
2020-05-10 10:54:30 -04:00
|
|
|
|
|
|
|
// Declare an internal global `extern_with_linkage_foo` which
|
|
|
|
// is initialized with the address of `foo`. If `foo` is
|
|
|
|
// discarded during linking (for example, if `foo` has weak
|
|
|
|
// linkage and there are no definitions), then
|
|
|
|
// `extern_with_linkage_foo` will instead be initialized to
|
|
|
|
// zero.
|
|
|
|
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
|
|
|
real_name.push_str(&sym);
|
2023-03-02 17:15:57 -05:00
|
|
|
let global2 = cx.define_global(&real_name, gcc_type, is_tls, attrs.link_section);
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo): set linkage.
|
2023-03-02 17:15:57 -05:00
|
|
|
let value = cx.const_ptrcast(global1.get_address(None), gcc_type);
|
|
|
|
global2.global_set_initializer_rvalue(value);
|
2021-08-15 08:28:46 -04:00
|
|
|
// TODO(antoyo): use global_set_initializer() when it will work.
|
2020-05-10 10:54:30 -04:00
|
|
|
global2
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Generate an external declaration.
|
|
|
|
// FIXME(nagisa): investigate whether it can be changed into define_global
|
|
|
|
|
|
|
|
// Thread-local statics in some other crate need to *always* be linked
|
|
|
|
// against in a thread-local fashion, so we need to be sure to apply the
|
|
|
|
// thread-local attribute locally if it was present remotely. If we
|
|
|
|
// don't do this then linker errors can be generated where the linker
|
|
|
|
// complains that one object files has a thread local version of the
|
|
|
|
// symbol and another one doesn't.
|
2023-03-02 17:15:57 -05:00
|
|
|
cx.declare_global(&sym, gcc_type, GlobalKind::Imported, is_tls, attrs.link_section)
|
2020-05-10 10:54:30 -04:00
|
|
|
}
|
|
|
|
}
|