Skip to content

Commit e2036ea

Browse files
committed
[AArch64] Fix handling of x29/x30/x31 in inline assembly clobbers
The AArch64 backend was silently ignoring inline assembly clobbers when numeric register names (x29, x30, x31) were used instead of their architectural aliases (fp, lr, sp). I found this bug via inline assembly in Zig, which not normalize the register names the way clang does. There is an incoplete workaround for this in Rust, but that only handles `x30/lr`, not `x29/fp` and `x31/sp`. I thought it would make sense to fix this properly rather than adding a workaround to Zig. This patch adds explicit handling in getRegForInlineAsmConstraint() to map both numeric and alias forms to the correct physical registers, following the same pattern used by the RISC-V backend.
1 parent 9361113 commit e2036ea

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/ADT/SmallVectorExtras.h"
3131
#include "llvm/ADT/Statistic.h"
3232
#include "llvm/ADT/StringRef.h"
33+
#include "llvm/ADT/StringSwitch.h"
3334
#include "llvm/ADT/Twine.h"
3435
#include "llvm/Analysis/LoopInfo.h"
3536
#include "llvm/Analysis/MemoryLocation.h"
@@ -13126,6 +13127,19 @@ AArch64TargetLowering::getRegForInlineAsmConstraint(
1312613127
return std::make_pair(unsigned(AArch64::ZT0), &AArch64::ZTRRegClass);
1312713128
}
1312813129

13130+
// Clang will correctly decode the usage of register name aliases into their
13131+
// official names. However, other frontends like `rustc` do not. This allows
13132+
// users of these frontends to use the ABI names for registers in LLVM-style
13133+
// register constraints.
13134+
unsigned XRegFromAlias = StringSwitch<unsigned>(Constraint.lower())
13135+
.Cases({"{x29}", "{fp}"}, AArch64::FP)
13136+
.Cases({"{x30}", "{lr}"}, AArch64::LR)
13137+
.Cases({"{x31}", "{sp}"}, AArch64::SP)
13138+
.Default(AArch64::NoRegister);
13139+
if (XRegFromAlias != AArch64::NoRegister) {
13140+
return std::make_pair(XRegFromAlias, &AArch64::GPR64RegClass);
13141+
}
13142+
1312913143
// Use the default implementation in TargetLowering to convert the register
1313013144
// constraint into a member of a register class.
1313113145
std::pair<unsigned, const TargetRegisterClass *> Res;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: llc -mtriple=aarch64 -verify-machineinstrs < %s | FileCheck %s
2+
3+
; Test that both numeric register names (x29, x30) and their architectural
4+
; aliases (fp, lr) work correctly as clobbers in inline assembly.
5+
6+
define void @clobber_x29() nounwind {
7+
; CHECK-LABEL: clobber_x29:
8+
; CHECK: str x29, [sp
9+
; CHECK: ldr x29, [sp
10+
tail call void asm sideeffect "", "~{x29}"()
11+
ret void
12+
}
13+
14+
define void @clobber_fp() nounwind {
15+
; CHECK-LABEL: clobber_fp:
16+
; CHECK: str x29, [sp
17+
; CHECK: ldr x29, [sp
18+
tail call void asm sideeffect "", "~{fp}"()
19+
ret void
20+
}
21+
22+
define void @clobber_x30() nounwind {
23+
; CHECK-LABEL: clobber_x30:
24+
; CHECK: str x30, [sp
25+
; CHECK: ldr x30, [sp
26+
tail call void asm sideeffect "", "~{x30}"()
27+
ret void
28+
}
29+
30+
define void @clobber_lr() nounwind {
31+
; CHECK-LABEL: clobber_lr:
32+
; CHECK: str x30, [sp
33+
; CHECK: ldr x30, [sp
34+
tail call void asm sideeffect "", "~{lr}"()
35+
ret void
36+
}

0 commit comments

Comments
 (0)