Skip to content

rustc: mark references w/anonymous lifetime nocapture #13001

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 3 commits 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
13 changes: 12 additions & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ fn get_extern_rust_fn(ccx: &CrateContext, inputs: &[ty::t], output: ty::t,
pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
inputs: &[ty::t], output: ty::t,
name: &str) -> ValueRef {
use middle::ty::{BrAnon, ReLateBound};

let llfty = type_of_rust_fn(ccx, has_env, inputs, output);
let llfn = decl_cdecl_fn(ccx.llmod, name, llfty, output);

Expand All @@ -265,7 +267,16 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
unsafe {
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
}
}
},
// When a reference in an argument has no named lifetime, it's
// impossible for that reference to escape this function(ie, be
// returned).
ty::ty_rptr(ReLateBound(_, BrAnon(_)), _) => {
debug!("marking argument of {} as nocapture because of anonymous lifetime", name);
unsafe {
llvm::LLVMAddAttribute(llarg, lib::llvm::NoCaptureAttribute as c_uint);
}
},
_ => {
// For non-immediate arguments the callee gets its own copy of
// the value on the stack, so there are no aliases
Expand Down
39 changes: 37 additions & 2 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::slice;

use back::abi;
use driver::session;
use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute};
use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute, NoCaptureAttribute};
use lib::llvm::llvm;
use metadata::csearch;
use middle::trans::base;
Expand Down Expand Up @@ -661,9 +661,15 @@ pub fn trans_call_inner<'a>(
llargs.push(opt_llretslot.unwrap());
}

// start at 1, because index 0 is the return value of the llvm func
let mut first_arg_offset = 1;

// Push the environment (or a trait object's self).
match (llenv, llself) {
(Some(llenv), None) => llargs.push(llenv),
(Some(llenv), None) => {
first_arg_offset += 1;
llargs.push(llenv)
},
(None, Some(llself)) => llargs.push(llself),
_ => {}
}
Expand All @@ -682,6 +688,11 @@ pub fn trans_call_inner<'a>(
let mut attrs = Vec::new();
if type_of::return_uses_outptr(ccx, ret_ty) {
attrs.push((1, StructRetAttribute));
// The outptr can be noalias and nocapture because it's entirely
// invisible to the program.
attrs.push((1, NoAliasAttribute));
attrs.push((1, NoCaptureAttribute));
first_arg_offset += 1;
}

// The `noalias` attribute on the return value is useful to a
Expand All @@ -695,6 +706,30 @@ pub fn trans_call_inner<'a>(
_ => {}
}

debug!("trans_callee_inner: first_arg_offset={}", first_arg_offset);

for (idx, &t) in ty::ty_fn_args(callee_ty).iter().enumerate()
.map(|(i, v)| (i+first_arg_offset, v)) {
use middle::ty::{BrAnon, ReLateBound};
if !type_is_immediate(ccx, t) {
// if it's not immediate, we have a program-invisible pointer,
// which it can't possibly capture
attrs.push((idx, NoCaptureAttribute));
debug!("trans_callee_inner: argument {} nocapture because it's non-immediate", idx);
continue;
}

let t_ = ty::get(t);
match t_.sty {
ty::ty_rptr(ReLateBound(_, BrAnon(_)), _) => {
debug!("trans_callee_inner: argument {} nocapture because \
of anonymous lifetime", idx);
attrs.push((idx, NoCaptureAttribute));
},
_ => { }
}
}

// Invoke the actual rust fn and update bcx/llresult.
let (llret, b) = base::invoke(bcx,
llfn,
Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass/out-of-stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn silent_recurse() {
fn loud_recurse() {
println!("hello!");
loud_recurse();
black_box(()); // don't optimize this into a tail call. please.
}

fn main() {
Expand Down