Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions clang/lib/Basic/Targets/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

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

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 should also check w?? registers. Actually, old backend code checked w?? regsters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! Since this patch is focused on fixing the crash, it doesn't seem appropriate to make AArch64TargetLowering::getRegisterByName() more restrictive here. However, I added checks for w?? registers in AArch64TargetInfo::validateGlobalRegisterVariable() and corresponding tests.

}
return false;
}

Expand Down
4 changes: 3 additions & 1 deletion clang/test/Sema/aarch64-fixed-global-register.c
Original file line number Diff line number Diff line change
@@ -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");
Loading