Skip to content
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

Implement an address_insignificant attribute #8421

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
9 changes: 9 additions & 0 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,9 @@ pub mod llvm {
Elements: ValueRef,
RunTimeLang: c_uint)
-> ValueRef;

#[fast_ffi]
pub fn LLVMSetUnnamedAddr(GlobalVar: ValueRef, UnnamedAddr: Bool);
}
}

Expand All @@ -2099,6 +2102,12 @@ pub fn SetLinkage(Global: ValueRef, Link: Linkage) {
}
}

pub fn SetUnnamedAddr(Global: ValueRef, Unnamed: bool) {
unsafe {
llvm::LLVMSetUnnamedAddr(Global, Unnamed as Bool);
}
}

pub fn ConstICmp(Pred: IntPredicate, V1: ValueRef, V2: ValueRef) -> ValueRef {
unsafe {
llvm::LLVMConstICmp(Pred as c_ushort, V1, V2)
Expand Down
34 changes: 21 additions & 13 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,19 +2159,18 @@ pub fn trans_item(ccx: @mut CrateContext, item: &ast::item) {
}
ast::item_static(_, m, expr) => {
consts::trans_const(ccx, m, item.id);
// Do static_assert checking. It can't really be done much earlier because we need to get
// the value of the bool out of LLVM
for attr in item.attrs.iter() {
if "static_assert" == attr.name() {
if m == ast::m_mutbl {
ccx.sess.span_fatal(expr.span,
"cannot have static_assert on a mutable static");
}
let v = ccx.const_values.get_copy(&item.id);
unsafe {
if !(llvm::LLVMConstIntGetZExtValue(v) as bool) {
ccx.sess.span_fatal(expr.span, "static assertion failed");
}
// Do static_assert checking. It can't really be done much earlier
// because we need to get the value of the bool out of LLVM
if attr::contains_name(item.attrs, "static_assert") {
if m == ast::m_mutbl {
ccx.sess.span_fatal(expr.span,
"cannot have static_assert on a mutable \
static");
}
let v = ccx.const_values.get_copy(&item.id);
unsafe {
if !(llvm::LLVMConstIntGetZExtValue(v) as bool) {
ccx.sess.span_fatal(expr.span, "static assertion failed");
}
}
}
Expand Down Expand Up @@ -2432,6 +2431,15 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
};

// Apply the `unnamed_addr` attribute if
// requested
if attr::contains_name(i.attrs,
"address_insignificant"){
lib::llvm::SetUnnamedAddr(g, true);
lib::llvm::SetLinkage(g,
lib::llvm::InternalLinkage);
}

ccx.item_symbols.insert(i.id, sym);
g
}
Expand Down
15 changes: 12 additions & 3 deletions src/libsyntax/ext/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,12 @@ impl Context {
let st = ast::item_static(ty, ast::m_imm, method);
let static_name = self.ecx.ident_of(fmt!("__static_method_%u",
self.method_statics.len()));
let item = self.ecx.item(sp, static_name, ~[], st);
// Flag these statics as `address_insignificant` so LLVM can
// merge duplicate globals as much as possible (which we're
// generating a whole lot of).
let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
let item = self.ecx.item(sp, static_name, ~[unnamed], st);
self.method_statics.push(item);
self.ecx.expr_ident(sp, static_name)
};
Expand Down Expand Up @@ -550,7 +555,10 @@ impl Context {
let ty = self.ecx.ty(self.fmtsp, ty);
let st = ast::item_static(ty, ast::m_imm, fmt);
let static_name = self.ecx.ident_of("__static_fmtstr");
let item = self.ecx.item(self.fmtsp, static_name, ~[], st);
// see above comment for `address_insignificant` and why we do it
let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
let item = self.ecx.item(self.fmtsp, static_name, ~[unnamed], st);
let decl = respan(self.fmtsp, ast::decl_item(item));
lets.push(@respan(self.fmtsp, ast::stmt_decl(@decl, self.ecx.next_id())));

Expand Down Expand Up @@ -613,6 +621,7 @@ impl Context {
if ty == Unknown {
ty = Known(@"?");
}

let argptr = self.ecx.expr_addr_of(sp, self.ecx.expr_ident(sp, ident));
match ty {
Known(tyname) => {
Expand Down Expand Up @@ -685,7 +694,7 @@ pub fn expand_syntax_ext(ecx: @ExtCtxt, sp: span,
};
cx.fmtsp = efmt.span;
let fmt = expr_to_str(ecx, efmt,
~"first argument to ifmt! must be a string literal.");
"first argument to ifmt! must be a string literal.");

let mut err = false;
do parse::parse_error::cond.trap(|m| {
Expand Down
4 changes: 4 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,7 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateUnionType(
unwrapDI<DIArray>(Elements),
RunTimeLang));
}

extern "C" void LLVMSetUnnamedAddr(LLVMValueRef Value, LLVMBool Unnamed) {
unwrap<GlobalValue>(Value)->setUnnamedAddr(Unnamed);
}
1 change: 1 addition & 0 deletions src/rustllvm/rustllvm.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,4 @@ LLVMDIBuilderInsertDeclareBefore
LLVMDIBuilderCreateEnumerator
LLVMDIBuilderCreateEnumerationType
LLVMDIBuilderCreateUnionType
LLVMSetUnnamedAddr