Skip to content

Commit 238ed47

Browse files
Generate code for const- and inline-fns if -Clink-dead-code is specified.
1 parent de38f49 commit 238ed47

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

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) => {

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 {

src/librustc_trans/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,13 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
10141014
MonoItemCollectionMode::Lazy
10151015
}
10161016
}
1017-
None => MonoItemCollectionMode::Lazy
1017+
None => {
1018+
if tcx.sess.opts.cg.link_dead_code {
1019+
MonoItemCollectionMode::Eager
1020+
} else {
1021+
MonoItemCollectionMode::Lazy
1022+
}
1023+
}
10181024
};
10191025

10201026
let (items, inlining_map) =

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)