[clang] [clang][AArch64] Avoid a crash when a non-reserved register is used (PR #117419)

Igor Kudrin via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 22 21:43:55 PST 2024


https://github.com/igorkudrin created https://github.com/llvm/llvm-project/pull/117419

Fixes #76426

The previous patch for this issue, #94271, generated an error message if a register and a global variable did not have the same size. This patch checks if the register is actually reserved.

>From 54fc788cca22b1b30313f4349d9ef2ba8cf58079 Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikudrin at accesssoftek.com>
Date: Fri, 22 Nov 2024 21:36:54 -0800
Subject: [PATCH] [clang][AArch64] Avoid a crash when a non-reserved register
 is used

Fixes #76426

The previous patch for this issue, #94271, generated an error message if
a register and a global variable did not have the same size. This patch
checks if the register is actually reserved.
---
 clang/lib/Basic/Targets/AArch64.cpp            | 18 ++++++++++++++----
 .../test/Sema/aarch64-fixed-global-register.c  |  4 +++-
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index eb8a3ada034482..7500c7bb045e54 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -232,13 +232,23 @@ bool AArch64TargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
 
 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")) {
+  if (RegName.starts_with("w")) {
     HasSizeMismatch = RegSize != 32;
     return true;
   }
+  if (RegName == "sp") {
+    HasSizeMismatch = RegSize != 64;
+    return true;
+  }
+  if (RegName.starts_with("x")) {
+    HasSizeMismatch = RegSize != 64;
+    // Check if the register is reserved. See also
+    // AArch64TargetLowering::getRegisterByName().
+    return RegName == "x0" ||
+           (RegName == "x18" &&
+            llvm::AArch64::isX18ReservedByDefault(getTriple())) ||
+           getTargetOpts().FeatureMap.lookup(("reserve-" + RegName).str());
+  }
   return false;
 }
 
diff --git a/clang/test/Sema/aarch64-fixed-global-register.c b/clang/test/Sema/aarch64-fixed-global-register.c
index 9b4a422d8c1b2c..2b7d39dbcdd6f8 100644
--- a/clang/test/Sema/aarch64-fixed-global-register.c
+++ b/clang/test/Sema/aarch64-fixed-global-register.c
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -triple aarch64-unknown-none-gnu %s -verify -fsyntax-only
+// RUN: %clang_cc1 -triple aarch64-unknown-none-gnu %s -target-feature +reserve-x4 -target-feature +reserve-x15 -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}}
+register long x3 __asm__ ("x3"); // expected-error {{register 'x3' unsuitable for global register variables on this target}}
+register long x4 __asm__ ("x4");



More information about the cfe-commits mailing list