add support for the cold function attribute

This allows a function to marked as infrequently called, resulting in
any branch calling it to be considered colder.
This commit is contained in:
Daniel Micay 2013-10-20 23:13:48 -04:00
parent d664ca2635
commit 541e5f84d7
6 changed files with 23 additions and 6 deletions

View File

@ -693,6 +693,8 @@ pub fn LLVMGetOrInsertFunction(M: ModuleRef,
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);

View File

@ -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);
@ -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) {
@ -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);
@ -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);

View File

@ -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);

View File

@ -16,11 +16,13 @@
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 {}",

View File

@ -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,

View File

@ -629,3 +629,4 @@ LLVMRustAddAlwaysInlinePass
LLVMAddReturnAttribute
LLVMRemoveReturnAttribute
LLVMTypeToString
LLVMAddColdAttribute