Skip to content

Commit

Permalink
[clang][AArch64] Add validation for Global Register Variable. (#94271)
Browse files Browse the repository at this point in the history
Fixes: #76426
  • Loading branch information
DanielKristofKiss authored Jun 17, 2024
1 parent ede27d8 commit 5fe7f73
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions clang/lib/Basic/Targets/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ bool AArch64TargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
return true;
}

bool AArch64TargetInfo::validateGlobalRegisterVariable(
StringRef RegName, unsigned RegSize, bool &HasSizeMismatch) const {
if ((RegName == "sp") || RegName.starts_with("x")) {
HasSizeMismatch = RegSize != 64;
return true;
} else if (RegName.starts_with("w")) {
HasSizeMismatch = RegSize != 32;
return true;
}
return false;
}

bool AArch64TargetInfo::validateBranchProtection(StringRef Spec, StringRef,
BranchProtectionInfo &BPI,
StringRef &Err) const {
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Basic/Targets/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
bool hasBitIntType() const override { return true; }

bool validateTarget(DiagnosticsEngine &Diags) const override;

bool validateGlobalRegisterVariable(StringRef RegName, unsigned RegSize,
bool &HasSizeMismatch) const override;
};

class LLVM_LIBRARY_VISIBILITY AArch64leTargetInfo : public AArch64TargetInfo {
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Driver/aarch64-fixed-register-global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Check that -ffixed register handled for globals.
// Regression test for #76426
// RUN: %clang --target=aarch64-none-gnu -ffixed-x15 -### %s 2>&1 | FileCheck %s
// CHECK-NOT: fatal error: error in backend: Invalid register name "x15".
register int i1 __asm__("x15");

int foo() {
return i1;
}
int main() {
return foo();
}
4 changes: 4 additions & 0 deletions clang/test/Sema/aarch64-fixed-global-register.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: %clang_cc1 -triple aarch64-unknown-none-gnu %s -verify -fsyntax-only

register char i1 __asm__ ("x15"); // expected-error {{size of register 'x15' does not match variable size}}
register long long l2 __asm__ ("w14"); // expected-error {{size of register 'w14' does not match variable size}}

0 comments on commit 5fe7f73

Please sign in to comment.