Skip to content

Commit 8e7a609

Browse files
committed
Auto merge of #46916 - michaelwoerister:generate-dead-code-plz, r=alexcrichton
Generate code for unused const- and inline-fns if -Clink-dead-code is specified. Fixes #46467. r? @alexcrichton
2 parents 4cd918c + 238ed47 commit 8e7a609

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

Diff for: src/librustc_mir/monomorphize/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
115115
let inline_in_all_cgus =
116116
tcx.sess.opts.debugging_opts.inline_in_all_cgus.unwrap_or_else(|| {
117117
tcx.sess.opts.optimize != OptLevel::No
118-
});
118+
}) && !tcx.sess.opts.cg.link_dead_code;
119119

120120
match *self.as_mono_item() {
121121
MonoItem::Fn(ref instance) => {

Diff for: src/librustc_mir/monomorphize/partitioning.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ pub fn partition<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
236236

237237
// Next we try to make as many symbols "internal" as possible, so LLVM has
238238
// more freedom to optimize.
239-
internalize_symbols(tcx, &mut post_inlining, inlining_map);
239+
if !tcx.sess.opts.cg.link_dead_code {
240+
internalize_symbols(tcx, &mut post_inlining, inlining_map);
241+
}
240242

241243
// Finally, sort by codegen unit name, so that we get deterministic results
242244
let PostInliningPartitioning {

Diff for: src/librustc_trans/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,13 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
10211021
MonoItemCollectionMode::Lazy
10221022
}
10231023
}
1024-
None => MonoItemCollectionMode::Lazy
1024+
None => {
1025+
if tcx.sess.opts.cg.link_dead_code {
1026+
MonoItemCollectionMode::Eager
1027+
} else {
1028+
MonoItemCollectionMode::Lazy
1029+
}
1030+
}
10251031
};
10261032

10271033
let (items, inlining_map) =

Diff for: src/test/codegen/link-dead-code.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:-Clink-dead-code
12+
13+
#![feature(const_fn)]
14+
#![crate_type = "rlib"]
15+
16+
// This test makes sure that, when -Clink-dead-code is specified, we generate
17+
// code for functions that would otherwise be skipped.
18+
19+
// CHECK-LABEL: define hidden i32 @_ZN14link_dead_code8const_fn
20+
const fn const_fn() -> i32 { 1 }
21+
22+
// CHECK-LABEL: define hidden i32 @_ZN14link_dead_code9inline_fn
23+
#[inline]
24+
fn inline_fn() -> i32 { 2 }
25+
26+
// CHECK-LABEL: define hidden i32 @_ZN14link_dead_code10private_fn
27+
fn private_fn() -> i32 { 3 }

0 commit comments

Comments
 (0)