Skip to content

Commit 71bebeb

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 3f99540 commit 71bebeb

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/librustc/middle/trans/base.rs

+11
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ pub fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
244244
}
245245
}
246246

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

0 commit comments

Comments
 (0)