r/cpp • u/bloon_block • Jun 06 '22
Clang for Windows
Is Clang still the optimal compiler for Windows? And if so then why is that the other windows compilers aren't as good?
17
u/AA11BB22c Jun 07 '22
I switched between MSVC and Clang in Visual Studio.
Clang generally generates faster code compared to MSVC (and a significant amount, potentially 25% faster... 25% is just an example, it varies... scalar or vectorized code).
Clang exposes more/better codegen/optimization options: control target ISA, optimize for a specific architecture, control inlining threshold, etc. For example, MSVC allows you to use AVX512 even without specifying the AVX512 support option ... which will cause the program to terminate if AVX512 is not supported (also generates non-optimal code for AVX512 since AVX512 wasn't specified). Also, MSVC AVX2 assumes FMA support and AVX512 assumes AVX512F, AVX512CD, AVX512BW, AVX512DQ, and AVX512VL.
Clang has better warnings imo.
However, there are a few problems with Clang (compared to MSVC).
Poor debugging experience.
Higher chance of misoptimization (from experience).
Have to go through the hassle of building the latest version of Clang and then test whether it works with Visual Studio (Visual Studio usually doesn't provide the latest version).
24
u/STL MSVC STL Dev Jun 07 '22
Visual Studio usually doesn't provide the latest version
Note that VS tries to provide the latest version of Clang/LLVM in a timely manner, but we need to wait for the final release version, do installer work and run testing, and then we're subject to the latency in the VS release pipeline (after a change is merged, it takes a while before it appears in a VS Preview, and then more time before it's available for production).
So when VS is behind the latest Clang, this is why. Unless something exceptional has happened (some major regression or whatever), it will generally catch up in short order.
MSVC's STL supports Clang/LLVM as a first-class citizen but we test the version that ships in VS.
1
u/Ameisen vemips, avr, rendering, systems Jun 26 '22
Out of curiosity, since clang-cl follows the same rule, why does MSVC not allow inline assembly with x64 targets? This has been a huge headache in one (two, actually) of my projects, and clang-cl obviously doesn't help.
1
u/STL MSVC STL Dev Jun 27 '22
I don't know for sure. (That decision was made a long time ago - longer than my 15-year tenure - and "far away" in the compiler team.) My vague understanding is that inline asm makes the compiler's job much harder because it can't reason about the inputs and outputs at all, whereas with intrinsics the compiler retains control and can still reason about code and potentially optimize it. Thus while x86 asm had to be supported for legacy reasons, when x64 was added, this was intentionally not allowed.
0
Jun 07 '22
[deleted]
1
u/tarranoth Jun 17 '22
Why are you using /clr flag? This is intended if you want to use your native dll with some dotnet dlls usually. I'd look into what std version you are compiling on first and check the header directories of the msvc toolset that you are using.
1
u/Ameisen vemips, avr, rendering, systems Jun 26 '22
I wonder why nobody has added C++/CLI support to clang.
9
u/pjmlp Jun 07 '22
4 - Doesn't work out of the box with traditional Windows development workloads (C++/CLI, MFC, ATL, COM tooling, .NET runtime, DDK, WDK,..)
1
u/Pzyche_ Jan 13 '25
Im new to tech and im trying to use clang as people say its much better now but im just wondering how did u change it? Did u change the json "cl.exe" to "clang-cl.exe" ?
1
u/ack_error Jun 07 '22
The explicit targeting is both a blessing and a curse. It helps generate better non-intrinsics code, but it can also get in the way since you can't vary the ISA target of the attribute. I had a template routine generating both SSE2 and SSSE3 variants via if constexpr off a template parameter and couldn't figure out how to adapt it for Clang.
2
u/AA11BB22c Jun 07 '22
You could separate it into two translation units or binaries with different target ISA (can be the same file but different sections enabled and feed that to the compiler twice).
If you want a single template (not specialized), can't do much ... well not that I know of anyways.
10
u/TTachyon Jun 06 '22
Setting up MinGW/Msys2/etc. is always a pain, and it's not something you can expect most users to do. Even shipping something compiled with mingw comes with a lot more dlls than it's comfortable. Msvc is just better integrated in the system and makes compiling things a lot easier for Windows. It does have really bad codegen sometimes, but for the most part it's okay. Clang's big advantage is that it's compatible with msvc's ecosystem, so it's pretty much a drop in replacement most of the time, and it has generally better codegen than msvc and far better diagnostics. I still default to msvc though.
7
u/James20k P2005R0 Jun 06 '22
Even shipping something compiled with mingw comes with a lot more dlls than it's comfortable
This is untrue, I've shipped single executable binaries before with mingw. If you check out the latest project I've released, the only shipped dependencies are libOpenCL.dll and the steam dll
3
u/TTachyon Jun 06 '22
Good to know it's possible. It's still more work than it's worth for me at the moment.
4
u/Jannik2099 Jun 06 '22
Setting up MinGW/Msys2/etc. is always a pain
Doesn't clang offer a native windows target?
3
u/TTachyon Jun 06 '22
The release clang provides contains executables, some headers specific to it and some ASAN libs. You still need a standard library from somewhere, be it MSVC or MinGW.
3
u/brechtsanders Jun 07 '22
If you don't need the bash shell and tools from MSYS2 you can avoid MSYS2 altogether. Get a standalone build MinGW-w64+GCC+LLVM from https://winlibs.com/ - just download and extract, no installation necessary. Can't be much more painless than that (of course you still have to point your IDE to the right build tools).
1
5
u/holyblackcat Jun 06 '22
What's painful about it? For MSYS2, you just run the installer, and type two commands: to update the packages and to install your preferred compiler (GCC or mingw-flavored Clang).
The number of dlls you need to ship is just ~3. And if that's too much, you can link them statically.
4
u/TTachyon Jun 06 '22
My experience of putting the bin folder in path means random things will break without a clear error message, and not having it in path is even more painful.
I guess I'm just lazy about setting the projects to link them statically. But I'd still have to support MSVC, so it's more work for me to do something that is not standard at my work.
2
u/holyblackcat Jun 06 '22
Yep, it can be annoying. Removing it from the system-wide PATH is not that bad, if you use MSYS2 tools from MSYS2 shell (which disregards global PATH and replaces it with its own).
1
u/Challanger__ Feb 22 '25
MSYS2 is super easy, cross compiling with clang and gcc on windows thanks to it
10
Jun 06 '22
I just stick with msvc, it's from the same company that made the os...
2
u/schmerm Jun 07 '22
There's also the compiler from the company that designs many of the chips that the generated machine code is gonna run on. ICC was supposed to be very solid. I'm wondering if that's still the case...
-10
-2
u/brechtsanders Jun 07 '22
I totally avoid MSVC as the resulting binaries need runtimes to be installed in order to run. You're right that MSVC is made by the same company that made Windows, but GCC and LLVM are made for many platforms, bringing in knowledge and expertise far beyond Windows. And their resulting binaries based on MinGW-w64 are native in the sense that they don't need any runtimes installed in order to run.
7
u/lukaasm Game/Engine/Tools Developer Jun 10 '22
Are you aware that YOU can decide which runtime libraries (static/dynamic) your project should link against?
0
54
u/sparant76 Jun 06 '22
Was it ever the optimal compiler for windows?