This is an archive of the discontinued LLVM Phabricator instance.

Produce a better diagnostic for global register variables
ClosedPublic

Authored by ahatanak on Oct 16 2015, 2:51 PM.

Details

Summary

clang doesn't print a very user-friendly message when an invalid register is used for a global register variable:

For example, when the following code is compiled,

$ cat f1.c
volatile register long long A asm ("rdi");

void foo1() {

A = 1;

}

clang prints this error message:

$ clang -c f1.c
fatal error: error in backend: Invalid register name global variable

The code fails to compile because "rdi" isn't a valid register for global register variables on x86 (rsp, rbp, esp, and ebp are the only registers that are currently valid), but the diagnostic doesn't give much detail on why it is an error or which line of the source code is not correct because the error is detected in the backend.

This patch makes changes in Sema to catch this kind of error earlier. In addition, it errors out if the size of the register doesn't match the declared variable size.

e.g., volatile register int B asm ("rbp");

Diff Detail

Repository
rL LLVM

Event Timeline

ahatanak updated this revision to Diff 37645.Oct 16 2015, 2:51 PM
ahatanak retitled this revision from to Produce a better diagnostic for global register variables.
ahatanak updated this object.
ahatanak added a subscriber: cfe-commits.
echristo edited edge metadata.Nov 9 2015, 3:08 PM

My preference for this sort of thing would be an enum of failure reasons, but I guess this is ok for now. One inline request.

-eric

lib/Basic/Targets.cpp
3978–3981 ↗(On Diff #37645)

This could use a comment, what are you testing here?

ahatanak updated this revision to Diff 39774.Nov 9 2015, 5:12 PM
ahatanak edited edge metadata.

Added comments to clarify what the code is doing.

echristo accepted this revision.Nov 13 2015, 3:53 PM
echristo edited edge metadata.

LGTM.

Thanks!

-eric

This revision is now accepted and ready to land.Nov 13 2015, 3:53 PM

Thanks, I'll commit this patch shortly.

If it makes the code cleaner, I can define enums in TargetInfo and change validateGlobalRegisterVariable to return one of them in a follow-up patch.

This revision was automatically updated to reflect the committed changes.