Skip to content

Commit f97f65f

Browse files
committed
avoid redundant translation of items during monomorphization
1 parent 3ab170f commit f97f65f

File tree

13 files changed

+81
-21
lines changed

13 files changed

+81
-21
lines changed

src/librustc/middle/trans/base.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,8 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
12351235
output_type: ty::t,
12361236
param_substs: &'a param_substs,
12371237
sp: Option<Span>,
1238-
block_arena: &'a TypedArena<Block<'a>>)
1238+
block_arena: &'a TypedArena<Block<'a>>,
1239+
handle_items: HandleItemsFlag)
12391240
-> FunctionContext<'a> {
12401241
param_substs.validate();
12411242

@@ -1268,7 +1269,8 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
12681269
block_arena: block_arena,
12691270
ccx: ccx,
12701271
debug_context: debug_context,
1271-
scopes: RefCell::new(Vec::new())
1272+
scopes: RefCell::new(Vec::new()),
1273+
handle_items: handle_items,
12721274
};
12731275

12741276
if has_env {
@@ -1579,7 +1581,8 @@ pub fn trans_closure(ccx: &CrateContext,
15791581
abi: Abi,
15801582
has_env: bool,
15811583
is_unboxed_closure: IsUnboxedClosureFlag,
1582-
maybe_load_env: <'a> |&'a Block<'a>| -> &'a Block<'a>) {
1584+
maybe_load_env: <'a> |&'a Block<'a>| -> &'a Block<'a>,
1585+
handle_items: HandleItemsFlag) {
15831586
ccx.stats.n_closures.set(ccx.stats.n_closures.get() + 1);
15841587

15851588
let _icx = push_ctxt("trans_closure");
@@ -1596,7 +1599,8 @@ pub fn trans_closure(ccx: &CrateContext,
15961599
output_type,
15971600
param_substs,
15981601
Some(body.span),
1599-
&arena);
1602+
&arena,
1603+
handle_items);
16001604
let mut bcx = init_function(&fcx, false, output_type);
16011605

16021606
// cleanup scope for the incoming arguments
@@ -1698,7 +1702,8 @@ pub fn trans_fn(ccx: &CrateContext,
16981702
llfndecl: ValueRef,
16991703
param_substs: &param_substs,
17001704
id: ast::NodeId,
1701-
attrs: &[ast::Attribute]) {
1705+
attrs: &[ast::Attribute],
1706+
handle_items: HandleItemsFlag) {
17021707
let _s = StatRecorder::new(ccx, ccx.tcx.map.path_to_string(id).to_string());
17031708
debug!("trans_fn(param_substs={})", param_substs.repr(ccx.tcx()));
17041709
let _icx = push_ctxt("trans_fn");
@@ -1718,7 +1723,8 @@ pub fn trans_fn(ccx: &CrateContext,
17181723
abi,
17191724
false,
17201725
NotUnboxedClosure,
1721-
|bcx| bcx);
1726+
|bcx| bcx,
1727+
handle_items);
17221728
}
17231729

17241730
pub fn trans_enum_variant(ccx: &CrateContext,
@@ -1824,7 +1830,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
18241830

18251831
let arena = TypedArena::new();
18261832
let fcx = new_fn_ctxt(ccx, llfndecl, ctor_id, false, result_ty,
1827-
param_substs, None, &arena);
1833+
param_substs, None, &arena, TranslateItems);
18281834
let bcx = init_function(&fcx, false, result_ty);
18291835

18301836
let arg_tys = ty::ty_fn_args(ctor_ty);
@@ -1925,7 +1931,8 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
19251931
llfn,
19261932
&param_substs::empty(),
19271933
item.id,
1928-
item.attrs.as_slice());
1934+
item.attrs.as_slice(),
1935+
TranslateItems);
19291936
} else {
19301937
// Be sure to travel more than just one layer deep to catch nested
19311938
// items in blocks and such.

src/librustc/middle/trans/callee.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ pub fn trans_unboxing_shim(bcx: &Block,
339339
return_type,
340340
&empty_param_substs,
341341
None,
342-
&block_arena);
342+
&block_arena,
343+
TranslateItems);
343344
let mut bcx = init_function(&fcx, false, return_type);
344345

345346
// Create the substituted versions of the self type.

src/librustc/middle/trans/closure.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ pub fn trans_expr_fn<'a>(
394394
ty::ty_fn_abi(fty),
395395
true,
396396
NotUnboxedClosure,
397-
|bcx| load_environment(bcx, cdata_ty, &freevars, store));
397+
|bcx| load_environment(bcx, cdata_ty, &freevars, store),
398+
bcx.fcx.handle_items);
398399
fill_fn_pair(bcx, dest_addr, llfn, llbox);
399400
bcx
400401
}
@@ -486,7 +487,8 @@ pub fn trans_unboxed_closure<'a>(
486487
ty::ty_fn_abi(function_type),
487488
true,
488489
IsUnboxedClosure,
489-
|bcx| load_unboxed_closure_environment(bcx, freevars_ptr));
490+
|bcx| load_unboxed_closure_environment(bcx, freevars_ptr),
491+
bcx.fcx.handle_items);
490492

491493
// Don't hoist this to the top of the function. It's perfectly legitimate
492494
// to have a zero-size unboxed closure (in which case dest will be
@@ -573,7 +575,7 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
573575
let arena = TypedArena::new();
574576
let empty_param_substs = param_substs::empty();
575577
let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output,
576-
&empty_param_substs, None, &arena);
578+
&empty_param_substs, None, &arena, TranslateItems);
577579
let bcx = init_function(&fcx, true, f.sig.output);
578580

579581
let args = create_datums_for_fn_args(&fcx,

src/librustc/middle/trans/common.rs

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ impl<T:Subst+Clone> SubstP for T {
221221
pub type RvalueDatum = datum::Datum<datum::Rvalue>;
222222
pub type LvalueDatum = datum::Datum<datum::Lvalue>;
223223

224+
#[deriving(Clone, Eq, PartialEq)]
225+
pub enum HandleItemsFlag {
226+
IgnoreItems,
227+
TranslateItems,
228+
}
229+
224230
// Function context. Every LLVM function we create will have one of
225231
// these.
226232
pub struct FunctionContext<'a> {
@@ -289,6 +295,9 @@ pub struct FunctionContext<'a> {
289295

290296
// Cleanup scopes.
291297
pub scopes: RefCell<Vec<cleanup::CleanupScope<'a>> >,
298+
299+
// How to handle items encountered during translation of this function.
300+
pub handle_items: HandleItemsFlag,
292301
}
293302

294303
impl<'a> FunctionContext<'a> {

src/librustc/middle/trans/controlflow.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ pub fn trans_stmt<'a>(cx: &'a Block<'a>,
6868
debuginfo::create_local_var_metadata(bcx, &**local);
6969
}
7070
}
71-
ast::DeclItem(ref i) => trans_item(cx.fcx.ccx, &**i)
71+
ast::DeclItem(ref i) => {
72+
match fcx.handle_items {
73+
TranslateItems => trans_item(cx.fcx.ccx, &**i),
74+
IgnoreItems => {}
75+
}
76+
}
7277
}
7378
}
7479
ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")

src/librustc/middle/trans/foreign.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
601601

602602
let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice());
603603
base::set_llvm_fn_attrs(attrs, llfn);
604-
base::trans_fn(ccx, decl, body, llfn, &param_substs::empty(), id, []);
604+
base::trans_fn(ccx, decl, body, llfn, &param_substs::empty(), id, [],
605+
TranslateItems);
605606
llfn
606607
}
607608

src/librustc/middle/trans/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ fn make_generic_glue(ccx: &CrateContext,
468468
let arena = TypedArena::new();
469469
let empty_param_substs = param_substs::empty();
470470
let fcx = new_fn_ctxt(ccx, llfn, -1, false, ty::mk_nil(),
471-
&empty_param_substs, None, &arena);
471+
&empty_param_substs, None, &arena, TranslateItems);
472472

473473
let bcx = init_function(&fcx, false, ty::mk_nil());
474474

src/librustc/middle/trans/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
133133
if unparameterized {
134134
let llfn = get_item_val(ccx, mth.id);
135135
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), llfn,
136-
&param_substs::empty(), mth.id, []);
136+
&param_substs::empty(), mth.id, [], TranslateItems);
137137
}
138138
local_def(mth.id)
139139
}

src/librustc/middle/trans/meth.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ pub fn trans_impl(ccx: &CrateContext,
7575
llfn,
7676
&param_substs::empty(),
7777
method.id,
78-
[]);
78+
[],
79+
TranslateItems);
7980
} else {
8081
let mut v = TransItemVisitor{ ccx: ccx };
8182
visit::walk_method_helper(&mut v, &**method, ());

src/librustc/middle/trans/monomorphize.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ pub fn monomorphic_fn(ccx: &CrateContext,
149149
} => {
150150
let d = mk_lldecl();
151151
set_llvm_fn_attrs(i.attrs.as_slice(), d);
152-
trans_fn(ccx, &**decl, &**body, d, &psubsts, fn_id.node, []);
152+
trans_fn(ccx, &**decl, &**body, d, &psubsts, fn_id.node, [],
153+
IgnoreItems);
153154
d
154155
}
155156
_ => {
@@ -181,7 +182,8 @@ pub fn monomorphic_fn(ccx: &CrateContext,
181182
ast_map::NodeMethod(mth) => {
182183
let d = mk_lldecl();
183184
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
184-
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d, &psubsts, mth.id, []);
185+
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d, &psubsts, mth.id, [],
186+
IgnoreItems);
185187
d
186188
}
187189
ast_map::NodeTraitMethod(method) => {
@@ -190,7 +192,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
190192
let d = mk_lldecl();
191193
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
192194
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d,
193-
&psubsts, mth.id, []);
195+
&psubsts, mth.id, [], IgnoreItems);
194196
d
195197
}
196198
_ => {

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'a, 'b> Reflector<'a, 'b> {
312312
let empty_param_substs = param_substs::empty();
313313
let fcx = new_fn_ctxt(ccx, llfdecl, -1, false,
314314
ty::mk_u64(), &empty_param_substs,
315-
None, &arena);
315+
None, &arena, TranslateItems);
316316
let bcx = init_function(&fcx, false, ty::mk_u64());
317317

318318
// we know the return type of llfdecl is an int here, so

src/test/run-make/issue-7349/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-include ../tools.mk
2+
3+
# Test to make sure that inner functions within a polymorphic outer function
4+
# don't get re-translated when the outer function is monomorphized. The test
5+
# code monomorphizes the outer function several times, but the magic constant
6+
# `8675309` used in the inner function should appear only once in the generated
7+
# IR.
8+
9+
all:
10+
$(RUSTC) foo.rs --emit=ir
11+
[ "$$(grep -c 8675309 "$(TMPDIR)/foo.ll")" -eq "1" ]

src/test/run-make/issue-7349/foo.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 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+
fn outer<T>() {
12+
#[allow(dead_code)]
13+
fn inner() -> uint {
14+
8675309
15+
}
16+
}
17+
18+
fn main() {
19+
outer::<int>();
20+
outer::<uint>();
21+
}

0 commit comments

Comments
 (0)