Skip to content

[clang][AArch64] Add validation for Global Register Variable. #94271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2024

Conversation

DanielKristofKiss
Copy link
Member

Fixes: #76426

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:AArch64 clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2024

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-clang

Author: Daniel Kiss (DanielKristofKiss)

Changes

Fixes: #76426


Full diff: https://github.com/llvm/llvm-project/pull/94271.diff

2 Files Affected:

  • (modified) clang/lib/Basic/Targets/AArch64.h (+12)
  • (added) clang/test/Driver/aarch64-fixed-register-global.c (+13)
diff --git a/clang/lib/Basic/Targets/AArch64.h b/clang/lib/Basic/Targets/AArch64.h
index 12fb50286f751..9165865029900 100644
--- a/clang/lib/Basic/Targets/AArch64.h
+++ b/clang/lib/Basic/Targets/AArch64.h
@@ -202,6 +202,18 @@ 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 {
+    if (RegName.equals("sp") || RegName.starts_with("x")) {
+      HasSizeMismatch = RegSize != 64;
+      return true;
+    } else if (RegName.starts_with("w")) {
+      HasSizeMismatch = RegSize != 32;
+      return true;
+    }
+    return false;
+  }
 };
 
 class LLVM_LIBRARY_VISIBILITY AArch64leTargetInfo : public AArch64TargetInfo {
diff --git a/clang/test/Driver/aarch64-fixed-register-global.c b/clang/test/Driver/aarch64-fixed-register-global.c
new file mode 100644
index 0000000000000..6a05228277cd6
--- /dev/null
+++ b/clang/test/Driver/aarch64-fixed-register-global.c
@@ -0,0 +1,13 @@
+// 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();
+}

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2024

@llvm/pr-subscribers-clang-driver

Author: Daniel Kiss (DanielKristofKiss)

Changes

Fixes: #76426


Full diff: https://github.com/llvm/llvm-project/pull/94271.diff

2 Files Affected:

  • (modified) clang/lib/Basic/Targets/AArch64.h (+12)
  • (added) clang/test/Driver/aarch64-fixed-register-global.c (+13)
diff --git a/clang/lib/Basic/Targets/AArch64.h b/clang/lib/Basic/Targets/AArch64.h
index 12fb50286f751..9165865029900 100644
--- a/clang/lib/Basic/Targets/AArch64.h
+++ b/clang/lib/Basic/Targets/AArch64.h
@@ -202,6 +202,18 @@ 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 {
+    if (RegName.equals("sp") || RegName.starts_with("x")) {
+      HasSizeMismatch = RegSize != 64;
+      return true;
+    } else if (RegName.starts_with("w")) {
+      HasSizeMismatch = RegSize != 32;
+      return true;
+    }
+    return false;
+  }
 };
 
 class LLVM_LIBRARY_VISIBILITY AArch64leTargetInfo : public AArch64TargetInfo {
diff --git a/clang/test/Driver/aarch64-fixed-register-global.c b/clang/test/Driver/aarch64-fixed-register-global.c
new file mode 100644
index 0000000000000..6a05228277cd6
--- /dev/null
+++ b/clang/test/Driver/aarch64-fixed-register-global.c
@@ -0,0 +1,13 @@
+// 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();
+}

@kawashima-fj
Copy link
Member

@DanielKristofKiss Thanks for the fix. At first glance, LGTM. I'll take a closer look in a few days.
I'm not so familiar with the code. If someone else can review, I'll leave it to them.

@DavidSpickett DavidSpickett changed the title [AArch64] Add validation for Global Register Variable. [clang][AArch64] Add validation for Global Register Variable. Jun 4, 2024
Copy link
Member

@kawashima-fj kawashima-fj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. I left a minor suggestion.

Some notes:

@DanielKristofKiss DanielKristofKiss merged commit 5fe7f73 into llvm:main Jun 17, 2024
7 checks passed
igorkudrin added a commit that referenced this pull request Dec 6, 2024
…117419)

Fixes #76426, #109778 (for AArch64)

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 reserved.
igorkudrin added a commit that referenced this pull request Dec 7, 2024
…s used (#117419)

Relanding the patch with a fix for a test failure on build bots that do
not build LLVM for AArch64.

Fixes #76426, #109778 (for AArch64)

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 reserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AArch64] Crash when compiling global register variable __asm__("x15") without -ffixed-x15
3 participants