Skip to content

Commit 28499b3

Browse files
Annotate functions in LLVM with target-cpu, same as Clang does.
1 parent 685c90e commit 28499b3

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/librustc_codegen_llvm/attributes.rs

+18
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
123123
.filter(|l| !l.is_empty())
124124
}
125125

126+
pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
127+
let target_cpu = CString::new(cx.tcx.sess.target_cpu().to_string()).unwrap();
128+
llvm::AddFunctionAttrStringValue(
129+
llfn,
130+
llvm::AttributePlace::Function,
131+
cstr("target-cpu\0"),
132+
target_cpu.as_c_str());
133+
}
134+
126135
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
127136
/// attributes.
128137
pub fn from_fn_attrs(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value, id: DefId) {
@@ -167,6 +176,15 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value, id: DefId) {
167176
Some(true) | None => {}
168177
}
169178

179+
// Always annotate functions with the target-cpu they are compiled for.
180+
// Without this, ThinLTO won't inline Rust functions into Clang generated
181+
// functions (because Clang annotates functions this way too).
182+
// NOTE: For now we just apply this if -Zcross-lang-lto is specified, since
183+
// it introduce a little overhead and isn't really necessary otherwise.
184+
if cx.tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() {
185+
apply_target_cpu_attr(cx, llfn);
186+
}
187+
170188
let features = llvm_target_features(cx.tcx.sess)
171189
.map(|s| s.to_string())
172190
.chain(

src/librustc_codegen_llvm/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx) {
596596

597597
// `main` should respect same config for frame pointer elimination as rest of code
598598
attributes::set_frame_pointer_elimination(cx, llfn);
599+
attributes::apply_target_cpu_attr(cx, llfn);
599600

600601
let bx = Builder::new_block(cx, llfn, "top");
601602

src/librustc_codegen_llvm/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use attributes;
1112
use common;
1213
use llvm;
1314
use rustc::dep_graph::DepGraphSafe;
@@ -381,6 +382,7 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
381382
declare::declare_cfn(self, name, fty)
382383
}
383384
};
385+
attributes::apply_target_cpu_attr(self, llfn);
384386
self.eh_personality.set(Some(llfn));
385387
llfn
386388
}
@@ -412,6 +414,7 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
412414

413415
let llfn = declare::declare_fn(self, "rust_eh_unwind_resume", ty);
414416
attributes::unwind(llfn, true);
417+
attributes::apply_target_cpu_attr(self, llfn);
415418
unwresume.set(Some(llfn));
416419
llfn
417420
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test makes sure that functions get annotated with the proper
12+
// "target-cpu" attribute in LLVM.
13+
14+
// only-x86_64
15+
// compile-flags: -C no-prepopulate-passes -C panic=abort
16+
17+
#![crate_type = "staticlib"]
18+
19+
// CHECK-LABEL: define {{.*}} @exported() {{.*}} #0
20+
#[no_mangle]
21+
pub extern fn exported() {
22+
not_exported();
23+
}
24+
25+
// CHECK-LABEL: define {{.*}} @_ZN23target_cpu_on_functions12not_exported{{.*}}() {{.*}} #0
26+
fn not_exported() {}
27+
28+
// CHECK: attributes #0 = {{.*}} "target-cpu"="x86-64"

0 commit comments

Comments
 (0)