Skip to content

Only instantiate inline- and const-fns if they are referenced (again). #45575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ impl DepGraph {
}

pub fn fingerprint_of(&self, dep_node: &DepNode) -> Fingerprint {
self.fingerprints.borrow()[dep_node]
match self.fingerprints.borrow().get(dep_node) {
Some(&fingerprint) => fingerprint,
None => {
bug!("Could not find current fingerprint for {:?}", dep_node)
}
}
}

pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
Expand Down
34 changes: 26 additions & 8 deletions src/librustc_trans_utils/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ use trans_item::{TransItemExt, DefPathBasedNames, InstantiationMode};

use rustc_data_structures::bitvec::BitVector;

use syntax::attr;

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum TransItemCollectionMode {
Eager,
Expand Down Expand Up @@ -324,9 +326,14 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let mut roots = Vec::new();

{
let entry_fn = tcx.sess.entry_fn.borrow().map(|(node_id, _)| {
tcx.hir.local_def_id(node_id)
});

let mut visitor = RootCollector {
tcx,
mode,
entry_fn,
output: &mut roots,
};

Expand Down Expand Up @@ -875,6 +882,7 @@ struct RootCollector<'b, 'a: 'b, 'tcx: 'a + 'b> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
mode: TransItemCollectionMode,
output: &'b mut Vec<TransItem<'tcx>>,
entry_fn: Option<DefId>,
}

impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
Expand Down Expand Up @@ -932,10 +940,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
let tcx = self.tcx;
let def_id = tcx.hir.local_def_id(item.id);

if (self.mode == TransItemCollectionMode::Eager ||
!tcx.is_const_fn(def_id) || tcx.is_exported_symbol(def_id)) &&
!item_has_type_parameters(tcx, def_id) {

if self.is_root(def_id) {
debug!("RootCollector: ItemFn({})",
def_id_to_string(tcx, def_id));

Expand All @@ -957,10 +962,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
let tcx = self.tcx;
let def_id = tcx.hir.local_def_id(ii.id);

if (self.mode == TransItemCollectionMode::Eager ||
!tcx.is_const_fn(def_id) ||
tcx.is_exported_symbol(def_id)) &&
!item_has_type_parameters(tcx, def_id) {
if self.is_root(def_id) {
debug!("RootCollector: MethodImplItem({})",
def_id_to_string(tcx, def_id));

Expand All @@ -973,6 +975,22 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
}
}

impl<'b, 'a, 'v> RootCollector<'b, 'a, 'v> {
fn is_root(&self, def_id: DefId) -> bool {
!item_has_type_parameters(self.tcx, def_id) && match self.mode {
TransItemCollectionMode::Eager => {
true
}
TransItemCollectionMode::Lazy => {
self.entry_fn == Some(def_id) ||
self.tcx.is_exported_symbol(def_id) ||
attr::contains_name(&self.tcx.get_attrs(def_id),
"rustc_std_internal_symbol")
}
}
}
}

fn item_has_type_parameters<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
let generics = tcx.generics_of(def_id);
generics.parent_types as usize + generics.types.len() > 0
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_trans_utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub fn requests_inline<'a, 'tcx>(
// available to normal end-users.
return true
}
attr::requests_inline(&instance.def.attrs(tcx)[..])
attr::requests_inline(&instance.def.attrs(tcx)[..]) ||
tcx.is_const_fn(instance.def.def_id())
}

pub fn is_inline_instance<'a, 'tcx>(
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_trans_utils/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ extern crate rustc_data_structures;
extern crate syntax;
extern crate syntax_pos;

use rustc::ty::TyCtxt;
use rustc::ty::{TyCtxt, Instance};
use rustc::hir;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::map as hir_map;
use rustc::util::nodemap::NodeSet;

use syntax::attr;

pub mod common;
pub mod link;
pub mod collector;
Expand Down Expand Up @@ -77,7 +75,7 @@ pub fn check_for_rustc_errors_attr(tcx: TyCtxt) {
///
/// This list is later used by linkers to determine the set of symbols needed to
/// be exposed from a dynamic library and it's also encoded into the metadata.
pub fn find_exported_symbols(tcx: TyCtxt) -> NodeSet {
pub fn find_exported_symbols<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> NodeSet {
tcx.reachable_set(LOCAL_CRATE).0.iter().cloned().filter(|&id| {
// Next, we want to ignore some FFI functions that are not exposed from
// this crate. Reachable FFI functions can be lumped into two
Expand Down Expand Up @@ -107,11 +105,10 @@ pub fn find_exported_symbols(tcx: TyCtxt) -> NodeSet {
node: hir::ImplItemKind::Method(..), .. }) => {
let def_id = tcx.hir.local_def_id(id);
let generics = tcx.generics_of(def_id);
let attributes = tcx.get_attrs(def_id);
(generics.parent_types == 0 && generics.types.is_empty()) &&
// Functions marked with #[inline] are only ever translated
// with "internal" linkage and are never exported.
!attr::requests_inline(&attributes)
!common::requests_inline(tcx, &Instance::mono(tcx, def_id))
}

_ => false
Expand Down
22 changes: 22 additions & 0 deletions src/test/codegen-units/item-collection/unreferenced-const-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-tidy-linelength
// compile-flags:-Zprint-trans-items=lazy

// NB: We do not expect *any* translation item to be generated here.

#![feature(const_fn)]
#![deny(dead_code)]
#![crate_type = "rlib"]

pub const fn foo(x: u32) -> u32 {
x + 0xf00
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-tidy-linelength
// compile-flags:-Zprint-trans-items=lazy

// NB: We do not expect *any* translation item to be generated here.

#![deny(dead_code)]
#![crate_type = "rlib"]

#[inline]
pub fn foo() -> bool {
[1, 2] == [3, 4]
}

12 changes: 6 additions & 6 deletions src/test/codegen-units/partitioning/extern-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// compile-flags:-Zinline-in-all-cgus

#![allow(dead_code)]
#![crate_type="lib"]
#![crate_type="rlib"]

// aux-build:cgu_extern_drop_glue.rs
extern crate cgu_extern_drop_glue;
Expand All @@ -25,20 +25,20 @@ extern crate cgu_extern_drop_glue;

struct LocalStruct(cgu_extern_drop_glue::Struct);

//~ TRANS_ITEM fn extern_drop_glue::user[0] @@ extern_drop_glue[Internal]
fn user()
//~ TRANS_ITEM fn extern_drop_glue::user[0] @@ extern_drop_glue[External]
pub fn user()
{
//~ TRANS_ITEM fn core::ptr[0]::drop_in_place[0]<extern_drop_glue::LocalStruct[0]> @@ extern_drop_glue[Internal]
let _ = LocalStruct(cgu_extern_drop_glue::Struct(0));
}

mod mod1 {
pub mod mod1 {
use cgu_extern_drop_glue;

struct LocalStruct(cgu_extern_drop_glue::Struct);

//~ TRANS_ITEM fn extern_drop_glue::mod1[0]::user[0] @@ extern_drop_glue-mod1[Internal]
fn user()
//~ TRANS_ITEM fn extern_drop_glue::mod1[0]::user[0] @@ extern_drop_glue-mod1[External]
pub fn user()
{
//~ TRANS_ITEM fn core::ptr[0]::drop_in_place[0]<extern_drop_glue::mod1[0]::LocalStruct[0]> @@ extern_drop_glue-mod1[Internal]
let _ = LocalStruct(cgu_extern_drop_glue::Struct(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub fn user()
cgu_explicit_inlining::never_inlined();
}

mod mod1 {
pub mod mod1 {
use cgu_explicit_inlining;

//~ TRANS_ITEM fn inlining_from_extern_crate::mod1[0]::user[0] @@ inlining_from_extern_crate-mod1[Internal]
//~ TRANS_ITEM fn inlining_from_extern_crate::mod1[0]::user[0] @@ inlining_from_extern_crate-mod1[External]
pub fn user()
{
cgu_explicit_inlining::inlined();
Expand All @@ -48,10 +48,10 @@ mod mod1 {
}
}

mod mod2 {
pub mod mod2 {
use cgu_explicit_inlining;

//~ TRANS_ITEM fn inlining_from_extern_crate::mod2[0]::user[0] @@ inlining_from_extern_crate-mod2[Internal]
//~ TRANS_ITEM fn inlining_from_extern_crate::mod2[0]::user[0] @@ inlining_from_extern_crate-mod2[External]
pub fn user()
{
cgu_explicit_inlining::always_inlined();
Expand Down
12 changes: 6 additions & 6 deletions src/test/codegen-units/partitioning/local-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// compile-flags:-Zinline-in-all-cgus

#![allow(dead_code)]
#![crate_type="lib"]
#![crate_type="rlib"]

//~ TRANS_ITEM fn core::ptr[0]::drop_in_place[0]<local_drop_glue::Struct[0]> @@ local_drop_glue[Internal] local_drop_glue-mod1[Internal]
struct Struct {
Expand All @@ -32,8 +32,8 @@ struct Outer {
_a: Struct
}

//~ TRANS_ITEM fn local_drop_glue::user[0] @@ local_drop_glue[Internal]
fn user()
//~ TRANS_ITEM fn local_drop_glue::user[0] @@ local_drop_glue[External]
pub fn user()
{
let _ = Outer {
_a: Struct {
Expand All @@ -42,7 +42,7 @@ fn user()
};
}

mod mod1
pub mod mod1
{
use super::Struct;

Expand All @@ -53,8 +53,8 @@ mod mod1
_b: (u32, Struct),
}

//~ TRANS_ITEM fn local_drop_glue::mod1[0]::user[0] @@ local_drop_glue-mod1[Internal]
fn user()
//~ TRANS_ITEM fn local_drop_glue::mod1[0]::user[0] @@ local_drop_glue-mod1[External]
pub fn user()
{
let _ = Struct2 {
_a: Struct { _a: 0 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ mod inline {
}
}

mod user1 {
pub mod user1 {
use super::inline;

//~ TRANS_ITEM fn local_inlining_but_not_all::user1[0]::foo[0] @@ local_inlining_but_not_all-user1[Internal]
fn foo() {
//~ TRANS_ITEM fn local_inlining_but_not_all::user1[0]::foo[0] @@ local_inlining_but_not_all-user1[External]
pub fn foo() {
inline::inlined_function();
}
}

mod user2 {
pub mod user2 {
use super::inline;

//~ TRANS_ITEM fn local_inlining_but_not_all::user2[0]::bar[0] @@ local_inlining_but_not_all-user2[Internal]
fn bar() {
//~ TRANS_ITEM fn local_inlining_but_not_all::user2[0]::bar[0] @@ local_inlining_but_not_all-user2[External]
pub fn bar() {
inline::inlined_function();
}
}

mod non_user {
pub mod non_user {

//~ TRANS_ITEM fn local_inlining_but_not_all::non_user[0]::baz[0] @@ local_inlining_but_not_all-non_user[Internal]
fn baz() {
//~ TRANS_ITEM fn local_inlining_but_not_all::non_user[0]::baz[0] @@ local_inlining_but_not_all-non_user[External]
pub fn baz() {

}
}
18 changes: 9 additions & 9 deletions src/test/codegen-units/partitioning/local-inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ mod inline {
}
}

mod user1 {
pub mod user1 {
use super::inline;

//~ TRANS_ITEM fn local_inlining::user1[0]::foo[0] @@ local_inlining-user1[Internal]
fn foo() {
//~ TRANS_ITEM fn local_inlining::user1[0]::foo[0] @@ local_inlining-user1[External]
pub fn foo() {
inline::inlined_function();
}
}

mod user2 {
pub mod user2 {
use super::inline;

//~ TRANS_ITEM fn local_inlining::user2[0]::bar[0] @@ local_inlining-user2[Internal]
fn bar() {
//~ TRANS_ITEM fn local_inlining::user2[0]::bar[0] @@ local_inlining-user2[External]
pub fn bar() {
inline::inlined_function();
}
}

mod non_user {
pub mod non_user {

//~ TRANS_ITEM fn local_inlining::non_user[0]::baz[0] @@ local_inlining-non_user[Internal]
fn baz() {
//~ TRANS_ITEM fn local_inlining::non_user[0]::baz[0] @@ local_inlining-non_user[External]
pub fn baz() {

}
}
14 changes: 7 additions & 7 deletions src/test/codegen-units/partitioning/local-transitive-inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// compile-flags:-Zinline-in-all-cgus

#![allow(dead_code)]
#![crate_type="lib"]
#![crate_type="rlib"]

mod inline {

Expand All @@ -37,19 +37,19 @@ mod direct_user {
}
}

mod indirect_user {
pub mod indirect_user {
use super::direct_user;

//~ TRANS_ITEM fn local_transitive_inlining::indirect_user[0]::bar[0] @@ local_transitive_inlining-indirect_user[Internal]
fn bar() {
//~ TRANS_ITEM fn local_transitive_inlining::indirect_user[0]::bar[0] @@ local_transitive_inlining-indirect_user[External]
pub fn bar() {
direct_user::foo();
}
}

mod non_user {
pub mod non_user {

//~ TRANS_ITEM fn local_transitive_inlining::non_user[0]::baz[0] @@ local_transitive_inlining-non_user[Internal]
fn baz() {
//~ TRANS_ITEM fn local_transitive_inlining::non_user[0]::baz[0] @@ local_transitive_inlining-non_user[External]
pub fn baz() {

}
}
Loading