[ARM32/ARM64] Introducing swap operation without temporary register
As you already know, there is a popular XOR swap which can exchange two values without temporary storage.
// XOR swap // Swap using temporary storage
X := X XOR Y T := X
Y := Y XOR X X := Y
X := X XOR Y Y := T
I just come up with some swap operations in ARM32 legacy backend which allocate a temporary register for swap operation and some swap operation in ARM64 codegen (some of them are NYI).
I'm going to exploit this XOR technique, especially for legacy backend to reduce register pressure where LSRA is not available. Of course, it's not always better when considering ILP, but removing temporary register may give us the freedom of implementation and design.
In addition, both A32 and A64 ISA from ARMv8 are lack of swap instruction such as xchg in x86 and x64.
FYI. ARMv8.1 introduces CAS and SWP related instruction but I think current coreclr only supports ARMv8 by now.
In short, I think XOR swap can be a candidate design choice for legacy backend of ARM32 to reduce register pressure where LSRA is not available. For ARM64, this should be considered carefully, because of ILP in ARMv8 and LSRA of RyuJIT backend can address register pressure problem.
I will make a PR for simple case. What do you think of this idea?
category:cq theme:register-allocator skill-level:intermediate cost:medium
Note that XOR swap won't work on object references and managed pointers, the intermediary X it produces can't be tracked by GC.
@mikedn XOR swap won't work on object references and managed pointers in CIL(ex. C# code) level. @hqueue 's idea is in machine code (generated by JIT compiler) level.
XOR swap won't work on object references and managed pointers in CIL(ex. C# code) level. @hqueue 's idea is in machine code (generated by JIT compiler) level.
The GC doesn't care about CIL, it cares about what the generated machine code does. If you have 2 object references in 2 registers and want to swap them it won't work with a XOR swap.
Well, maybe it will work if you prevent GC from running during the XOR swap.
@mikedn You are right, I understand your concern and we should not apply this swap for variable containing addresses if GC should track X defined in 1st staement and Y defined at 2nd statement.. BTW I've some questions about GC in coreclr, because I don't know GC in coreclr yet. How does GC know which registers or memory(stack, heap) location contain address at runtime ? (conservative GC, precise GC?) Is there any information maintained and JIT should update the information?
How does GC know which registers or memory(stack, heap) location contain address at runtime ? (conservative GC, precise GC?) Is there any information maintained and JIT should update the information?
Yes, the JIT emits information for the GC, see gcinfo.cpp in the JIT source directory. There's a bit of info in the RyuJIT doc's GC Info section.
CC @sdmaclea
I'm going to close this issue if there is no more feedback, since PR was already merged long time before and there is no change regarding GC traking for references in this case.
@gkhanna79 Thanks. I looked at patch dotnet/coreclr#6274 which implemented this feature for ARM32. Looks like it only applies to Arm32 on legacy code gen. I was assume you mentioned me to look at the GC issues this might have introduced on arm64. However I see that you added the enhancement tag above. Should an issue like this be opened for RyuJit enhancement for arm & arm64
Yes, I CCed you @sdmaclea incase there needs to be similar work that needs to be done for Arm64 Linux and if so, do file appropriate issues for it.
@BruceForstall Can we close this or leave it open for RyuJIT enhancement ?
@hqueue seems reasonable to leave open to track for RyuJIT enhancement.
LSRA generates GT_SWAP nodes to generate register swaps, if necessary, during block resolution, for integer registers only, for x86/x64 only. See resolveEdge() and insertSwap(). Otherwise, it spills to stack then reloads from stack. It seems we could extend GT_SWAP to use the 3-instruction XOR code on arm32/arm64 for non-GC-ref pointers, using the model previously implemented for legacy arm32 back-end: https://github.com/dotnet/coreclr/pull/6274.
Swaps like this are expected to be relatively rare, so it's not clear how much this would buy us.
There is a bunch of code in codegenarm64.cpp for genCodeForSwap() which is a copy of the xarch code, with an NYI. So we should either implement it or decide not to and remove this code.
LSE atomic also added a swp instruction to arm64. So at least on some newer arm64 systems this will become available.
@echesakovMSFT Looks like the JIT has an implementation of SWP for arm64; maybe we should use it when available?
cc @kunalspathak
@echesakovMSFT Looks like the JIT has an implementation of SWP for arm64; maybe we should use it when available?
I thought this proposal was about swapping two registers without requiring a temporary one. swp instruction on arm64 does
swap in memory.
Ah, ok.
swp support Armv8.1-a not support Armv8-a if i want to do swap on Armv8-a. how to do that?
This issue is outdated. If we want to pursue this further, please open a new issue and telling a use case when it will benefit.