Skip to content

Commit 4a739fb

Browse files
authored
[Clang] [docs] [MSVC] Add sections on __forceinline and intrinsic behaviour differences between Clang and MSVC (#99426)
We have had quite a few issues created around how Clang treats intrinsics vs how MSVC treats intrinsics. While I was writing this I also added some sections on behaviour changes that caught me while porting my MSVC codebase to clang-cl. Hopefully we can point issues around intrinsics to this doc and hopefully it is useful to others who run into similar behaviour differences. The behaviour differences highlighted here are differences, as far as I am aware, that we do not intend to change or fix for MSVC.
1 parent 6747f12 commit 4a739fb

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

clang/docs/MSVCCompatibility.rst

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,133 @@ a hint suggesting how to fix the problem.
154154
As of this writing, Clang is able to compile a simple ATL hello world
155155
application. There are still issues parsing WRL headers for modern Windows 8
156156
apps, but they should be addressed soon.
157+
158+
__forceinline behavior
159+
======================
160+
161+
``__forceinline`` behaves like ``[[clang::always_inline]]``.
162+
Inlining is always attempted regardless of optimization level.
163+
164+
This differs from MSVC where ``__forceinline`` is only respected once inline expansion is enabled
165+
which allows any function marked implicitly or explicitly ``inline`` or ``__forceinline`` to be expanded.
166+
Therefore functions marked ``__forceinline`` will be expanded when the optimization level is ``/Od`` unlike
167+
MSVC where ``__forceinline`` will not be expanded under ``/Od``.
168+
169+
SIMD and instruction set intrinsic behavior
170+
===========================================
171+
172+
Clang follows the GCC model for intrinsics and not the MSVC model.
173+
There are currently no plans to support the MSVC model.
174+
175+
MSVC intrinsics always emit the machine instruction the intrinsic models regardless of the compile time options specified.
176+
For example ``__popcnt`` always emits the x86 popcnt instruction even if the compiler does not have the option enabled to emit popcnt on its own volition.
177+
178+
There are two common cases where code that compiles with MSVC will need reworking to build on clang.
179+
Assume the examples are only built with `-msse2` so we do not have the intrinsics at compile time.
180+
181+
.. code-block:: c++
182+
183+
unsigned PopCnt(unsigned v) {
184+
if (HavePopCnt)
185+
return __popcnt(v);
186+
else
187+
return GenericPopCnt(v);
188+
}
189+
190+
.. code-block:: c++
191+
192+
__m128 dot4_sse3(__m128 v0, __m128 v1) {
193+
__m128 r = _mm_mul_ps(v0, v1);
194+
r = _mm_hadd_ps(r, r);
195+
r = _mm_hadd_ps(r, r);
196+
return r;
197+
}
198+
199+
Clang expects that either you have compile time support for the target features, `-msse3` and `-mpopcnt`, you mark the function with the expected target feature or use runtime detection with an indirect call.
200+
201+
.. code-block:: c++
202+
203+
__attribute__((__target__("sse3"))) __m128 dot4_sse3(__m128 v0, __m128 v1) {
204+
__m128 r = _mm_mul_ps(v0, v1);
205+
r = _mm_hadd_ps(r, r);
206+
r = _mm_hadd_ps(r, r);
207+
return r;
208+
}
209+
210+
The SSE3 dot product can be easily fixed by either building the translation unit with SSE3 support or using `__target__` to compile that specific function with SSE3 support.
211+
212+
.. code-block:: c++
213+
214+
unsigned PopCnt(unsigned v) {
215+
if (HavePopCnt)
216+
return __popcnt(v);
217+
else
218+
return GenericPopCnt(v);
219+
}
220+
221+
The above ``PopCnt`` example must be changed to work with clang. If we mark the function with `__target__("popcnt")` then the compiler is free to emit popcnt at will which we do not want. While this isn't a concern in our small example it is a concern in larger functions with surrounding code around the intrinsics. Similar reasoning for compiling the translation unit with `-mpopcnt`.
222+
We must split each branch into its own function that can be called indirectly instead of using the intrinsic directly.
223+
224+
.. code-block:: c++
225+
226+
__attribute__((__target__("popcnt"))) unsigned hwPopCnt(unsigned v) { return __popcnt(v); }
227+
unsigned (*PopCnt)(unsigned) = HavePopCnt ? hwPopCnt : GenericPopCnt;
228+
229+
.. code-block:: c++
230+
231+
__attribute__((__target__("popcnt"))) unsigned hwPopCnt(unsigned v) { return __popcnt(v); }
232+
unsigned PopCnt(unsigned v) {
233+
if (HavePopCnt)
234+
return hwPopCnt(v);
235+
else
236+
return GenericPopCnt(v);
237+
}
238+
239+
In the above example ``hwPopCnt`` will not be inlined into ``PopCnt`` since ``PopCnt`` doesn't have the popcnt target feature.
240+
With a larger function that does real work the function call overhead is negligible. However in our popcnt example there is the function call
241+
overhead. There is no analog for this specific MSVC behavior in clang.
242+
243+
For clang we effectively have to create the dispatch function ourselves to each specfic implementation.
244+
245+
SIMD vector types
246+
=================
247+
248+
Clang's simd vector types are builtin types and not user defined types as in MSVC. This does have some observable behavior changes.
249+
We will look at the x86 `__m128` type for the examples below but the statements apply to all vector types including ARM's `float32x4_t`.
250+
251+
There are no members that can be accessed on the vector types. Vector types are not structs in clang.
252+
You cannot use ``__m128.m128_f32[0]`` to access the first element of the `__m128`.
253+
This also means struct initialization like ``__m128{ { 0.0f, 0.0f, 0.0f, 0.0f } }`` will not compile with clang.
254+
255+
Since vector types are builtin types, clang implements operators on them natively.
256+
257+
.. code-block:: c++
258+
259+
#ifdef _MSC_VER
260+
__m128 operator+(__m128 a, __m128 b) { return _mm_add_ps(a, b); }
261+
#endif
262+
263+
The above code will fail to compile since overloaded 'operator+' must have at least one parameter of class or enumeration type.
264+
You will need to fix such code to have the check ``#if defined(_MSC_VER) && !defined(__clang__)``.
265+
266+
Since `__m128` is not a class type in clang any overloads after a template definition will not be considered.
267+
268+
.. code-block:: c++
269+
270+
template<class T>
271+
void foo(T) {}
272+
273+
template<class T>
274+
void bar(T t) {
275+
foo(t);
276+
}
277+
278+
void foo(__m128) {}
279+
280+
int main() {
281+
bar(_mm_setzero_ps());
282+
}
283+
284+
With MSVC ``foo(__m128)`` will be selected but with clang ``foo<__m128>()`` will be selected since on clang `__m128` is a builtin type.
285+
286+
In general the takeaway is `__m128` is a builtin type on clang while a class type on MSVC.

0 commit comments

Comments
 (0)