Skip to content

Commit d64662f

Browse files
authored
Merge pull request #353 from rust-lang/fix/int-types-alignment
Fix/int types alignment
2 parents fabdc1a + 9d5e0ba commit d64662f

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

src/abi.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use gccjit::{FnAttribute, ToLValue, ToRValue, Type};
1+
#[cfg(feature = "master")]
2+
use gccjit::FnAttribute;
3+
use gccjit::{ToLValue, ToRValue, Type};
24
use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
35
use rustc_data_structures::fx::FxHashSet;
46
use rustc_middle::bug;
@@ -101,6 +103,7 @@ pub struct FnAbiGcc<'gcc> {
101103
pub arguments_type: Vec<Type<'gcc>>,
102104
pub is_c_variadic: bool,
103105
pub on_stack_param_indices: FxHashSet<usize>,
106+
#[cfg(feature = "master")]
104107
pub fn_attributes: Vec<FnAttribute<'gcc>>,
105108
}
106109

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

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

196198
FnAbiGcc {
197199
return_type,
198200
arguments_type: argument_tys,
199201
is_c_variadic: self.c_variadic,
200202
on_stack_param_indices,
203+
#[cfg(feature = "master")]
201204
fn_attributes: fn_attrs,
202205
}
203206
}

src/common.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -424,35 +424,35 @@ impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
424424
}
425425

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

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

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

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

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

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

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

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

458458
fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {

src/context.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,39 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
129129
pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>, supports_128bit_integers: bool) -> Self {
130130
let check_overflow = tcx.sess.overflow_checks();
131131

132-
let i8_type = context.new_c_type(CType::Int8t);
133-
let i16_type = context.new_c_type(CType::Int16t);
134-
let i32_type = context.new_c_type(CType::Int32t);
135-
let i64_type = context.new_c_type(CType::Int64t);
136-
let u8_type = context.new_c_type(CType::UInt8t);
137-
let u16_type = context.new_c_type(CType::UInt16t);
138-
let u32_type = context.new_c_type(CType::UInt32t);
139-
let u64_type = context.new_c_type(CType::UInt64t);
132+
let create_type = |ctype, rust_type| {
133+
let layout = tcx.layout_of(ParamEnv::reveal_all().and(rust_type)).unwrap();
134+
let align = layout.align.abi.bytes();
135+
#[cfg(feature="master")]
136+
{
137+
context.new_c_type(ctype).get_aligned(align)
138+
}
139+
#[cfg(not(feature="master"))]
140+
{
141+
// Since libgccjit 12 doesn't contain the fix to compare aligned integer types,
142+
// only align u128 and i128.
143+
if layout.ty.int_size_and_signed(tcx).0.bytes() == 16 {
144+
context.new_c_type(ctype).get_aligned(align)
145+
}
146+
else {
147+
context.new_c_type(ctype)
148+
}
149+
}
150+
};
151+
152+
let i8_type = create_type(CType::Int8t, tcx.types.i8);
153+
let i16_type = create_type(CType::Int16t, tcx.types.i16);
154+
let i32_type = create_type(CType::Int32t, tcx.types.i32);
155+
let i64_type = create_type(CType::Int64t, tcx.types.i64);
156+
let u8_type = create_type(CType::UInt8t, tcx.types.u8);
157+
let u16_type = create_type(CType::UInt16t, tcx.types.u16);
158+
let u32_type = create_type(CType::UInt32t, tcx.types.u32);
159+
let u64_type = create_type(CType::UInt64t, tcx.types.u64);
140160

141161
let (i128_type, u128_type) =
142162
if supports_128bit_integers {
143-
let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
144-
let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
163+
let i128_type = create_type(CType::Int128t, tcx.types.i128);
164+
let u128_type = create_type(CType::UInt128t, tcx.types.u128);
145165
(i128_type, u128_type)
146166
}
147167
else {

src/declare.rs

+2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
8585
arguments_type,
8686
is_c_variadic,
8787
on_stack_param_indices,
88+
#[cfg(feature="master")]
8889
fn_attributes,
8990
} = fn_abi.gcc_type(self);
9091
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &arguments_type, is_c_variadic);
9192
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
93+
#[cfg(feature="master")]
9294
for fn_attr in fn_attributes {
9395
func.add_attribute(fn_attr);
9496
}

0 commit comments

Comments
 (0)