@@ -98,7 +98,7 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
98
98
ast:: LitBool ( b) => C_bool ( cx, b) ,
99
99
ast:: LitStr ( ref s, _) => C_str_slice ( cx, ( * s) . clone ( ) ) ,
100
100
ast:: LitByteStr ( ref data) => {
101
- addr_of ( cx, C_bytes ( cx, & data[ ..] ) , "byte_str" )
101
+ addr_of ( cx, C_bytes ( cx, & data[ ..] ) , 1 , "byte_str" )
102
102
}
103
103
}
104
104
}
@@ -111,6 +111,7 @@ pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
111
111
112
112
fn addr_of_mut ( ccx : & CrateContext ,
113
113
cv : ValueRef ,
114
+ align : machine:: llalign ,
114
115
kind : & str )
115
116
-> ValueRef {
116
117
unsafe {
@@ -122,6 +123,7 @@ fn addr_of_mut(ccx: &CrateContext,
122
123
ccx. sess ( ) . bug ( & format ! ( "symbol `{}` is already defined" , name) ) ;
123
124
} ) ;
124
125
llvm:: LLVMSetInitializer ( gv, cv) ;
126
+ llvm:: LLVMSetAlignment ( gv, align) ;
125
127
SetLinkage ( gv, InternalLinkage ) ;
126
128
SetUnnamedAddr ( gv, true ) ;
127
129
gv
@@ -130,13 +132,23 @@ fn addr_of_mut(ccx: &CrateContext,
130
132
131
133
pub fn addr_of ( ccx : & CrateContext ,
132
134
cv : ValueRef ,
135
+ align : machine:: llalign ,
133
136
kind : & str )
134
137
-> ValueRef {
135
138
match ccx. const_globals ( ) . borrow ( ) . get ( & cv) {
136
- Some ( & gv) => return gv,
139
+ Some ( & gv) => {
140
+ unsafe {
141
+ // Upgrade the alignment in cases where the same constant is used with different
142
+ // alignment requirements
143
+ if align > llvm:: LLVMGetAlignment ( gv) {
144
+ llvm:: LLVMSetAlignment ( gv, align) ;
145
+ }
146
+ }
147
+ return gv;
148
+ }
137
149
None => { }
138
150
}
139
- let gv = addr_of_mut ( ccx, cv, kind) ;
151
+ let gv = addr_of_mut ( ccx, cv, align , kind) ;
140
152
unsafe {
141
153
llvm:: LLVMSetGlobalConstant ( gv, True ) ;
142
154
}
@@ -254,11 +266,11 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
254
266
Some ( & val) => return val,
255
267
None => { }
256
268
}
269
+ let ty = monomorphize:: apply_param_substs ( ccx. tcx ( ) , param_substs,
270
+ & ccx. tcx ( ) . expr_ty ( expr) ) ;
257
271
let val = if qualif. intersects ( check_const:: ConstQualif :: NON_STATIC_BORROWS ) {
258
272
// Avoid autorefs as they would create global instead of stack
259
273
// references, even when only the latter are correct.
260
- let ty = monomorphize:: apply_param_substs ( ccx. tcx ( ) , param_substs,
261
- & ccx. tcx ( ) . expr_ty ( expr) ) ;
262
274
const_expr_unadjusted ( ccx, expr, ty, param_substs, None )
263
275
} else {
264
276
const_expr ( ccx, expr, param_substs, None ) . 0
@@ -274,7 +286,7 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
274
286
}
275
287
} ;
276
288
277
- let lvalue = addr_of ( ccx, val, "const" ) ;
289
+ let lvalue = addr_of ( ccx, val, type_of :: align_of ( ccx , ty ) , "const" ) ;
278
290
ccx. const_values ( ) . borrow_mut ( ) . insert ( key, lvalue) ;
279
291
lvalue
280
292
}
@@ -314,7 +326,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
314
326
if adj. autoderefs == 0 {
315
327
// Don't copy data to do a deref+ref
316
328
// (i.e., skip the last auto-deref).
317
- llconst = addr_of ( cx, llconst, "autoref" ) ;
329
+ llconst = addr_of ( cx, llconst, type_of :: align_of ( cx , ty ) , "autoref" ) ;
318
330
ty = cx. tcx ( ) . mk_imm_ref ( cx. tcx ( ) . mk_region ( ty:: ReStatic ) , ty) ;
319
331
}
320
332
} else {
@@ -720,13 +732,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
720
732
} else {
721
733
// If this isn't the address of a static, then keep going through
722
734
// normal constant evaluation.
723
- let ( v, _ ) = const_expr ( cx, & * * sub, param_substs, fn_args) ;
724
- addr_of ( cx, v, "ref" )
735
+ let ( v, ty ) = const_expr ( cx, & * * sub, param_substs, fn_args) ;
736
+ addr_of ( cx, v, type_of :: align_of ( cx , ty ) , "ref" )
725
737
}
726
738
} ,
727
739
hir:: ExprAddrOf ( hir:: MutMutable , ref sub) => {
728
- let ( v, _ ) = const_expr ( cx, & * * sub, param_substs, fn_args) ;
729
- addr_of_mut ( cx, v, "ref_mut_slice" )
740
+ let ( v, ty ) = const_expr ( cx, & * * sub, param_substs, fn_args) ;
741
+ addr_of_mut ( cx, v, type_of :: align_of ( cx , ty ) , "ref_mut_slice" )
730
742
} ,
731
743
hir:: ExprTup ( ref es) => {
732
744
let repr = adt:: represent_type ( cx, ety) ;
@@ -934,6 +946,7 @@ pub fn trans_static(ccx: &CrateContext,
934
946
ccx. statics_to_rauw ( ) . borrow_mut ( ) . push ( ( g, new_g) ) ;
935
947
new_g
936
948
} ;
949
+ llvm:: LLVMSetAlignment ( g, type_of:: align_of ( ccx, ty) ) ;
937
950
llvm:: LLVMSetInitializer ( g, v) ;
938
951
939
952
// As an optimization, all shared statics which do not have interior
0 commit comments