Skip to content

Fix/int types alignment #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
9 changes: 6 additions & 3 deletions src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use gccjit::{FnAttribute, ToLValue, ToRValue, Type};
#[cfg(feature = "master")]
use gccjit::FnAttribute;
use gccjit::{ToLValue, ToRValue, Type};
use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
use rustc_data_structures::fx::FxHashSet;
use rustc_middle::bug;
Expand Down Expand Up @@ -101,6 +103,7 @@ pub struct FnAbiGcc<'gcc> {
pub arguments_type: Vec<Type<'gcc>>,
pub is_c_variadic: bool,
pub on_stack_param_indices: FxHashSet<usize>,
#[cfg(feature = "master")]
pub fn_attributes: Vec<FnAttribute<'gcc>>,
}

Expand Down Expand Up @@ -129,6 +132,7 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
cx.type_void()
}
};
#[cfg(feature = "master")]
let mut non_null_args = Vec::new();

#[cfg(feature = "master")]
Expand Down Expand Up @@ -190,14 +194,13 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
} else {
vec![FnAttribute::NonNull(non_null_args)]
};
#[cfg(not(feature = "master"))]
let fn_attrs = Vec::new();

FnAbiGcc {
return_type,
arguments_type: argument_tys,
is_c_variadic: self.c_variadic,
on_stack_param_indices,
#[cfg(feature = "master")]
fn_attributes: fn_attrs,
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,35 +424,35 @@ impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
}

fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.i8_type
self.is_compatible_with(cx.i8_type)
}

fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.u8_type
self.is_compatible_with(cx.u8_type)
}

fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.i16_type
self.is_compatible_with(cx.i16_type)
}

fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.u16_type
self.is_compatible_with(cx.u16_type)
}

fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.i32_type
self.is_compatible_with(cx.i32_type)
}

fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.u32_type
self.is_compatible_with(cx.u32_type)
}

fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.i64_type
self.is_compatible_with(cx.i64_type)
}

fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
self.unqualified() == cx.u64_type
self.is_compatible_with(cx.u64_type)
}

fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
Expand Down
40 changes: 30 additions & 10 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,39 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>, supports_128bit_integers: bool) -> Self {
let check_overflow = tcx.sess.overflow_checks();

let i8_type = context.new_c_type(CType::Int8t);
let i16_type = context.new_c_type(CType::Int16t);
let i32_type = context.new_c_type(CType::Int32t);
let i64_type = context.new_c_type(CType::Int64t);
let u8_type = context.new_c_type(CType::UInt8t);
let u16_type = context.new_c_type(CType::UInt16t);
let u32_type = context.new_c_type(CType::UInt32t);
let u64_type = context.new_c_type(CType::UInt64t);
let create_type = |ctype, rust_type| {
let layout = tcx.layout_of(ParamEnv::reveal_all().and(rust_type)).unwrap();
let align = layout.align.abi.bytes();
#[cfg(feature="master")]
{
context.new_c_type(ctype).get_aligned(align)
}
#[cfg(not(feature="master"))]
{
// Since libgccjit 12 doesn't contain the fix to compare aligned integer types,
// only align u128 and i128.
if layout.ty.int_size_and_signed(tcx).0.bytes() == 16 {
context.new_c_type(ctype).get_aligned(align)
}
else {
context.new_c_type(ctype)
}
}
};

let i8_type = create_type(CType::Int8t, tcx.types.i8);
let i16_type = create_type(CType::Int16t, tcx.types.i16);
let i32_type = create_type(CType::Int32t, tcx.types.i32);
let i64_type = create_type(CType::Int64t, tcx.types.i64);
let u8_type = create_type(CType::UInt8t, tcx.types.u8);
let u16_type = create_type(CType::UInt16t, tcx.types.u16);
let u32_type = create_type(CType::UInt32t, tcx.types.u32);
let u64_type = create_type(CType::UInt64t, tcx.types.u64);

let (i128_type, u128_type) =
if supports_128bit_integers {
let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
let i128_type = create_type(CType::Int128t, tcx.types.i128);
let u128_type = create_type(CType::UInt128t, tcx.types.u128);
(i128_type, u128_type)
}
else {
Expand Down
2 changes: 2 additions & 0 deletions src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
arguments_type,
is_c_variadic,
on_stack_param_indices,
#[cfg(feature="master")]
fn_attributes,
} = fn_abi.gcc_type(self);
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &arguments_type, is_c_variadic);
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
#[cfg(feature="master")]
for fn_attr in fn_attributes {
func.add_attribute(fn_attr);
}
Expand Down