This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Reserve more physical registers
AbandonedPublic

Authored by Carrot on Aug 23 2022, 10:08 PM.

Details

Reviewers
dmgreen
efriedma
Summary

Reserved physical registers can't be assigned to virtual registers, but they can still be used as ABI required. So we can reserve X8, X16, X17 and X19.

Similarly argument registers can be reserved even if they are used by function call.

With this change we can reserve most of the physical registers, it is much easier to test and debug register allocator.

Diff Detail

Event Timeline

Carrot created this revision.Aug 23 2022, 10:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2022, 10:08 PM
Carrot requested review of this revision.Aug 23 2022, 10:08 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 23 2022, 10:08 PM

My understanding is that X8, X16, X17 and X19 cannot be reserved because the code generator in places will make use of them.
For example, using X19 as a base register in some cases. X16 and X17 are defined by the ABI to potentially be clobbered on function calls & when a veneer needs to be inserted by a linker, it does get clobbered. IIRC, some of the security mitigations implemented in LLVM also clobber these 2 registers on function calls.
I'm not fully sure why X8 cannot be reserved.

In short, I don't think it's a good idea to enable users to reserve these registers as the compiler/toolchain/ABI will not respect their request to reserve these registers.

It seems your motivation for being able to reserve more registers is to be able to more easily write regression test for register allocation.
Are there other ways to achieve that?

A reserved physical register doesn't mean it can't be used by compiler/linker, it just means it can't be used by register allocator, see the comments in TargetRegisterInfo::getReservedRegs(). The most common example is the stack pointer. So when we reserve X8, X16, X17 and X19, it means we can't allocate them to virtual registers, compiler/linker can still use X8 to pass return address, and use X16 and X17 in a veneer.

Allen added a subscriber: Allen.Aug 24 2022, 12:10 AM

See https://reviews.llvm.org/D56305 and https://reviews.llvm.org/D48580 for previous related discussions.
I think it would be helpful to understand the use case for being able to reserve x8, x16, x17 and x19 better.

I want to reserve most of the physical registers, so only a small number of registers can be used by register allocator. Then I can write small test cases to simulate high register pressure situation, it will be much easier to test and debug register allocation changes.

A reserved physical register doesn't mean it can't be used by compiler/linker, it just means it can't be used by register allocator, see the comments in TargetRegisterInfo::getReservedRegs().

The target feature to "reserve" a register is meant to be "don't use this register for anything". For some registers, making sure the register isn't used by the register allocator is sufficient because the compiler/linker won't use them in any other way... but if the compiler uses a register outside register allocation, that doesn't work.

If you want a target feature to simulate high register pressure without any expectation that the resulting code actually works, please add a separate "regalloc-test-reserve-all-regs" feature or something like that.

Carrot abandoned this revision.Aug 24 2022, 12:07 PM

Thanks for the clarification, I will propose another patch for my purpose.

llvm/test/CodeGen/AArch64/arm64-platform-reg.ll