Skip to content

add noalias attribute to ~ return values #9071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,12 @@ pub mod llvm {
pub fn LLVMAddFunctionAttrString(Fn: ValueRef, Name: *c_char);
#[fast_ffi]
pub fn LLVMGetFunctionAttr(Fn: ValueRef) -> c_ulonglong;

#[fast_ffi]
pub fn LLVMAddReturnAttribute(Fn: ValueRef, PA: c_uint);
#[fast_ffi]
pub fn LLVMRemoveReturnAttribute(Fn: ValueRef, PA: c_uint);

#[fast_ffi]
pub fn LLVMRemoveFunctionAttr(Fn: ValueRef,
PA: c_ulonglong,
Expand Down
24 changes: 15 additions & 9 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,21 +1716,15 @@ pub fn create_llargs_for_fn_args(cx: @mut FunctionContext,
let llarg = unsafe {llvm::LLVMGetParam(cx.llfn, arg_n as c_uint) };

match ty::get(arg_ty).sty {
// `~` pointers never alias other parameters, because
// ownership was transferred
// `~` pointer parameters never alias because ownership is transferred
ty::ty_uniq(*) |
ty::ty_evec(_, ty::vstore_uniq) |
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, _}) => {
unsafe {
llvm::LLVMAddAttribute(
llarg, lib::llvm::NoAliasAttribute as c_uint);
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
}
}
// FIXME: #6785: `&mut` can only alias `&const` and
// `@mut`, we should check for those in the other
// parameters and then mark it as `noalias` if there
// aren't any
_ => {}
_ => ()
}

llarg
Expand Down Expand Up @@ -1952,6 +1946,18 @@ pub fn trans_fn(ccx: @mut CrateContext,
param_substs.repr(ccx.tcx));
let _icx = push_ctxt("trans_fn");
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, id));

match ty::get(output_type).sty {
// `~` pointer return values never alias because ownership is transferred
ty::ty_uniq(*) |
ty::ty_evec(_, ty::vstore_uniq) => {
unsafe {
llvm::LLVMAddReturnAttribute(llfndecl, lib::llvm::NoAliasAttribute as c_uint);
}
}
_ => ()
}

trans_closure(ccx,
path.clone(),
decl,
Expand Down
15 changes: 15 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ extern "C" void LLVMAddFunctionAttrString(LLVMValueRef fn, const char *Name) {
unwrap<Function>(fn)->addFnAttr(Name);
}


extern "C" void LLVMAddReturnAttribute(LLVMValueRef Fn, LLVMAttribute PA) {
Function *A = unwrap<Function>(Fn);
AttrBuilder B(PA);
A->addAttributes(AttributeSet::ReturnIndex,
AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B));
}

extern "C" void LLVMRemoveReturnAttribute(LLVMValueRef Fn, LLVMAttribute PA) {
Function *A = unwrap<Function>(Fn);
AttrBuilder B(PA);
A->removeAttributes(AttributeSet::ReturnIndex,
AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B));
}

extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,
LLVMValueRef source,
const char* Name,
Expand Down
2 changes: 2 additions & 0 deletions src/rustllvm/rustllvm.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,5 @@ LLVMRustSetLLVMOptions
LLVMRustPrintPasses
LLVMRustSetNormalizedTarget
LLVMRustAddAlwaysInlinePass
LLVMAddReturnAttribute
LLVMRemoveReturnAttribute