Skip to content

Commit 354e098

Browse files
committed
Auto merge of rust-lang#134282 - matthiaskrgr:rollup-txnn8yz, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#132150 (Fix powerpc64 big-endian FreeBSD ABI) - rust-lang#133633 (don't show the full linker args unless `--verbose` is passed) - rust-lang#133942 (Clarify how to use `black_box()`) - rust-lang#134081 (Try to evaluate constants in legacy mangling) - rust-lang#134192 (Remove `Lexer`'s dependency on `Parser`.) - rust-lang#134208 (coverage: Tidy up creation of covmap and covfun records) - rust-lang#134211 (On Neutrino QNX, reduce the need to set archiver via environment variables) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4a204be + 9668539 commit 354e098

File tree

28 files changed

+453
-263
lines changed

28 files changed

+453
-263
lines changed

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+32-35
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
7575

7676
// Encode all filenames referenced by coverage mappings in this CGU.
7777
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
78-
79-
let filenames_size = filenames_buffer.len();
80-
let filenames_val = cx.const_bytes(&filenames_buffer);
81-
let filenames_ref = llvm_cov::hash_bytes(&filenames_buffer);
78+
// The `llvm-cov` tool uses this hash to associate each covfun record with
79+
// its corresponding filenames table, since the final binary will typically
80+
// contain multiple covmap records from different compilation units.
81+
let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
8282

8383
let mut unused_function_names = Vec::new();
8484

@@ -101,7 +101,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
101101
for covfun in &covfun_records {
102102
unused_function_names.extend(covfun.mangled_function_name_if_unused());
103103

104-
covfun::generate_covfun_record(cx, filenames_ref, covfun)
104+
covfun::generate_covfun_record(cx, filenames_hash, covfun)
105105
}
106106

107107
// For unused functions, we need to take their mangled names and store them
@@ -126,7 +126,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
126126
// Generate the coverage map header, which contains the filenames used by
127127
// this CGU's coverage mappings, and store it in a well-known global.
128128
// (This is skipped if we returned early due to having no covfun records.)
129-
generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
129+
generate_covmap_record(cx, covmap_version, &filenames_buffer);
130130
}
131131

132132
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
@@ -225,38 +225,35 @@ fn span_file_name(tcx: TyCtxt<'_>, span: Span) -> Symbol {
225225
/// Generates the contents of the covmap record for this CGU, which mostly
226226
/// consists of a header and a list of filenames. The record is then stored
227227
/// as a global variable in the `__llvm_covmap` section.
228-
fn generate_covmap_record<'ll>(
229-
cx: &CodegenCx<'ll, '_>,
230-
version: u32,
231-
filenames_size: usize,
232-
filenames_val: &'ll llvm::Value,
233-
) {
234-
debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
235-
236-
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
237-
// as of `llvm::coverage::CovMapVersion::Version4`.)
238-
let zero_was_n_records_val = cx.const_u32(0);
239-
let filenames_size_val = cx.const_u32(filenames_size as u32);
240-
let zero_was_coverage_size_val = cx.const_u32(0);
241-
let version_val = cx.const_u32(version);
242-
let cov_data_header_val = cx.const_struct(
243-
&[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
244-
/*packed=*/ false,
228+
fn generate_covmap_record<'ll>(cx: &CodegenCx<'ll, '_>, version: u32, filenames_buffer: &[u8]) {
229+
// A covmap record consists of four target-endian u32 values, followed by
230+
// the encoded filenames table. Two of the header fields are unused in
231+
// modern versions of the LLVM coverage mapping format, and are always 0.
232+
// <https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation>
233+
// See also `src/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp`.
234+
let covmap_header = cx.const_struct(
235+
&[
236+
cx.const_u32(0), // (unused)
237+
cx.const_u32(filenames_buffer.len() as u32),
238+
cx.const_u32(0), // (unused)
239+
cx.const_u32(version),
240+
],
241+
/* packed */ false,
245242
);
246-
247-
// Create the complete LLVM coverage data value to add to the LLVM IR
248-
let covmap_data =
249-
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false);
250-
251-
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(covmap_data), &llvm_cov::covmap_var_name());
252-
llvm::set_initializer(llglobal, covmap_data);
253-
llvm::set_global_constant(llglobal, true);
254-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
255-
llvm::set_section(llglobal, &llvm_cov::covmap_section_name(cx.llmod));
243+
let covmap_record = cx
244+
.const_struct(&[covmap_header, cx.const_bytes(filenames_buffer)], /* packed */ false);
245+
246+
let covmap_global =
247+
llvm::add_global(cx.llmod, cx.val_ty(covmap_record), &llvm_cov::covmap_var_name());
248+
llvm::set_initializer(covmap_global, covmap_record);
249+
llvm::set_global_constant(covmap_global, true);
250+
llvm::set_linkage(covmap_global, llvm::Linkage::PrivateLinkage);
251+
llvm::set_section(covmap_global, &llvm_cov::covmap_section_name(cx.llmod));
256252
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
257253
// <https://llvm.org/docs/CoverageMappingFormat.html>
258-
llvm::set_alignment(llglobal, Align::EIGHT);
259-
cx.add_used_global(llglobal);
254+
llvm::set_alignment(covmap_global, Align::EIGHT);
255+
256+
cx.add_used_global(covmap_global);
260257
}
261258

262259
/// Each CGU will normally only emit coverage metadata for the functions that it actually generates.

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

+29-30
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn fill_region_tables<'tcx>(
136136
/// as a global variable in the `__llvm_covfun` section.
137137
pub(crate) fn generate_covfun_record<'tcx>(
138138
cx: &CodegenCx<'_, 'tcx>,
139-
filenames_ref: u64,
139+
filenames_hash: u64,
140140
covfun: &CovfunRecord<'tcx>,
141141
) {
142142
let &CovfunRecord {
@@ -155,46 +155,45 @@ pub(crate) fn generate_covfun_record<'tcx>(
155155
regions,
156156
);
157157

158-
// Concatenate the encoded coverage mappings
159-
let coverage_mapping_size = coverage_mapping_buffer.len();
160-
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
161-
158+
// A covfun record consists of four target-endian integers, followed by the
159+
// encoded mapping data in bytes. Note that the length field is 32 bits.
160+
// <https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation>
161+
// See also `src/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp` and
162+
// `COVMAP_V3` in `src/llvm-project/llvm/include/llvm/ProfileData/InstrProfData.inc`.
162163
let func_name_hash = llvm_cov::hash_bytes(mangled_function_name.as_bytes());
163-
let func_name_hash_val = cx.const_u64(func_name_hash);
164-
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
165-
let source_hash_val = cx.const_u64(source_hash);
166-
let filenames_ref_val = cx.const_u64(filenames_ref);
167-
let func_record_val = cx.const_struct(
164+
let covfun_record = cx.const_struct(
168165
&[
169-
func_name_hash_val,
170-
coverage_mapping_size_val,
171-
source_hash_val,
172-
filenames_ref_val,
173-
coverage_mapping_val,
166+
cx.const_u64(func_name_hash),
167+
cx.const_u32(coverage_mapping_buffer.len() as u32),
168+
cx.const_u64(source_hash),
169+
cx.const_u64(filenames_hash),
170+
cx.const_bytes(&coverage_mapping_buffer),
174171
],
175-
/*packed=*/ true,
172+
// This struct needs to be packed, so that the 32-bit length field
173+
// doesn't have unexpected padding.
174+
true,
176175
);
177176

178177
// Choose a variable name to hold this function's covfun data.
179178
// Functions that are used have a suffix ("u") to distinguish them from
180179
// unused copies of the same function (from different CGUs), so that if a
181180
// linker sees both it won't discard the used copy's data.
182-
let func_record_var_name =
183-
CString::new(format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" }))
184-
.unwrap();
185-
debug!("function record var name: {:?}", func_record_var_name);
186-
187-
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(func_record_val), &func_record_var_name);
188-
llvm::set_initializer(llglobal, func_record_val);
189-
llvm::set_global_constant(llglobal, true);
190-
llvm::set_linkage(llglobal, llvm::Linkage::LinkOnceODRLinkage);
191-
llvm::set_visibility(llglobal, llvm::Visibility::Hidden);
192-
llvm::set_section(llglobal, cx.covfun_section_name());
181+
let u = if is_used { "u" } else { "" };
182+
let covfun_var_name = CString::new(format!("__covrec_{func_name_hash:X}{u}")).unwrap();
183+
debug!("function record var name: {covfun_var_name:?}");
184+
185+
let covfun_global = llvm::add_global(cx.llmod, cx.val_ty(covfun_record), &covfun_var_name);
186+
llvm::set_initializer(covfun_global, covfun_record);
187+
llvm::set_global_constant(covfun_global, true);
188+
llvm::set_linkage(covfun_global, llvm::Linkage::LinkOnceODRLinkage);
189+
llvm::set_visibility(covfun_global, llvm::Visibility::Hidden);
190+
llvm::set_section(covfun_global, cx.covfun_section_name());
193191
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
194192
// <https://llvm.org/docs/CoverageMappingFormat.html>
195-
llvm::set_alignment(llglobal, Align::EIGHT);
193+
llvm::set_alignment(covfun_global, Align::EIGHT);
196194
if cx.target_spec().supports_comdat() {
197-
llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name);
195+
llvm::set_comdat(cx.llmod, covfun_global, &covfun_var_name);
198196
}
199-
cx.add_used_global(llglobal);
197+
198+
cx.add_used_global(covfun_global);
200199
}

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,12 @@ fn link_natively(
992992
let mut output = prog.stderr.clone();
993993
output.extend_from_slice(&prog.stdout);
994994
let escaped_output = escape_linker_output(&output, flavor);
995-
// FIXME: Add UI tests for this error.
996995
let err = errors::LinkingFailed {
997996
linker_path: &linker_path,
998997
exit_status: prog.status,
999-
command: &cmd,
998+
command: cmd,
1000999
escaped_output,
1000+
verbose: sess.opts.verbose,
10011001
};
10021002
sess.dcx().emit_err(err);
10031003
// If MSVC's `link.exe` was expected but the return code

Diff for: compiler/rustc_codegen_ssa/src/errors.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Errors emitted by codegen_ssa
22
33
use std::borrow::Cow;
4+
use std::ffi::OsString;
45
use std::io::Error;
56
use std::num::ParseIntError;
67
use std::path::{Path, PathBuf};
@@ -345,21 +346,82 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ThorinErrorWrapper {
345346
}
346347

347348
pub(crate) struct LinkingFailed<'a> {
348-
pub linker_path: &'a PathBuf,
349+
pub linker_path: &'a Path,
349350
pub exit_status: ExitStatus,
350-
pub command: &'a Command,
351+
pub command: Command,
351352
pub escaped_output: String,
353+
pub verbose: bool,
352354
}
353355

354356
impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
355-
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
357+
fn into_diag(mut self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
356358
let mut diag = Diag::new(dcx, level, fluent::codegen_ssa_linking_failed);
357359
diag.arg("linker_path", format!("{}", self.linker_path.display()));
358360
diag.arg("exit_status", format!("{}", self.exit_status));
359361

360362
let contains_undefined_ref = self.escaped_output.contains("undefined reference to");
361363

362-
diag.note(format!("{:?}", self.command)).note(self.escaped_output);
364+
if self.verbose {
365+
diag.note(format!("{:?}", self.command));
366+
} else {
367+
enum ArgGroup {
368+
Regular(OsString),
369+
Objects(usize),
370+
Rlibs(PathBuf, Vec<OsString>),
371+
}
372+
373+
// Omit rust object files and fold rlibs in the error by default to make linker errors a
374+
// bit less verbose.
375+
let orig_args = self.command.take_args();
376+
let mut args: Vec<ArgGroup> = vec![];
377+
for arg in orig_args {
378+
if arg.as_encoded_bytes().ends_with(b".rcgu.o") {
379+
if let Some(ArgGroup::Objects(n)) = args.last_mut() {
380+
*n += 1;
381+
} else {
382+
args.push(ArgGroup::Objects(1));
383+
}
384+
} else if arg.as_encoded_bytes().ends_with(b".rlib") {
385+
let rlib_path = Path::new(&arg);
386+
let dir = rlib_path.parent().unwrap();
387+
let filename = rlib_path.file_name().unwrap().to_owned();
388+
if let Some(ArgGroup::Rlibs(parent, rlibs)) = args.last_mut() {
389+
if parent == dir {
390+
rlibs.push(filename);
391+
} else {
392+
args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename]));
393+
}
394+
} else {
395+
args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename]));
396+
}
397+
} else {
398+
args.push(ArgGroup::Regular(arg));
399+
}
400+
}
401+
self.command.args(args.into_iter().map(|arg_group| match arg_group {
402+
ArgGroup::Regular(arg) => arg,
403+
ArgGroup::Objects(n) => OsString::from(format!("<{n} object files omitted>")),
404+
ArgGroup::Rlibs(dir, rlibs) => {
405+
let mut arg = dir.into_os_string();
406+
arg.push("/{");
407+
let mut first = true;
408+
for rlib in rlibs {
409+
if !first {
410+
arg.push(",");
411+
}
412+
first = false;
413+
arg.push(rlib);
414+
}
415+
arg.push("}");
416+
arg
417+
}
418+
}));
419+
420+
diag.note(format!("{:?}", self.command));
421+
diag.note("some arguments are omitted. use `--verbose` to show all linker arguments");
422+
}
423+
424+
diag.note(self.escaped_output);
363425

364426
// Trying to match an error from OS linkers
365427
// which by now we have no way to translate.

Diff for: compiler/rustc_parse/src/lexer/mod.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,30 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
6969
token: Token::dummy(),
7070
diag_info: TokenTreeDiagInfo::default(),
7171
};
72-
let (_open_spacing, stream, res) = lexer.lex_token_trees(/* is_delimited */ false);
73-
let unmatched_delims = lexer.diag_info.unmatched_delims;
74-
75-
if res.is_ok() && unmatched_delims.is_empty() {
76-
Ok(stream)
77-
} else {
78-
// Return error if there are unmatched delimiters or unclosed delimiters.
79-
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
80-
// because the delimiter mismatch is more likely to be the root cause of error
81-
let mut buffer: Vec<_> = unmatched_delims
82-
.into_iter()
83-
.filter_map(|unmatched_delim| make_unclosed_delims_error(unmatched_delim, psess))
84-
.collect();
85-
if let Err(errs) = res {
86-
// Add unclosing delimiter or diff marker errors
87-
buffer.extend(errs);
72+
let res = lexer.lex_token_trees(/* is_delimited */ false);
73+
74+
let mut unmatched_delims: Vec<_> = lexer
75+
.diag_info
76+
.unmatched_delims
77+
.into_iter()
78+
.filter_map(|unmatched_delim| make_unclosed_delims_error(unmatched_delim, psess))
79+
.collect();
80+
81+
match res {
82+
Ok((_open_spacing, stream)) => {
83+
if unmatched_delims.is_empty() {
84+
Ok(stream)
85+
} else {
86+
// Return error if there are unmatched delimiters or unclosed delimiters.
87+
Err(unmatched_delims)
88+
}
89+
}
90+
Err(errs) => {
91+
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
92+
// because the delimiter mismatch is more likely to be the root cause of error
93+
unmatched_delims.extend(errs);
94+
Err(unmatched_delims)
8895
}
89-
Err(buffer)
9096
}
9197
}
9298

0 commit comments

Comments
 (0)