Skip to content

Commit 64fd2ec

Browse files
committed
[AVR] Disallow the LDDWRdPtrQ instruction with Z as the destination
This is an AVR-specific workaround for a limitation of the register allocator that only exposes itself on targets with high register contention like AVR, which only has three pointer registers. The three pointer registers are X, Y, and Z. In most nontrivial functions, Y is reserved for the frame pointer, as per the calling convention. This leaves X and Z. Some instructions, such as LPM ("load program memory"), are only defined for the Z register. Sometimes this just leaves X. When the backend generates a LDDWRdPtrQ instruction with Z as the destination pointer, it usually trips up the register allocator with this error message: LLVM ERROR: ran out of registers during register allocation This patch is a hacky workaround. We ban the LDDWRdPtrQ instruction from ever using the Z register as an operand. This gives the register allocator a bit more space to allocate, fixing the regalloc exhaustion error. Here is a description from the patch author Peter Nimmervoll As far as I understand the problem occurs when LDDWRdPtrQ uses the ptrdispregs register class as target register. This should work, but the allocator can't deal with this for some reason. So from my testing, it seams like (and I might be totally wrong on this) the allocator reserves the Z register for the ICALL instruction and then the register class ptrdispregs only has 1 register left and we can't use Y for source and destination. Removing the Z register from DREGS fixes the problem but removing Y register does not. More information about the bug can be found on the avr-rust issue tracker at avr-rust/rust-legacy-fork#37. A bug has raised to track the removal of this workaround and a proper fix; PR39553 at https://bugs.llvm.org/show_bug.cgi?id=39553. Patch by Peter Nimmervoll llvm-svn=346114
1 parent 8a8577b commit 64fd2ec

File tree

6 files changed

+91
-9
lines changed

6 files changed

+91
-9
lines changed

llvm/lib/Target/AVR/AVRInstrInfo.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ isReMaterializable = 1 in
12221222
// ldd Rd, P+q
12231223
// ldd Rd+1, P+q+1
12241224
let Constraints = "@earlyclobber $dst" in
1225-
def LDDWRdPtrQ : Pseudo<(outs DREGS:$dst),
1225+
def LDDWRdPtrQ : Pseudo<(outs DREGS_WITHOUT_Z_WORKAROUND:$dst),
12261226
(ins memri:$memri),
12271227
"lddw\t$dst, $memri",
12281228
[(set i16:$dst, (load addr:$memri))]>,

llvm/lib/Target/AVR/AVRRegisterInfo.td

+20
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ def DREGS : RegisterClass<"AVR", [i16], 8,
157157
R9R8, R7R6, R5R4, R3R2, R1R0
158158
)>;
159159

160+
// The 16-bit DREGS register class, excluding the Z pointer register.
161+
//
162+
// This is used by instructions which cause high pointer register
163+
// contention which leads to an assertion in the register allocator.
164+
//
165+
// There is no technical reason why instructions that use this class
166+
// cannot use Z; it's simply a workaround a regalloc bug.
167+
//
168+
// More information can be found in PR39553.
169+
def DREGS_WITHOUT_Z_WORKAROUND : RegisterClass<"AVR", [i16], 8,
170+
(
171+
// Return value and arguments.
172+
add R25R24, R19R18, R21R20, R23R22,
173+
// Scratch registers.
174+
R27R26,
175+
// Callee saved registers.
176+
R29R28, R17R16, R15R14, R13R12, R11R10,
177+
R9R8, R7R6, R5R4, R3R2, R1R0
178+
)>;
179+
160180
// 16-bit register class for immediate instructions.
161181
def DLDREGS : RegisterClass<"AVR", [i16], 8,
162182
(

llvm/test/CodeGen/AVR/pseudo/LDDWRdPtrQ-same-src-dst.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ body: |
2525
2626
; CHECK-LABEL: test_lddwrdptrq
2727
28-
; CHECK: ldd [[SCRATCH:r[0-9]+]], Z+10
28+
; CHECK: ldd [[SCRATCH:r[0-9]+]], Y+10
2929
; CHECK-NEXT: push [[SCRATCH]]
30-
; CHECK-NEXT: ldd [[SCRATCH]], Z+11
31-
; CHECK-NEXT: mov r31, [[SCRATCH]]
32-
; CHECK-NEXT: pop r30
30+
; CHECK-NEXT: ldd [[SCRATCH]], Y+11
31+
; CHECK-NEXT: mov r29, [[SCRATCH]]
32+
; CHECK-NEXT: pop r28
3333
34-
early-clobber $r31r30 = LDDWRdPtrQ undef $r31r30, 10
34+
early-clobber $r29r28 = LDDWRdPtrQ undef $r29r28, 10
3535
...

llvm/test/CodeGen/AVR/pseudo/LDDWRdPtrQ.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ body: |
1818
1919
; CHECK-LABEL: test_lddwrdptrq
2020
21-
; CHECK: ldd r30, Y+10
22-
; CHECK-NEXT: ldd r31, Y+11
21+
; CHECK: ldd r28, Z+10
22+
; CHECK-NEXT: ldd r29, Z+11
2323
24-
early-clobber $r31r30 = LDDWRdPtrQ undef $r29r28, 10
24+
early-clobber $r29r28 = LDDWRdPtrQ undef $r31r30, 10
2525
...
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llc < %s -march=avr | FileCheck %s
2+
3+
%"fmt::Formatter" = type { i32, { i8*, void (i8*)** } }
4+
5+
@str.1b = external constant [0 x i8]
6+
7+
define void @"TryFromIntError::Debug"(%"fmt::Formatter"* dereferenceable(32)) unnamed_addr #0 personality i32 (...)* @rust_eh_personality {
8+
; CHECK-LABEL: "TryFromIntError::Debug"
9+
start:
10+
%builder = alloca i8, align 8
11+
%1 = getelementptr inbounds %"fmt::Formatter", %"fmt::Formatter"* %0, i16 0, i32 1
12+
%2 = bitcast { i8*, void (i8*)** }* %1 to {}**
13+
%3 = load {}*, {}** %2, align 2
14+
%4 = getelementptr inbounds %"fmt::Formatter", %"fmt::Formatter"* %0, i16 0, i32 1, i32 1
15+
%5 = load void (i8*)**, void (i8*)*** %4, align 2
16+
%6 = getelementptr inbounds void (i8*)*, void (i8*)** %5, i16 3
17+
%7 = bitcast void (i8*)** %6 to i8 ({}*, i8*, i16)**
18+
%8 = load i8 ({}*, i8*, i16)*, i8 ({}*, i8*, i16)** %7, align 2
19+
%9 = tail call i8 %8({}* nonnull %3, i8* noalias nonnull readonly getelementptr inbounds ([0 x i8], [0 x i8]* @str.1b, i16 0, i16 0), i16 15)
20+
unreachable
21+
}
22+
23+
declare i32 @rust_eh_personality(...) unnamed_addr
24+
25+
attributes #0 = { uwtable }
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; RUN: llc < %s -march=avr | FileCheck %s
2+
3+
%"fmt::Formatter.1.77.153.229.305.381.1673" = type { [0 x i8], i32, [0 x i8], i32, [0 x i8], i8, [0 x i8], %"option::Option<usize>.0.76.152.228.304.380.1672", [0 x i8], %"option::Option<usize>.0.76.152.228.304.380.1672", [0 x i8], { {}*, {}* }, [0 x i8], { i8*, i8* }, [0 x i8], { [0 x { i8*, i8* }]*, i16 }, [0 x i8] }
4+
%"option::Option<usize>.0.76.152.228.304.380.1672" = type { [0 x i8], i8, [2 x i8] }
5+
6+
@str.4S = external constant [5 x i8]
7+
8+
; Function Attrs: uwtable
9+
define void @"_ZN65_$LT$lib..str..Chars$LT$$u27$a$GT$$u20$as$u20$lib..fmt..Debug$GT$3fmt17h76a537e22649f739E"(%"fmt::Formatter.1.77.153.229.305.381.1673"* dereferenceable(27) %__arg_0) unnamed_addr #0 personality i32 (...)* @rust_eh_personality {
10+
; CHECK-LABEL: "_ZN65_$LT$lib..str..Chars$LT$$u27$a$GT$$u20$as$u20$lib..fmt..Debug$GT$3fmt17h76a537e22649f739E"
11+
start:
12+
%0 = getelementptr inbounds %"fmt::Formatter.1.77.153.229.305.381.1673", %"fmt::Formatter.1.77.153.229.305.381.1673"* %__arg_0, i16 0, i32 11, i32 0
13+
%1 = load {}*, {}** %0, align 1, !noalias !0, !nonnull !9
14+
%2 = getelementptr inbounds %"fmt::Formatter.1.77.153.229.305.381.1673", %"fmt::Formatter.1.77.153.229.305.381.1673"* %__arg_0, i16 0, i32 11, i32 1
15+
%3 = bitcast {}** %2 to i1 ({}*, [0 x i8]*, i16)***
16+
%4 = load i1 ({}*, [0 x i8]*, i16)**, i1 ({}*, [0 x i8]*, i16)*** %3, align 1, !noalias !0, !nonnull !9
17+
%5 = getelementptr inbounds i1 ({}*, [0 x i8]*, i16)*, i1 ({}*, [0 x i8]*, i16)** %4, i16 3
18+
%6 = load i1 ({}*, [0 x i8]*, i16)*, i1 ({}*, [0 x i8]*, i16)** %5, align 1, !invariant.load !9, !noalias !0, !nonnull !9
19+
%7 = tail call zeroext i1 %6({}* nonnull %1, [0 x i8]* noalias nonnull readonly bitcast ([5 x i8]* @str.4S to [0 x i8]*), i16 5), !noalias !10
20+
unreachable
21+
}
22+
23+
declare i32 @rust_eh_personality(...) unnamed_addr
24+
25+
attributes #0 = { uwtable }
26+
27+
!0 = !{!1, !3, !5, !6, !8}
28+
!1 = distinct !{!1, !2, !"_ZN3lib3fmt9Formatter9write_str17ha1a9656fc66ccbe5E: %data.0"}
29+
!2 = distinct !{!2, !"_ZN3lib3fmt9Formatter9write_str17ha1a9656fc66ccbe5E"}
30+
!3 = distinct !{!3, !4, !"_ZN3lib3fmt8builders16debug_struct_new17h352a1de8f89c2bc3E: argument 0"}
31+
!4 = distinct !{!4, !"_ZN3lib3fmt8builders16debug_struct_new17h352a1de8f89c2bc3E"}
32+
!5 = distinct !{!5, !4, !"_ZN3lib3fmt8builders16debug_struct_new17h352a1de8f89c2bc3E: %name.0"}
33+
!6 = distinct !{!6, !7, !"_ZN3lib3fmt9Formatter12debug_struct17ha1ff79f633171b68E: argument 0"}
34+
!7 = distinct !{!7, !"_ZN3lib3fmt9Formatter12debug_struct17ha1ff79f633171b68E"}
35+
!8 = distinct !{!8, !7, !"_ZN3lib3fmt9Formatter12debug_struct17ha1ff79f633171b68E: %name.0"}
36+
!9 = !{}
37+
!10 = !{!3, !6}

0 commit comments

Comments
 (0)