简体   繁体   中英

Can't use _m_prefetchw intrinsic with gcc/clang -march=native on older Intel CPU?

I ran into this problem while compiling my project with Clang . I want to use the intrinsic function _m_prefetchw for that I included x86intrin.h, but for some reason my flow is not reaching the _m_prefetchw definition. I checked the x86intrin.h header file of Clang and I dont have the __PRFCHW__ defined in order to include prfchwintrin.h although I do have PREFETCHW supported by my PC (I ran coreinfo to know this).

does anyone know why __PRFCHW__ isn't defined although I have PREFETCHW supported?

code example:

#include <x86intrin.h>

int main(){
    int i = 10;
    _m_prefetchw(&i);
    return 0;
}

After running I get the error error LNK2019: unresolved external symbol _m_prefetchw referenced in function main

I dug into my clang include header files and found this in x86intrin.h:

#if !defined(_MSC_VER) || __has_feature(modules) || defined(__PRFCHW__)
#include <prfchwintrin.h>
#endif

And _m_prefetchw is defined in the prfchwintrin.h file.

My processor is Intel Xeon E5-2690, Clang version is 9.0.1.

Manually use -mprfchw to tell the compiler to let you use _m_prefetchw even when compiling for a -march= where prefetchw is only a NOP.

-march=native only includes -mprfchw if it will actually have an effect. See What is the effect of second argument in _builtin_prefetch()? for more details on how compilers "think about" availability of prefetch instructions and CPUID.


Your E5-2690 is a Sandybridge, older than Broadwell which introduced (on the Intel side) real support for PREFETCHW.

Any non-ancient Intel CPUs can run prefetchw as a NOP ( http://ref.x86asm.net/coder64.html#gen_note_NOP_0F0D ), but only Broadwell and later actually advertizes the CPU feature in its CPUID, and only Broadwell and later actually do anything different from a NOP. (AMD CPUs support it as an actual prefetch into Exclusive state ever since 3DNow. introduced it.)

Running as a NOP instead of faulting is apparently necessary for installing 64-bit Windows, so a lot of discussion about "supporting" PREFETCHW revolves around not faulting, rather than its CPUID bit and actually doing anything. For example, comments on Windows 10 64-bit requirements: Does my CPU support PrefetchW? discuss this difference in "support" (as in won't fault) vs. "support" as in actually does something.

This forum thread mentions that P4 Nocona faults on prefetchw , and thus can't install Windows 8.1. But Core2 and later do have "won't fault" support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM