This is an archive of the discontinued LLVM Phabricator instance.

[clang][X86] Add __cpuidex function to cpuid.h
Needs ReviewPublic

Authored by aidengrossman on Aug 19 2023, 1:40 PM.

Details

Summary

MSVC has a __cpuidex function implemented to call the underlying cpuid
instruction which accepts a leaf, subleaf, and data array that the output
data is written into. This patch adds this functionality into clang
under the cpuid.h header. This also makes clang match GCC's behavior.
GCC has had __cpuidex in its cpuid.h since 2020.

Diff Detail

Event Timeline

aidengrossman created this revision.Aug 19 2023, 1:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2023, 1:40 PM
aidengrossman requested review of this revision.Aug 19 2023, 1:40 PM
Herald added a project: Restricted Project. · View Herald Transcript

This is an updated version of https://reviews.llvm.org/D150646 that uses __has_builtin instead of checking for a definition of _MSC_EXTENSIONS. This fixes the cases that I have received so far and shouldn't cause any other issues.

There is one thing that needs some discussion: include order relating to intrin.h. When using a MSVC target triple with -fms-extensions (and maybe in other cases), intrin.h needs to be included to get the declaration of the built-in (or otherwise __has_builtin won't detect it and it will error out later). This means that the way the patch is setup currently intrin.h needs to be included before cpuid.h. The only way I see to fix this is to use the MSVC __cpuidex signature which drops the static keyword. This makes the implementation slightly different from the GCC implementation (where they include static), but it would allow us to declare the function outside of the preprocessor conditional in cpuid.h which would make cpuid.h include-order-agnostic again with respect to intrin.h.

aidengrossman edited the summary of this revision. (Show Details)Aug 19 2023, 1:48 PM
aidengrossman edited the summary of this revision. (Show Details)

@eandrews tested this patch out internally at Intel and it seems to work well for us (didn't break the internal tests that we had troubles with before). I think the changes here look reasonable, but I leave it to the original reviewers of https://reviews.llvm.org/D150646 to do the final sign-off.