@@ -1024,6 +1024,40 @@ impl Session {
1024
1024
|| self . opts . debugging_opts . sanitizer . intersects ( SanitizerSet :: ADDRESS | SanitizerSet :: MEMORY )
1025
1025
}
1026
1026
1027
+ pub fn link_dead_code ( & self ) -> bool {
1028
+ match self . opts . cg . link_dead_code {
1029
+ Some ( explicitly_set) => explicitly_set,
1030
+ None => {
1031
+ self . opts . debugging_opts . instrument_coverage
1032
+ && !self . target . target . options . is_like_msvc
1033
+ // Issue #76038: (rustc `-Clink-dead-code` causes MSVC linker to produce invalid
1034
+ // binaries when LLVM InstrProf counters are enabled). As described by this issue,
1035
+ // the "link dead code" option produces incorrect binaries when compiled and linked
1036
+ // under MSVC. The resulting Rust programs typically crash with a segmentation
1037
+ // fault, or produce an empty "*.profraw" file (profiling counter results normally
1038
+ // generated during program exit).
1039
+ //
1040
+ // If not targeting MSVC, `-Z instrument-coverage` implies `-C link-dead-code`, so
1041
+ // unexecuted code is still counted as zero, rather than be optimized out. Note that
1042
+ // instrumenting dead code can be explicitly disabled with:
1043
+ //
1044
+ // `-Z instrument-coverage -C link-dead-code=no`.
1045
+ //
1046
+ // FIXME(richkadel): Investigate if `instrument-coverage` implementation can inject
1047
+ // [zero counters](https://llvm.org/docs/CoverageMappingFormat.html#counter) in the
1048
+ // coverage map when "dead code" is removed, rather than forcing `link-dead-code`.
1049
+ // This may not be possible, however, if (as it seems to appear) the "dead code"
1050
+ // that would otherwise not be linked is only identified as "dead" by the native
1051
+ // linker. If that's the case, I believe it is too late for the Rust compiler to
1052
+ // leverage any information it might be able to get from the linker regarding what
1053
+ // code is dead, to be able to add those counters.
1054
+ //
1055
+ // On the other hand, if any Rust compiler passes are optimizing out dead code blocks
1056
+ // we should inject "zero" counters for those code regions.
1057
+ }
1058
+ }
1059
+ }
1060
+
1027
1061
pub fn mark_attr_known ( & self , attr : & Attribute ) {
1028
1062
self . known_attrs . lock ( ) . mark ( attr)
1029
1063
}
@@ -1432,20 +1466,6 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
1432
1466
) ;
1433
1467
}
1434
1468
1435
- // FIXME(richkadel): See `src/test/run-make-fulldeps/instrument-coverage/Makefile`. After
1436
- // compiling with `-Zinstrument-coverage`, the resulting binary generates a segfault during
1437
- // the program's exit process (likely while attempting to generate the coverage stats in
1438
- // the "*.profraw" file). An investigation to resolve the problem on Windows is ongoing,
1439
- // but until this is resolved, the option is disabled on Windows, and the test is skipped
1440
- // when targeting `MSVC`.
1441
- if sess. opts . debugging_opts . instrument_coverage && sess. target . target . options . is_like_msvc {
1442
- sess. warn (
1443
- "Rust source-based code coverage instrumentation (with `-Z instrument-coverage`) \
1444
- is not yet supported on Windows when targeting MSVC. The resulting binaries will \
1445
- still be instrumented for experimentation purposes, but may not execute correctly.",
1446
- ) ;
1447
- }
1448
-
1449
1469
const ASAN_SUPPORTED_TARGETS : & [ & str ] = & [
1450
1470
"aarch64-fuchsia" ,
1451
1471
"aarch64-unknown-linux-gnu" ,
0 commit comments