Skip to content

Commit 5fe7f73

Browse files
[clang][AArch64] Add validation for Global Register Variable. (#94271)
Fixes: #76426
1 parent ede27d8 commit 5fe7f73

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ bool AArch64TargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
221221
return true;
222222
}
223223

224+
bool AArch64TargetInfo::validateGlobalRegisterVariable(
225+
StringRef RegName, unsigned RegSize, bool &HasSizeMismatch) const {
226+
if ((RegName == "sp") || RegName.starts_with("x")) {
227+
HasSizeMismatch = RegSize != 64;
228+
return true;
229+
} else if (RegName.starts_with("w")) {
230+
HasSizeMismatch = RegSize != 32;
231+
return true;
232+
}
233+
return false;
234+
}
235+
224236
bool AArch64TargetInfo::validateBranchProtection(StringRef Spec, StringRef,
225237
BranchProtectionInfo &BPI,
226238
StringRef &Err) const {

clang/lib/Basic/Targets/AArch64.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
202202
bool hasBitIntType() const override { return true; }
203203

204204
bool validateTarget(DiagnosticsEngine &Diags) const override;
205+
206+
bool validateGlobalRegisterVariable(StringRef RegName, unsigned RegSize,
207+
bool &HasSizeMismatch) const override;
205208
};
206209

207210
class LLVM_LIBRARY_VISIBILITY AArch64leTargetInfo : public AArch64TargetInfo {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check that -ffixed register handled for globals.
2+
// Regression test for #76426
3+
// RUN: %clang --target=aarch64-none-gnu -ffixed-x15 -### %s 2>&1 | FileCheck %s
4+
// CHECK-NOT: fatal error: error in backend: Invalid register name "x15".
5+
register int i1 __asm__("x15");
6+
7+
int foo() {
8+
return i1;
9+
}
10+
int main() {
11+
return foo();
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang_cc1 -triple aarch64-unknown-none-gnu %s -verify -fsyntax-only
2+
3+
register char i1 __asm__ ("x15"); // expected-error {{size of register 'x15' does not match variable size}}
4+
register long long l2 __asm__ ("w14"); // expected-error {{size of register 'w14' does not match variable size}}

0 commit comments

Comments
 (0)