Begin New Subject | Threaded View | Search | List | List Messageboards | Help |
Intrinsics vs Assembly - Nathan Kurz - 2013-04-05 |
Intrinsics vs Assembly - Agner - 2013-04-06 |
Intrinsics vs Assembly |
---|
Author: | Date: 2013-04-05 15:46 |
Hi Agner -- The library looks great, and is very clear to read. I have a general question about the choice of intrinsics versus assembly versus templates libraries such as yours. With intrinsics, I'm having trouble convincing the compilers I've tried (gcc, clang, icc) to do things in the order I want, or to actually use the instructions I want. My particular case involves codecs for bit-packing integers SIMD. The general code looks like this: vec4 = _mm_load_si128(in); ... (enough cycles for L1 latency) vec0 = _mm_load_si128(in); vec1 = _mm_load_si128(in); ... This arrangement fits the execution ports of Sandy Bridge quite well. But the compilers all want to "optimize" it to something like this: movdqu (%rdi), %xmm11 // read 'in' once from memory movdqa %xmm11, %xmm5 // copy 'in' reg to reg The moves from memory on P23 have been replaced by register copies on P015, and the stores have all been put at the end. The instructions have been reordered so we spend a few extra cycles waiting for data. Instead of using P0, P1, P2, P4, and P5, we now have contention. Does your approach make it easier to keep things in the desired ordering and executing the exact operations? Have you found any compiler tricks to make this more likely to happen? I haven't figured out how to do so without disabling all optimizations globally. |
Reply To This Message |
Intrinsics vs Assembly |
---|
Author: Agner | Date: 2013-04-06 02:27 |
It's a matter of how the compiler optimizes. The compiler will often reuse a memory load to minimize cache contentions. But you are right, there are situations where it is better to re-load the same value in order to reduce the load on other execution ports or to make dependency chains shorter. In many cases, the best compilers (Gnu, Intel) optimize better than a decent assembly programmer does. But there are also many cases where compilers are doing incredibly silly things. If you have a very critical hotspot and the compiler is not optimizing it good enough, then the only alternative is to use assembly. The tradeoff between copying a previously loaded value versus loading it again disappears with the AVX instruction set where you have non-destructive three-operand instructions. |
Reply To This Message |
Begin New Subject | Threaded View | Search | List | List Messageboards | Help |