Skip to content

Commit 4a10160

Browse files
committed
Auto merge of rust-lang#113923 - DianQK:restore-no-builtins-lto, r=pnkfelix
Restore `#![no_builtins]` crates participation in LTO. After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again. `#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again. \(^▽^)/ I will test the following issues later to verify. The task format is `Fixes {issue} {nightly-2023-07-20 result} {PR rust-lang#113923 result}`. - [x] Fixes rust-lang#72140. ❌ ✅ - [x] Fixes rust-lang#112245. ❌ ✅ - [x] Fixes rust-lang#110606. ❌ ✅ - [ ] Fixes rust-lang#105734. - [ ] Fixes rust-lang#96486. - [ ] Fixes rust-lang#108853. - [x] Fixes rust-lang/compiler-builtins#347. ❌ ✅ - [ ] Fixes rust-lang#108893. - [ ] Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears. Some issues were not tested due to the difficulty of reproducing them. r? pnkfelix cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
2 parents 19149d1 + 4df4d43 commit 4a10160

File tree

15 files changed

+154
-79
lines changed

15 files changed

+154
-79
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
4545
}
4646
}
4747

48+
fn get_llvm_preserved_symbols() -> Vec<String> {
49+
let mut len = 0;
50+
unsafe {
51+
let symbols = llvm::LLVMRustPreservedSymbols(&mut len);
52+
let symbols: &[*const _] = slice::from_raw_parts(symbols, len);
53+
symbols
54+
.iter()
55+
.filter_map(|&symbol| {
56+
if symbol.is_null() {
57+
None
58+
} else {
59+
Some(String::from_utf8(CStr::from_ptr(symbol).to_bytes().to_vec()).unwrap())
60+
}
61+
})
62+
.collect()
63+
}
64+
}
65+
4866
fn prepare_lto(
4967
cgcx: &CodegenContext<LlvmCodegenBackend>,
5068
diag_handler: &Handler,
@@ -59,8 +77,13 @@ fn prepare_lto(
5977
Lto::No => panic!("didn't request LTO but we're doing LTO"),
6078
};
6179

80+
let llvm_reserved_symbols = get_llvm_preserved_symbols();
81+
6282
let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| {
63-
if info.level.is_below_threshold(export_threshold) || info.used {
83+
if info.level.is_below_threshold(export_threshold)
84+
|| info.used
85+
|| llvm_reserved_symbols.contains(name)
86+
{
6487
Some(CString::new(name.as_str()).unwrap())
6588
} else {
6689
None

compiler/rustc_codegen_llvm/src/back/write.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ pub(crate) unsafe fn llvm_optimize(
563563
unroll_loops,
564564
config.vectorize_slp,
565565
config.vectorize_loop,
566-
config.no_builtins,
567566
config.emit_lifetime_markers,
568567
sanitizer_options.as_ref(),
569568
pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
@@ -678,15 +677,14 @@ pub(crate) unsafe fn codegen(
678677
unsafe fn with_codegen<'ll, F, R>(
679678
tm: &'ll llvm::TargetMachine,
680679
llmod: &'ll llvm::Module,
681-
no_builtins: bool,
682680
f: F,
683681
) -> R
684682
where
685683
F: FnOnce(&'ll mut PassManager<'ll>) -> R,
686684
{
687685
let cpm = llvm::LLVMCreatePassManager();
688686
llvm::LLVMAddAnalysisPasses(tm, cpm);
689-
llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
687+
llvm::LLVMRustAddLibraryInfo(cpm, llmod);
690688
f(cpm)
691689
}
692690

@@ -789,7 +787,7 @@ pub(crate) unsafe fn codegen(
789787
} else {
790788
llmod
791789
};
792-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
790+
with_codegen(tm, llmod, |cpm| {
793791
write_output_file(
794792
diag_handler,
795793
tm,
@@ -824,7 +822,7 @@ pub(crate) unsafe fn codegen(
824822
(_, SplitDwarfKind::Split) => Some(dwo_out.as_path()),
825823
};
826824

827-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
825+
with_codegen(tm, llmod, |cpm| {
828826
write_output_file(
829827
diag_handler,
830828
tm,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2139,13 +2139,8 @@ extern "C" {
21392139
ArgsCstrBuff: *const c_char,
21402140
ArgsCstrBuffLen: usize,
21412141
) -> *mut TargetMachine;
2142-
21432142
pub fn LLVMRustDisposeTargetMachine(T: *mut TargetMachine);
2144-
pub fn LLVMRustAddLibraryInfo<'a>(
2145-
PM: &PassManager<'a>,
2146-
M: &'a Module,
2147-
DisableSimplifyLibCalls: bool,
2148-
);
2143+
pub fn LLVMRustAddLibraryInfo<'a>(PM: &PassManager<'a>, M: &'a Module);
21492144
pub fn LLVMRustWriteOutputFile<'a>(
21502145
T: &'a TargetMachine,
21512146
PM: &PassManager<'a>,
@@ -2167,7 +2162,6 @@ extern "C" {
21672162
UnrollLoops: bool,
21682163
SLPVectorize: bool,
21692164
LoopVectorize: bool,
2170-
DisableSimplifyLibCalls: bool,
21712165
EmitLifetimeMarkers: bool,
21722166
SanitizerOptions: Option<&SanitizerOptions>,
21732167
PGOGenPath: *const c_char,
@@ -2193,6 +2187,7 @@ extern "C" {
21932187
pub fn LLVMRustSetLLVMOptions(Argc: c_int, Argv: *const *const c_char);
21942188
pub fn LLVMRustPrintPasses();
21952189
pub fn LLVMRustSetNormalizedTarget(M: &Module, triple: *const c_char);
2190+
pub fn LLVMRustPreservedSymbols(len: *mut usize) -> *const *const c_char;
21962191
pub fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
21972192

21982193
pub fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;

compiler/rustc_codegen_ssa/src/back/link.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,7 @@ fn link_staticlib<'a>(
510510
&codegen_results.crate_info,
511511
Some(CrateType::Staticlib),
512512
&mut |cnum, path| {
513-
let lto = are_upstream_rust_objects_already_included(sess)
514-
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
513+
let lto = are_upstream_rust_objects_already_included(sess);
515514

516515
let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
517516
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, &lib));
@@ -1250,24 +1249,6 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
12501249
}
12511250
}
12521251

1253-
/// Returns a boolean indicating whether the specified crate should be ignored
1254-
/// during LTO.
1255-
///
1256-
/// Crates ignored during LTO are not lumped together in the "massive object
1257-
/// file" that we create and are linked in their normal rlib states. See
1258-
/// comments below for what crates do not participate in LTO.
1259-
///
1260-
/// It's unusual for a crate to not participate in LTO. Typically only
1261-
/// compiler-specific and unstable crates have a reason to not participate in
1262-
/// LTO.
1263-
pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool {
1264-
// If our target enables builtin function lowering in LLVM then the
1265-
// crates providing these functions don't participate in LTO (e.g.
1266-
// no_builtins or compiler builtins crates).
1267-
!sess.target.no_builtins
1268-
&& (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
1269-
}
1270-
12711252
/// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
12721253
pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12731254
fn infer_from(
@@ -2733,10 +2714,6 @@ fn rehome_sysroot_lib_dir<'a>(sess: &'a Session, lib_dir: &Path) -> PathBuf {
27332714
// symbols). We must continue to include the rest of the rlib, however, as
27342715
// it may contain static native libraries which must be linked in.
27352716
//
2736-
// (*) Crates marked with `#![no_builtins]` don't participate in LTO and
2737-
// their bytecode wasn't included. The object files in those libraries must
2738-
// still be passed to the linker.
2739-
//
27402717
// Note, however, that if we're not doing LTO we can just pass the rlib
27412718
// blindly to the linker (fast) because it's fine if it's not actually
27422719
// included as we're at the end of the dependency chain.
@@ -2762,9 +2739,7 @@ fn add_static_crate<'a>(
27622739
cmd.link_rlib(&rlib_path);
27632740
};
27642741

2765-
if !are_upstream_rust_objects_already_included(sess)
2766-
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum)
2767-
{
2742+
if !are_upstream_rust_objects_already_included(sess) {
27682743
link_upstream(cratepath);
27692744
return;
27702745
}
@@ -2778,8 +2753,6 @@ fn add_static_crate<'a>(
27782753
let canonical_name = name.replace('-', "_");
27792754
let upstream_rust_objects_already_included =
27802755
are_upstream_rust_objects_already_included(sess);
2781-
let is_builtins =
2782-
sess.target.no_builtins || !codegen_results.crate_info.is_no_builtins.contains(&cnum);
27832756

27842757
let mut archive = archive_builder_builder.new_archive_builder(sess);
27852758
if let Err(error) = archive.add_archive(
@@ -2796,9 +2769,8 @@ fn add_static_crate<'a>(
27962769

27972770
// If we're performing LTO and this is a rust-generated object
27982771
// file, then we don't need the object file as it's part of the
2799-
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
2800-
// though, so we let that object file slide.
2801-
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
2772+
// LTO module.
2773+
if upstream_rust_objects_already_included && is_rust_object {
28022774
return true;
28032775
}
28042776

compiler/rustc_codegen_ssa/src/back/write.rs

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

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

10411030
let mut each_linked_rlib_for_lto = Vec::new();
10421031
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1043-
if link::ignored_for_lto(sess, crate_info, cnum) {
1044-
return;
1045-
}
10461032
each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
10471033
}));
10481034

compiler/rustc_codegen_ssa/src/base.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,7 @@ impl CrateInfo {
885885
// If global LTO is enabled then almost everything (*) is glued into a single object file,
886886
// so this logic is not necessary and can cause issues on some targets (due to weak lang
887887
// item symbols being "privatized" to that object file), so we disable it.
888-
// (*) Native libs, and `#[compiler_builtins]` and `#[no_builtins]` crates are not glued,
889-
// and we assume that they cannot define weak lang items. This is not currently enforced
890-
// by the compiler, but that's ok because all this stuff is unstable anyway.
888+
// (*) Native libs are not glued, and we assume that they cannot define weak lang items.
891889
let target = &tcx.sess.target;
892890
if !are_upstream_rust_objects_already_included(tcx.sess) {
893891
let missing_weak_lang_items: FxHashSet<Symbol> = info

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,9 @@ extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
535535

536536
// Unfortunately, the LLVM C API doesn't provide a way to create the
537537
// TargetLibraryInfo pass, so we use this method to do so.
538-
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M,
539-
bool DisableSimplifyLibCalls) {
538+
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M) {
540539
Triple TargetTriple(unwrap(M)->getTargetTriple());
541540
TargetLibraryInfoImpl TLII(TargetTriple);
542-
if (DisableSimplifyLibCalls)
543-
TLII.disableAllFunctions();
544541
unwrap(PMR)->add(new TargetLibraryInfoWrapperPass(TLII));
545542
}
546543

@@ -707,7 +704,7 @@ LLVMRustOptimize(
707704
bool IsLinkerPluginLTO,
708705
bool NoPrepopulatePasses, bool VerifyIR, bool UseThinLTOBuffers,
709706
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
710-
bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers,
707+
bool EmitLifetimeMarkers,
711708
LLVMRustSanitizerOptions *SanitizerOptions,
712709
const char *PGOGenPath, const char *PGOUsePath,
713710
bool InstrumentCoverage, const char *InstrProfileOutput,
@@ -813,8 +810,6 @@ LLVMRustOptimize(
813810

814811
Triple TargetTriple(TheModule->getTargetTriple());
815812
std::unique_ptr<TargetLibraryInfoImpl> TLII(new TargetLibraryInfoImpl(TargetTriple));
816-
if (DisableSimplifyLibCalls)
817-
TLII->disableAllFunctions();
818813
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
819814

820815
PB.registerModuleAnalyses(MAM);
@@ -1125,6 +1120,20 @@ extern "C" void LLVMRustPrintPasses() {
11251120
PB.printPassNames(outs());
11261121
}
11271122

1123+
// from https://github.com/llvm/llvm-project/blob/7021182d6b43de9488ab70de626192ce70b3a4a6/llvm/lib/Object/IRSymtab.cpp#L48-L57
1124+
static const char *PreservedSymbols[] = {
1125+
#define HANDLE_LIBCALL(code, name) name,
1126+
#include "llvm/IR/RuntimeLibcalls.def"
1127+
#undef HANDLE_LIBCALL
1128+
"__ssp_canary_word",
1129+
"__stack_chk_guard",
1130+
};
1131+
1132+
extern "C" const char **LLVMRustPreservedSymbols(size_t *len) {
1133+
*len = sizeof(PreservedSymbols) / sizeof(PreservedSymbols[0]);
1134+
return PreservedSymbols;
1135+
}
1136+
11281137
extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
11291138
size_t Len) {
11301139
auto PreserveFunctions = [=](const GlobalValue &GV) {
+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 @no_builtins
3+
CHECK-NEXT: call void @llvm.memcpy
4+
5+
CHECK: define{{.*}} i32 @main
6+
CHECK: call void @bar
7+
8+
CHECK: define{{.*}} void @foo
9+
CHECK-NEXT: call void @llvm.memcpy
10+
11+
CHECK: define{{.*}} void @no_builtins
12+
CHECK-SAME: #[[ATTR:[0-9]+]] {
13+
CHECK: call void @foo
14+
CHECK-NEXT: call{{.*}} @memcpy
15+
16+
CHECK: attributes #[[ATTR]]
17+
CHECK-SAME: no-builtins

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

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(lang_items, no_core)]
2+
#![no_std]
3+
#![no_core]
4+
#![crate_type = "lib"]
5+
6+
#[inline(never)]
7+
#[no_mangle]
8+
pub unsafe fn foo(dest: *mut u8, src: *const u8) {
9+
// should call `@llvm.memcpy`.
10+
memcpy(dest, src, 1024);
11+
}
12+
13+
#[no_mangle]
14+
#[inline(never)]
15+
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, _n: usize) -> *mut u8 {
16+
*dest = 0;
17+
return src as *mut u8;
18+
}
19+
20+
#[lang = "sized"]
21+
trait Sized {}
22+
#[lang = "copy"]
23+
trait Copy {}
24+
impl Copy for *mut u8 {}
25+
impl Copy for *const u8 {}
26+
27+
#[lang = "drop_in_place"]
28+
#[allow(unconditional_recursion)]
29+
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
30+
// Code here does not matter - this is replaced by the
31+
// real drop glue by the compiler.
32+
drop_in_place(to_drop);
33+
}
+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() {}

0 commit comments

Comments
 (0)