Skip to content

[C++] Use existing library for CPU detection #45331

Closed

Description

@pitrou

Describe the enhancement requested

We currently use our own home-grown module for detecting CPU features, but it mainly supports x86, which implies that for now we cannot do dynamic dispatch on non-x86 CPUs (for example ARM where we might want to detect ARM or even SVE support at runtime).

We could probably reuse https://github.com/google/cpu_features instead. It doesn't seem to have any dependencies, and could be bundled as a private static library.

This was already proposed years ago as #16715

Component(s)

C++

Activity

  1. pitrou commented on Jan 22, 2025

    @pitrou
    MemberAuthor

    Looking at https://github.com/google/cpu_features/blob/main/include/cpuinfo_aarch64.h , it's not obvious to me how we could get the supported SVE vector widths. Do you have any idea @cyb70289 ?

  2. pitrou commented on Jan 22, 2025

    @pitrou
    MemberAuthor

    Edit: apparently there's an intrinsic to get the vector width: https://lemire.me/blog/2022/11/29/how-big-are-your-sve-registers-aws-graviton/

  3. kou commented on Jan 22, 2025

    @kou
    Member

    +1

  4. cyb70289 commented on Jan 23, 2025

    @cyb70289
    Contributor

    +1

  5. wgtmac commented on Jan 23, 2025

    @wgtmac
    Member

    The library itself looks good. My main concern is the slowdown of its release cadence: https://github.com/google/cpu_features/releases

  6. pitrou commented on Jan 23, 2025

    @pitrou
    MemberAuthor

    Hmm, that's a good point. Maintenance also seems to have slowed down a bit: https://github.com/google/cpu_features/graphs/contributors?from=20%2F01%2F2024

  7. pitrou commented on Jan 23, 2025

    @pitrou
    MemberAuthor

    There's also https://github.com/anrieff/libcpuid , which only supports x86 and ARM but exposes a lot of information.

  8. pitrou commented on Jan 23, 2025

    @pitrou
    MemberAuthor

    And by the way, xsimd has a more limited functionality for CPU feature detection: https://xsimd.readthedocs.io/en/latest/api/arch.html . It only supports x86 and ARM, does not offer detailed CPU information, does not expose things like BMI2 etc.

  9. h-vetinari commented on Jan 23, 2025

    @h-vetinari
    Contributor

    For completeness, you might be interested in https://github.com/archspec/archspec - it supports x64, aarch, ppc & risc, plus a lot of meta-information about the (partial) inclusion ordering of the various CPUs in terms of feature sets.

    I think it doesn't have a C/C++ API though, so this is probably a long shot (slow to call python from C++), but OTOH, querying CPU features is not a common occurrence (once per session...?) and could be cached.

    Numpy does it the other way around, detecting individual CPU features, rather than processor models, with a result à la:

    Supported SIMD extensions in this NumPy install:
        baseline = SSE,SSE2,SSE3
        found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
        not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL
    

    It's implemented in C, but I don't think it has a public API.

  10. pitrou commented on Jan 23, 2025

    @pitrou
    MemberAuthor

    @h-vetinari Well, we don't want the core Arrow C++ library to depend on the existence of a Python interpreter and Python libraries. As for the NumPy C code, since it's internal it's probably not trivial to vendor, and I suppose its APIs have no stability guarantee either.

  11. h-vetinari commented on Jan 23, 2025

    @h-vetinari
    Contributor

    Yeah, I understand. That's why I said "for completeness". :)

  12. pitrou commented on May 6, 2025

    @pitrou
    MemberAuthor

    The library itself looks good. My main concern is the slowdown of its release cadence: https://github.com/google/cpu_features/releases

    For the record, a new release was just made (v0.10.0).

  13. pitrou commented on Jul 15, 2025

    @pitrou
    MemberAuthor

    Also cc @AntoinePrv

  14. AntoinePrv commented on Jul 15, 2025

    @AntoinePrv
    Collaborator

    My 2c as not an arrow maintainer: if we want to use more of xsimd in arrow, it could be relevant to extend support for what we lack in xsimd. It would make static/dynamic dispatch of xsimd code in arrow more convenient as opposed to requiring a conversion layer and another dependency.

  15. pitrou commented on Jul 15, 2025

    @pitrou
    MemberAuthor

    Perhaps, but some information such as cache sizes might be out of scope for xsimd anyway.

  16. pitrou commented on Jul 17, 2025

    @pitrou
    MemberAuthor

    So, IMHO, we have two reasonable options:

    1. Use xsimd. Pros: header-only, already a dependency. Cons: might not have all desired functionality, so we'd either need to add them to xsimd (if that's ok to the xsimd maintainers) or keep maintaining code for it in Arrow.
    2. Use Google's cpu-features. Pros: it's more featureful. Cons: it's an additional dependency, and requires a compilation step as it's not header-only; therefore it will some complication to the build system.

    @AntoinePrv Do you want to do a bit more research to find out which functionality is needed in xsimd and whether it can be added there? (and feel free it to contribute it of course :))

  17. AntoinePrv commented on Apr 10, 2026

    @AntoinePrv
    Collaborator

    Update on this, the current idea discussed with @pitrou is to use xsimd (already a dependency).

    We've done some work on xsimd to match Arrow CPU detection needs (excluding cache size, but that is a separate topic and apparently only used for tests at the moment). There is a good basis to easily add more feature as needed.

  18. added a commit that references this issue on Jun 3, 2026
  19. added a commit that references this issue on Jun 10, 2026
  20. added this to the 25.0.0 milestone on Jun 10, 2026
  21. pitrou commented on Jun 10, 2026

    @pitrou
    MemberAuthor

    Issue resolved by pull request 49940
    #49940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions