Skip to content

Commit 7601adc

Browse files
committed
Auto merge of #125463 - GuillaumeGomez:rollup-287wx4y, r=GuillaumeGomez
Rollup of 6 pull requests Successful merges: - #125263 (rust-lld: fallback to rustc's sysroot if there's no path to the linker in the target sysroot) - #125345 (rustc_codegen_llvm: add support for writing summary bitcode) - #125362 (Actually use TAIT instead of emulating it) - #125412 (Don't suggest adding the unexpected cfgs to the build-script it-self) - #125445 (Migrate `run-make/rustdoc-with-short-out-dir-option` to `rmake.rs`) - #125452 (Cleanup check-cfg handling in core and std) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 78dd504 + a8a71d0 commit 7601adc

File tree

32 files changed

+335
-187
lines changed

32 files changed

+335
-187
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn produce_final_output_artifacts(
200200
// to get rid of it.
201201
for output_type in crate_output.outputs.keys() {
202202
match *output_type {
203-
OutputType::Bitcode => {
203+
OutputType::Bitcode | OutputType::ThinLinkBitcode => {
204204
// Cranelift doesn't have bitcode
205205
// user_wants_bitcode = true;
206206
// // Copy to .bc, but always keep the .0.bc. There is a later

compiler/rustc_codegen_gcc/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ impl ThinBufferMethods for ThinBuffer {
335335
fn data(&self) -> &[u8] {
336336
unimplemented!();
337337
}
338+
339+
fn thin_link_data(&self) -> &[u8] {
340+
unimplemented!();
341+
}
338342
}
339343

340344
pub struct GccContext {
@@ -414,7 +418,7 @@ impl WriteBackendMethods for GccCodegenBackend {
414418
back::write::codegen(cgcx, dcx, module, config)
415419
}
416420

417-
fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
421+
fn prepare_thin(_module: ModuleCodegen<Self::Module>, _emit_summary: bool) -> (String, Self::ThinBuffer) {
418422
unimplemented!();
419423
}
420424

compiler/rustc_codegen_llvm/src/back/lto.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ pub(crate) fn run_thin(
230230
thin_lto(cgcx, &dcx, modules, upstream_modules, cached_modules, &symbols_below_threshold)
231231
}
232232

233-
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBuffer) {
233+
pub(crate) fn prepare_thin(
234+
module: ModuleCodegen<ModuleLlvm>,
235+
emit_summary: bool,
236+
) -> (String, ThinBuffer) {
234237
let name = module.name;
235-
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true);
238+
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true, emit_summary);
236239
(name, buffer)
237240
}
238241

@@ -672,9 +675,9 @@ unsafe impl Send for ThinBuffer {}
672675
unsafe impl Sync for ThinBuffer {}
673676

674677
impl ThinBuffer {
675-
pub fn new(m: &llvm::Module, is_thin: bool) -> ThinBuffer {
678+
pub fn new(m: &llvm::Module, is_thin: bool, emit_summary: bool) -> ThinBuffer {
676679
unsafe {
677-
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin);
680+
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin, emit_summary);
678681
ThinBuffer(buffer)
679682
}
680683
}
@@ -688,6 +691,14 @@ impl ThinBufferMethods for ThinBuffer {
688691
slice::from_raw_parts(ptr, len)
689692
}
690693
}
694+
695+
fn thin_link_data(&self) -> &[u8] {
696+
unsafe {
697+
let ptr = llvm::LLVMRustThinLTOBufferThinLinkDataPtr(self.0) as *const _;
698+
let len = llvm::LLVMRustThinLTOBufferThinLinkDataLen(self.0);
699+
slice::from_raw_parts(ptr, len)
700+
}
701+
}
691702
}
692703

693704
impl Drop for ThinBuffer {

compiler/rustc_codegen_llvm/src/back/write.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,15 @@ pub(crate) unsafe fn codegen(
709709
// asm from LLVM and use `gcc` to create the object file.
710710

711711
let bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
712+
let bc_summary_out =
713+
cgcx.output_filenames.temp_path(OutputType::ThinLinkBitcode, module_name);
712714
let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
713715

714716
if config.bitcode_needed() {
715717
let _timer = cgcx
716718
.prof
717719
.generic_activity_with_arg("LLVM_module_codegen_make_bitcode", &*module.name);
718-
let thin = ThinBuffer::new(llmod, config.emit_thin_lto);
720+
let thin = ThinBuffer::new(llmod, config.emit_thin_lto, config.emit_thin_lto_summary);
719721
let data = thin.data();
720722

721723
if let Some(bitcode_filename) = bc_out.file_name() {
@@ -726,6 +728,25 @@ pub(crate) unsafe fn codegen(
726728
);
727729
}
728730

731+
if config.emit_thin_lto_summary
732+
&& let Some(thin_link_bitcode_filename) = bc_summary_out.file_name()
733+
{
734+
let summary_data = thin.thin_link_data();
735+
cgcx.prof.artifact_size(
736+
"llvm_bitcode_summary",
737+
thin_link_bitcode_filename.to_string_lossy(),
738+
summary_data.len() as u64,
739+
);
740+
741+
let _timer = cgcx.prof.generic_activity_with_arg(
742+
"LLVM_module_codegen_emit_bitcode_summary",
743+
&*module.name,
744+
);
745+
if let Err(err) = fs::write(&bc_summary_out, summary_data) {
746+
dcx.emit_err(WriteBytecode { path: &bc_summary_out, err });
747+
}
748+
}
749+
729750
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
730751
let _timer = cgcx
731752
.prof

compiler/rustc_codegen_llvm/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,11 @@ impl WriteBackendMethods for LlvmCodegenBackend {
237237
) -> Result<CompiledModule, FatalError> {
238238
back::write::codegen(cgcx, dcx, module, config)
239239
}
240-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
241-
back::lto::prepare_thin(module)
240+
fn prepare_thin(
241+
module: ModuleCodegen<Self::Module>,
242+
emit_summary: bool,
243+
) -> (String, Self::ThinBuffer) {
244+
back::lto::prepare_thin(module, emit_summary)
242245
}
243246
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
244247
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2350,10 +2350,16 @@ extern "C" {
23502350
#[allow(improper_ctypes)]
23512351
pub fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString);
23522352

2353-
pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer;
2353+
pub fn LLVMRustThinLTOBufferCreate(
2354+
M: &Module,
2355+
is_thin: bool,
2356+
emit_summary: bool,
2357+
) -> &'static mut ThinLTOBuffer;
23542358
pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);
23552359
pub fn LLVMRustThinLTOBufferPtr(M: &ThinLTOBuffer) -> *const c_char;
23562360
pub fn LLVMRustThinLTOBufferLen(M: &ThinLTOBuffer) -> size_t;
2361+
pub fn LLVMRustThinLTOBufferThinLinkDataPtr(M: &ThinLTOBuffer) -> *const c_char;
2362+
pub fn LLVMRustThinLTOBufferThinLinkDataLen(M: &ThinLTOBuffer) -> size_t;
23572363
pub fn LLVMRustCreateThinLTOData(
23582364
Modules: *const ThinLTOModule,
23592365
NumModules: c_uint,

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ codegen_ssa_rlib_only_rmeta_found = could not find rlib for: `{$crate_name}`, fo
212212
213213
codegen_ssa_select_cpp_build_tool_workload = in the Visual Studio installer, ensure the "C++ build tools" workload is selected
214214
215+
codegen_ssa_self_contained_linker_missing = the self-contained linker was requested, but it wasn't found in the target's sysroot, or in rustc's sysroot
216+
215217
codegen_ssa_shuffle_indices_evaluation = could not evaluate shuffle_indices at compile time
216218
217219
codegen_ssa_specify_libraries_to_link = use the `-l` flag to specify native libraries to link

compiler/rustc_codegen_ssa/src/back/link.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -3141,13 +3141,21 @@ fn add_lld_args(
31413141

31423142
let self_contained_linker = self_contained_cli || self_contained_target;
31433143
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
3144+
let mut linker_path_exists = false;
31443145
for path in sess.get_tools_search_paths(false) {
3146+
let linker_path = path.join("gcc-ld");
3147+
linker_path_exists |= linker_path.exists();
31453148
cmd.arg({
31463149
let mut arg = OsString::from("-B");
3147-
arg.push(path.join("gcc-ld"));
3150+
arg.push(linker_path);
31483151
arg
31493152
});
31503153
}
3154+
if !linker_path_exists {
3155+
// As a sanity check, we emit an error if none of these paths exist: we want
3156+
// self-contained linking and have no linker.
3157+
sess.dcx().emit_fatal(errors::SelfContainedLinkerMissing);
3158+
}
31513159
}
31523160

31533161
// 2. Implement the "linker flavor" part of this feature by asking `cc` to use some kind of

compiler/rustc_codegen_ssa/src/back/write.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub struct ModuleConfig {
108108
pub emit_asm: bool,
109109
pub emit_obj: EmitObj,
110110
pub emit_thin_lto: bool,
111+
pub emit_thin_lto_summary: bool,
111112
pub bc_cmdline: String,
112113

113114
// Miscellaneous flags. These are mostly copied from command-line
@@ -232,6 +233,10 @@ impl ModuleConfig {
232233
),
233234
emit_obj,
234235
emit_thin_lto: sess.opts.unstable_opts.emit_thin_lto,
236+
emit_thin_lto_summary: if_regular!(
237+
sess.opts.output_types.contains_key(&OutputType::ThinLinkBitcode),
238+
false
239+
),
235240
bc_cmdline: sess.target.bitcode_llvm_cmdline.to_string(),
236241

237242
verify_llvm_ir: sess.verify_llvm_ir(),
@@ -283,6 +288,7 @@ impl ModuleConfig {
283288

284289
pub fn bitcode_needed(&self) -> bool {
285290
self.emit_bc
291+
|| self.emit_thin_lto_summary
286292
|| self.emit_obj == EmitObj::Bitcode
287293
|| self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
288294
}
@@ -630,6 +636,9 @@ fn produce_final_output_artifacts(
630636
// them for making an rlib.
631637
copy_if_one_unit(OutputType::Bitcode, true);
632638
}
639+
OutputType::ThinLinkBitcode => {
640+
copy_if_one_unit(OutputType::ThinLinkBitcode, false);
641+
}
633642
OutputType::LlvmAssembly => {
634643
copy_if_one_unit(OutputType::LlvmAssembly, false);
635644
}
@@ -883,7 +892,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
883892
match lto_type {
884893
ComputedLtoType::No => finish_intra_module_work(cgcx, module, module_config),
885894
ComputedLtoType::Thin => {
886-
let (name, thin_buffer) = B::prepare_thin(module);
895+
let (name, thin_buffer) = B::prepare_thin(module, false);
887896
if let Some(path) = bitcode {
888897
fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
889898
panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);

compiler/rustc_codegen_ssa/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ pub struct UnableToExeLinker {
413413
#[diag(codegen_ssa_msvc_missing_linker)]
414414
pub struct MsvcMissingLinker;
415415

416+
#[derive(Diagnostic)]
417+
#[diag(codegen_ssa_self_contained_linker_missing)]
418+
pub struct SelfContainedLinkerMissing;
419+
416420
#[derive(Diagnostic)]
417421
#[diag(codegen_ssa_check_installed_visual_studio)]
418422
pub struct CheckInstalledVisualStudio;

compiler/rustc_codegen_ssa/src/traits/write.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
5656
module: ModuleCodegen<Self::Module>,
5757
config: &ModuleConfig,
5858
) -> Result<CompiledModule, FatalError>;
59-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer);
59+
fn prepare_thin(
60+
module: ModuleCodegen<Self::Module>,
61+
want_summary: bool,
62+
) -> (String, Self::ThinBuffer);
6063
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
6164
}
6265

6366
pub trait ThinBufferMethods: Send + Sync {
6467
fn data(&self) -> &[u8];
68+
fn thin_link_data(&self) -> &[u8];
6569
}
6670

6771
pub trait ModuleBufferMethods: Send + Sync {

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -
4242
}
4343
}
4444

45+
fn cargo_help_sub(
46+
sess: &Session,
47+
inst: &impl Fn(EscapeQuotes) -> String,
48+
) -> lints::UnexpectedCfgCargoHelp {
49+
// We don't want to suggest the `build.rs` way to expected cfgs if we are already in a
50+
// `build.rs`. We therefor do a best effort check (looking if the `--crate-name` is
51+
// `build_script_build`) to try to figure out if we are building a Cargo build script
52+
53+
let unescaped = &inst(EscapeQuotes::No);
54+
if matches!(&sess.opts.crate_name, Some(crate_name) if crate_name == "build_script_build") {
55+
lints::UnexpectedCfgCargoHelp::lint_cfg(unescaped)
56+
} else {
57+
lints::UnexpectedCfgCargoHelp::lint_cfg_and_build_rs(unescaped, &inst(EscapeQuotes::Yes))
58+
}
59+
}
60+
4561
pub(super) fn unexpected_cfg_name(
4662
sess: &Session,
4763
(name, name_span): (Symbol, Span),
@@ -162,14 +178,7 @@ pub(super) fn unexpected_cfg_name(
162178
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
163179

164180
let invocation_help = if is_from_cargo {
165-
let sub = if !is_feature_cfg {
166-
Some(lints::UnexpectedCfgCargoHelp::new(
167-
&inst(EscapeQuotes::No),
168-
&inst(EscapeQuotes::Yes),
169-
))
170-
} else {
171-
None
172-
};
181+
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
173182
lints::unexpected_cfg_name::InvocationHelp::Cargo { sub }
174183
} else {
175184
lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new(
@@ -267,10 +276,7 @@ pub(super) fn unexpected_cfg_value(
267276
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
268277
}
269278
} else if !is_cfg_a_well_know_name {
270-
Some(lints::unexpected_cfg_value::CargoHelp::Other(lints::UnexpectedCfgCargoHelp::new(
271-
&inst(EscapeQuotes::No),
272-
&inst(EscapeQuotes::Yes),
273-
)))
279+
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
274280
} else {
275281
None
276282
};

compiler/rustc_lint/src/lints.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -1962,21 +1962,33 @@ pub struct UnitBindingsDiag {
19621962
pub struct BuiltinNamedAsmLabel;
19631963

19641964
#[derive(Subdiagnostic)]
1965-
#[help(lint_unexpected_cfg_add_cargo_feature)]
1966-
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1967-
#[help(lint_unexpected_cfg_add_build_rs_println)]
1968-
pub struct UnexpectedCfgCargoHelp {
1969-
pub build_rs_println: String,
1970-
pub cargo_toml_lint_cfg: String,
1965+
pub enum UnexpectedCfgCargoHelp {
1966+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1967+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1968+
LintCfg { cargo_toml_lint_cfg: String },
1969+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1970+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1971+
#[help(lint_unexpected_cfg_add_build_rs_println)]
1972+
LintCfgAndBuildRs { cargo_toml_lint_cfg: String, build_rs_println: String },
19711973
}
19721974

19731975
impl UnexpectedCfgCargoHelp {
1974-
pub fn new(unescaped: &str, escaped: &str) -> Self {
1975-
Self {
1976-
cargo_toml_lint_cfg: format!(
1977-
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}",
1978-
),
1979-
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");",),
1976+
fn cargo_toml_lint_cfg(unescaped: &str) -> String {
1977+
format!(
1978+
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}"
1979+
)
1980+
}
1981+
1982+
pub fn lint_cfg(unescaped: &str) -> Self {
1983+
UnexpectedCfgCargoHelp::LintCfg {
1984+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1985+
}
1986+
}
1987+
1988+
pub fn lint_cfg_and_build_rs(unescaped: &str, escaped: &str) -> Self {
1989+
UnexpectedCfgCargoHelp::LintCfgAndBuildRs {
1990+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1991+
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");"),
19801992
}
19811993
}
19821994
}

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -1488,13 +1488,15 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M,
14881488
// a ThinLTO summary attached.
14891489
struct LLVMRustThinLTOBuffer {
14901490
std::string data;
1491+
std::string thin_link_data;
14911492
};
14921493

14931494
extern "C" LLVMRustThinLTOBuffer*
1494-
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
1495+
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin, bool emit_summary) {
14951496
auto Ret = std::make_unique<LLVMRustThinLTOBuffer>();
14961497
{
14971498
auto OS = raw_string_ostream(Ret->data);
1499+
auto ThinLinkOS = raw_string_ostream(Ret->thin_link_data);
14981500
{
14991501
if (is_thin) {
15001502
PassBuilder PB;
@@ -1508,7 +1510,10 @@ LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
15081510
PB.registerLoopAnalyses(LAM);
15091511
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
15101512
ModulePassManager MPM;
1511-
MPM.addPass(ThinLTOBitcodeWriterPass(OS, nullptr));
1513+
// We only pass ThinLinkOS to be filled in if we want the summary,
1514+
// because otherwise LLVM does extra work and may double-emit some
1515+
// errors or warnings.
1516+
MPM.addPass(ThinLTOBitcodeWriterPass(OS, emit_summary ? &ThinLinkOS : nullptr));
15121517
MPM.run(*unwrap(M), MAM);
15131518
} else {
15141519
WriteBitcodeToFile(*unwrap(M), OS);
@@ -1533,6 +1538,16 @@ LLVMRustThinLTOBufferLen(const LLVMRustThinLTOBuffer *Buffer) {
15331538
return Buffer->data.length();
15341539
}
15351540

1541+
extern "C" const void*
1542+
LLVMRustThinLTOBufferThinLinkDataPtr(const LLVMRustThinLTOBuffer *Buffer) {
1543+
return Buffer->thin_link_data.data();
1544+
}
1545+
1546+
extern "C" size_t
1547+
LLVMRustThinLTOBufferThinLinkDataLen(const LLVMRustThinLTOBuffer *Buffer) {
1548+
return Buffer->thin_link_data.length();
1549+
}
1550+
15361551
// This is what we used to parse upstream bitcode for actual ThinLTO
15371552
// processing. We'll call this once per module optimized through ThinLTO, and
15381553
// it'll be called concurrently on many threads.

0 commit comments

Comments
 (0)