We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5539 - Clang 11 fails to compile a CMake build with conflicting types for _m_prefetchw
Summary: Clang 11 fails to compile a CMake build with conflicting types for _m_prefetchw
Status: RESPONDED
Alias: None
Product: SDL
Classification: Unclassified
Component: build (show other bugs)
Version: HG 2.0
Hardware: x86_64 Windows 10
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-09 12:11 UTC by vladius
Modified: 2021-02-10 18:55 UTC (History)
0 users

See Also:


Attachments
Clang compilation on Windows with support for intrinsics (5.36 KB, patch)
2021-02-10 13:07 UTC, vladius
Details | Diff
Use CPU id intrinsic in Clang (781 bytes, patch)
2021-02-10 16:13 UTC, vladius
Details | Diff
Linker warning fix for Clang (800 bytes, patch)
2021-02-10 18:27 UTC, vladius
Details | Diff
Compose using stringification when Clang is used (789 bytes, patch)
2021-02-10 18:55 UTC, vladius
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description vladius 2021-02-09 12:11:15 UTC
I'm currently trying to compile the latest Hg version with Clang 11 on Windows 10.
I get the following kind of errors during the compilation:

> [build] C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winnt.h:3324:1: error: conflicting types for '_m_prefetchw'
> [build] _m_prefetchw (
> [build] ^
> [build] C:\PROGRA~1\LLVM\lib\clang\11.0.0\include\prfchwintrin.h:50:1: note: previous definition is here
> [build] _m_prefetchw(void *__P)
> [build] ^
> [build] 1 error generated.

Seems like those intrinsics are included from SDL_cpuinfo.h with <mm3dnow.h>.

I also get the massive warnings like:
> [build] clang: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled: -fsanitize=object-size [-Winvalid-command-line-argument]

Thank you.
Comment 1 vladius 2021-02-09 12:37:53 UTC
In SDL_cpuinfo.h it seems like <intrin.h> is not included when __clang__ is defined, as the comment in the file explicitly reads:
"Many of the intrinsics SDL uses are not implemented by clang with Visual Studio"

However, the SDL_endian.h header does include <intrin.h> without any precautions like:
>#ifdef _MSC_VER
>#include <intrin.h>
>#endif

Maybe it should be changed to something like:
>#ifdef _MSC_VER
>#ifndef __clang__
>#include <intrin.h>
>#endif
>#endif
Comment 2 Sam Lantinga 2021-02-09 23:10:57 UTC
Does this fix the bug?
https://hg.libsdl.org/SDL/rev/d8f0192cb8a4
Comment 3 vladius 2021-02-10 12:34:15 UTC
I've already tested the fix myself and apparently it introduces other errors like:

>[build] ../src/libs/sdl/src/video/SDL_blit_A.c:594:5: error: use of undeclared identifier '__m64'
>[build]     __m64 src1, dst1, mm_alpha, mm_zero, mm_alpha2;

Seems like the whole blitting functionality is highly dependent on those intrinsics.

I have actually managed to compile in that scenario with adding one more #undef __SSE3__ to SDL_cpuinfo.h like so:

>/* Many of the intrinsics SDL uses are not implemented by clang with Visual Studio */
>#undef __MMX__
>#undef __SSE__
>#undef __SSE2__
>#undef __SSE3__

It was a bit upsetting, however, that we miss so much optimization potential on a pretty common scenario of Clang + Windows.

There are good news however is that I've managed to successfully Clang-compile and launch the test SDL application by simply renaming that pesky _m_prefetchw in the 'winnt.h' header.

As of now it seems like the only conflicting intrinsic for my scenario and I suppose we could get rid of all of the #if !defined(__clang__) #include <intrin.h>

Hurray! After some time and hiccups, I was now able to produce (compile) an SDL build with intrinsics included, including 3dNOW functionality.

I'm now both in SDL_cpuinfo.h and SDL_endian.h is using a special code snippet, based on Clang's declaration of the needed '_m_prefetch' function. The snippet is enclosed in the same __PRFCHWINTRIN_H macro testing, so that the conflicting header won't be included and won't override the winnt.h's version of the '_m_prefetchw' declaration.

That is of course a hack, but the current behavior is also quite hacky and disables the useful functionality which is quite bad. I will add a patch soon.
Comment 4 vladius 2021-02-10 13:07:47 UTC
Created attachment 4790 [details]
Clang compilation on Windows with support for intrinsics

This now makes it possible to compile SDL with Clang 11 under Windows 10, at least using the CMake pipeline (but possible even the classic way).
The intrinsics are now available and not turned off.
Comment 5 vladius 2021-02-10 16:13:47 UTC
Created attachment 4794 [details]
Use CPU id intrinsic in Clang

With the help of the former patches, it is now possible to use intrinsics even in Clang. Thereby I have removed the additional check for __clang__. This patch also fixes at least one test (testmultiaudio.c) when compiled with Clang.
Comment 6 vladius 2021-02-10 18:27:31 UTC
Created attachment 4796 [details]
Linker warning fix for Clang

Added one more patch for better Clang compatibility. The warning is generated if the linker is invoked with --no-undefined flag. The patches adds an additional condition to suppress that.
Comment 7 vladius 2021-02-10 18:55:44 UTC
Created attachment 4797 [details]
Compose using stringification when Clang is used

Added a patch to stringify __FUNCTION__ under Windows, when using Clang.