Skip to content

Commit d48d610

Browse files
authored
cranelift: Rewrite operands with allocations in-place (bytecodealliance#8524)
* cranelift: Rewrite operands with allocations in-place This makes all other code that uses AllocationConsumer into no-ops because the list of allocations in all other places are now always empty. There is a bunch of dead code still left over after this. This part is just the stuff I can delete without changing function signatures everywhere. The various functions which are now no-ops are called in a lot of places and cleaning those up will take more careful review. * Make OperandVisitor implementable with a closure Instead of extending AllocationConsumer to update operands in place, introduce a new type and delete the rest of the internals of AllocationConsumer. This involves deleting the lifetime annotations from all uses of AllocationConsumer. I could have kept a `PhantomData` or something around but didn't feel like bothering. * Debug-assert that AllocationConsumer has no allocations to consume This is what justifies deleting all this code, so let's go ahead and assert it until the code is actually gone.
1 parent 24c1388 commit d48d610

File tree

19 files changed

+123
-843
lines changed

19 files changed

+123
-843
lines changed

cranelift/codegen/src/isa/aarch64/inst/args.rs

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -146,63 +146,16 @@ impl AMode {
146146
}
147147
}
148148

149-
pub(crate) fn with_allocs(&self, allocs: &mut AllocationConsumer<'_>) -> Self {
150-
// This should match `memarg_operands()`.
151-
match self {
152-
&AMode::Unscaled { rn, simm9 } => AMode::Unscaled {
153-
rn: allocs.next(rn),
154-
simm9,
155-
},
156-
&AMode::UnsignedOffset { rn, uimm12 } => AMode::UnsignedOffset {
157-
rn: allocs.next(rn),
158-
uimm12,
159-
},
160-
&AMode::RegReg { rn, rm } => AMode::RegReg {
161-
rn: allocs.next(rn),
162-
rm: allocs.next(rm),
163-
},
164-
&AMode::RegScaled { rn, rm } => AMode::RegScaled {
165-
rn: allocs.next(rn),
166-
rm: allocs.next(rm),
167-
},
168-
&AMode::RegScaledExtended { rn, rm, extendop } => AMode::RegScaledExtended {
169-
rn: allocs.next(rn),
170-
rm: allocs.next(rm),
171-
extendop,
172-
},
173-
&AMode::RegExtended { rn, rm, extendop } => AMode::RegExtended {
174-
rn: allocs.next(rn),
175-
rm: allocs.next(rm),
176-
extendop,
177-
},
178-
&AMode::RegOffset { rn, off } => AMode::RegOffset {
179-
rn: allocs.next(rn),
180-
off,
181-
},
182-
&AMode::SPPreIndexed { .. }
183-
| &AMode::SPPostIndexed { .. }
184-
| &AMode::FPOffset { .. }
185-
| &AMode::SPOffset { .. }
186-
| &AMode::IncomingArg { .. }
187-
| &AMode::NominalSPOffset { .. }
188-
| &AMode::Const { .. }
189-
| AMode::Label { .. } => self.clone(),
190-
}
149+
pub(crate) fn with_allocs(&self, _allocs: &mut AllocationConsumer) -> Self {
150+
self.clone()
191151
}
192152
}
193153

194154
pub use crate::isa::aarch64::lower::isle::generated_code::PairAMode;
195155

196156
impl PairAMode {
197-
pub(crate) fn with_allocs(&self, allocs: &mut AllocationConsumer<'_>) -> Self {
198-
// Should match `pairmemarg_operands()`.
199-
match self {
200-
&PairAMode::SignedOffset { reg, simm7 } => PairAMode::SignedOffset {
201-
reg: allocs.next(reg),
202-
simm7,
203-
},
204-
&PairAMode::SPPreIndexed { .. } | &PairAMode::SPPostIndexed { .. } => self.clone(),
205-
}
157+
pub(crate) fn with_allocs(&self, _allocs: &mut AllocationConsumer) -> Self {
158+
self.clone()
206159
}
207160
}
208161

@@ -357,19 +310,19 @@ impl BranchTarget {
357310
}
358311

359312
impl PrettyPrint for ShiftOpAndAmt {
360-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
313+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
361314
format!("{:?} {}", self.op(), self.amt().value())
362315
}
363316
}
364317

365318
impl PrettyPrint for ExtendOp {
366-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
319+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
367320
format!("{:?}", self)
368321
}
369322
}
370323

371324
impl PrettyPrint for MemLabel {
372-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
325+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
373326
match self {
374327
MemLabel::PCRel(off) => format!("pc+{}", off),
375328
MemLabel::Mach(off) => format!("label({})", off.get()),
@@ -389,7 +342,7 @@ fn shift_for_type(size_bytes: u8) -> usize {
389342
}
390343

391344
impl PrettyPrint for AMode {
392-
fn pretty_print(&self, size_bytes: u8, allocs: &mut AllocationConsumer<'_>) -> String {
345+
fn pretty_print(&self, size_bytes: u8, allocs: &mut AllocationConsumer) -> String {
393346
debug_assert!(size_bytes != 0);
394347
match self {
395348
&AMode::Unscaled { rn, simm9 } => {
@@ -466,7 +419,7 @@ impl PrettyPrint for AMode {
466419
}
467420

468421
impl PrettyPrint for PairAMode {
469-
fn pretty_print(&self, _: u8, allocs: &mut AllocationConsumer<'_>) -> String {
422+
fn pretty_print(&self, _: u8, allocs: &mut AllocationConsumer) -> String {
470423
match self {
471424
&PairAMode::SignedOffset { reg, simm7 } => {
472425
let reg = pretty_print_reg(reg, allocs);
@@ -490,15 +443,15 @@ impl PrettyPrint for PairAMode {
490443
}
491444

492445
impl PrettyPrint for Cond {
493-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
446+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
494447
let mut s = format!("{:?}", self);
495448
s.make_ascii_lowercase();
496449
s
497450
}
498451
}
499452

500453
impl PrettyPrint for BranchTarget {
501-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
454+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
502455
match self {
503456
&BranchTarget::Label(label) => format!("label{:?}", label.get()),
504457
&BranchTarget::ResolvedOffset(off) => format!("{}", off),

cranelift/codegen/src/isa/aarch64/inst/emit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn enc_cbr(op_31_24: u32, off_18_0: u32, op_4: u32, cond: u32) -> u32 {
168168
fn enc_conditional_br(
169169
taken: BranchTarget,
170170
kind: CondBrKind,
171-
allocs: &mut AllocationConsumer<'_>,
171+
allocs: &mut AllocationConsumer,
172172
) -> u32 {
173173
match kind {
174174
CondBrKind::Zero(reg) => {
@@ -3793,7 +3793,7 @@ impl MachInstEmit for Inst {
37933793
}
37943794

37953795
fn emit_return_call_common_sequence(
3796-
allocs: &mut AllocationConsumer<'_>,
3796+
allocs: &mut AllocationConsumer,
37973797
sink: &mut MachBuffer<Inst>,
37983798
emit_info: &EmitInfo,
37993799
state: &mut EmitState,

cranelift/codegen/src/isa/aarch64/inst/imms.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ impl ASIMDFPModImm {
827827
}
828828

829829
impl PrettyPrint for NZCV {
830-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
830+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
831831
let fmt = |c: char, v| if v { c.to_ascii_uppercase() } else { c };
832832
format!(
833833
"#{}{}{}{}",
@@ -840,63 +840,63 @@ impl PrettyPrint for NZCV {
840840
}
841841

842842
impl PrettyPrint for UImm5 {
843-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
843+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
844844
format!("#{}", self.value)
845845
}
846846
}
847847

848848
impl PrettyPrint for Imm12 {
849-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
849+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
850850
let shift = if self.shift12 { 12 } else { 0 };
851851
let value = u32::from(self.bits) << shift;
852852
format!("#{}", value)
853853
}
854854
}
855855

856856
impl PrettyPrint for SImm7Scaled {
857-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
857+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
858858
format!("#{}", self.value)
859859
}
860860
}
861861

862862
impl PrettyPrint for FPULeftShiftImm {
863-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
863+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
864864
format!("#{}", self.amount)
865865
}
866866
}
867867

868868
impl PrettyPrint for FPURightShiftImm {
869-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
869+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
870870
format!("#{}", self.amount)
871871
}
872872
}
873873

874874
impl PrettyPrint for SImm9 {
875-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
875+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
876876
format!("#{}", self.value)
877877
}
878878
}
879879

880880
impl PrettyPrint for UImm12Scaled {
881-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
881+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
882882
format!("#{}", self.value)
883883
}
884884
}
885885

886886
impl PrettyPrint for ImmLogic {
887-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
887+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
888888
format!("#{}", self.value())
889889
}
890890
}
891891

892892
impl PrettyPrint for ImmShift {
893-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
893+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
894894
format!("#{}", self.imm)
895895
}
896896
}
897897

898898
impl PrettyPrint for MoveWideConst {
899-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
899+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
900900
if self.shift == 0 {
901901
format!("#{}", self.bits)
902902
} else {
@@ -906,7 +906,7 @@ impl PrettyPrint for MoveWideConst {
906906
}
907907

908908
impl PrettyPrint for ASIMDMovModImm {
909-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
909+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
910910
if self.is_64bit {
911911
debug_assert_eq!(self.shift, 0);
912912

@@ -930,7 +930,7 @@ impl PrettyPrint for ASIMDMovModImm {
930930
}
931931

932932
impl PrettyPrint for ASIMDFPModImm {
933-
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer<'_>) -> String {
933+
fn pretty_print(&self, _: u8, _: &mut AllocationConsumer) -> String {
934934
if self.is_64bit {
935935
format!("#{}", f64::from_bits(Self::value64(self.imm)))
936936
} else {

cranelift/codegen/src/isa/aarch64/inst/regs.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn show_reg(reg: Reg) -> String {
176176
}
177177
}
178178

179-
pub fn pretty_print_reg(reg: Reg, allocs: &mut AllocationConsumer<'_>) -> String {
179+
pub fn pretty_print_reg(reg: Reg, allocs: &mut AllocationConsumer) -> String {
180180
let reg = allocs.next(reg);
181181
show_reg(reg)
182182
}
@@ -254,19 +254,15 @@ pub fn show_vreg_element(reg: Reg, idx: u8, size: ScalarSize) -> String {
254254
format!("{}{}[{}]", s, suffix, idx)
255255
}
256256

257-
pub fn pretty_print_ireg(
258-
reg: Reg,
259-
size: OperandSize,
260-
allocs: &mut AllocationConsumer<'_>,
261-
) -> String {
257+
pub fn pretty_print_ireg(reg: Reg, size: OperandSize, allocs: &mut AllocationConsumer) -> String {
262258
let reg = allocs.next(reg);
263259
show_ireg_sized(reg, size)
264260
}
265261

266262
pub fn pretty_print_vreg_scalar(
267263
reg: Reg,
268264
size: ScalarSize,
269-
allocs: &mut AllocationConsumer<'_>,
265+
allocs: &mut AllocationConsumer,
270266
) -> String {
271267
let reg = allocs.next(reg);
272268
show_vreg_scalar(reg, size)
@@ -275,7 +271,7 @@ pub fn pretty_print_vreg_scalar(
275271
pub fn pretty_print_vreg_vector(
276272
reg: Reg,
277273
size: VectorSize,
278-
allocs: &mut AllocationConsumer<'_>,
274+
allocs: &mut AllocationConsumer,
279275
) -> String {
280276
let reg = allocs.next(reg);
281277
show_vreg_vector(reg, size)
@@ -285,7 +281,7 @@ pub fn pretty_print_vreg_element(
285281
reg: Reg,
286282
idx: usize,
287283
size: ScalarSize,
288-
allocs: &mut AllocationConsumer<'_>,
284+
allocs: &mut AllocationConsumer,
289285
) -> String {
290286
let reg = allocs.next(reg);
291287
show_vreg_element(reg, idx as u8, size)

cranelift/codegen/src/isa/riscv64/inst/args.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,11 @@ pub enum AMode {
113113
}
114114

115115
impl AMode {
116-
pub(crate) fn with_allocs(self, allocs: &mut AllocationConsumer<'_>) -> Self {
117-
match self {
118-
AMode::RegOffset(reg, offset) => AMode::RegOffset(allocs.next(reg), offset),
119-
AMode::SPOffset(..)
120-
| AMode::FPOffset(..)
121-
| AMode::NominalSPOffset(..)
122-
| AMode::IncomingArg(..)
123-
| AMode::Const(..)
124-
| AMode::Label(..) => self,
125-
}
116+
pub(crate) fn with_allocs(self, _allocs: &mut AllocationConsumer) -> Self {
117+
self
126118
}
127119

128120
/// Add the registers referenced by this AMode to `collector`.
129-
/// Keep this in sync with `with_allocs`.
130121
pub(crate) fn get_operands(&mut self, collector: &mut impl OperandVisitor) {
131122
match self {
132123
AMode::RegOffset(reg, ..) => collector.reg_use(reg),
@@ -186,7 +177,7 @@ impl AMode {
186177
}
187178
}
188179

189-
pub(crate) fn to_string_with_alloc(&self, allocs: &mut AllocationConsumer<'_>) -> String {
180+
pub(crate) fn to_string_with_alloc(&self, allocs: &mut AllocationConsumer) -> String {
190181
format!("{}", self.clone().with_allocs(allocs))
191182
}
192183
}

0 commit comments

Comments
 (0)