Skip to content

Commit aa874c5

Browse files
committed
Revert "Auto merge of #113923 - DianQK:restore-no-builtins-lto, r=pnkfelix"
This reverts commit 8c2b577, reversing changes made to 9cf18e9.
1 parent 6d29eac commit aa874c5

File tree

16 files changed

+89
-133
lines changed

16 files changed

+89
-133
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ pub(crate) unsafe fn llvm_optimize(
569569
unroll_loops,
570570
config.vectorize_slp,
571571
config.vectorize_loop,
572+
config.no_builtins,
572573
config.emit_lifetime_markers,
573574
sanitizer_options.as_ref(),
574575
pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
@@ -677,14 +678,15 @@ pub(crate) unsafe fn codegen(
677678
unsafe fn with_codegen<'ll, F, R>(
678679
tm: &'ll llvm::TargetMachine,
679680
llmod: &'ll llvm::Module,
681+
no_builtins: bool,
680682
f: F,
681683
) -> R
682684
where
683685
F: FnOnce(&'ll mut PassManager<'ll>) -> R,
684686
{
685687
let cpm = llvm::LLVMCreatePassManager();
686688
llvm::LLVMAddAnalysisPasses(tm, cpm);
687-
llvm::LLVMRustAddLibraryInfo(cpm, llmod);
689+
llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
688690
f(cpm)
689691
}
690692

@@ -785,7 +787,7 @@ pub(crate) unsafe fn codegen(
785787
} else {
786788
llmod
787789
};
788-
with_codegen(tm, llmod, |cpm| {
790+
with_codegen(tm, llmod, config.no_builtins, |cpm| {
789791
write_output_file(
790792
dcx,
791793
tm,
@@ -820,7 +822,7 @@ pub(crate) unsafe fn codegen(
820822
(_, SplitDwarfKind::Split) => Some(dwo_out.as_path()),
821823
};
822824

823-
with_codegen(tm, llmod, |cpm| {
825+
with_codegen(tm, llmod, config.no_builtins, |cpm| {
824826
write_output_file(
825827
dcx,
826828
tm,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2173,8 +2173,13 @@ extern "C" {
21732173
ArgsCstrBuff: *const c_char,
21742174
ArgsCstrBuffLen: usize,
21752175
) -> *mut TargetMachine;
2176+
21762177
pub fn LLVMRustDisposeTargetMachine(T: *mut TargetMachine);
2177-
pub fn LLVMRustAddLibraryInfo<'a>(PM: &PassManager<'a>, M: &'a Module);
2178+
pub fn LLVMRustAddLibraryInfo<'a>(
2179+
PM: &PassManager<'a>,
2180+
M: &'a Module,
2181+
DisableSimplifyLibCalls: bool,
2182+
);
21782183
pub fn LLVMRustWriteOutputFile<'a>(
21792184
T: &'a TargetMachine,
21802185
PM: &PassManager<'a>,
@@ -2196,6 +2201,7 @@ extern "C" {
21962201
UnrollLoops: bool,
21972202
SLPVectorize: bool,
21982203
LoopVectorize: bool,
2204+
DisableSimplifyLibCalls: bool,
21992205
EmitLifetimeMarkers: bool,
22002206
SanitizerOptions: Option<&SanitizerOptions>,
22012207
PGOGenPath: *const c_char,

compiler/rustc_codegen_ssa/src/back/link.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,8 @@ pub fn each_linked_rlib(
270270

271271
for &cnum in crates {
272272
match fmts.get(cnum.as_usize() - 1) {
273-
Some(&Linkage::NotLinked | &Linkage::Dynamic) => continue,
274-
Some(&Linkage::IncludedFromDylib) => {
275-
// We always link crate `compiler_builtins` statically. When enabling LTO, we include it as well.
276-
if info.compiler_builtins != Some(cnum) {
277-
continue;
278-
}
279-
}
280-
Some(&Linkage::Static) => {}
273+
Some(&Linkage::NotLinked | &Linkage::Dynamic | &Linkage::IncludedFromDylib) => continue,
274+
Some(_) => {}
281275
None => return Err(errors::LinkRlibError::MissingFormat),
282276
}
283277
let crate_name = info.crate_name[&cnum];
@@ -526,7 +520,8 @@ fn link_staticlib<'a>(
526520
&codegen_results.crate_info,
527521
Some(CrateType::Staticlib),
528522
&mut |cnum, path| {
529-
let lto = are_upstream_rust_objects_already_included(sess);
523+
let lto = are_upstream_rust_objects_already_included(sess)
524+
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
530525

531526
let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
532527
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, lib));
@@ -1277,6 +1272,24 @@ fn link_sanitizer_runtime(
12771272
}
12781273
}
12791274

1275+
/// Returns a boolean indicating whether the specified crate should be ignored
1276+
/// during LTO.
1277+
///
1278+
/// Crates ignored during LTO are not lumped together in the "massive object
1279+
/// file" that we create and are linked in their normal rlib states. See
1280+
/// comments below for what crates do not participate in LTO.
1281+
///
1282+
/// It's unusual for a crate to not participate in LTO. Typically only
1283+
/// compiler-specific and unstable crates have a reason to not participate in
1284+
/// LTO.
1285+
pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool {
1286+
// If our target enables builtin function lowering in LLVM then the
1287+
// crates providing these functions don't participate in LTO (e.g.
1288+
// no_builtins or compiler builtins crates).
1289+
!sess.target.no_builtins
1290+
&& (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
1291+
}
1292+
12801293
/// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
12811294
pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12821295
fn infer_from(
@@ -2742,6 +2755,10 @@ fn rehome_sysroot_lib_dir<'a>(sess: &'a Session, lib_dir: &Path) -> PathBuf {
27422755
// symbols). We must continue to include the rest of the rlib, however, as
27432756
// it may contain static native libraries which must be linked in.
27442757
//
2758+
// (*) Crates marked with `#![no_builtins]` don't participate in LTO and
2759+
// their bytecode wasn't included. The object files in those libraries must
2760+
// still be passed to the linker.
2761+
//
27452762
// Note, however, that if we're not doing LTO we can just pass the rlib
27462763
// blindly to the linker (fast) because it's fine if it's not actually
27472764
// included as we're at the end of the dependency chain.
@@ -2767,7 +2784,9 @@ fn add_static_crate<'a>(
27672784
cmd.link_rlib(&rlib_path);
27682785
};
27692786

2770-
if !are_upstream_rust_objects_already_included(sess) {
2787+
if !are_upstream_rust_objects_already_included(sess)
2788+
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum)
2789+
{
27712790
link_upstream(cratepath);
27722791
return;
27732792
}
@@ -2781,6 +2800,8 @@ fn add_static_crate<'a>(
27812800
let canonical_name = name.replace('-', "_");
27822801
let upstream_rust_objects_already_included =
27832802
are_upstream_rust_objects_already_included(sess);
2803+
let is_builtins =
2804+
sess.target.no_builtins || !codegen_results.crate_info.is_no_builtins.contains(&cnum);
27842805

27852806
let mut archive = archive_builder_builder.new_archive_builder(sess);
27862807
if let Err(error) = archive.add_archive(
@@ -2797,8 +2818,9 @@ fn add_static_crate<'a>(
27972818

27982819
// If we're performing LTO and this is a rust-generated object
27992820
// file, then we don't need the object file as it's part of the
2800-
// LTO module.
2801-
if upstream_rust_objects_already_included && is_rust_object {
2821+
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
2822+
// though, so we let that object file slide.
2823+
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
28022824
return true;
28032825
}
28042826

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
5454
// export level, however, as they're just implementation details.
5555
// Down below we'll hardwire all of the symbols to the `Rust` export
5656
// level instead.
57-
let is_compiler_builtins = tcx.is_compiler_builtins(LOCAL_CRATE);
58-
let special_runtime_crate = tcx.is_panic_runtime(LOCAL_CRATE) || is_compiler_builtins;
57+
let special_runtime_crate =
58+
tcx.is_panic_runtime(LOCAL_CRATE) || tcx.is_compiler_builtins(LOCAL_CRATE);
5959

6060
let mut reachable_non_generics: DefIdMap<_> = tcx
6161
.reachable_set(())
@@ -107,11 +107,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
107107
.map(|def_id| {
108108
// We won't link right if this symbol is stripped during LTO.
109109
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
110-
// We have to preserve the symbols of the built-in functions during LTO.
111-
let is_builtin_fn = is_compiler_builtins
112-
&& symbol_export_level(tcx, def_id.to_def_id())
113-
.is_below_threshold(SymbolExportLevel::C);
114-
let used = is_builtin_fn || name == "rust_eh_personality";
110+
let used = name == "rust_eh_personality";
115111

116112
let export_level = if special_runtime_crate {
117113
SymbolExportLevel::Rust

compiler/rustc_codegen_ssa/src/back/write.rs

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

149149
let emit_obj = if !should_emit_obj {
150150
EmitObj::None
151-
} else if sess.target.obj_is_bitcode || sess.opts.cg.linker_plugin_lto.enabled() {
151+
} else if sess.target.obj_is_bitcode
152+
|| (sess.opts.cg.linker_plugin_lto.enabled() && !no_builtins)
153+
{
152154
// This case is selected if the target uses objects as bitcode, or
153155
// if linker plugin LTO is enabled. In the linker plugin LTO case
154156
// the assumption is that the final link-step will read the bitcode
155157
// and convert it to object code. This may be done by either the
156158
// 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.
157168
EmitObj::Bitcode
158169
} else if need_bitcode_in_object(tcx) {
159170
EmitObj::ObjectCode(BitcodeSection::Full)
@@ -1023,6 +1034,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
10231034

10241035
let mut each_linked_rlib_for_lto = Vec::new();
10251036
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1037+
if link::ignored_for_lto(sess, crate_info, cnum) {
1038+
return;
1039+
}
10261040
each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
10271041
}));
10281042

compiler/rustc_codegen_ssa/src/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ impl CrateInfo {
859859
local_crate_name,
860860
compiler_builtins,
861861
profiler_runtime: None,
862+
is_no_builtins: Default::default(),
862863
native_libraries: Default::default(),
863864
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
864865
crate_name: Default::default(),
@@ -885,14 +886,19 @@ impl CrateInfo {
885886
if tcx.is_profiler_runtime(cnum) {
886887
info.profiler_runtime = Some(cnum);
887888
}
889+
if tcx.is_no_builtins(cnum) {
890+
info.is_no_builtins.insert(cnum);
891+
}
888892
}
889893

890894
// Handle circular dependencies in the standard library.
891895
// See comment before `add_linked_symbol_object` function for the details.
892896
// If global LTO is enabled then almost everything (*) is glued into a single object file,
893897
// so this logic is not necessary and can cause issues on some targets (due to weak lang
894898
// item symbols being "privatized" to that object file), so we disable it.
895-
// (*) Native libs are not glued, and we assume that they cannot define weak lang items.
899+
// (*) Native libs, and `#[compiler_builtins]` and `#[no_builtins]` crates are not glued,
900+
// and we assume that they cannot define weak lang items. This is not currently enforced
901+
// by the compiler, but that's ok because all this stuff is unstable anyway.
896902
let target = &tcx.sess.target;
897903
if !are_upstream_rust_objects_already_included(tcx.sess) {
898904
let missing_weak_lang_items: FxHashSet<Symbol> = info

compiler/rustc_codegen_ssa/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern crate tracing;
2525
extern crate rustc_middle;
2626

2727
use rustc_ast as ast;
28-
use rustc_data_structures::fx::FxHashMap;
28+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2929
use rustc_data_structures::sync::Lrc;
3030
use rustc_hir::def_id::CrateNum;
3131
use rustc_middle::dep_graph::WorkProduct;
@@ -158,6 +158,7 @@ pub struct CrateInfo {
158158
pub local_crate_name: Symbol,
159159
pub compiler_builtins: Option<CrateNum>,
160160
pub profiler_runtime: Option<CrateNum>,
161+
pub is_no_builtins: FxHashSet<CrateNum>,
161162
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
162163
pub crate_name: FxHashMap<CrateNum, Symbol>,
163164
pub used_libraries: Vec<NativeLib>,

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,12 @@ extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
531531

532532
// Unfortunately, the LLVM C API doesn't provide a way to create the
533533
// TargetLibraryInfo pass, so we use this method to do so.
534-
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M) {
534+
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M,
535+
bool DisableSimplifyLibCalls) {
535536
Triple TargetTriple(unwrap(M)->getTargetTriple());
536537
TargetLibraryInfoImpl TLII(TargetTriple);
538+
if (DisableSimplifyLibCalls)
539+
TLII.disableAllFunctions();
537540
unwrap(PMR)->add(new TargetLibraryInfoWrapperPass(TLII));
538541
}
539542

@@ -700,7 +703,7 @@ LLVMRustOptimize(
700703
bool IsLinkerPluginLTO,
701704
bool NoPrepopulatePasses, bool VerifyIR, bool UseThinLTOBuffers,
702705
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
703-
bool EmitLifetimeMarkers,
706+
bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers,
704707
LLVMRustSanitizerOptions *SanitizerOptions,
705708
const char *PGOGenPath, const char *PGOUsePath,
706709
bool InstrumentCoverage, const char *InstrProfileOutput,
@@ -800,6 +803,8 @@ LLVMRustOptimize(
800803

801804
Triple TargetTriple(TheModule->getTargetTriple());
802805
std::unique_ptr<TargetLibraryInfoImpl> TLII(new TargetLibraryInfoImpl(TargetTriple));
806+
if (DisableSimplifyLibCalls)
807+
TLII->disableAllFunctions();
803808
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
804809

805810
PB.registerModuleAnalyses(MAM);
+6-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
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-
103
all:
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
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'

tests/run-make/no-builtins-lto/filecheck.lto.txt

-17
This file was deleted.

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

-33
This file was deleted.

0 commit comments

Comments
 (0)