This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Emit warning when SP is clobbered by asm
ClosedPublic

Authored by quinnp on Oct 19 2021, 7:11 AM.

Details

Reviewers
nemanjai
stefanp
Group Reviewers
Restricted Project
Commits
rG6a028296fe62: [PowerPC] Emit warning when SP is clobbered by asm
Summary

This patch emits a warning when the stack pointer register (R1) is found in
the clobber list of an inline asm statement. Clobbering the stack pointer is
not supported.

Diff Detail

Event Timeline

quinnp created this revision.Oct 19 2021, 7:11 AM
quinnp requested review of this revision.Oct 19 2021, 7:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 19 2021, 7:11 AM
quinnp added reviewers: Restricted Project, nemanjai, stefanp.Oct 19 2021, 7:17 AM
shchenz added inline comments.Nov 23 2021, 6:27 AM
llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
396

PowerPC overrides getReservedRegs(), why don't we use this function and only warn for R1 even no X1?

nemanjai requested changes to this revision.Nov 29 2021, 8:05 AM
nemanjai added inline comments.
llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
396

It is unfortunately not as simple as that. We do have some reserved registers that can be clobbered. For example R2 can be clobbered since the compiler will spill and restore it.
There should be a comment to that end in the code. The test should definitely cover X1 in 64-bit mode.

Furthermore, there needs to be a 64-bit test as well.

This revision now requires changes to proceed.Nov 29 2021, 8:05 AM
quinnp updated this revision to Diff 390695.Nov 30 2021, 6:43 AM

Addressing review comments. Added a comment about why we are not using getReservedRegs(). Added implementation for PPC64.

quinnp marked 2 inline comments as done.Nov 30 2021, 6:47 AM
quinnp edited the summary of this revision. (Show Details)
shchenz added inline comments.Nov 30 2021, 5:40 PM
llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
396

Only marking R1/X1 as reserved for PowerPC should be working. But I still think using getReservedRegs() is a better way. If we reserve some registers which can be allocated for some cases conservatively, maybe we should change the logic in getReservedRegs? For the R2/X2 example, I saw there is a TODO to make it allocatable.

And even we are aggressive to mark R2/X2(all registers in getReservedRegs()) as reserved, we have to choose which one of the below two cases is worse:
1: aggressively mark all registers in getReservedRegs() as reserved. We may give a false warning about the clobber register in the inline assembly. For example, we should be able to clobber R2/X2 in a function that does not access TOC.
2: We only emit warnings for R1/X1 like this patch does. We may miss giving a warning for other reserved registers, for example R13/X13. (I saw a real-world case for this wrong usage of thread pointer in the inline assembly).

Anyhow I will not be a blocker for this patch to get in as it is also right.

This needs more work.

  1. We need a clang test case similar to clang/test/Misc/inline-asm-clobber-warning.c
  2. We need to handle r1 on both 64-bit and 32-bit because x1 is just an internal LLVM name
  3. We need to handle sp as an alias for r1 as GCC does
llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp
396

While I certainly agree that only focusing on R1 is a bit problematic, false warnings aren't free. There are definitely projects that use inline asm and build with -Werror. Breaking such projects would definitely be an undesirable outcome.

nemanjai requested changes to this revision.Dec 8 2021, 6:37 PM
This revision now requires changes to proceed.Dec 8 2021, 6:37 PM
quinnp updated this revision to Diff 395687.EditedDec 21 2021, 8:15 AM

Adressing review comments. Added register alias sp for the register r1.
Added a front end testcase to test the location information and all of the
aliases for r1. Merged the back end test cases so that we are testing both
r1 and x1 in 32bit and 64bit.

Herald added a project: Restricted Project. · View Herald TranscriptDec 21 2021, 8:15 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
quinnp edited the summary of this revision. (Show Details)Dec 21 2021, 8:21 AM
quinnp edited the summary of this revision. (Show Details)
quinnp marked 2 inline comments as done.Dec 21 2021, 8:23 AM
This revision is now accepted and ready to land.Jan 21 2022, 3:32 AM
This revision was landed with ongoing or failed builds.Jan 24 2022, 1:12 PM
This revision was automatically updated to reflect the committed changes.