4 ms·
When compiling at full optimization and link-time code gen optimization, will the compiler and linker "get smart" about passing structs around? I'm inexperience
by MichaelGG 13y ago
When compiling at full optimization and link-time code gen optimization, will the compiler and linker "get smart" about passing structs around? I'm inexperienced here, but it always seemed odd that a struct with two words can't just be shoved into registers (calling or returning).
Is there a reason the x86/64 ABIs don't specify such a way? Also, it seems that the 128-bit and larger registers aren't used as often to pass values. For something like a GUID, wouldn't they be a perfect fit? Or does the processor optimize stack handling so that it's almost as cheap as registers?
- vinkelhake 13y agoWith full optimizations and LTO, the compiler may very well decide to inline a function. When a function is inlined, nothing is really "passed". That said, structs can absolutely be passed by registers. I whipped up a quick example that shows a simple 2-word struct being passed in two registers on x86-64: http://goo.gl/2Yxooe http://goo.gl/2Yxooe On x86-64, the SSE registers are used to pass floating point parameters.
- pcwalton 13y agoIn LLVM, internalized functions (which get created in LTO mode) are converted to fastcall. So yes, the ABI becomes smarter in that case. The platform ABIs mostly are the way they are for backwards compatibility at this point. They aren't particularly efficient and have weird corner cases (e.g. complex numbers are treated differently than the equivalent struct, IIRC). On x86-64 the situation is a little better, but still not ideal.
- pmjordan 13y agoThe x86-64 ABI does specify for multi-word structs to be returned in registers. If I remember correctly, it's up to 4 registers - rax, rdx and 2 others which I'll need to look up. This only happens if the struct fields on their own would be returned in this way, so an array of 16 chars would not typically be returned in 2 8-byte registers. To use SSE or AVX registers for anything other than float or double scalars, you'll need to use the special vector types. I think x86 lets you return in eax and edx, but only if you're returning a single 64-bit value, not a struct with 2 32-bits. Not 100% on all of this.
- stephencanon 13y ago> I think x86 lets you return in eax and edx, but only if you're returning a single 64-bit value, not a struct with 2 32-bits. Depends entirely on the platform ABI. Under the OS X calling conventions 2x32b is returned in eax:edx (even if the 32b values are floats, which is annoying).
- pmjordan 13y agoAh, true. x86 ABIs are much less unified than x86-64 ones.
- deleted 13y ago[deleted]
- ruggeri 13y agovinkelhake, you seem to be dead (for no discernible reason).