Skip to content

Allow C11 feature: alignas #61

Closed
Closed
@encukou

Description

@encukou

Let's allow the alignas specifier in public headers, as _Py_ALIGN_AS.
Start with the free-threaded build to limit impact.

Right now, this would help PyASCIIObject retain pre-3.14 ABI (which is not guaranteed but was stable for a long time, and was requested in python/cpython#128972), while supporting free-threaded builds (where interned can't be a bitfield).channel.

To retain compatibility with C99 and older C++ on supported platforms (which we don't guarantee), spell this _Py_ALIGN_AS and define it with a monster macro like this (details might change of course):

#ifdef Py_GIL_DISABLED
#   ifndef _Py_ALIGN_AS
#       ifdef __cplusplus
#           if (__cplusplus < 201103L) \
                  && (defined(__GNUC__) || defined(__clang__))
#               define _Py_ALIGN_AS(V) __attribute__((aligned(V)))
#           else
#               define _Py_ALIGN_AS(V) alignas(V)
#           endif
#       elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
#           define _Py_ALIGN_AS(V) alignas(V)
#       elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#           define _Py_ALIGN_AS(V) _Alignas(V)
#       elif (defined(__GNUC__) || defined(__clang__)) \
              && defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L
#           define _Py_ALIGN_AS(V) __attribute__((aligned(V)))
#       elif defined(_MSC_VER)
#           define _Py_ALIGN_AS(V) __declspec(align(V))
#       else
#           define _Py_ALIGN_AS(V) _Alignas(V)
#       endif
#   endif
#endif

(Note that when porting to unsupported platform, users can use -D_Py_ALIGN_AS= with their own spelling.)

PoC (shared with #59): https://github.com/python/cpython/pull/130697/files#diff-c33c79e156be4137aeaaa15bf4b5c7f023a78b6a54abd2d519e7cc88caed2cca

Vote: Allow this for the free-threaded build

Vote: Allow this always

Should we later remove the #ifdef Py_GIL_DISABLED without another vote?
(I'm not looking to do this right now, but if we all agree, the WG won't need to revisit this.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions