Skip to content

Commit a543ed6

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
1 parent 917d3c2 commit a543ed6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,18 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
16181618
}
16191619
};
16201620
let uses_outptr = type_of::return_uses_outptr(ccx.tcx, substd_output_type);
1621+
1622+
// The out pointer will never alias with any other pointers, as the object only exists at a
1623+
// language level after the call. It can also be tagged with SRet to indicate that it is
1624+
// guaranteed to point to a usable block of memory for the type.
1625+
if uses_outptr {
1626+
unsafe {
1627+
let outptr = llvm::LLVMGetParam(llfndecl, 0);
1628+
llvm::LLVMAddAttribute(outptr, lib::llvm::StructRetAttribute as c_uint);
1629+
llvm::LLVMAddAttribute(outptr, lib::llvm::NoAliasAttribute as c_uint);
1630+
}
1631+
}
1632+
16211633
let debug_context = debuginfo::create_function_debug_context(ccx, id, param_substs, llfndecl);
16221634

16231635
let fcx = @mut FunctionContext {

0 commit comments

Comments
 (0)