Skip to content
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

Expose generic atomicops on Clang #21

Merged
merged 2 commits into from
Sep 18, 2014
Merged

Expose generic atomicops on Clang #21

merged 2 commits into from
Sep 18, 2014

Conversation

edmonds
Copy link
Contributor

@edmonds edmonds commented Sep 14, 2014

The generic atomicops implementation is only exposed if GCC >= 4.7 is
available, but Clang, where the underlying __atomic built-ins are also
available, typically only claims to be GCC 4.2. This causes build
failures when compiling protobuf or the output of protoc's C++ code
generator on an architecture that needs the generic atomicops
implementation with Clang.

Clang has a "c_atomic" extension which can be tested for which almost
does what we want:

C11 atomic operations

Use __has_feature(c_atomic) or __has_extension(c_atomic) to
determine if support for atomic types using _Atomic is enabled.
Clang also provides a set of builtins which can be used to implement
the <stdatomic.h> operations on _Atomic types.

I'm not sure if this guarantees that the GNU atomic builtins (the ones
with the __atomic prefix) are also available, but in practice this
should guarantee that Clang is new enough.

With this change in place, Clang generates several diagnostics when
compiling the generic atomicops implementation. These appear to be bugs
in the generic atomicops implementation and are not Clang-specific.

The macro GOOGLE_PROTOBUF_ARCH_PPC is not used anywhere in the protobuf
source; there is no Power-specific atomics implementation, etc.

Funnily enough, the macro __ppc__ is not actually defined on 32-bit
Power on GCC/Linux, according to the following webpage:

    http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros#POWER

and verified on a 32-bit Debian sid 'powerpc' chroot:

    (sid_powerpc-dchroot)edmonds@partch:~$ gcc -dM -E - < /dev/null | grep -c __ppc__
    0
    (sid_powerpc-dchroot)edmonds@partch:~$ gcc -dM -E - < /dev/null | grep -c __LP64__
    0
The generic atomicops implementation is only exposed if GCC >= 4.7 is
available, but Clang, where the underlying __atomic built-ins are also
available, typically only claims to be GCC 4.2. This causes build
failures when compiling protobuf or the output of protoc's C++ code
generator on an architecture that needs the generic atomicops
implementation with Clang.

Clang has a "c_atomic" extension which can be tested for which almost
does what we want:

    C11 atomic operations

    Use __has_feature(c_atomic) or __has_extension(c_atomic) to
    determine if support for atomic types using _Atomic is enabled.
    Clang also provides a set of builtins which can be used to implement
    the <stdatomic.h> operations on _Atomic types.

I'm not sure if this guarantees that the GNU atomic builtins (the ones
with the __atomic prefix) are also available, but in practice this
should guarantee that Clang is new enough.

With this change in place, Clang generates several diagnostics when
compiling the generic atomicops implementation. These appear to be bugs
in the generic atomicops implementation and are not Clang-specific.
@edmonds
Copy link
Contributor Author

edmonds commented Sep 14, 2014

I should note, this PR is built on top of my previous PR ("Remove GOOGLE_PROTOBUF_ARCH_PPC") because it touches immediately adjacent sources lines in platform_macros.h.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Sep 18, 2014

Thanks. I'll merge this patch in once you sign Google CLA.

@edmonds
Copy link
Contributor Author

edmonds commented Sep 18, 2014

CLA was just submitted.

xfxyjwf added a commit that referenced this pull request Sep 18, 2014
@xfxyjwf xfxyjwf merged commit 5c8ab2c into protocolbuffers:master Sep 18, 2014
@edmonds
Copy link
Contributor Author

edmonds commented Sep 18, 2014

Oops, this causes a build failure when compiling with non-Clang. I'll submit a new PR with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants