From e5fa9f869287f85ddc6b3d72457d8c56669638f6 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 7 Oct 2023 15:13:55 -0400 Subject: [PATCH 1/2] Use the correct alignment for integer types --- src/common.rs | 16 ++++++++-------- src/context.rs | 43 +++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/common.rs b/src/common.rs index 5f54cb16d8e..93fe27e547a 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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 { diff --git a/src/context.rs b/src/context.rs index dcebd92a61c..b01ac7b57af 100644 --- a/src/context.rs +++ b/src/context.rs @@ -129,19 +129,25 @@ 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(); + context.new_c_type(ctype).get_aligned(align) + }; + + 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 { @@ -265,15 +271,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { } pub fn is_native_int_type(&self, typ: Type<'gcc>) -> bool { + // TODO: cache those types to not query libgccjit everytime this is called. let types = [ - self.u8_type, - self.u16_type, - self.u32_type, - self.u64_type, - self.i8_type, - self.i16_type, - self.i32_type, - self.i64_type, + self.context.new_c_type(CType::UInt8t), + self.context.new_c_type(CType::UInt16t), + self.context.new_c_type(CType::UInt32t), + self.context.new_c_type(CType::UInt64t), + self.context.new_c_type(CType::Int8t), + self.context.new_c_type(CType::Int16t), + self.context.new_c_type(CType::Int32t), + self.context.new_c_type(CType::Int64t), ]; for native_type in types { From 9d5e0ba1f51f61ba8ccaa6c37eef25ac497ea70c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 17 Oct 2023 19:43:20 -0400 Subject: [PATCH 2/2] Fixes including fixing compilation for --no-default-features --- src/abi.rs | 9 ++++++--- src/context.rs | 33 +++++++++++++++++++++++---------- src/declare.rs | 2 ++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/abi.rs b/src/abi.rs index 5600f1ba8a9..f601cd95f2a 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -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; @@ -101,6 +103,7 @@ pub struct FnAbiGcc<'gcc> { pub arguments_type: Vec>, pub is_c_variadic: bool, pub on_stack_param_indices: FxHashSet, + #[cfg(feature = "master")] pub fn_attributes: Vec>, } @@ -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")] @@ -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, } } diff --git a/src/context.rs b/src/context.rs index b01ac7b57af..243556a0e52 100644 --- a/src/context.rs +++ b/src/context.rs @@ -132,7 +132,21 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { let create_type = |ctype, rust_type| { let layout = tcx.layout_of(ParamEnv::reveal_all().and(rust_type)).unwrap(); let align = layout.align.abi.bytes(); - context.new_c_type(ctype).get_aligned(align) + #[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); @@ -271,16 +285,15 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { } pub fn is_native_int_type(&self, typ: Type<'gcc>) -> bool { - // TODO: cache those types to not query libgccjit everytime this is called. let types = [ - self.context.new_c_type(CType::UInt8t), - self.context.new_c_type(CType::UInt16t), - self.context.new_c_type(CType::UInt32t), - self.context.new_c_type(CType::UInt64t), - self.context.new_c_type(CType::Int8t), - self.context.new_c_type(CType::Int16t), - self.context.new_c_type(CType::Int32t), - self.context.new_c_type(CType::Int64t), + self.u8_type, + self.u16_type, + self.u32_type, + self.u64_type, + self.i8_type, + self.i16_type, + self.i32_type, + self.i64_type, ]; for native_type in types { diff --git a/src/declare.rs b/src/declare.rs index 0b583c074dd..247454fa58e 100644 --- a/src/declare.rs +++ b/src/declare.rs @@ -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); }