Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/rustc_codegen_gcc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,14 @@ impl<'a, 'gcc, 'tcx> Deref for Builder<'a, 'gcc, 'tcx> {
}

impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
type Metadata = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Metadata;
type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;

type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
type FunctionSignature = <CodegenCx<'gcc, 'tcx> as BackendTypes>::FunctionSignature;

type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
Expand Down
34 changes: 10 additions & 24 deletions compiler/rustc_codegen_gcc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use rustc_codegen_ssa::traits::{
BaseTypeCodegenMethods, ConstCodegenMethods, MiscCodegenMethods, StaticCodegenMethods,
};
use rustc_middle::mir::Mutability;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, PointerArithmetic, Scalar};
use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar};
use rustc_middle::ty::layout::LayoutOf;

use crate::consts::const_alloc_to_gcc;
use crate::context::{CodegenCx, new_array_type};
use crate::type_of::LayoutGccExt;

Expand Down Expand Up @@ -260,11 +261,13 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
};
}

let init = self.const_data_from_alloc(alloc);
let alloc = alloc.inner();
let value = match alloc.mutability {
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
_ => self.static_addr_of(init, alloc.align, None),
let value = match alloc.inner().mutability {
Mutability::Mut => self.static_addr_of_mut(
const_alloc_to_gcc(self, alloc),
alloc.inner().align,
None,
),
_ => self.static_addr_of(alloc, None),
};
if !self.sess().fewer_names() {
// TODO(antoyo): set value name.
Expand All @@ -282,8 +285,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
}),
)))
.unwrap_memory();
let init = self.const_data_from_alloc(alloc);
self.static_addr_of(init, alloc.inner().align, None)
self.static_addr_of(alloc, None)
}
GlobalAlloc::TypeId { .. } => {
let val = self.const_usize(offset.bytes());
Expand Down Expand Up @@ -311,22 +313,6 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
}
}

fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
// We ignore the alignment for the purpose of deduping RValues
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
// so it is OK to overwrite it here.
let mut mock_alloc = alloc.inner().clone();
mock_alloc.align = rustc_abi::Align::MAX;
// Check if the rvalue is already in the cache - if so, just return it directly.
if let Some(res) = self.const_cache.borrow().get(&mock_alloc) {
return *res;
}
// Rvalue not in the cache - convert and add it.
let res = crate::consts::const_alloc_to_gcc_uncached(self, alloc);
self.const_cache.borrow_mut().insert(mock_alloc, res);
res
}

fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
self.context
.new_array_access(None, base_addr, self.const_usize(offset.bytes()))
Expand Down
26 changes: 24 additions & 2 deletions compiler/rustc_codegen_gcc/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ use crate::base;
use crate::context::CodegenCx;
use crate::type_of::LayoutGccExt;

pub(crate) fn const_alloc_to_gcc<'gcc, 'tcx>(
cx: &CodegenCx<'gcc, 'tcx>,
alloc: ConstAllocation<'_>,
) -> RValue<'gcc> {
// We ignore the alignment for the purpose of deduping RValues
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
// so it is OK to overwrite it here.
let mut mock_alloc = alloc.inner().clone();
mock_alloc.align = rustc_abi::Align::MAX;
// Check if the rvalue is already in the cache - if so, just return it directly.
if let Some(res) = cx.const_cache.borrow().get(&mock_alloc) {
return *res;
}
// Rvalue not in the cache - convert and add it.
let res = crate::consts::const_alloc_to_gcc_uncached(cx, alloc);
cx.const_cache.borrow_mut().insert(mock_alloc, res);
res
}

fn set_global_alignment<'gcc, 'tcx>(
cx: &CodegenCx<'gcc, 'tcx>,
gv: LValue<'gcc>,
Expand All @@ -37,7 +56,10 @@ fn set_global_alignment<'gcc, 'tcx>(
}

impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
fn static_addr_of(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> RValue<'gcc> {
let cv = const_alloc_to_gcc(self, alloc);
let align = alloc.inner().align;

if let Some(variable) = self.const_globals.borrow().get(&cv) {
if let Some(global_variable) = self.global_lvalues.borrow().get(variable) {
let alignment = align.bits() as i32;
Expand Down Expand Up @@ -361,7 +383,7 @@ fn codegen_static_initializer<'gcc, 'tcx>(
def_id: DefId,
) -> Result<(RValue<'gcc>, ConstAllocation<'tcx>), ErrorHandled> {
let alloc = cx.tcx.eval_static_initializer(def_id)?;
Ok((cx.const_data_from_alloc(alloc), alloc))
Ok((const_alloc_to_gcc(cx, alloc), alloc))
}

fn check_and_apply_linkage<'gcc, 'tcx>(
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}

impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> {
type Value = RValue<'gcc>;
type Metadata = RValue<'gcc>;
type Function = Function<'gcc>;

type BasicBlock = Block<'gcc>;
type Type = Type<'gcc>;
type Funclet = (); // TODO(antoyo)

type Value = RValue<'gcc>;
type Type = Type<'gcc>;
type FunctionSignature = Type<'gcc>;

type DIScope = (); // TODO(antoyo)
type DILocation = Location<'gcc>;
type DIVariable = (); // TODO(antoyo)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
&mut self,
_vtable: Self::Value,
_vtable_byte_offset: u64,
_typeid: Self::Value,
_typeid: &[u8],
) -> Self::Value {
// Unsupported.
self.context.new_rvalue_from_int(self.int_type, 0)
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
pub(crate) const UNNAMED: *const c_char = c"".as_ptr();

impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericBuilder<'_, 'll, CX> {
type Value = <GenericCx<'ll, CX> as BackendTypes>::Value;
type Metadata = <GenericCx<'ll, CX> as BackendTypes>::Metadata;
type Function = <GenericCx<'ll, CX> as BackendTypes>::Function;
type BasicBlock = <GenericCx<'ll, CX> as BackendTypes>::BasicBlock;
type Type = <GenericCx<'ll, CX> as BackendTypes>::Type;
type Funclet = <GenericCx<'ll, CX> as BackendTypes>::Funclet;

type Value = <GenericCx<'ll, CX> as BackendTypes>::Value;
type Type = <GenericCx<'ll, CX> as BackendTypes>::Type;
type FunctionSignature = <GenericCx<'ll, CX> as BackendTypes>::FunctionSignature;

type DIScope = <GenericCx<'ll, CX> as BackendTypes>::DIScope;
type DILocation = <GenericCx<'ll, CX> as BackendTypes>::DILocation;
type DIVariable = <GenericCx<'ll, CX> as BackendTypes>::DIVariable;
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hashes::Hash128;
use rustc_hir::def_id::DefId;
use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, PointerArithmetic, Scalar};
use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar};
use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::DllImport;
use tracing::debug;

use crate::consts::const_alloc_to_llvm;
pub(crate) use crate::context::CodegenCx;
use crate::context::{GenericCx, SCx};
use crate::llvm::{self, BasicBlock, ConstantInt, FALSE, Metadata, TRUE, ToLlvmBool, Type, Value};
use crate::llvm::{self, BasicBlock, ConstantInt, FALSE, TRUE, ToLlvmBool, Type, Value};

/*
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
Expand Down Expand Up @@ -82,15 +82,15 @@ impl<'ll> Funclet<'ll> {
}

impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericCx<'ll, CX> {
type Value = &'ll Value;
type Metadata = &'ll Metadata;
// FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
type Function = &'ll Value;

type BasicBlock = &'ll BasicBlock;
type Type = &'ll Type;
type Funclet = Funclet<'ll>;

type Value = &'ll Value;
type Type = &'ll Type;
type FunctionSignature = &'ll Type;

type DIScope = &'ll llvm::debuginfo::DIScope;
type DILocation = &'ll llvm::debuginfo::DILocation;
type DIVariable = &'ll llvm::debuginfo::DIVariable;
Expand Down Expand Up @@ -359,10 +359,6 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
}
}

fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
const_alloc_to_llvm(self, alloc.inner(), /*static*/ false)
}

fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
unsafe {
llvm::LLVMConstInBoundsGEP2(
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,12 @@ impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
///
/// The pointer will always be in the default address space. If global variables default to a
/// different address space, an addrspacecast is inserted.
fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
let gv = self.static_addr_of_impl(cv, align, kind);
fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> &'ll Value {
// FIXME: should we cache `const_alloc_to_llvm` to avoid repeating this for the
// same `ConstAllocation`?
let cv = const_alloc_to_llvm(self, alloc.inner(), /*static*/ false);

let gv = self.static_addr_of_impl(cv, alloc.inner().align, kind);
// static_addr_of_impl returns the bare global variable, which might not be in the default
// address space. Cast to the default address space if necessary.
self.const_pointercast(gv, self.type_ptr())
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::declare::declare_raw_fn;
use crate::errors::{
AutoDiffWithoutEnable, AutoDiffWithoutLto, OffloadWithoutEnable, OffloadWithoutFatLTO,
};
use crate::llvm::{self, Metadata, Type, Value};
use crate::llvm::{self, Type, Value};
use crate::type_of::LayoutLlvmExt;
use crate::va_arg::emit_va_arg;

Expand Down Expand Up @@ -799,8 +799,9 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
&mut self,
llvtable: &'ll Value,
vtable_byte_offset: u64,
typeid: &'ll Metadata,
typeid: &[u8],
) -> Self::Value {
let typeid = self.create_metadata(typeid);
let typeid = self.get_metadata_value(typeid);
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
let type_checked_load = self.call_intrinsic(
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_target::callconv::{CastTarget, FnAbi};
use crate::abi::{FnAbiLlvmExt, LlvmType};
use crate::common;
use crate::context::{CodegenCx, GenericCx, SCx};
use crate::llvm::{self, FALSE, Metadata, TRUE, ToGeneric, ToLlvmBool, Type, Value};
use crate::llvm::{self, FALSE, TRUE, ToGeneric, ToLlvmBool, Type, Value};
use crate::type_of::LayoutLlvmExt;

impl PartialEq for Type {
Expand Down Expand Up @@ -319,10 +319,6 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
self.global_set_metadata_node(function, llvm::MD_type, &v);
}

fn typeid_metadata(&self, typeid: &[u8]) -> Option<&'ll Metadata> {
Some(self.create_metadata(typeid))
}

fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
self.global_add_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_codegen_ssa/src/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ pub(crate) fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(

let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
let align = cx.data_layout().pointer_align().abi;
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
let vtable = cx.static_addr_of(vtable_allocation, Some("vtable"));

cx.apply_vcall_visibility_metadata(ty, trait_ref, vtable);
cx.create_vtable_debuginfo(ty, trait_ref, vtable);
Expand All @@ -139,9 +137,8 @@ pub(crate) fn load_vtable<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
&& bx.cx().sess().lto() == Lto::Fat
{
if let Some(trait_ref) = dyn_trait_in_self(bx.tcx(), ty) {
let typeid =
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), trait_ref).as_bytes()).unwrap();
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
let typeid = typeid_for_trait_ref(bx.tcx(), trait_ref);
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid.as_bytes());
return func;
} else if nonnull {
bug!("load nonnull value from a vtable without a principal trait")
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_codegen_ssa/src/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
_ if layout.is_zst() => OperandRef::zero_sized(layout),
_ => {
// Neither a scalar nor scalar pair. Load from a place
// FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
// same `ConstAllocation`?
let init = bx.const_data_from_alloc(alloc);
let base_addr = bx.static_addr_of(init, alloc_align, None);
let base_addr = bx.static_addr_of(alloc, None);

let llval = bx.const_ptr_byte_offset(base_addr, offset);
bx.load_operand(PlaceRef::new_sized(llval, layout))
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ use crate::back::write::TargetMachineFactoryFn;
use crate::{CodegenResults, ModuleCodegen, TargetConfig};

pub trait BackendTypes {
type Value: CodegenObject + PartialEq;
type Metadata: CodegenObject;
type Function: CodegenObject;

type BasicBlock: Copy;
type Type: CodegenObject + PartialEq;
type Funclet;

type Value: CodegenObject + PartialEq;
type Type: CodegenObject + PartialEq;
type FunctionSignature: CodegenObject + PartialEq;

// FIXME(eddyb) find a common convention for all of the debuginfo-related
// names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
type DIScope: Copy + Hash + PartialEq + Eq;
Expand Down Expand Up @@ -137,7 +137,7 @@ pub trait CodegenBackend {
}

pub trait ExtraBackendMethods:
CodegenBackend + WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
WriteBackendMethods + Sized + Send + Sync + DynSend + DynSync
{
fn codegen_allocator<'tcx>(
&self,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ pub trait BuilderMethods<'a, 'tcx>:
type CodegenCx: CodegenMethods<
'tcx,
Value = Self::Value,
Metadata = Self::Metadata,
Function = Self::Function,
BasicBlock = Self::BasicBlock,
Type = Self::Type,
FunctionSignature = Self::FunctionSignature,
Funclet = Self::Funclet,
DIScope = Self::DIScope,
DILocation = Self::DILocation,
Expand Down Expand Up @@ -125,7 +125,7 @@ pub trait BuilderMethods<'a, 'tcx>:

fn invoke(
&mut self,
llty: Self::Type,
llty: Self::FunctionSignature,
fn_attrs: Option<&CodegenFnAttrs>,
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
llfn: Self::Value,
Expand Down Expand Up @@ -622,7 +622,7 @@ pub trait BuilderMethods<'a, 'tcx>:
/// assuming the function does not explicitly pass the destination as a pointer in `args`.
fn call(
&mut self,
llty: Self::Type,
llty: Self::FunctionSignature,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
fn_val: Self::Value,
Expand All @@ -633,7 +633,7 @@ pub trait BuilderMethods<'a, 'tcx>:

fn tail_call(
&mut self,
llty: Self::Type,
llty: Self::FunctionSignature,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
llfn: Self::Value,
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_codegen_ssa/src/traits/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_abi as abi;
use rustc_middle::mir::interpret::{ConstAllocation, Scalar};
use rustc_middle::mir::interpret::Scalar;

use super::BackendTypes;

Expand Down Expand Up @@ -37,8 +37,6 @@ pub trait ConstCodegenMethods: BackendTypes {
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;
fn const_to_opt_u128(&self, v: Self::Value, sign_ext: bool) -> Option<u128>;

fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value;

fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: Self::Type) -> Self::Value;

fn const_ptr_byte_offset(&self, val: Self::Value, offset: abi::Size) -> Self::Value;
Expand Down
Loading
Loading