-
Notifications
You must be signed in to change notification settings - Fork 163
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
Clarify Atomic type alignment requirements #112
base: master
Are you sure you want to change the base?
Conversation
ping? |
I am not happy with the exception around non-power-of-two-sized atomic types. Given this ABI is relatively new, I have updated the proposed addition so non-power-of-two sized atomic types are padded to the next power-of-two, and then aligned to the same power-of-two. The System V ABI does not define this case. This behaviour allegedly matches the Darwin ABI, not that I can find any documentation on that beyond a comment in the LLVM bug tracker. It seems reasonable to settle on defining this behaviour to avoid incompatibility between code involving atomics, when compiled with different toolchains. |
x86-64 clang, x86-64 gcc, and RV64 GCC all print "4" when running this program:
I'm reluctant to (a) change the ABI for GCC and (b) diverge from what x86-64 GCC and clang are doing here. |
GCC's alignment rules are complicated to write down, but they seem to do reasonable things for my test cases:
Given this is the ABI that already exists, I don't see a compelling reason to change it. |
We fixed the ABI when we upstreamed glibc. Changing it now would be problematic, as this might break existing linux distros. So we should only be documenting the existing (gcc implemented) ABI, not changing it, unless we find a serious problem. We do need compatibility between llvm and gcc though. So I looked at the details of what gcc is doing. The key detail is in gcc/tree.c which does
The tree_atomic_core_type function only looks at the type size, and returns the corresponding integer atomic type if there is one of the same size. So if you declare a struct as atomic, and it happens to have the same size as a supported atomic integer type, then it gets the alignment of that integer type. Otherwise, it gets the normal struct alignment, which is the max alignment of the fields. This code is part of the original patch that added the atomic qualifier support. If clang is implementing atomic structure types differently than gcc, then this isn't a RISC-V problem, it is a generic clang/gcc compatibility issue which needs to be handled elsewhere. Just checking x86 with your atomic-alignment.c testcase, I see that there are x86 gcc/clang differences. It looks like clang has an additional rule, if an atomic struct is not power of 2 sized, then it is padded to the next power of 2. So a struct of size 3 made atomic is given size and alignment of 4 in clang but size 3 align 1 in gcc. This definitely needs to be handled at a higher level. I can start a thread on the gcc mailing lists. |
My understanding of The clang/llvm approach to this seems to be that it should be well defined by the ABI, and they are have so far been unwilling to change until ABI specifications are updated. Unfortunately it also seems that while the darwin ABI has thought about non-power-of-two-sized atomic types, they have failed to write anything down so far (in documents I could find, at least), and have ignored the fact that gcc may or may not be producing code for their system that matches the ABI. Thanks for taking this up on the GCC mailing list, I look forward to seeing the outcome of that discussion, and hope we can find a good compromise. |
Thanks for the link to the llvm bug. The gcc thread is at It looks like the current LLVM solution is better than the current GCC solution, as the LLVM one requires less locking. So I think it is more likely that gcc will change than llvm, but this is going to be a mess. This will probably require a libstdc++ ABI version increment for instance. I'd rather leave the general gcc stuff to other people, and only worry about the RISC-V specific stuff, so this is easier if the x86 ABI is fixed first, but that could be a while. |
This patch documents alignment requirements for atomic types. This issue came up in https://reviews.llvm.org/D57450
Using a small test program, I have deduced the rules for atomic type size and alignment in GCC. There are more details about how I did this in a comment on that review, which I quote below (
MaxAtomicPromoteWidth
is a target-specific configuration option in Clang, which controls changing the alignment of atomic types based on their size):