-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
implemented alignas pragma #12643
implemented alignas pragma #12643
Conversation
@mratsim since you are working with many types that do have alignment constraints. Would this feature be beneficial for you in any way? Note though allocations and |
Yes, I have 2 very-specific use-cases that need alignment:
The first is actually the most critical for me because I had no workaround besides spraying padding. That was actually one of my complaints in nim-lang/RFCs#160 (comment) (at the very bottom): type
ChannelMpscBounded*[T] = object
pad0: array[PI_CacheLineSize - 3*sizeof(int32), byte]
backLock: Lock # Padding? - pthread_lock is 40 bytes on Linux, unknown on windows.
capacity: int32
buffer: ptr UncheckedArray[T]
pad1: array[PI_CacheLineSize - sizeof(int32), byte]
front: Atomic[int32]
pad2: array[PI_CacheLineSize - sizeof(int32), byte]
back: Atomic[int32] https://github.com/mratsim/weave/blob/796b9cc3/picasso/memory/persistacks.nim#L13-L53 type
Persistack*[N: static int8, T: object] = object
pad: array[PI_CacheLineSize - N*sizeof(ptr T) - sizeof(pointer) - sizeof(int8), byte]
stack: array[N, ptr T]
rawMem: ptr array[N, T]
len*: int8 And even the Nim system needs it: Nim/lib/pure/concurrency/threadpool.nim Lines 56 to 64 in 7e68987
|
I have reviewed looks great. One minor question is why new word |
@mratsim thanks a lot for the feedback. I also marked the two issues to be closed when this PR gets merged. Regarding the @cooldome There are reasons for Line 79 in 7e68987
|
* implemented alignas pragma * fix bootstrap * generate c++ compatible syntax for alignas * Make it work. * Multiple alignof expressions. Implement top level alignof.
Well this implements https://en.cppreference.com/w/c/language/_Alignas for Nim. A special word in
TSpecialWord
was already reserved for this, it was just waiting for the implementation.The difference compared to the C version is, this implementation requires an integer argument, in C a type argument is allowed as well. The reason for this limitation is simple: A solution that would cover imported types (unknown alignment to Nim) would be too much effort, at least for now. For non-imported types it is possible it is possible to align to another type with
alignof
inside ofalignas
:{.alignas(alignof(float64)).}
.This feature is actually needed in Nim itself. During my work on newSeq on Aligned Type I stumbled upon this code:
Nim/lib/system/alloc.nim
Lines 63 to 70 in 7e68987
With this feature the code could be rewritten as following:
The when statement isn't needed anymore to make it work for 32 bit systems. The constant MemAlign could be changed into a constant that can be changed with the "compile time define pragmas", as they are listed in
nim --fullhelp
, and it will work with any alignment value.closes #1930
closes #5315