Skip to content

enc.cc,msvc: add clang_rt.builtins for __builtin_cpu_* - #185

Open
jzern wants to merge 1 commit into
webmproject:mainfrom
jzern:msvc-avx2-issue163
Open

enc.cc,msvc: add clang_rt.builtins for __builtin_cpu_*#185
jzern wants to merge 1 commit into
webmproject:mainfrom
jzern:msvc-avx2-issue163

Conversation

@jzern

@jzern jzern commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes link errors related to __builtin_cpu_init and
__builtin_cpu_supports when using clang-cl.exe or clang.exe to
target the Windows ABI.

Issue #163

Fixes link errors related to `__builtin_cpu_init` and
`__builtin_cpu_supports` when using `clang-cl.exe` or `clang.exe` to
target the Windows ABI.

Issue webmproject#163
@jzern

jzern commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Detection in CMake looked something like:

+  if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC" AND
+     CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+    # Add clang_rt.builtins-*.lib for __builtin_cpu_*().
+    if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "X86")
+      target_link_options(sjpeg PUBLIC
+                          "/DEFAULTLIB:clang_rt.builtins-i386.lib")
+    elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "x64")
+      target_link_options(sjpeg PUBLIC
+                          "/DEFAULTLIB:clang_rt.builtins-x86_64.lib")
+    endif()
+  endif()

But this only covered clang-cl, not clang.

@jzern
jzern requested a review from skal65535 September 2, 2026 20:47
@jzern
jzern marked this pull request as ready for review September 2, 2026 20:47
Comment thread src/enc.cc
#else
#pragma comment(lib, "clang_rt.builtins-i386.lib")
#endif
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally non-clang Windows would return false - this will fail to compile on MSVC, I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC (cl.exe) will not have __i386__ or __x86_64__ defined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC (cl.exe) will not have __i386__ or __x86_64__ defined.

It fails to link due to the deficient SSE2 check, mentioned in #163 (comment).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works in the 3 configurations I tested with Visual Studio 2026:

  • -G Visual Studio 18 2026 -T ClangCl (clang-cl.exe)
  • -G Visual Studio 18 2026 (cl.exe, hits the #else)
  • -G Ninja Multi-Config (clang.exe)

So, later some work can be done to add cl.exe support using the __cpuid(ex) and _xgetbv intrinsics like we do in libwebp.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

Comment thread src/enc.cc
#else
#pragma comment(lib, "clang_rt.builtins-i386.lib")
#endif
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

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.

3 participants