Skip to content

Commit d5f6b9c

Browse files
committed
code-cov: generate dead functions with private/default linkage
As discovered in #85461, the MSVC linker treats weak symbols slightly differently than unix-y linkers do. This causes link.exe to fail with LNK1227 "conflicting weak extern definition" where as other targets are able to link successfully. This changes the dead functions from being generated as weak/hidden to private/default which, as the LLVM reference says: > Global values with “private” linkage are only directly accessible by objects in the current module. In particular, linking code into a module with a private global value may cause the private to be renamed as necessary to avoid collisions. Because the symbol is private to the module, all references can be updated. This doesn’t show up in any symbol table in the object file. This fixes the conflicting weak symbols but doesn't address the reason *why* we have conflicting symbols for these dead functions. The test cases added in this commit contain a minimal repro of the fundamental issue which is that the logic used to decide what dead code functions should be codegen'd in the current CGU doesn't take into account that functions can be duplicated across multiple CGUs (for instance, in the case of `#[inline(always)]` functions). Fixing that is likely to be a more complex change (see #85461 (comment)). Fixes #85461
1 parent d47a6cc commit d5f6b9c

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ fn declare_unused_fn(cx: &CodegenCx<'ll, 'tcx>, def_id: &DefId) -> Instance<'tcx
212212
),
213213
);
214214

215-
llvm::set_linkage(llfn, llvm::Linkage::WeakAnyLinkage);
216-
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
215+
llvm::set_linkage(llfn, llvm::Linkage::PrivateLinkage);
216+
llvm::set_visibility(llfn, llvm::Visibility::Default);
217217

218218
assert!(cx.instances.borrow_mut().insert(instance, llfn).is_none());
219219

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
../coverage/issue-85461.rs:
2+
1| |// Regression test for #85461: MSVC sometimes fail to link with dead code and #[inline(always)]
3+
2| |
4+
3| |extern crate inline_always_with_dead_code;
5+
4| |
6+
5| |use inline_always_with_dead_code::{bar, baz};
7+
6| |
8+
7| 1|fn main() {
9+
8| 1| bar::call_me();
10+
9| 1| baz::call_me();
11+
10| 1|}
12+
13+
../coverage/lib/inline_always_with_dead_code.rs:
14+
1| |// compile-flags: -Zinstrument-coverage -Ccodegen-units=4 -Copt-level=0
15+
2| |
16+
3| |#![allow(dead_code)]
17+
4| |
18+
5| |mod foo {
19+
6| | #[inline(always)]
20+
7| 2| pub fn called() { }
21+
8| |
22+
9| 0| fn uncalled() { }
23+
10| |}
24+
11| |
25+
12| |pub mod bar {
26+
13| 1| pub fn call_me() {
27+
14| 1| super::foo::called();
28+
15| 1| }
29+
16| |}
30+
17| |
31+
18| |pub mod baz {
32+
19| 1| pub fn call_me() {
33+
20| 1| super::foo::called();
34+
21| 1| }
35+
22| |}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for #85461: MSVC sometimes fail to link with dead code and #[inline(always)]
2+
3+
extern crate inline_always_with_dead_code;
4+
5+
use inline_always_with_dead_code::{bar, baz};
6+
7+
fn main() {
8+
bar::call_me();
9+
baz::call_me();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile-flags: -Zinstrument-coverage -Ccodegen-units=4 -Copt-level=0
2+
3+
#![allow(dead_code)]
4+
5+
mod foo {
6+
#[inline(always)]
7+
pub fn called() { }
8+
9+
fn uncalled() { }
10+
}
11+
12+
pub mod bar {
13+
pub fn call_me() {
14+
super::foo::called();
15+
}
16+
}
17+
18+
pub mod baz {
19+
pub fn call_me() {
20+
super::foo::called();
21+
}
22+
}

src/test/ui/issues/issue-85461.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// compile-flags: -Zinstrument-coverage -Ccodegen-units=4 --crate-type dylib -Copt-level=0
2+
// build-pass
3+
// needs-profiler-support
4+
5+
// Regression test for #85461 where MSVC sometimes fails to link instrument-coverage binaries
6+
// with dead code and #[inline(always)].
7+
8+
#![allow(dead_code)]
9+
10+
mod foo {
11+
#[inline(always)]
12+
pub fn called() { }
13+
14+
fn uncalled() { }
15+
}
16+
17+
pub mod bar {
18+
pub fn call_me() {
19+
super::foo::called();
20+
}
21+
}
22+
23+
pub mod baz {
24+
pub fn call_me() {
25+
super::foo::called();
26+
}
27+
}

0 commit comments

Comments
 (0)