Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

r-devulap
Copy link
Member

This pull request improves cross-platform support and CPU feature detection for the x86 SIMD sort library, primarily targeting compatibility with Microsoft's Visual C++ (MSVC) compiler. The changes ensure that both build configuration and runtime CPU feature checks work correctly on MSVC, while maintaining compatibility with other compilers.

Build system compatibility:

  • Updated lib/meson.build to use MSVC-specific compiler flags (/arch:AVX2 and /arch:AVX512) instead of the GCC/Clang flags (-march=...), ensuring correct SIMD instruction set selection on Windows/MSVC. [1] [2] [3] [4]

CPU feature detection abstraction:

  • Added new header lib/x86simdsortcpuid.h to abstract CPU feature detection, using MSVC intrinsics for runtime checks and caching results, while falling back to __builtin_cpu_supports for other compilers.
  • Replaced direct calls to __builtin_cpu_supports and __builtin_cpu_init in lib/x86simdsort.cpp with the new xss_cpu_supports and xss_cpu_init functions/macros, ensuring consistent feature detection across platforms. [1] [2] [3]

Symbol visibility and constructor attribute compatibility:

  • Defined XSS_EXPORT_SYMBOL, XSS_HIDE_SYMBOL, and XSS_ATTRIBUTE_CONSTRUCTOR macros in lib/x86simdsort.h and lib/x86simdsort.cpp to handle symbol visibility and static initialization attributes for both MSVC and non-MSVC compilers. [1] [2]

Code style and correctness:

  • Improved static assertions and formatting in object_qsort for clarity and correctness.

Miscellaneous:

  • Added a blank line at the top of meson.build for style consistency.

@fabiocannizzo
Copy link

fabiocannizzo commented Sep 14, 2025

Using as HEAD: cfe57c4

I fix a few typos:

-#if defined(__INTEL_COMPILER) and !defined(__SANITIZE_ADDRESS__)
+#if defined(__INTEL_COMPILER) && !defined(__SANITIZE_ADDRESS__)
-#elif __GNUC__ >= 8 and !defined(__SANITIZE_ADDRESS__)
+#elif __GNUC__ >= 8 && !defined(__SANITIZE_ADDRESS__)

Then I open the x64 Native Tools Command Prompt for VS2022 and run the following commands from the lib folder:

cl /nologo /c /arch:AVX2 /I../src /std:c++17 x86simdsort-avx2.cpp
cl /nologo /c /arch:AVX512 /I../src /std:c++17 x86simdsort-skx.cpp
cl /nologo /c /arch:AVX512 /I../src /std:c++17 x86simdsort-icl.cpp
cl /nologo /c /arch:AVX512 /I../src /std:c++17 x86simdsort-spr.cpp

The first 2 compile, but the last 2 do not. See compilation logs attached:
x86simdsort-icl.cpp.log
x86simdsort-spr.cpp.log

This is the reason why I fell back to clang with mingw and introduced the C-api in #211.

The cl compiler version I use is: Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35211 for x64

here you mention you could build the library with MSVC. What compiler flags and compiler version do you use?

Hope this helps.

@r-devulap
Copy link
Member Author

cl /nologo /c /arch:AVX512 /I../src /std:c++17 x86simdsort-icl.cpp

4dc9609 fixes this error.

cl /nologo /c /arch:AVX512 /I../src /std:c++17 x86simdsort-spr.cpp

This is specific to _Float16 dispatch, if MSVC doesn't support it, then nothing much we can do about it. I would recommend using meson to build the library though, _Float16 is an optional requirement, meson build will auto detect if your compiler supports it and skips it if it doesn't.

#211 (comment) you mention you could build the library with MSVC. What compiler flags and compiler version do you use?

This is what worked for me:

meson setup  --warnlevel 2 --buildtype release builddir
cd builddir
ninja

@r-devulap
Copy link
Member Author

@fabiocannizzo c1994cc adds a CI job to build with MSVC and that runs successfully. There are a few warnings that we can work on fixing (see #222) but I don't think that's gating the build itself. Does this address your concerns of building with MSVC?

@r-devulap r-devulap requested a review from Copilot September 17, 2025 03:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds Microsoft Visual C++ (MSVC) compiler support for the x86 SIMD sort library on Windows platforms. The changes enable cross-platform compatibility by providing MSVC-specific compiler flags and CPU feature detection while maintaining compatibility with existing GCC/Clang builds.

  • Introduced MSVC-specific compiler flags (/arch:AVX2, /arch:AVX512) to replace GCC/Clang march flags
  • Added cross-platform CPU feature detection abstraction using MSVC intrinsics or GCC builtins
  • Updated build system to conditionally apply compiler-specific configurations

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
meson.build Added compiler argument validation for GCC/Clang and formatting improvements
lib/x86simdsortcpuid.h New header providing cross-platform CPU feature detection abstraction
lib/x86simdsort.h Added MSVC-compatible symbol visibility macros and improved code formatting
lib/x86simdsort.cpp Updated to use abstracted CPU detection functions and MSVC-compatible constructor attributes
lib/x86simdsort-icl.cpp Added MSVC-specific include guard for AVX512 headers
lib/meson.build Replaced conditional compilation with MSVC/GCC compiler flag selection
.github/workflows/c-cpp.yml Added Windows MSVC CI build job and simplified existing test configurations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@@ -1,3 +1,4 @@

Copy link
Preview

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

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

[nitpick] Adding a blank line at the beginning of the file serves no functional purpose and deviates from standard file formatting practices. Consider removing this empty line.

Suggested change

Copilot uses AI. Check for mistakes.

#include <string>
#include <unordered_map>

static std::unordered_map<std::string, bool> xss_cpu_features;
Copy link
Preview

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

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

Using a static global variable in a header file can lead to multiple definition issues when the header is included in multiple translation units. Consider using a function-local static variable or moving this to the implementation file.

Copilot uses AI. Check for mistakes.

Comment on lines +16 to +19
int nIds = cpuInfo[0];
__cpuid(cpuInfo, 1);
bool osxsave = (cpuInfo[2] & (1 << 27)) != 0;
bool avx = (cpuInfo[2] & (1 << 28)) != 0;
Copy link
Preview

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

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

The variables nIds, osxsave, and avx are defined but never used in the function. Consider removing these unused variables to improve code clarity.

Suggested change
int nIds = cpuInfo[0];
__cpuid(cpuInfo, 1);
bool osxsave = (cpuInfo[2] & (1 << 27)) != 0;
bool avx = (cpuInfo[2] & (1 << 28)) != 0;
__cpuid(cpuInfo, 1);

Copilot uses AI. Check for mistakes.

@fabiocannizzo
Copy link

fabiocannizzo commented Sep 17, 2025

@r-devulap : thank you for this work.
I can build with meson using c1994cc
but I cannot compile an executable linking with the import lib of the DLL.
dumpbin /exports x86simdsort.lib
shows no C++ exported symbols.

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.

2 participants