Skip to content

add support for the cold function attribute #10127

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

Merged
merged 1 commit into from
Oct 29, 2013
Merged
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
2 changes: 2 additions & 0 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ pub mod llvm {
pub fn LLVMAddReturnAttribute(Fn: ValueRef, PA: c_uint);
pub fn LLVMRemoveReturnAttribute(Fn: ValueRef, PA: c_uint);

pub fn LLVMAddColdAttribute(Fn: ValueRef);

pub fn LLVMRemoveFunctionAttr(Fn: ValueRef,
PA: c_ulonglong,
HighPA: c_ulonglong);
Expand Down
18 changes: 12 additions & 6 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,21 @@ pub fn get_extern_fn(externs: &mut ExternMap, llmod: ModuleRef, name: &str,
f
}

pub fn get_extern_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
name: &str) -> ValueRef {
fn get_extern_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
name: &str, did: ast::DefId) -> ValueRef {
match ccx.externs.find_equiv(&name) {
Some(n) => return *n,
None => ()
}
let f = decl_rust_fn(ccx, inputs, output, name);
do csearch::get_item_attrs(ccx.tcx.cstore, did) |meta_items| {
set_llvm_fn_attrs(meta_items.iter().map(|&x| attr::mk_attr(x)).to_owned_vec(), f)
}
ccx.externs.insert(name.to_owned(), f);
f
}

pub fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
name: &str) -> ValueRef {
fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t, name: &str) -> ValueRef {
let llfty = type_of_rust_fn(ccx, inputs, output);
let llfn = decl_cdecl_fn(ccx.llmod, name, llfty);

Expand Down Expand Up @@ -481,6 +483,10 @@ pub fn set_llvm_fn_attrs(attrs: &[ast::Attribute], llfn: ValueRef) {
if contains_name(attrs, "no_split_stack") {
set_no_split_stack(llfn);
}

if contains_name(attrs, "cold") {
unsafe { llvm::LLVMAddColdAttribute(llfn) }
}
}

pub fn set_always_inline(f: ValueRef) {
Expand Down Expand Up @@ -840,7 +846,7 @@ pub fn trans_external_path(ccx: &mut CrateContext, did: ast::DefId, t: ty::t) ->
ty::ty_bare_fn(ref fn_ty) => {
match fn_ty.abis.for_arch(ccx.sess.targ_cfg.arch) {
Some(Rust) | Some(RustIntrinsic) => {
get_extern_rust_fn(ccx, fn_ty.sig.inputs, fn_ty.sig.output, name)
get_extern_rust_fn(ccx, fn_ty.sig.inputs, fn_ty.sig.output, name, did)
}
Some(*) | None => {
let c = foreign::llvm_calling_convention(ccx, fn_ty.abis);
Expand All @@ -851,7 +857,7 @@ pub fn trans_external_path(ccx: &mut CrateContext, did: ast::DefId, t: ty::t) ->
}
}
ty::ty_closure(ref f) => {
get_extern_rust_fn(ccx, f.sig.inputs, f.sig.output, name)
get_extern_rust_fn(ccx, f.sig.inputs, f.sig.output, name, did)
}
_ => {
let llty = type_of(ccx, t);
Expand Down
1 change: 1 addition & 0 deletions src/libstd/rt/borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn clear_task_borrow_list() {
let _ = try_take_task_borrow_list();
}

#[cold]
unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) -> ! {
debug_borrow("fail_borrowed: ", box, 0, 0, file, line);

Expand Down
2 changes: 2 additions & 0 deletions src/libstd/unstable/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use libc::{c_char, size_t, uintptr_t};
use rt::task;
use rt::borrowck;

#[cold]
#[lang="fail_"]
pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
task::begin_unwind(expr, file, line);
}

#[cold]
#[lang="fail_bounds_check"]
pub fn fail_bounds_check(file: *c_char, line: size_t, index: size_t, len: size_t) -> ! {
let msg = format!("index out of bounds: the len is {} but the index is {}",
Expand Down
5 changes: 5 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ extern "C" void LLVMRemoveReturnAttribute(LLVMValueRef Fn, LLVMAttribute PA) {
AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B));
}

extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) {
Function *A = unwrap<Function>(Fn);
A->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
}

extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,
LLVMValueRef source,
const char* Name,
Expand Down
1 change: 1 addition & 0 deletions src/rustllvm/rustllvm.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,4 @@ LLVMRustAddAlwaysInlinePass
LLVMAddReturnAttribute
LLVMRemoveReturnAttribute
LLVMTypeToString
LLVMAddColdAttribute