Skip to content

Commit dacd33b

Browse files
authored
winch: Simplify constant handling, part 2/N (#10989)
* winch: Introduce register allocation for scratch registers This commit introduces a register allocator for scratch registers. The objective of this change is to make it generally safer to work with scratch registers and prevent accidental clobbering of said registers. This approach also has the advantage that allows for a more natural abstraction over ISA-dependent scratch register definitions, e.g., we can easily encode that fact that in aarch64 x16 and x17 are considered scratch registers, while in x64 Winch's ABI defines a single global scratch register. * winch: Hook up the scratch allocator in each of the backends This commit makes use of the scratch register allocator in both the x64 and aarch64 backends for immediate value loading. Given that the MacroAssembler is the boundary between ISA-agnostic code and ISA-dependent code, it seems to be the natural location for this allocator to live. The allocator gives exclusive access to a scratch register of a particular class, through the `Masm::with_scratch` method. Note that the semantics of this allocator don't involve spilling or any other form of register availability resolution. If a register is requested and it's not available, this method will panic. * Apply `cargo fmt` * Update disassembly tests Even though the entire change doens't contain major funcitonal changes, a side effect of improving the constant handling in aarch64 is that we perform better instruction selection for instructions that deal with immediattes, improving the generated code in some cases. * Review edits
1 parent a80dd80 commit dacd33b

File tree

18 files changed

+1526
-1090
lines changed

18 files changed

+1526
-1090
lines changed

tests/disas/winch/aarch64/br_table/large.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@
757757
;; stur x1, [x28, #8]
758758
;; stur w2, [x28, #4]
759759
;; ldur w0, [x28, #4]
760-
;; mov x16, #0x6027
761-
;; cmp x0, x16, uxtx
760+
;; mov x1, #0x6027
761+
;; cmp w0, w1, uxtx
762762
;; b.hs #0x18110
763763
;; 54: csel x1, xzr, x0, hs
764764
;; csdb

tests/disas/winch/aarch64/br_table/nested_br_table_loop_block.wat

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,45 @@
2929
;; movk x17, #0x18
3030
;; add x16, x16, x17
3131
;; cmp sp, x16
32-
;; b.lo #0xc4
32+
;; b.lo #0xcc
3333
;; 2c: mov x9, x0
3434
;; sub x28, x28, #0x18
3535
;; mov sp, x28
3636
;; stur x0, [x28, #0x10]
3737
;; stur x1, [x28, #8]
3838
;; stur w2, [x28, #4]
3939
;; ldur w0, [x28, #4]
40-
;; cmp x0, #2
41-
;; b.hs #0x74
42-
;; 50: csel x1, xzr, x0, hs
40+
;; mov x1, #2
41+
;; cmp w0, w1, uxtx
42+
;; b.hs #0x78
43+
;; 54: csel x1, xzr, x0, hs
4344
;; csdb
44-
;; adr x16, #0x68
45+
;; adr x16, #0x6c
4546
;; ldrsw x1, [x16, w1, uxtw #2]
4647
;; add x16, x16, x1
4748
;; br x16
48-
;; 68: .byte 0xdc, 0xff, 0xff, 0xff
49+
;; 6c: .byte 0xd8, 0xff, 0xff, 0xff
4950
;; .byte 0x0c, 0x00, 0x00, 0x00
5051
;; b #0x44
51-
;; 74: mov x0, #0
52+
;; 78: mov x0, #0
5253
;; stur w0, [x28, #4]
5354
;; ldur w0, [x28, #4]
54-
;; cmp x0, #2
55-
;; b.hs #0x7c
56-
;; 88: csel x1, xzr, x0, hs
55+
;; mov x1, #2
56+
;; cmp w0, w1, uxtx
57+
;; b.hs #0x80
58+
;; 90: csel x1, xzr, x0, hs
5759
;; csdb
58-
;; adr x16, #0xa0
60+
;; adr x16, #0xa8
5961
;; ldrsw x1, [x16, w1, uxtw #2]
6062
;; add x16, x16, x1
6163
;; br x16
62-
;; a0: .byte 0x08, 0x00, 0x00, 0x00
63-
;; .byte 0xdc, 0xff, 0xff, 0xff
64+
;; a8: .byte 0x08, 0x00, 0x00, 0x00
65+
;; .byte 0xd8, 0xff, 0xff, 0xff
6466
;; mov x0, #3
6567
;; add x28, x28, #0x18
6668
;; mov sp, x28
6769
;; mov sp, x28
6870
;; ldr x28, [sp], #0x10
6971
;; ldp x29, x30, [sp], #0x10
7072
;; ret
71-
;; c4: .byte 0x1f, 0xc1, 0x00, 0x00
73+
;; cc: .byte 0x1f, 0xc1, 0x00, 0x00

tests/disas/winch/aarch64/i32_rotl/16_const.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
;; stur x0, [x28, #8]
2727
;; stur x1, [x28]
2828
;; mov x0, #1
29-
;; neg w0, w0
3029
;; mov x16, #0x200
30+
;; neg w16, w16
3131
;; ror w0, w0, w16
3232
;; add x28, x28, #0x10
3333
;; mov sp, x28

tests/disas/winch/aarch64/i64_rotl/16_const.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
;; stur x0, [x28, #8]
2727
;; stur x1, [x28]
2828
;; mov x0, #1
29-
;; neg x0, x0
3029
;; mov x16, #0x200
30+
;; neg x16, x16
3131
;; ror x0, x0, x16
3232
;; add x28, x28, #0x10
3333
;; mov sp, x28

winch/codegen/src/codegen/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
frame::Frame,
1010
isa::reg::RegClass,
1111
masm::{
12-
ExtractLaneKind, MacroAssembler, MemMoveDirection, OperandSize, RegImm, ReplaceLaneKind,
13-
SPOffset, ShiftKind, StackSlot,
12+
ExtractLaneKind, Imm, MacroAssembler, MemMoveDirection, OperandSize, RegImm,
13+
ReplaceLaneKind, SPOffset, ShiftKind, StackSlot,
1414
},
1515
reg::{Reg, WritableReg, writable},
1616
regalloc::RegAlloc,
@@ -64,7 +64,7 @@ impl<'a> CodeGenContext<'a, Emission> {
6464
let typed_reg = self.pop_to_reg(masm, None)?;
6565
masm.shift_ir(
6666
writable!(typed_reg.reg),
67-
val as u64,
67+
Imm::i32(val),
6868
typed_reg.reg,
6969
kind,
7070
OperandSize::S32,
@@ -93,7 +93,7 @@ impl<'a> CodeGenContext<'a, Emission> {
9393
let typed_reg = self.pop_to_reg(masm, None)?;
9494
masm.shift_ir(
9595
writable!(typed_reg.reg),
96-
val as u64,
96+
Imm::i64(val),
9797
typed_reg.reg,
9898
kind,
9999
OperandSize::S64,

winch/codegen/src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ where
11021102
let pow = heap_data.memory.page_size_log2;
11031103
self.masm.shift_ir(
11041104
writable!(dst.reg),
1105-
pow as u64,
1105+
Imm::i32(pow as i32),
11061106
dst.into(),
11071107
ShiftKind::ShrU,
11081108
heap_data.index_type().try_into()?,

winch/codegen/src/isa/aarch64/abi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ impl ABI for Aarch64ABI {
122122

123123
fn scratch_for(ty: &WasmValType) -> Reg {
124124
match ty {
125+
#[expect(deprecated, reason = "Pending migration to Masm::with_scratch")]
125126
WasmValType::I32
126127
| WasmValType::I64
127128
| WasmValType::Ref(WasmRefType {
128129
heap_type: WasmHeapType::Func,
129130
..
130131
}) => regs::scratch(),
132+
#[expect(deprecated, reason = "Pending migration to Masm::with_scratch")]
131133
WasmValType::F32 | WasmValType::F64 | WasmValType::V128 => regs::float_scratch(),
132134
_ => unimplemented!(),
133135
}

0 commit comments

Comments
 (0)