Clang vs MSVC

Clang vs MSVC

x64penguin

Clang is a cross-platform C and C++ frontend to LLVM compiler which is completely free and open-source. Clang translates C code to an intermediate, platform-independent code (LLVM-IR) and then LLVM optimizes and compiles it for specific arch. Such infrastructure allows to easily build highly optimized, cross-platform compilers.

On the other hand MSVC is a Windows-only proprietary C and C++ compiler, it is free but not open-source.

Installation

MSVC comes with Visual Studio and is not a standalone compiler today. Clang can be installed as a component of Visual Studio via Visual Studio Installer on Windows, via packet manager on Linux or you can build it yourself. On Windows it is recommended to use MSVC-compatiable version and have MSVC installed aswell as it relies on MSVC libraries. You can also bootstrap Clang: build Clang with MSVC and then build Clang again with recently built Clang for better optimization.

C++ Standard support

Both compilers tend to support newest C++ features, but with the help of the community Clang usually implements them earlier. Also Clang is better at compile time expressions, for example most of cmath functions can be used as constexpr in Clang like sqrt, pow, exp, etc. MSVC support much less constexpr functions. You can compare other supported features by different compilers here.

Code quality and "intelligence"

Lets test it on some common math expressions. A little spoiler: using /fp:fast flag with MSVC (which allows compiler to do unsafe math optimizations) often makes code equal to Clang's without fast math. BUT, all of the optimizations performed by Clang are completely safe in theese cases and there is no point to not do them without fast math. Also fast math can really do unsafe things, so I recommend to never use it.

1. 3D Vector length. While Clang code is the exact same operations written in assembly (except xorps, here and here you can find explanations why it is added), MSVC added if statement where it uses sqrtss instruction in one case and sqrtf function in other. Why it happend? That's because IEEE standard stands that in case of incorrect math (in our case square root of a negative value) a NaN should be returned, which is not guranteed by hardware sqrt instruction, but sqrtf function does this and some other exception handling. This behavior can be disabled with fast math (-ffast-math for Clang and /fp:fast for MSVC). However in our case it is obvious (for us and Clang but not MSVC) that the value we take square root of is always equal or more than zero, and Clang notices it and omits this check. 1 point for Clang, 1-0

3D vector length calculation

2. pow(x, 2) instead of x * x. Clang easily replaces it with x * x and generates the same code as in previous case. MSVC calls powf function 3 times which kills performance. Using /fp:fast allows MSVC to optimize it to x * x, but thats dumb that it is considered as unsafe optimization. 1 point for Clang, 2 - 0.

Replacing x * x with powf(x, 2.f)

3. Very dumb and pointless function. This function is absolutely wrong and can be written as "return a % 2". Unfortunately thats not what Clang did, but still much better. Clang does its best to fix your stupid code. MSVC leaves it as is. 1 point for Clang, 3 - 0.

Dumb and pointless function
Correct variant is fine in both

4. Common math expressions. Clang generated slightly smaller code and used less stack space, which is better. But if we use fast math, MSVC did it better. 1 point to both, 4 - 1.

Common math expressions
With fast math

I don't see a point to give Clang more points for math optimizations like sqrtf(49.f) -> 7.f, which MSVC considers as unsafe. Thats weird.

Build speed

For small projects MSVC is much faster due to Clang has longer startup because it is cross-platform and need to fully scan your system for libs, etc... For a big project test I will build CMake in Release configuration. Tested Clang version is 20.0.0 (Clang built with Clang from git repo in Release configuration), MSVC version is 19.41.34120.0. Used ninja build system.

So building it via Clang on my Intel i5-1135G7 laptop with 16GB RAM took 5 minutes 34 seconds. Building via MSVC in same configuration took 6 minutes and 2 seconds. So Clang is slightly faster for big projects, and chances are for larger projects Clang will be even more faster than MSVC. Clang also usually consumes less RAM than MSVC.

1 point to Clang, 5 - 1.

Debugging experience

Visual Studio debugger works excellent with both compilers, no variables are optimized (even unused), everything is visible and execution pointer is not jumping back and forth (like it happens in release build due to aggressive optimization). However when it comes to profiling, Visual Studio displays more accurate results in code compiled with MSVC, with Clang some lines may be missing and results become incorrect.

1 point to MSVC, 5 - 2.

Conclusion

It is easier to correctly configure MSVC for Visual Studio, Clang is slightly harder, though Visual Studio has good support for it. If you need Windows-only development and don't care about optimization much, MSVC will be better for you since it is native Visual Studio compiler. If you need best optimizations or your app is cross-platform, Clang is your choice. However it is a good practice to support both compilers. Each has its own unique non-standard extensions, structure definitions and behavior (especially when it comes to constexpr).

Report Page