Skip to content

Commit cfe7a8d

Browse files
committed
Reduce code bloat in closure
For the rust-call ABI, the last function argument is a tuple that gets untupled for the actual call. For bare functions using this ABI, the code has access to the tuple, so we need to tuple the arguments again. But closures can't actually access the tuple. Their arguments map to the elements in the tuple. So what we currently do is to tuple the arguments and then immediately untuple them again, which is pretty useless and we can just omit it.
1 parent ecf8c64 commit cfe7a8d

File tree

1 file changed

+8
-72
lines changed

1 file changed

+8
-72
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,55 +1575,6 @@ fn copy_args_to_allocas<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
15751575
bcx
15761576
}
15771577

1578-
fn copy_closure_args_to_allocas<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
1579-
arg_scope: cleanup::CustomScopeIndex,
1580-
args: &[ast::Arg],
1581-
arg_datums: Vec<RvalueDatum<'tcx>>,
1582-
monomorphized_arg_types: &[Ty<'tcx>])
1583-
-> Block<'blk, 'tcx> {
1584-
let _icx = push_ctxt("copy_closure_args_to_allocas");
1585-
let arg_scope_id = cleanup::CustomScope(arg_scope);
1586-
1587-
assert_eq!(arg_datums.len(), 1);
1588-
1589-
let arg_datum = arg_datums.into_iter().next().unwrap();
1590-
1591-
// Untuple the rest of the arguments.
1592-
let tuple_datum =
1593-
unpack_datum!(bcx,
1594-
arg_datum.to_lvalue_datum_in_scope(bcx,
1595-
"argtuple",
1596-
arg_scope_id));
1597-
let untupled_arg_types = match monomorphized_arg_types[0].sty {
1598-
ty::ty_tup(ref types) => &types[..],
1599-
_ => {
1600-
bcx.tcx().sess.span_bug(args[0].pat.span,
1601-
"first arg to `rust-call` ABI function \
1602-
wasn't a tuple?!")
1603-
}
1604-
};
1605-
for j in 0..args.len() {
1606-
let tuple_element_type = untupled_arg_types[j];
1607-
let tuple_element_datum =
1608-
tuple_datum.get_element(bcx,
1609-
tuple_element_type,
1610-
|llval| GEPi(bcx, llval, &[0, j]));
1611-
let tuple_element_datum = tuple_element_datum.to_expr_datum();
1612-
let tuple_element_datum =
1613-
unpack_datum!(bcx,
1614-
tuple_element_datum.to_rvalue_datum(bcx,
1615-
"arg"));
1616-
bcx = _match::store_arg(bcx,
1617-
&*args[j].pat,
1618-
tuple_element_datum,
1619-
arg_scope_id);
1620-
1621-
debuginfo::create_argument_metadata(bcx, &args[j]);
1622-
}
1623-
1624-
bcx
1625-
}
1626-
16271578
// Ties up the llstaticallocas -> llloadenv -> lltop edges,
16281579
// and builds the return block.
16291580
pub fn finish_fn<'blk, 'tcx>(fcx: &'blk FunctionContext<'blk, 'tcx>,
@@ -1781,33 +1732,18 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
17811732
debug!("trans_closure: function lltype: {}",
17821733
bcx.fcx.ccx.tn().val_to_string(bcx.fcx.llfn));
17831734

1784-
let arg_datums = if abi != RustCall {
1785-
create_datums_for_fn_args(&fcx,
1786-
&monomorphized_arg_types[..])
1787-
} else {
1788-
create_datums_for_fn_args_under_call_abi(
1789-
bcx,
1790-
arg_scope,
1791-
&monomorphized_arg_types[..])
1792-
};
1793-
1794-
bcx = match closure_env {
1795-
closure::ClosureEnv::NotClosure => {
1796-
copy_args_to_allocas(bcx,
1797-
arg_scope,
1798-
&decl.inputs,
1799-
arg_datums)
1735+
let arg_datums = match closure_env {
1736+
closure::ClosureEnv::NotClosure if abi == RustCall => {
1737+
create_datums_for_fn_args_under_call_abi(bcx, arg_scope, &monomorphized_arg_types[..])
18001738
}
1801-
closure::ClosureEnv::Closure(_) => {
1802-
copy_closure_args_to_allocas(
1803-
bcx,
1804-
arg_scope,
1805-
&decl.inputs,
1806-
arg_datums,
1807-
&monomorphized_arg_types[..])
1739+
_ => {
1740+
let arg_tys = untuple_arguments_if_necessary(ccx, &monomorphized_arg_types, abi);
1741+
create_datums_for_fn_args(&fcx, &arg_tys)
18081742
}
18091743
};
18101744

1745+
bcx = copy_args_to_allocas(bcx, arg_scope, &decl.inputs, arg_datums);
1746+
18111747
bcx = closure_env.load(bcx, cleanup::CustomScope(arg_scope));
18121748

18131749
// Up until here, IR instructions for this function have explicitly not been annotated with

0 commit comments

Comments
 (0)