Skip to content

Commit 097d3b8

Browse files
committed
Tweak neighbor spilling heuristic
We now spill only neighboring fragments that have no attached instructions at all, rather than spilling ones that contain a single def as well. The rationale is that the better split point selection should now trim closer to the def anyway, achieving the same end result as a spill of the entire range. Not spilling the defs eagerly, however, will allow defs in higher-weight regions to be kept in registers and spilled only later (once split placement is aware of weight). This did cause a minor regression with instruction defs that can be placed directly in memory, but we can revisit that later if it becomes a serious problem.
1 parent 8a246bd commit 097d3b8

File tree

9 files changed

+5363
-5350
lines changed

9 files changed

+5363
-5350
lines changed

crates/codegen/src/regalloc/assign.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,11 @@ impl<M: MachineRegalloc> RegAllocContext<'_, M> {
262262
fn spill_fragment_and_neighbors(&mut self, fragment: LiveSetFragment) {
263263
self.spill_fragment(fragment);
264264

265-
// Spill any neighbors (in both directions) that don't have any use instructions. Note that
266-
// neighbors containing just a def should be spilled as well, as we'll need to copy into the
267-
// spill anyway at some point.
265+
// Spill any neighbors (in both directions) that don't have any attached instructions.
268266

269267
let mut cursor = fragment;
270268
while let Some(prev) = self.live_set_fragments[cursor].prev_split_neighbor.expand() {
271-
if self.fragment_has_uses(prev) {
269+
if self.fragment_has_instrs(prev) {
272270
break;
273271
}
274272

@@ -282,7 +280,7 @@ impl<M: MachineRegalloc> RegAllocContext<'_, M> {
282280

283281
let mut cursor = fragment;
284282
while let Some(next) = self.live_set_fragments[cursor].next_split_neighbor.expand() {
285-
if self.fragment_has_uses(next) {
283+
if self.fragment_has_instrs(next) {
286284
break;
287285
}
288286

@@ -833,10 +831,6 @@ impl<M: MachineRegalloc> RegAllocContext<'_, M> {
833831
self.fragment_instrs(fragment).next().is_some()
834832
}
835833

836-
fn fragment_has_uses(&self, fragment: LiveSetFragment) -> bool {
837-
self.fragment_instrs(fragment).any(|instr| !instr.is_def())
838-
}
839-
840834
fn fragment_instrs(
841835
&self,
842836
fragment: LiveSetFragment,

crates/filetests/cases/codegen/tdn/array_jagged.spdr

Lines changed: 130 additions & 128 deletions
Large diffs are not rendered by default.

crates/filetests/cases/codegen/tdn/bittest_run.spdr

Lines changed: 379 additions & 376 deletions
Large diffs are not rendered by default.

crates/filetests/cases/codegen/tdn/large_spills.spdr

Lines changed: 4676 additions & 4675 deletions
Large diffs are not rendered by default.

crates/filetests/cases/isel-regalloc/tdn/array_jagged.spdr

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ func @"System.Int32 Tests.CodeGenBringUpTests::ArrayJagged(System.Int32)":i32(i3
5353
# nextln: 0004: $$rax = MovRmS32(2)
5454
# nextln: 0005: $$r12 = MovsxRRm(Ext32_64) $$rax
5555
# nextln: $$spill7 = $$rax
56-
# nextln: 0006: $$spill1 = MovRmS32(4)
56+
# nextln: 0006: $$rax = MovRmS32(4)
57+
# nextln: $$spill1 = $$rax
5758
# nextln: 0007: $$rcx = ImulRRmI(S64, 4) $$r12
58-
# nextln: 0008: $$spill5 = MovRmS32(8)
59+
# nextln: 0008: $$rax = MovRmS32(8)
60+
# nextln: $$spill5 = $$rax
5961
# nextln: 0009: $$rax = ImulRRmI(S64, 8) $$r12
6062
# nextln: 0010: $$rdx = MovRmS32(20)
6163
# nextln: $$rdi = $$rdx
@@ -135,8 +137,8 @@ func @"System.Int32 Tests.CodeGenBringUpTests::ArrayJagged(System.Int32)":i32(i3
135137
# nextln: 0051: $$r9 = AluRRm(S64, Add) $$r9, $$r8
136138
# nextln: $$r8 = $$rax
137139
# nextln: 0052: $$r8 = AluRRm(S64, Add) $$r8, $$r9
138-
# nextln: $$r9 = $$spill7
139-
# nextln: 0053: MovMR(S32) $$r8, $$r9
140+
# nextln: $$rcx = $$spill7
141+
# nextln: 0053: MovMR(S32) $$r8, $$rcx
140142
# nextln: 0054: $$r8 = MovRM(S32) $$rsi
141143
# nextln: $$rcx = $$spill4
142144
# nextln: 0055: AluRRm(S64, Cmp) $$rcx, $$r8
@@ -169,8 +171,8 @@ func @"System.Int32 Tests.CodeGenBringUpTests::ArrayJagged(System.Int32)":i32(i3
169171
# nextln: $$rdi = $$rax
170172
# nextln: 0075: $$rdi = AluRmI(S64, Add, 16) $$rdi
171173
# nextln: 0076: $$rsi = MovRM(S32) $$rdi
172-
# nextln: $$rdi = $$spill2
173-
# nextln: 0077: $$rdi = MovsxRRm(Ext32_64) $$rdi
174+
# nextln: $$rcx = $$spill2
175+
# nextln: 0077: $$rdi = MovsxRRm(Ext32_64) $$rcx
174176
# nextln: 0078: AluRRm(S64, Cmp) $$rdi, $$rsi
175177
# nextln: 0079: Jumpcc(L, block16, block17)
176178
# nextln: block16:

crates/filetests/cases/isel-regalloc/tdn/bittest_run.spdr

Lines changed: 135 additions & 133 deletions
Large diffs are not rendered by default.

crates/filetests/cases/isel-regalloc/tdn/bittest_run_canon.spdr

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
4444
# nextln: block0:
4545
# nextln: 0000: $$rax = StackAddr(!0)
4646
# nextln: $$spill14 = $$rax
47-
# nextln: 0001: $$rbx = MovRmS32(7)
47+
# nextln: 0001: $$rax = MovRmS32(7)
4848
# nextln: 0002: $$rcx = StackAddr(!1)
49-
# nextln: 0003: $$rax = MovRmS32(1)
49+
# nextln: 0003: $$rdx = MovRmS32(1)
5050
# nextln: 0004: $$r15 = MovRmS32(8)
5151
# nextln: $$r13 = $$rcx
52+
# nextln: $$rbx = $$rax
53+
# nextln: $$rax = $$rdx
5254
# nextln: 0005: $$r12 = MovRZ
5355
# nextln: 0006: $$rcx = StackAddr(!2)
5456
# nextln: $$spill13 = $$rcx
@@ -57,20 +59,24 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
5759
# nextln: $$spill11 = $$rdx
5860
# nextln: 0009: $$rdx = StackAddr(!4)
5961
# nextln: $$spill9 = $$rdx
60-
# nextln: 0010: $$spill12 = MovRmS32(15)
61-
# nextln: 0011: $$spill10 = MovRmS32(16)
62+
# nextln: 0010: $$rdx = MovRmS32(15)
63+
# nextln: $$spill12 = $$rdx
64+
# nextln: 0011: $$rdx = MovRmS32(16)
65+
# nextln: $$spill10 = $$rdx
6266
# nextln: 0012: $$rdx = StackAddr(!5)
6367
# nextln: $$spill8 = $$rdx
6468
# nextln: 0013: $$rdx = StackAddr(!6)
6569
# nextln: $$spill6 = $$rdx
6670
# nextln: 0014: $$rdx = StackAddr(!7)
6771
# nextln: $$spill5 = $$rdx
68-
# nextln: 0015: $$spill7 = MovRmS32(3)
72+
# nextln: 0015: $$rdx = MovRmS32(3)
73+
# nextln: $$spill7 = $$rdx
6974
# nextln: 0016: $$rdx = StackAddr(!8)
7075
# nextln: $$spill2 = $$rdx
7176
# nextln: 0017: $$rdx = StackAddr(!9)
7277
# nextln: $$spill1 = $$rdx
73-
# nextln: 0018: $$spill3 = MovRmS32(64)
78+
# nextln: 0018: $$rdx = MovRmS32(64)
79+
# nextln: $$spill3 = $$rdx
7480
# nextln: 0019: $$r9 = MovRU32(4294967168)
7581
# nextln: 0020: $$r8 = MovRU32(4294934528)
7682
# nextln: 0021: $$r10 = MovRZ
@@ -157,22 +163,22 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
157163
# nextln: 0085: $$rax = CallRm $$rax, $$rdi, $$rsi
158164
# nextln: 0086: $$r14 = AluRRm(S32, And) $$r14, $$rax
159165
# nextln: 0087: $$rax = FuncAddrAbs(External(extfunc8))
160-
# nextln: $$rdi = $$r13
161166
# nextln: $$rsi = $$r12
167+
# nextln: $$rdi = $$r13
162168
# nextln: 0088: $$rax = CallRm $$rax, $$rdi, $$rsi
163169
# nextln: 0089: $$r14 = AluRRm(S32, And) $$r14, $$rax
164170
# nextln: 0090: $$rax = FuncAddrAbs(External(extfunc8))
165-
# nextln: $$rdi = $$r13
166171
# nextln: $$rsi = $$r15
172+
# nextln: $$rdi = $$r13
167173
# nextln: 0091: $$rax = CallRm $$rax, $$rdi, $$rsi
168174
# nextln: 0092: $$rcx = MovRZ
169175
# nextln: 0093: AluRRm(S32, Test) $$rax, $$rax
170176
# nextln: 0094: $$rcx = Setcc(E) $$rcx
171177
# nextln: 0095: $$r14 = AluRRm(S32, And) $$r14, $$rcx
172178
# nextln: 0096: $$rax = FuncAddrAbs(External(extfunc8))
173179
# nextln: $$r15 = $$spill4
174-
# nextln: $$rdi = $$r13
175180
# nextln: $$rsi = $$r15
181+
# nextln: $$rdi = $$r13
176182
# nextln: 0097: $$rax = CallRm $$rax, $$rdi, $$rsi
177183
# nextln: 0098: $$r14 = AluRRm(S32, And) $$r14, $$rax
178184
# nextln: 0099: $$rax = FuncAddrAbs(External(extfunc8))
@@ -240,13 +246,13 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
240246
# nextln: 0146: $$r14 = AluRRm(S32, And) $$r14, $$rax
241247
# nextln: 0147: $$rax = FuncAddrAbs(External(extfunc4))
242248
# nextln: $$r13 = $$spill9
243-
# nextln: $$rdi = $$r13
244249
# nextln: $$rsi = $$r12
250+
# nextln: $$rdi = $$r13
245251
# nextln: 0148: $$rax = CallRm $$rax, $$rdi, $$rsi
246252
# nextln: 0149: $$r14 = AluRRm(S32, And) $$r14, $$rax
247253
# nextln: 0150: $$rax = FuncAddrAbs(External(extfunc4))
248-
# nextln: $$rdi = $$r13
249254
# nextln: $$rsi = $$rbx
255+
# nextln: $$rdi = $$r13
250256
# nextln: 0151: $$rax = CallRm $$rax, $$rdi, $$rsi
251257
# nextln: 0152: $$rcx = MovRZ
252258
# nextln: 0153: AluRRm(S32, Test) $$rax, $$rax
@@ -332,13 +338,13 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
332338
# nextln: 0218: $$r14 = AluRRm(S32, And) $$r14, $$rcx
333339
# nextln: 0219: $$rax = FuncAddrAbs(External(extfunc6))
334340
# nextln: $$rbx = $$spill6
335-
# nextln: $$rdi = $$rbx
336341
# nextln: $$rsi = $$r12
342+
# nextln: $$rdi = $$rbx
337343
# nextln: 0220: $$rax = CallRm $$rax, $$rdi, $$rsi
338344
# nextln: 0221: $$r14 = AluRRm(S32, And) $$r14, $$rax
339345
# nextln: 0222: $$rax = FuncAddrAbs(External(extfunc6))
340-
# nextln: $$rdi = $$rbx
341346
# nextln: $$rsi = $$r15
347+
# nextln: $$rdi = $$rbx
342348
# nextln: 0223: $$rax = CallRm $$rax, $$rdi, $$rsi
343349
# nextln: 0224: $$r14 = AluRRm(S32, And) $$r14, $$rax
344350
# nextln: 0225: $$rax = FuncAddrAbs(External(extfunc6))
@@ -378,13 +384,13 @@ func @"System.Boolean Tests.BitTest::Run()":i32() {
378384
# nextln: 0252: $$r14 = AluRRm(S32, And) $$r14, $$rcx
379385
# nextln: 0253: $$rax = FuncAddrAbs(External(extfunc2))
380386
# nextln: $$r13 = $$spill2
381-
# nextln: $$rsi = $$r12
382387
# nextln: $$rdi = $$r13
388+
# nextln: $$rsi = $$r12
383389
# nextln: 0254: $$rax = CallRm $$rax, $$rdi, $$rsi
384390
# nextln: 0255: $$r14 = AluRRm(S32, And) $$r14, $$rax
385391
# nextln: 0256: $$rax = FuncAddrAbs(External(extfunc2))
386-
# nextln: $$rsi = $$r15
387392
# nextln: $$rdi = $$r13
393+
# nextln: $$rsi = $$r15
388394
# nextln: 0257: $$rax = CallRm $$rax, $$rdi, $$rsi
389395
# nextln: $$r15 = $$rax
390396
# nextln: 0258: $$rax = FuncAddrAbs(External(extfunc2))

crates/filetests/cases/isel-regalloc/tdn/int_no_op_sub_funclet_10.spdr

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ func @"System.Int32 Tests.Int_No_Op::Sub_Funclet_10()":i32() {
6666
# nextln: $$rdi = $$r14
6767
# nextln: 0029: $$rax = CallRm $$rax, $$rdi, $$rsi
6868
# nextln: 0030: $$rax = AluRmI(S64, Add, 32) $$rax
69-
# nextln: $$r15 = $$spill6
70-
# nextln: 0031: MovMR(S32) $$rax, $$r15
69+
# nextln: $$rcx = $$spill6
70+
# nextln: 0031: MovMR(S32) $$rax, $$rcx
7171
# nextln: 0032: $$rax = FuncAddrAbs(External(extfunc0))
72+
# nextln: $$r15 = $$rcx
7273
# nextln: $$rsi = $$r13
7374
# nextln: $$rdi = $$r14
7475
# nextln: 0033: $$rax = CallRm $$rax, $$rdi, $$rsi
7576
# nextln: $$rdx = $$rax
7677
# nextln: 0034: $$rdx = AluRmI(S64, Add, 32) $$rdx
77-
# nextln: 0035: MovMR(S32) $$rdx, $$r15
7878
# nextln: $$r13 = $$r15
79+
# nextln: 0035: MovMR(S32) $$rdx, $$r13
7980
# nextln: 0036: $$rdi = MovRM(S32) $$rdx
8081
# nextln: $$rcx = $$spill5
8182
# nextln: 0037: AluRRm(S64, Cmp) $$rcx, $$rdi
@@ -87,10 +88,11 @@ func @"System.Int32 Tests.Int_No_Op::Sub_Funclet_10()":i32() {
8788
# nextln: $$rcx = $$spill3
8889
# nextln: $$rsi = $$rcx
8990
# nextln: 0040: $$rsi = AluRRm(S64, Add) $$rsi, $$rdi
90-
# nextln: $$r8 = $$rax
91-
# nextln: 0041: $$r8 = AluRRm(S64, Add) $$r8, $$rsi
92-
# nextln: $$rdi = $$spill4
93-
# nextln: 0042: MovMR(S8) $$r8, $$rdi
91+
# nextln: $$rdi = $$rax
92+
# nextln: 0041: $$rdi = AluRRm(S64, Add) $$rdi, $$rsi
93+
# nextln: $$rsi = $$spill4
94+
# nextln: 0042: MovMR(S8) $$rdi, $$rsi
95+
# nextln: $$rdi = $$rsi
9496
# nextln: 0043: $$rsi = MovRM(S32) $$rdx
9597
# nextln: 0044: $$r14 = MovsxRRm(Ext32_64) $$rdi
9698
# nextln: 0045: AluRRm(S64, Cmp) $$r14, $$rsi

crates/filetests/cases/isel-regalloc/tdn/large_spills.spdr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func @"System.Int32 Tests.Bool_No_Op::Sub_Funclet_0()":i32() {
4040
# nextln: $$spill1 = $$rax
4141
# nextln: 0014: $$rax = MovRmS32(3)
4242
# nextln: 0015: $$r14 = MovsxRRm(Ext32_64) $$rax
43-
# nextln: 0016: $$spill2 = MovRmS32(1)
43+
# nextln: 0016: $$rax = MovRmS32(1)
44+
# nextln: $$spill2 = $$rax
4445
# nextln: 0017: $$rbx = ImulRRmI(S64, 1) $$r14
4546
# nextln: 0018: $$rbx = AluRmI(S64, Add, 48) $$rbx
4647
# nextln: 0019: $$r12 = MovRI64(106721347401152)

0 commit comments

Comments
 (0)