Skip to content

Commit b2eb1c0

Browse files
committed
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers. Example: pub fn bar() -> [uint, .. 8] { let a = [0, .. 8]; a } Before: ; Function Attrs: nounwind uwtable define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 { "function top level": %a = alloca [8 x i64], align 8 %2 = bitcast [8 x i64]* %a to i8* call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false) %3 = bitcast [8 x i64]* %0 to i8* call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false) ret void } After: ; Function Attrs: nounwind uwtable define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 { "function top level": %2 = bitcast [8 x i64]* %0 to i8* call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false) ret void } Closes #9072 Closes #7298 Closes #9154
1 parent 3c31cf2 commit b2eb1c0

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/librustc/middle/trans/base.rs

+11
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ pub fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
248248
}
249249
}
250250

251+
// The out pointer will never alias with any other pointers, as the object only exists at a
252+
// language level after the call. It can also be tagged with SRet to indicate that it is
253+
// guaranteed to point to a usable block of memory for the type.
254+
if uses_outptr {
255+
unsafe {
256+
let outptr = llvm::LLVMGetParam(llfn, 0);
257+
llvm::LLVMAddAttribute(outptr, lib::llvm::StructRetAttribute as c_uint);
258+
llvm::LLVMAddAttribute(outptr, lib::llvm::NoAliasAttribute as c_uint);
259+
}
260+
}
261+
251262
llfn
252263
}
253264

src/librustc/middle/trans/callee.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::vec;
2020

2121
use back::abi;
2222
use driver::session;
23-
use lib::llvm::ValueRef;
23+
use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute};
2424
use lib::llvm::llvm;
2525
use metadata::csearch;
2626
use middle::trans::base;
@@ -707,7 +707,21 @@ pub fn trans_call_inner(in_cx: @mut Block,
707707
}
708708

709709
// Invoke the actual rust fn and update bcx/llresult.
710-
let (llret, b) = base::invoke(bcx, llfn, llargs, []);
710+
let mut attrs = ~[];
711+
if type_of::return_uses_outptr(in_cx.tcx(), ret_ty) {
712+
attrs.push((1, StructRetAttribute));
713+
}
714+
715+
match ty::get(ret_ty).sty {
716+
// `~` pointer return values never alias because ownership is transferred
717+
ty::ty_uniq(*) |
718+
ty::ty_evec(_, ty::vstore_uniq) => {
719+
attrs.push((0, NoAliasAttribute));
720+
}
721+
_ => ()
722+
}
723+
724+
let (llret, b) = base::invoke(bcx, llfn, llargs, attrs);
711725
bcx = b;
712726
llresult = llret;
713727

src/librustc/middle/trans/foreign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: @mut CrateContext,
497497
// Rust expects to use an outpointer. If the foreign fn
498498
// also uses an outpointer, we can reuse it, but the types
499499
// may vary, so cast first to the Rust type. If the
500-
// foriegn fn does NOT use an outpointer, we will have to
500+
// foreign fn does NOT use an outpointer, we will have to
501501
// alloca some scratch space on the stack.
502502
match foreign_outptr {
503503
Some(llforeign_outptr) => {

0 commit comments

Comments
 (0)