Skip to content

Commit 48edb32

Browse files
committed
mark &T params without UnsafeCell<U> as readonly
These are already marked as `noalias` due to the immutability guarantee (see 4c2d4cd), but more information can be bubbled up to the caller via `readonly`.
1 parent cafa475 commit 48edb32

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/librustc/middle/trans/base.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
22802280
match ty::get(ret_ty).sty {
22812281
// `~` pointer return values never alias because ownership
22822282
// is transferred
2283-
ty::ty_uniq(it) if match ty::get(it).sty {
2283+
ty::ty_uniq(it) if match ty::get(it).sty {
22842284
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
22852285
} => {}
22862286
ty::ty_uniq(_) => {
@@ -2356,15 +2356,21 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
23562356
}
23572357

23582358
// `&mut` pointer parameters never alias other parameters, or mutable global data
2359-
// `&` pointer parameters never alias either (for LLVM's purposes) as long as the
2360-
// interior is safe
2359+
//
2360+
// `&T` where `T` contains no `UnsafeCell<U>` is immutable, and can be marked as both
2361+
// `readonly` and `noalias`, as LLVM's definition of `noalias` is based solely on
2362+
// memory dependencies rather than pointer equality
23612363
ty::ty_rptr(b, mt) if mt.mutbl == ast::MutMutable ||
23622364
!ty::type_contents(ccx.tcx(), mt.ty).interior_unsafe() => {
23632365

23642366
let llsz = llsize_of_real(ccx, type_of::type_of(ccx, mt.ty));
23652367
attrs.arg(idx, llvm::NoAliasAttribute)
23662368
.arg(idx, llvm::DereferenceableAttribute(llsz));
23672369

2370+
if mt.mutbl == ast::MutImmutable {
2371+
attrs.arg(idx, llvm::ReadOnlyAttribute);
2372+
}
2373+
23682374
match b {
23692375
ReLateBound(_, BrAnon(_)) => {
23702376
attrs.arg(idx, llvm::NoCaptureAttribute);

0 commit comments

Comments
 (0)