[RyuJIT/ARM32] Reduce arm32/arm64 code duplication
As new code is implemented for RyuJIT/ARM32, it is mostly implemented by copying from lsraarm64.cpp to lsraarm.cpp and codegenarm64.cpp to codegenarm.cpp, and modifying a few lines to make it work for ARM32. This is creating a huge amount of almost-identical code between the two platforms. This is undesirable.
Given that ARM32 and ARM64 are so similar in so many ways, I suggest we change from this model of separate files for each, and instead share back-end files, with appropriate per-platform #ifdefs as necessary.
There are two options here:
- Share all files: merge lsraarm64.cpp/lsraarm.cpp, merge codegenarm64.cpp/codegenarm.cpp. (I suggest using the non-64 name for simplicity.)
- Create a set of shared files: lsraarmarch.cpp, codegenarmarch.cpp (analogous to the
*xarch*files), but allow the per-architecture files to remain, for use in cases where the two architectures don't have much in common and using#ifdefsto distinguish them would be too complicated.
I propose going with (1), assuming there isn't too much code that can't be easily shared.
Note that now it is easy to do arm64 assembly diffs using the jit-diff tool, on Windows using the arm64 protononjit.dll compiler, as follows:
set COMPlus_AltJitNgen=*
set COMPlus_AltJitName=protononjit.dll
// build x64
jit-diff diff --arch x64 <other arguments>
This allows anyone to verify that a change to common arm32/arm64 code does not create any changes to generated assembly for arm64, even if they don't have access to the still-private Microsoft arm64 SDK.
Comments?
Related to dotnet/runtime#7099
@dotnet/arm64-contrib @dotnet/jit-contrib @YongseopKim @chunseoklee @hseok-oh @hqueue @mskvortsov @sjsinju @alpencolt
@BruceForstall
- The current separation allows relative certainty that a code change could not have affected the other platform. Until both platforms are stable and CI is accessible it may be to early to do this merge. Arm64 CI has been broken for two weeks. That would mean no arm32 change could have been merged over the same period. Over the last few month arm32 has seen similar outages.
- I currently do not use Windows for CoreCLR development. Forcing dependence on Windows tools may be problematic for arm32 and arm64 developers.
- Merging creates an additional testing burden for all arm32 and arm64 changes.
- There is also a lot of duplication between arm64 and amd64 also. Many times the fix for an arm64 issue is copying missing code from amd64.
- One of my complaints against arm64 code is it seems to be incorrectly using arm32 ABI in some places. This type of defect is more likely to creep in with merging files.
Based on that I would tend to prefer option (2). The common code from all platforms can be migrated up the common hierarchy.
Looking to codegenlegacy.cpp and morph.cpp I think 2nd option is better. It is quite difficult to understand and support such code, especially when you new in project. #ifdef is good for checking features.
But any way, an imperfect solution is generally better than not having any solution at all.
Understanding of common and different things in both architectures same may help make the right decision. Do you how to check it quick?
+@Dmitri-Botcharnikov
@sdmaclea You are correct that doing this merge increases the testing burden. I am ok with that. The alternative is building up a backlog of "cleanup" merging work that is less likely to happen, and is hard to get right.
Having a flaky CI system is quite unfortunate. Hopefully that will improve; there are resources on it.
I left out a slightly selfish reason for wanting to merge, which is the current situation makes it difficult, time consuming, and error prone for me to do code reviews, as I end up doing essentially a manual comparison of new (arm32) and old (arm64/amd64) code to see what is different.
@alpencolt You write:
Understanding of common and different things in both architectures same may help make the right decision. Do you how to check it quick?
I don't understand what you are asking. Can you ask again?
@BruceForstall I agree that there are bunch of common code between arm32 and arm64 and I understand that it's inefficient to review new arm32 implementation in current code structure. However I also agree with @sdmaclea and @alpencolt.
I think the purpose of this refactoring should be "to ease implementing and reviewing arm32 port" and we should make decision considering all the concerns.
If we really need this refactoring, I prefer 2nd option, because there are difference in register use and instruction set , esp. non-integer cases which requires more complex architecture-specific implementation.
I think all of us may agree that refactoring itself require some time and efforts and it may not be easy to merge implementation when arm32 is still incomplete. What do you think of it ?
it may not be easy to merge implementation when arm32 is still incomplete. What do you think of it ?
I think it will be much easier to develop arm32 with sharing / refactoring of arm64 in mind. If we continue developing as today, we will end up with essentially duplicate copies of everything and merging them later will be very difficult. So, I think we must merge them now and establish a sharing framework now, A judgement call will always need to be made between (1) sharing a function entirely, (2) sharing a function with #ifdefs, (3) sharing a function with some other sharing mechanism, such as architecture-specific callbacks, or (4) not sharing functions, because the two architectures are too different.
@BruceForstall, let's look to CodeGen::genCodeForArrOffset(). This methods for ARM and ARM64 almost the same except few line code where we call emitter. It's better to use #ifdef statements than create callback there. But what about others? I want to estimate how many such cases are there. Also good to know how many methods are different more than few line of emitting. I guess no one wants to look into the diff and I want to find the way how to do it without spending hours and days to research.
@alpencolt One example is your GT_LCLHEAP implementation (https://github.com/dotnet/coreclr/pull/10609). The code is very similar between arm32 and arm64. Ideally, this would have already been in a common place, then your change would be easily code reviewed just by looking at the newly added #ifdefs, for instance.
I'm agree with you. The code of legacy backend also quite similar, but when I look to codegenlegacy.cpp it isn't easy to understand what code will be generated for each platform. In case of using callback readability will no be increased dramatically but code from other platforms will not disturb to read.
Basically fixed. There is room for more improvement, such as merging parts of emitarm.cpp/emitarm64.cpp, or moving some things from codegenarm* to codegencommon.cpp.