Skip to content

Commit fe4518d

Browse files
authored
Rollup merge of #59630 - nnethercote:shrink-mir-Statement, r=pnkfelix
Shrink `mir::Statement`. The `InlineAsm` variant is extremely rare, and `mir::Statement` often contributes significantly to peak memory usage.
2 parents 6fe9bb1 + d00d639 commit fe4518d

File tree

8 files changed

+48
-47
lines changed

8 files changed

+48
-47
lines changed

src/librustc/mir/mod.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::hir::def::{CtorKind, Namespace};
66
use crate::hir::def_id::DefId;
7-
use crate::hir::{self, HirId, InlineAsm};
7+
use crate::hir::{self, HirId, InlineAsm as HirInlineAsm};
88
use crate::mir::interpret::{ConstValue, InterpError, Scalar};
99
use crate::mir::visit::MirVisitable;
1010
use rustc_apfloat::ieee::{Double, Single};
@@ -1735,7 +1735,7 @@ pub struct Statement<'tcx> {
17351735

17361736
// `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
17371737
#[cfg(target_arch = "x86_64")]
1738-
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Statement<'_>>() == 56);
1738+
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Statement<'_>>() == 48);
17391739

17401740
impl<'tcx> Statement<'tcx> {
17411741
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
@@ -1779,12 +1779,9 @@ pub enum StatementKind<'tcx> {
17791779
/// End the current live range for the storage of the local.
17801780
StorageDead(Local),
17811781

1782-
/// Executes a piece of inline Assembly.
1783-
InlineAsm {
1784-
asm: Box<InlineAsm>,
1785-
outputs: Box<[Place<'tcx>]>,
1786-
inputs: Box<[(Span, Operand<'tcx>)]>,
1787-
},
1782+
/// Executes a piece of inline Assembly. Stored in a Box to keep the size
1783+
/// of `StatementKind` low.
1784+
InlineAsm(Box<InlineAsm<'tcx>>),
17881785

17891786
/// Retag references in the given place, ensuring they got fresh tags. This is
17901787
/// part of the Stacked Borrows model. These statements are currently only interpreted
@@ -1858,6 +1855,13 @@ pub enum FakeReadCause {
18581855
ForLet,
18591856
}
18601857

1858+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
1859+
pub struct InlineAsm<'tcx> {
1860+
pub asm: HirInlineAsm,
1861+
pub outputs: Box<[Place<'tcx>]>,
1862+
pub inputs: Box<[(Span, Operand<'tcx>)]>,
1863+
}
1864+
18611865
impl<'tcx> Debug for Statement<'tcx> {
18621866
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
18631867
use self::StatementKind::*;
@@ -1880,11 +1884,8 @@ impl<'tcx> Debug for Statement<'tcx> {
18801884
ref place,
18811885
variant_index,
18821886
} => write!(fmt, "discriminant({:?}) = {:?}", place, variant_index),
1883-
InlineAsm {
1884-
ref asm,
1885-
ref outputs,
1886-
ref inputs,
1887-
} => write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs),
1887+
InlineAsm(ref asm) =>
1888+
write!(fmt, "asm!({:?} : {:?} : {:?})", asm.asm, asm.outputs, asm.inputs),
18881889
AscribeUserType(ref place, ref variance, ref c_ty) => {
18891890
write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
18901891
}
@@ -3149,13 +3150,21 @@ EnumTypeFoldableImpl! {
31493150
(StatementKind::SetDiscriminant) { place, variant_index },
31503151
(StatementKind::StorageLive)(a),
31513152
(StatementKind::StorageDead)(a),
3152-
(StatementKind::InlineAsm) { asm, outputs, inputs },
3153+
(StatementKind::InlineAsm)(a),
31533154
(StatementKind::Retag)(kind, place),
31543155
(StatementKind::AscribeUserType)(a, v, b),
31553156
(StatementKind::Nop),
31563157
}
31573158
}
31583159

3160+
BraceStructTypeFoldableImpl! {
3161+
impl<'tcx> TypeFoldable<'tcx> for InlineAsm<'tcx> {
3162+
asm,
3163+
outputs,
3164+
inputs,
3165+
}
3166+
}
3167+
31593168
EnumTypeFoldableImpl! {
31603169
impl<'tcx, T> TypeFoldable<'tcx> for ClearCrossCrate<T> {
31613170
(ClearCrossCrate::Clear),

src/librustc/mir/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,15 @@ macro_rules! make_mir_visitor {
391391
location
392392
);
393393
}
394-
StatementKind::InlineAsm { outputs, inputs, asm: _ } => {
395-
for output in & $($mutability)? outputs[..] {
394+
StatementKind::InlineAsm(asm) => {
395+
for output in & $($mutability)? asm.outputs[..] {
396396
self.visit_place(
397397
output,
398398
PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
399399
location
400400
);
401401
}
402-
for (span, input) in & $($mutability)? inputs[..] {
402+
for (span, input) in & $($mutability)? asm.inputs[..] {
403403
self.visit_span(span);
404404
self.visit_operand(input, location);
405405
}

src/librustc_codegen_ssa/mir/statement.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6868
}
6969
bx
7070
}
71-
mir::StatementKind::InlineAsm { ref asm, ref outputs, ref inputs } => {
72-
let outputs = outputs.iter().map(|output| {
71+
mir::StatementKind::InlineAsm(ref asm) => {
72+
let outputs = asm.outputs.iter().map(|output| {
7373
self.codegen_place(&mut bx, output)
7474
}).collect();
7575

76-
let input_vals = inputs.iter()
77-
.fold(Vec::with_capacity(inputs.len()), |mut acc, (span, input)| {
76+
let input_vals = asm.inputs.iter()
77+
.fold(Vec::with_capacity(asm.inputs.len()), |mut acc, (span, input)| {
7878
let op = self.codegen_operand(&mut bx, input);
7979
if let OperandValue::Immediate(_) = op.val {
8080
acc.push(op.immediate());
@@ -85,8 +85,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8585
acc
8686
});
8787

88-
if input_vals.len() == inputs.len() {
89-
let res = bx.codegen_inline_asm(asm, outputs, input_vals);
88+
if input_vals.len() == asm.inputs.len() {
89+
let res = bx.codegen_inline_asm(&asm.asm, outputs, input_vals);
9090
if !res {
9191
span_err!(bx.sess(), statement.source_info.span, E0668,
9292
"malformed inline assembly");

src/librustc_mir/borrow_check/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -525,16 +525,12 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
525525
flow_state,
526526
);
527527
}
528-
StatementKind::InlineAsm {
529-
ref asm,
530-
ref outputs,
531-
ref inputs,
532-
} => {
528+
StatementKind::InlineAsm(ref asm) => {
533529
let context = ContextKind::InlineAsm.new(location);
534-
for (o, output) in asm.outputs.iter().zip(outputs.iter()) {
530+
for (o, output) in asm.asm.outputs.iter().zip(asm.outputs.iter()) {
535531
if o.is_indirect {
536532
// FIXME(eddyb) indirect inline asm outputs should
537-
// be encoeded through MIR place derefs instead.
533+
// be encoded through MIR place derefs instead.
538534
self.access_place(
539535
context,
540536
(output, o.span),
@@ -558,7 +554,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
558554
);
559555
}
560556
}
561-
for (_, input) in inputs.iter() {
557+
for (_, input) in asm.inputs.iter() {
562558
self.consume_operand(context, (input, span), flow_state);
563559
}
564560
}

src/librustc_mir/borrow_check/nll/invalidation.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,12 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
9292
JustWrite,
9393
);
9494
}
95-
StatementKind::InlineAsm {
96-
ref asm,
97-
ref outputs,
98-
ref inputs,
99-
} => {
95+
StatementKind::InlineAsm(ref asm) => {
10096
let context = ContextKind::InlineAsm.new(location);
101-
for (o, output) in asm.outputs.iter().zip(outputs.iter()) {
97+
for (o, output) in asm.asm.outputs.iter().zip(asm.outputs.iter()) {
10298
if o.is_indirect {
10399
// FIXME(eddyb) indirect inline asm outputs should
104-
// be encoeded through MIR place derefs instead.
100+
// be encoded through MIR place derefs instead.
105101
self.access_place(
106102
context,
107103
output,
@@ -117,7 +113,7 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
117113
);
118114
}
119115
}
120-
for (_, input) in inputs.iter() {
116+
for (_, input) in asm.inputs.iter() {
121117
self.consume_operand(context, input);
122118
}
123119
}

src/librustc_mir/build/expr/stmt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
188188
block,
189189
Statement {
190190
source_info,
191-
kind: StatementKind::InlineAsm {
192-
asm: box asm.clone(),
191+
kind: StatementKind::InlineAsm(box InlineAsm {
192+
asm: asm.clone(),
193193
outputs,
194194
inputs,
195-
},
195+
}),
196196
},
197197
);
198198
this.block_context.pop();

src/librustc_mir/dataflow/impls/borrows.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ impl<'a, 'gcx, 'tcx> BitDenotation<'tcx> for Borrows<'a, 'gcx, 'tcx> {
288288
self.kill_borrows_on_place(sets, &Place::Base(PlaceBase::Local(local)));
289289
}
290290

291-
mir::StatementKind::InlineAsm { ref outputs, ref asm, .. } => {
292-
for (output, kind) in outputs.iter().zip(&asm.outputs) {
291+
mir::StatementKind::InlineAsm(ref asm) => {
292+
for (output, kind) in asm.outputs.iter().zip(&asm.asm.outputs) {
293293
if !kind.is_indirect && !kind.is_rw {
294294
self.kill_borrows_on_place(sets, output);
295295
}

src/librustc_mir/dataflow/move_paths/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
272272
StatementKind::FakeRead(_, ref place) => {
273273
self.create_move_path(place);
274274
}
275-
StatementKind::InlineAsm { ref outputs, ref inputs, ref asm } => {
276-
for (output, kind) in outputs.iter().zip(&asm.outputs) {
275+
StatementKind::InlineAsm(ref asm) => {
276+
for (output, kind) in asm.outputs.iter().zip(&asm.asm.outputs) {
277277
if !kind.is_indirect {
278278
self.gather_init(output, InitKind::Deep);
279279
}
280280
}
281-
for (_, input) in inputs.iter() {
281+
for (_, input) in asm.inputs.iter() {
282282
self.gather_operand(input);
283283
}
284284
}

0 commit comments

Comments
 (0)