Skip to content
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

Specify a platform reserved register #370

Closed
appujee opened this issue Mar 20, 2023 · 20 comments
Closed

Specify a platform reserved register #370

appujee opened this issue Mar 20, 2023 · 20 comments

Comments

@appujee
Copy link

appujee commented Mar 20, 2023

ARM reserves x18 for platform but RISC-v doesn't specify a platform register.

https://developer.arm.com/documentation/den0024/a/The-ABI-for-ARM-64-bit-Architecture/Register-use-in-the-AArch64-Procedure-Call-Standard/Parameters-in-general-purpose-registers

Recent changes in llvm has reserved the same x18 for Android and Fuschia platforms to align with the name w.r.t. ARM spec. But x18 may not be an ideal choice for RISC-V because:

  • it is the 3rd callee saved register
  • -msave-restore does not interact well if x18 gets reserved.

Some discussion has been conitnuing here: google/android-riscv64#72 but it would be ideal to have a spec or at least a recommendation on choice of platform registers and trade-offs around that

In the current setup x27 appears to be a better choice as this would reduce:

  • interaction with -msave-restore
  • amount of work in assembler code out there. X18 is used in more places (3rd callee saved register) than X27.
@kito-cheng
Copy link
Collaborator

kito-cheng commented Mar 20, 2023

It would be little bit later to reserve a reserved register for RISC-V since the ABI already used for a while, so adding one general reserved register would be annoying to the current ecosystem especially for the linux distro guys...

However here is an evil thought in my mind, GP relaxation might not very useful on non-baremetal environment so we might use that as special use for fuchsia and Android other than GP relaxation, that come with one more advantage is: all existing code base didn't use gp register in program including Assembly codes.

@aswaterman
Copy link
Contributor

Android can have a different ABI that reserves a register--perhaps x3 as @kito-cheng mentioned, perhaps something else. Changing the existing Linux ABI to reserve a register is a non-starter.

@nick-knight
Copy link
Contributor

As I understand it, this issue is not a request to change the Linux ABI, but to solicit recommendations on minimally disruptive specializations of it. It is understood that this would result in a new, incompatible ABI. Aside from the technical question, there is also the procedural question of how/where to standardize this. I think my suggestion to @appujee to jump-start the discussion here was misguided, and it would be better to start at sig-toolchains.

FWIW, I think the Android folks would be perfectly happy with a nonnormative note in the psABI --- e.g., "variant ABIs that add a platform register are encouraged to throw away GP-relaxation and use x3 for this purpose" --- so they could just point to this to push their LLVM patches through. Of course, it'll eventually be necessary to have an actual spec.

@aswaterman
Copy link
Contributor

Please quantitatively evaluate the value of the gp register in this context before presupposing that throwing it away is the right thing to do.

@jrtc27
Copy link
Collaborator

jrtc27 commented Mar 20, 2023

https://reviews.llvm.org/D84414 is the review that contains the x18 choice; see my various comments there on this issue. I agree x18 is entirely unprincipled and wish there had been more willingness to think carefully about what register made sense for SCS.

@appujee
Copy link
Author

appujee commented Mar 20, 2023

I think my suggestion to @appujee to jump-start the discussion here was misguided, and it would be better to start at sig-toolchains.

sgtm, i'll start the discussion on sig-toolchain.

FWIW, I think the Android folks would be perfectly happy with a nonnormative note in the psABI --- e.g., "variant ABIs that add a platform register are encouraged to throw away GP-relaxation and use x3 for this purpose" --- so they could just point to this to push their LLVM patches through. Of course, it'll eventually be necessary to have an actual spec.

Yes. It is better to have something that one can point to.

@appujee
Copy link
Author

appujee commented Mar 20, 2023

https://reviews.llvm.org/D84414 is the review that contains the x18 choice; see my various comments there on this issue. I agree x18 is entirely unprincipled and wish there had been more willingness to think carefully about what register made sense for SCS.

agreed with your concerns there. Let me start a discussion on sig-toolchain to address this.

@appujee
Copy link
Author

appujee commented Mar 20, 2023

@ilovepi
Copy link
Contributor

ilovepi commented Mar 20, 2023

I've proposed a change in LLVM doing this, since it would be better to do sooner rather than later. Thankfully the changes are minimal, and there are not likely to be too many users depending of RISCV SCS. https://reviews.llvm.org/D146463

@appujee
Copy link
Author

appujee commented Mar 21, 2023

FYI: Anders Berg mentioned in https://lists.riscv.org/g/sig-toolchains/topic/97736679#544 :

This will and should be covered by one of the upcoming ABI-breaking features where one can reserve some registers for platform related usages. A proposal is on its way in the psABI TG (hopefully during this spring). So please do not rush away with some dirty fix implementations (at least not yet anyway)!

@jrtc27
Copy link
Collaborator

jrtc27 commented Mar 21, 2023

FYI: Anders Berg mentioned in https://lists.riscv.org/g/sig-toolchains/topic/97736679#544 :

This will and should be covered by one of the upcoming ABI-breaking features where one can reserve some registers for platform related usages. A proposal is on its way in the psABI TG (hopefully during this spring). So please do not rush away with some dirty fix implementations (at least not yet anyway)!

That work has been promised for over a year at this point; blocking changing the shadow call stack ABI on that is not reasonable in my opinion, especially given it is already implemented and in use, just with a potentially bad choice of register. If and when the EABI deviations/whatever it's called these days finally turns up it can be adopted for annotating such binaries to prevent incompatible objects being mixed.

@appujee
Copy link
Author

appujee commented Mar 21, 2023

makes sense. I've approved the patch https://reviews.llvm.org/D146463 by @ilovepi. Do review when you can.

@asb
Copy link
Collaborator

asb commented Mar 21, 2023

Oh I wasn't sure if Anders was referring to the EABI stuff or not. If so, I'm not completely sure it's relevant to this request, but it's hard to tell until there's a full proposal to review.

I agree there shouldn't be too many SCS users and it would be good to change sooner rather than later. I think we should make a little more effort to check for downstream users who may be affected though.

@jrtc27
Copy link
Collaborator

jrtc27 commented Mar 21, 2023

Well, the EABI stuff has become the same as the ABI deviations stuff, so yes and no.

@kito-cheng
Copy link
Collaborator

#371 created for the possibility of using GP, that's also discussed during the EABI/ABI deviations stuffs, so I think sooner or later we will need that.

@ilovepi
Copy link
Contributor

ilovepi commented Mar 28, 2023

We do not want to slow this process down, because it is important that we come to a resolution as soon as possible, but we do want to raise these concerns now, while there is still time to take them into account.

Ideally, projects that place a premium on code size wouldn’t have to forgo code size savings from global relaxation if they want to use SCS, or make use of the platform register. We see no fundamental reason why users in the embedded space who care deeply about code size would not fall into this category, and it would be nice if they could also make use of the feature without being required to drop global relaxation.

As mentioned in the sig-toolchain meeting and the ps ABI issues, there are many benefits to using gp, but before we make a choice we should be sure that we are explicit about the tradeoffs are why we believe they are worth making.

@kito-cheng
Copy link
Collaborator

kito-cheng commented Mar 29, 2023

@ilovepi thanks for the comment, SCS is kind of special is that eventually will break the ABI (for RISC-V) since that require one extra reserved register, so that give we few more freedom to having more option than other ABI issues.

Of cause the why it become an issue that must did a ABI breakage change is we didn't specify a platform register at beginning, but anyway the boat sailed.

So that's back to the SCS first, I think we have three options:

  • Pick a GPR as SCS
  • Re-define gp to allow that use other than gp relaxation.
  • Use Zisslpcfi extension, that provide dedicated instruction and CSRs to implement SCS.

Pick a GPR as SCS

There are actually two candidate during the discussion x18 and x27, but x18 is kind of many potential issues, so now we are discussion the other candidate, and then x27 has purposed.

x27 obviously better than x18 just like @appujee has listed on the first post.

but it's still has low probability might screwed up in some asm code since it was not reserved before, so I am not prefer to pick up a non-reserved register if possible.

Re-define gp to allow that use other than gp relaxation.

Already listed several reason in #371, so not duplicate here, so just jump to the concern @ilovepi raised, what if user want SCS and gp relaxation, I think it's the most potential drawback of this proposal.

GP Relax No GP relax
SCS Enable NOT OK OK
SCS disable OK OK

What's the possible solution if people want more code size saving AND SCS?

  • Use Zisslpcfi, that would be most simple but require HW support.
  • GlobalMerge optimization pass from LLVM*, it could archive similar optimization like gp relaxation, but for local, that should be able to improve by LTO.
  • Proposal around the EABI/deviations: using tp as gp (this can be only apply on those system not require thread)

NOTE: * GCC has similar stuffs but called section anchor optimization.

Use Zisslpcfi extension, that provide dedicated instruction and CSRs to implement SCS.

Okay, that should be everyone happy, no extra reserved register needed since the extension has provide dedicated one, the only issue is that require HW has implement that.

Compare

Just drop Zisslpcfi from the comparison table since it's not the point in this thread.

Option 1 Option 2
Used in existing asm code? Yes *1 No
Impact code size Yes, but very minor Yes *2
Break ABI Hard break Soft break compare to option 1

So based on the comparison table and several reason listed in #371, I believe we should go with option 2

*1: google/android-riscv64#78 Android have an issue to tracked where has use the SCS register candidate.
*2: We have some compiler optimization to make up the gap like GlobalMerge optimization, and be noted there is no loss on those system already disable GP relaxation.

@ilovepi
Copy link
Contributor

ilovepi commented Mar 29, 2023

@kito-cheng , Thank you for the summary. I'm pretty confident at this point that we will adopt gp as the platform register, for all the reasons you listed above, in #371, and brought up in sig-toolchain. There are some niceties for using s11, but I think they are outweighed by the benefits of using gp.

As you mention, there may be compiler side work we can do to ameliorate the situation to some extent(i.e., GlobalMerge in LLVM or its GCC equivalent). I think that's probably something we all may want to investigate more: how our toolchains can close any remaining gaps for the tradeoffs we make in the ABI.

@MaskRay
Copy link
Collaborator

MaskRay commented Mar 30, 2023

Note that if the register gp is reserved as a platform register in the platform-independent ABI, it is still possible to reserve one additional platform register in a platform ABI specification. In other words, reserving gp does not preclude also reserving s11 in a platform ABI specification.

ilovepi added a commit to llvm/llvm-project that referenced this issue Apr 12, 2023
ShadowCallStack implementation uses s2 register on RISC-V, but that
choice is problematic for reasons described in:

https://lists.riscv.org/g/sig-toolchains/message/544,
riscv-non-isa/riscv-elf-psabi-doc#370, and
google/android-riscv64#72

The concern over the register choice was also brought up in
https://reviews.llvm.org/D84414.

https://reviews.llvm.org/D84414#2228666 said:

```
  "If the register choice is the only concern about this work, then I think
  we can probably land it as-is and fixup the register choice if we see
  major drawbacks later. Yes, it's an ABI issue, but on the other hand the
  shadow call stack is not a standard ABI anyway.""
```

Since we have now found a sufficient reason to fixup the register
choice, we should go ahead and update the implementation. We propose
using x3(gp) which is now the platform register in the RISC-V ABI.

Reviewed By: asb, hiraditya, mcgrathr, craig.topper

Differential Revision: https://reviews.llvm.org/D146463
@kito-cheng
Copy link
Collaborator

Closed via #371

flemairen6 pushed a commit to Xilinx/llvm-project that referenced this issue May 10, 2023
ShadowCallStack implementation uses s2 register on RISC-V, but that
choice is problematic for reasons described in:

https://lists.riscv.org/g/sig-toolchains/message/544,
riscv-non-isa/riscv-elf-psabi-doc#370, and
google/android-riscv64#72

The concern over the register choice was also brought up in
https://reviews.llvm.org/D84414.

https://reviews.llvm.org/D84414#2228666 said:

```
  "If the register choice is the only concern about this work, then I think
  we can probably land it as-is and fixup the register choice if we see
  major drawbacks later. Yes, it's an ABI issue, but on the other hand the
  shadow call stack is not a standard ABI anyway.""
```

Since we have now found a sufficient reason to fixup the register
choice, we should go ahead and update the implementation. We propose
using x3(gp) which is now the platform register in the RISC-V ABI.

Reviewed By: asb, hiraditya, mcgrathr, craig.topper

Differential Revision: https://reviews.llvm.org/D146463
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue Aug 21, 2024
ShadowCallStack implementation uses s2 register on RISC-V, but that
choice is problematic for reasons described in:

https://lists.riscv.org/g/sig-toolchains/message/544,
riscv-non-isa/riscv-elf-psabi-doc#370, and
google/android-riscv64#72

The concern over the register choice was also brought up in
https://reviews.llvm.org/D84414.

https://reviews.llvm.org/D84414#2228666 said:

```
  "If the register choice is the only concern about this work, then I think
  we can probably land it as-is and fixup the register choice if we see
  major drawbacks later. Yes, it's an ABI issue, but on the other hand the
  shadow call stack is not a standard ABI anyway.""
```

Since we have now found a sufficient reason to fixup the register
choice, we should go ahead and update the implementation. We propose
using x3(gp) which is now the platform register in the RISC-V ABI.

Reviewed By: asb, hiraditya, mcgrathr, craig.topper

Differential Revision: https://reviews.llvm.org/D146463
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants