Skip to content

Commit cf71f1c

Browse files
committed
auto merge of #16525 : thestinger/rust/readonly, r=pcwalton
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`.
2 parents ec9476c + 48edb32 commit cf71f1c

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
@@ -2278,7 +2278,7 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
22782278
match ty::get(ret_ty).sty {
22792279
// `~` pointer return values never alias because ownership
22802280
// is transferred
2281-
ty::ty_uniq(it) if match ty::get(it).sty {
2281+
ty::ty_uniq(it) if match ty::get(it).sty {
22822282
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
22832283
} => {}
22842284
ty::ty_uniq(_) => {
@@ -2354,15 +2354,21 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
23542354
}
23552355

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

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

2368+
if mt.mutbl == ast::MutImmutable {
2369+
attrs.arg(idx, llvm::ReadOnlyAttribute);
2370+
}
2371+
23662372
match b {
23672373
ReLateBound(_, BrAnon(_)) => {
23682374
attrs.arg(idx, llvm::NoCaptureAttribute);

0 commit comments

Comments
 (0)