Skip to content

Commit d5899d2

Browse files
committed
copy arguments
1 parent c188ab3 commit d5899d2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use rustc_middle::mir::*;
2+
use rustc_middle::ty::TyCtxt;
3+
use rustc_session::config::OptLevel;
4+
5+
pub(super) struct CopyArgs;
6+
7+
impl<'tcx> crate::MirPass<'tcx> for CopyArgs {
8+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
9+
sess.opts.optimize != OptLevel::No && sess.opts.incremental.is_none()
10+
}
11+
12+
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13+
for (_, block) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
14+
if let TerminatorKind::Call { ref mut args, .. } = block.terminator_mut().kind {
15+
for arg in args {
16+
if let Operand::Move(place) = arg.node {
17+
if place.is_indirect() {
18+
continue;
19+
}
20+
let Some(local) = place.as_local() else {
21+
continue
22+
};
23+
if 1 <= local.index() && local.index() <= body.arg_count {
24+
arg.node = Operand::Copy(place);
25+
}
26+
}
27+
}
28+
};
29+
}
30+
}
31+
32+
fn is_required(&self) -> bool {
33+
false
34+
}
35+
}

compiler/rustc_mir_transform/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ declare_passes! {
127127
// This pass is public to allow external drivers to perform MIR cleanup
128128
pub mod cleanup_post_borrowck : CleanupPostBorrowck;
129129

130+
mod copy_args : CopyArgs;
130131
mod copy_prop : CopyProp;
131132
mod coroutine : StateTransform;
132133
mod coverage : InstrumentCoverage;
@@ -725,6 +726,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
725726
&large_enums::EnumSizeOpt { discrepancy: 128 },
726727
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
727728
&add_call_guards::CriticalCallEdges,
729+
&copy_args::CopyArgs,
728730
// Cleanup for human readability, off by default.
729731
&prettify::ReorderBasicBlocks,
730732
&prettify::ReorderLocals,

0 commit comments

Comments
 (0)