@@ -13,6 +13,7 @@ use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRan
1313
1414use crate :: base;
1515use crate :: context:: CodegenCx ;
16+ use crate :: errors:: InvalidMinimumAlignment ;
1617use crate :: type_of:: LayoutGccExt ;
1718
1819impl < ' gcc , ' tcx > CodegenCx < ' gcc , ' tcx > {
@@ -30,6 +31,21 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
3031 }
3132}
3233
34+ fn set_global_alignment < ' gcc , ' tcx > ( cx : & CodegenCx < ' gcc , ' tcx > , gv : LValue < ' gcc > , mut align : Align ) {
35+ // The target may require greater alignment for globals than the type does.
36+ // Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
37+ // which can force it to be smaller. Rust doesn't support this yet.
38+ if let Some ( min) = cx. sess ( ) . target . min_global_align {
39+ match Align :: from_bits ( min) {
40+ Ok ( min) => align = align. max ( min) ,
41+ Err ( err) => {
42+ cx. sess ( ) . emit_err ( InvalidMinimumAlignment { err } ) ;
43+ }
44+ }
45+ }
46+ gv. set_alignment ( align. bytes ( ) as i32 ) ;
47+ }
48+
3349impl < ' gcc , ' tcx > StaticMethods for CodegenCx < ' gcc , ' tcx > {
3450 fn static_addr_of ( & self , cv : RValue < ' gcc > , align : Align , kind : Option < & str > ) -> RValue < ' gcc > {
3551 // TODO(antoyo): implement a proper rvalue comparison in libgccjit instead of doing the
@@ -81,7 +97,7 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
8197 let ty = instance. ty ( self . tcx , ty:: ParamEnv :: reveal_all ( ) ) ;
8298 let gcc_type = self . layout_of ( ty) . gcc_type ( self ) ;
8399
84- // TODO(antoyo): set alignment.
100+ set_global_alignment ( self , global , self . align_of ( ty ) ) ;
85101
86102 let value = self . bitcast_if_needed ( value, gcc_type) ;
87103 global. global_set_initializer_rvalue ( value) ;
0 commit comments