From b2eb1c01a45cbb7bfc40f24073b60de61e3fad47 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 10 Sep 2013 14:28:59 -0400 Subject: [PATCH] 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 --- src/librustc/middle/trans/base.rs | 11 +++++++++++ src/librustc/middle/trans/callee.rs | 18 ++++++++++++++++-- src/librustc/middle/trans/foreign.rs | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index e88080accaa4..0493f9c8bff8 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -248,6 +248,17 @@ pub fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t, } } + // The out pointer will never alias with any other pointers, as the object only exists at a + // language level after the call. It can also be tagged with SRet to indicate that it is + // guaranteed to point to a usable block of memory for the type. + if uses_outptr { + unsafe { + let outptr = llvm::LLVMGetParam(llfn, 0); + llvm::LLVMAddAttribute(outptr, lib::llvm::StructRetAttribute as c_uint); + llvm::LLVMAddAttribute(outptr, lib::llvm::NoAliasAttribute as c_uint); + } + } + llfn } diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs index 45da026afd06..d55589cb7e8b 100644 --- a/src/librustc/middle/trans/callee.rs +++ b/src/librustc/middle/trans/callee.rs @@ -20,7 +20,7 @@ use back::abi; use driver::session; -use lib::llvm::ValueRef; +use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute}; use lib::llvm::llvm; use metadata::csearch; use middle::trans::base; @@ -707,7 +707,21 @@ pub fn trans_call_inner(in_cx: @mut Block, } // Invoke the actual rust fn and update bcx/llresult. - let (llret, b) = base::invoke(bcx, llfn, llargs, []); + let mut attrs = ~[]; + if type_of::return_uses_outptr(in_cx.tcx(), ret_ty) { + attrs.push((1, StructRetAttribute)); + } + + match ty::get(ret_ty).sty { + // `~` pointer return values never alias because ownership is transferred + ty::ty_uniq(*) | + ty::ty_evec(_, ty::vstore_uniq) => { + attrs.push((0, NoAliasAttribute)); + } + _ => () + } + + let (llret, b) = base::invoke(bcx, llfn, llargs, attrs); bcx = b; llresult = llret; diff --git a/src/librustc/middle/trans/foreign.rs b/src/librustc/middle/trans/foreign.rs index cfed6e883c82..3836bd5c47c7 100644 --- a/src/librustc/middle/trans/foreign.rs +++ b/src/librustc/middle/trans/foreign.rs @@ -497,7 +497,7 @@ unsafe fn build_wrap_fn(ccx: @mut CrateContext, // Rust expects to use an outpointer. If the foreign fn // also uses an outpointer, we can reuse it, but the types // may vary, so cast first to the Rust type. If the - // foriegn fn does NOT use an outpointer, we will have to + // foreign fn does NOT use an outpointer, we will have to // alloca some scratch space on the stack. match foreign_outptr { Some(llforeign_outptr) => {