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

KAWASHIMA Takahiro via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 26 04:59:23 PST 2024


================
@@ -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());
----------------
kawashima-fj wrote:

To avoid the `fatal error: error in backend: Invalid register name "x??"` error in #109778, this check is sufficient.

I think the original purpose of rejecting a global register variable associated with a non-reserved register is to avoid unintended register use. `w??` registers are lower half bits of the corresponding `x??` registers. So when `x??` register is not reserved, should we reject a global register variable associated with the corresponding `w??`?

For this purpose, maybe [the backend](https://github.com/llvm/llvm-project/blob/bc28260/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp#L11611) should also check `w??` registers. Actually, [old backend code checked `w??` regsters](https://github.com/llvm/llvm-project/commit/fcbec02ea6fb2a76352b64790cd9ae300f6a9943#diff-6291e4657dea1f4fecdcb9dc96bfb014f79d4c617f080b2f033943e52732cf69).

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


More information about the cfe-commits mailing list