Skip to content

Commit 448a86d

Browse files
committed
[AArch64] Fix handling of x29/x30 in inline assembly clobbers
The AArch64 backend was silently ignoring inline assembly clobbers when numeric register names (x29, x30) were used instead of their architectural aliases (fp, lr). 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`. 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. I've left `x31/sp` without changes, it would nice to have to have warning when trying to clobber `x31`, just like there is for `sp`, but that register needs different handling, so it's best done separately. If you have code like this: define void @clobber_x30() nounwind { tail call void asm sideeffect "nop", "~{x30}"() ret void } Here is the generated assembly before: clobber_x30: // @clobber_x30 //APP nop //NO_APP ret And after: clobber_x30: // @clobber_x30 str x30, [sp, #-16]! // 8-byte Folded Spill //APP nop //NO_APP ldr x30, [sp], #16 // 8-byte Folded Reload ret
1 parent cf9cb54 commit 448a86d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 15 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,20 @@ 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+
//
13135+
// x31->sp is not included here because it's not a general register and
13136+
// needs different handling
13137+
unsigned XRegFromAlias = StringSwitch<unsigned>(Constraint.lower())
13138+
.Cases({"{x29}", "{fp}"}, AArch64::FP)
13139+
.Cases({"{x30}", "{lr}"}, AArch64::LR)
13140+
.Default(AArch64::NoRegister);
13141+
if (XRegFromAlias != AArch64::NoRegister)
13142+
return std::make_pair(XRegFromAlias, &AArch64::GPR64RegClass);
13143+
1312913144
// Use the default implementation in TargetLowering to convert the register
1313013145
// constraint into a member of a register class.
1313113146
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)