Skip to content

Commit 30613d8

Browse files
committed
Restore #![no_builtins] crates participation in LTO.
After #113716, we can make `#![no_builtins]` crates participate in LTO again. `#![no_builtins]` with LTO does not result in undefined references to the error.
1 parent 307c573 commit 30613d8

File tree

8 files changed

+105
-57
lines changed

8 files changed

+105
-57
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ fn link_staticlib<'a>(
516516
&codegen_results.crate_info,
517517
Some(CrateType::Staticlib),
518518
&mut |cnum, path| {
519-
let lto = are_upstream_rust_objects_already_included(sess)
520-
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
519+
let lto = are_upstream_rust_objects_already_included(sess);
521520

522521
let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
523522
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, &lib));
@@ -1256,24 +1255,6 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
12561255
}
12571256
}
12581257

1259-
/// Returns a boolean indicating whether the specified crate should be ignored
1260-
/// during LTO.
1261-
///
1262-
/// Crates ignored during LTO are not lumped together in the "massive object
1263-
/// file" that we create and are linked in their normal rlib states. See
1264-
/// comments below for what crates do not participate in LTO.
1265-
///
1266-
/// It's unusual for a crate to not participate in LTO. Typically only
1267-
/// compiler-specific and unstable crates have a reason to not participate in
1268-
/// LTO.
1269-
pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool {
1270-
// If our target enables builtin function lowering in LLVM then the
1271-
// crates providing these functions don't participate in LTO (e.g.
1272-
// no_builtins or compiler builtins crates).
1273-
!sess.target.no_builtins
1274-
&& (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
1275-
}
1276-
12771258
/// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
12781259
pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12791260
fn infer_from(
@@ -2739,10 +2720,6 @@ fn rehome_sysroot_lib_dir<'a>(sess: &'a Session, lib_dir: &Path) -> PathBuf {
27392720
// symbols). We must continue to include the rest of the rlib, however, as
27402721
// it may contain static native libraries which must be linked in.
27412722
//
2742-
// (*) Crates marked with `#![no_builtins]` don't participate in LTO and
2743-
// their bytecode wasn't included. The object files in those libraries must
2744-
// still be passed to the linker.
2745-
//
27462723
// Note, however, that if we're not doing LTO we can just pass the rlib
27472724
// blindly to the linker (fast) because it's fine if it's not actually
27482725
// included as we're at the end of the dependency chain.
@@ -2768,9 +2745,7 @@ fn add_static_crate<'a>(
27682745
cmd.link_rlib(&rlib_path);
27692746
};
27702747

2771-
if !are_upstream_rust_objects_already_included(sess)
2772-
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum)
2773-
{
2748+
if !are_upstream_rust_objects_already_included(sess) {
27742749
link_upstream(cratepath);
27752750
return;
27762751
}
@@ -2784,8 +2759,6 @@ fn add_static_crate<'a>(
27842759
let canonical_name = name.replace('-', "_");
27852760
let upstream_rust_objects_already_included =
27862761
are_upstream_rust_objects_already_included(sess);
2787-
let is_builtins =
2788-
sess.target.no_builtins || !codegen_results.crate_info.is_no_builtins.contains(&cnum);
27892762

27902763
let mut archive = archive_builder_builder.new_archive_builder(sess);
27912764
if let Err(error) = archive.add_archive(
@@ -2802,9 +2775,8 @@ fn add_static_crate<'a>(
28022775

28032776
// If we're performing LTO and this is a rust-generated object
28042777
// file, then we don't need the object file as it's part of the
2805-
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
2806-
// though, so we let that object file slide.
2807-
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
2778+
// LTO module.
2779+
if upstream_rust_objects_already_included && is_rust_object {
28082780
return true;
28092781
}
28102782

compiler/rustc_codegen_ssa/src/back/write.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,12 @@ impl ModuleConfig {
148148

149149
let emit_obj = if !should_emit_obj {
150150
EmitObj::None
151-
} else if sess.target.obj_is_bitcode
152-
|| (sess.opts.cg.linker_plugin_lto.enabled() && !no_builtins)
153-
{
151+
} else if sess.target.obj_is_bitcode || sess.opts.cg.linker_plugin_lto.enabled() {
154152
// This case is selected if the target uses objects as bitcode, or
155153
// if linker plugin LTO is enabled. In the linker plugin LTO case
156154
// the assumption is that the final link-step will read the bitcode
157155
// and convert it to object code. This may be done by either the
158156
// native linker or rustc itself.
159-
//
160-
// Note, however, that the linker-plugin-lto requested here is
161-
// explicitly ignored for `#![no_builtins]` crates. These crates are
162-
// specifically ignored by rustc's LTO passes and wouldn't work if
163-
// loaded into the linker. These crates define symbols that LLVM
164-
// lowers intrinsics to, and these symbol dependencies aren't known
165-
// until after codegen. As a result any crate marked
166-
// `#![no_builtins]` is assumed to not participate in LTO and
167-
// instead goes on to generate object code.
168157
EmitObj::Bitcode
169158
} else if need_bitcode_in_object(sess) {
170159
EmitObj::ObjectCode(BitcodeSection::Full)
@@ -1025,9 +1014,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
10251014

10261015
let mut each_linked_rlib_for_lto = Vec::new();
10271016
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1028-
if link::ignored_for_lto(sess, crate_info, cnum) {
1029-
return;
1030-
}
10311017
each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
10321018
}));
10331019

compiler/rustc_codegen_ssa/src/base.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,7 @@ impl CrateInfo {
872872
// If global LTO is enabled then almost everything (*) is glued into a single object file,
873873
// so this logic is not necessary and can cause issues on some targets (due to weak lang
874874
// item symbols being "privatized" to that object file), so we disable it.
875-
// (*) Native libs, and `#[compiler_builtins]` and `#[no_builtins]` crates are not glued,
876-
// and we assume that they cannot define weak lang items. This is not currently enforced
877-
// by the compiler, but that's ok because all this stuff is unstable anyway.
875+
// (*) Native libs are not glued, and we assume that they cannot define weak lang items.
878876
let target = &tcx.sess.target;
879877
if !are_upstream_rust_objects_already_included(tcx.sess) {
880878
let missing_weak_lang_items: FxHashSet<Symbol> = info
+12-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
include ../tools.mk
22

3+
# only-x86_64
4+
5+
# We want to check that `no_builtins` is correctly participating in LTO.
6+
# First, verify that the `foo::foo` symbol can be found when linking.
7+
# Next, verify that `memcpy` can be customized using `no_builtins` under LTO.
8+
# Others will use the built-in memcpy.
9+
310
all:
4-
# Compile a `#![no_builtins]` rlib crate
5-
$(RUSTC) no_builtins.rs
6-
# Build an executable that depends on that crate using LTO. The no_builtins crate doesn't
7-
# participate in LTO, so its rlib must be explicitly linked into the final binary. Verify this by
8-
# grepping the linker arguments.
9-
$(RUSTC) main.rs -C lto --print link-args | $(CGREP) 'libno_builtins.rlib'
11+
$(RUSTC) -C linker-plugin-lto -C opt-level=2 -C debuginfo=0 foo.rs
12+
$(RUSTC) -C linker-plugin-lto -C opt-level=2 -C debuginfo=0 no_builtins.rs
13+
$(RUSTC) main.rs -C lto -C opt-level=2 -C debuginfo=0 -C save-temps -C metadata=1 -C codegen-units=1
14+
$(LLVM_BIN_DIR)/llvm-dis $(TMPDIR)/main.main.*-cgu.0.rcgu.lto.input.bc -o $(TMPDIR)/lto.ll
15+
cat "$(TMPDIR)"/lto.ll | "$(LLVM_FILECHECK)" filecheck.lto.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CHECK: define{{.*}} void @bar
2+
CHECK-NEXT: call void @_ZN11no_builtins11no_builtins17hcda8eeb3c587012fE
3+
CHECK-NEXT: call void @llvm.memcpy
4+
5+
CHECK: define{{.*}} i32 @main
6+
CHECK: call void @bar
7+
8+
CHECK: define{{.*}} void @_ZN3foo3foo17hfad400915e51b713E
9+
CHECK-NEXT: call void @llvm.memcpy
10+
11+
CHECK: define{{.*}} void @_ZN11no_builtins11no_builtins17hcda8eeb3c587012fE
12+
CHECK-SAME: #[[ATTR:[0-9]+]] {
13+
CHECK: call void @_ZN3foo3foo17hfad400915e51b713E
14+
CHECK-NEXT: call{{.*}} @memcpy
15+
16+
CHECK: attributes #[[ATTR]]
17+
CHECK-SAME: no-builtins

tests/run-make/no-builtins-lto/foo.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(lang_items, no_core)]
2+
#![no_std]
3+
#![no_core]
4+
#![crate_type = "lib"]
5+
6+
#[inline(never)]
7+
pub unsafe fn foo(dest: *mut u8, src: *const u8) {
8+
// should call `@llvm.memcpy`.
9+
memcpy(dest, src, 1024);
10+
}
11+
12+
#[no_mangle]
13+
#[inline(never)]
14+
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, _n: usize) -> *mut u8 {
15+
*dest = 0;
16+
return src as *mut u8;
17+
}
18+
19+
#[lang = "sized"]
20+
trait Sized {}
21+
#[lang = "copy"]
22+
trait Copy {}
23+
impl Copy for *mut u8 {}
24+
impl Copy for *const u8 {}
25+
26+
#[lang = "drop_in_place"]
27+
#[allow(unconditional_recursion)]
28+
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
29+
// Code here does not matter - this is replaced by the
30+
// real drop glue by the compiler.
31+
drop_in_place(to_drop);
32+
}
+26-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
#![feature(no_core, start, lang_items)]
2+
#![no_std]
3+
// We use `no_core` to reduce the LTO products is small enough.
4+
#![no_core]
5+
16
extern crate no_builtins;
7+
extern crate foo;
8+
9+
#[link(name = "c")]
10+
extern "C" {}
11+
12+
#[start]
13+
fn main(_: isize, p: *const *const u8) -> isize {
14+
// Make sure the symbols are retained.
15+
unsafe { bar(*p as *mut u8, *p); }
16+
0
17+
}
18+
19+
#[no_mangle]
20+
#[inline(never)]
21+
pub unsafe extern "C" fn bar(dest: *mut u8, src: *const u8) {
22+
no_builtins::no_builtins(dest, src);
23+
// should call `@llvm.memcpy`
24+
foo::memcpy(dest, src, 1024);
25+
}
226

3-
fn main() {}
27+
#[lang = "eh_personality"]
28+
fn eh_personality() {}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
#![feature(lang_items, no_core)]
2+
#![no_std]
3+
#![no_core]
14
#![crate_type = "lib"]
25
#![no_builtins]
6+
7+
extern crate foo;
8+
9+
pub unsafe fn no_builtins(dest: *mut u8, src: *const u8) {
10+
// There should be no "undefined reference to `foo::foo'".
11+
foo::foo(dest, src);
12+
// should call `@memcpy` instead of `@llvm.memcpy`.
13+
foo::memcpy(dest, src, 1024);
14+
}

0 commit comments

Comments
 (0)