3 ms·
From my experience with C99: if the struct size is 16 bytes or less, the values will be packed into registers. But true as soon as the struct grows bigger, the
by flohofwoe 6y ago
From my experience with C99: if the struct size is 16 bytes or less, the values will be packed into registers. But true as soon as the struct grows bigger, the entire content will be passed on the stack.
IMHO this sort of named arguments "easter egg" is fine for big "option bag structs" passed to functions that are not performance critical (or for small struct <= 16 bytes). For other cases, regular args are better.
OTH it seems like passing normal args also spills over into the stack very soon (only 4 registers used?):
https://www.godbolt.org/z/Mz9MTG https://www.godbolt.org/z/Mz9MTG
The main difference seems to be that normals arguments "spill over", while passing structs by value puts everything on the stack once the threshold size is passed.
- archgoon 6y agoSix arguments use registers (and rax for return). The first two in your example are in the lea instruction. Using rdi, rsi, rdx, rcx, r8, r9 and then the stack is the calling convention used on x64 linux. 32-bit (-m32 in godbolt) linux would use the stack exclusively.
- account42 6y ago> Using rdi, rsi, rdx, rcx, r8, r9 and then the stack is the calling convention used on x64 linux It's more complicated than that - floats/doubles and structs of them are put in SSE registers instead. Mixed structs are packed into integer registers (!) if they are small enough. And with C++ classes with non-trivial destructor/etc. are always passed on the stack.
- nikki93 6y agoSix of the parameters use registers here. the `lea` is putting the sum of the first two into `eax`, then the rest accumulate more adds into `eax`. Only the last 3 parameters are reading from stack memory (the `dword ptr [*]` stuff). In any case you don't get to actually see the passing at the callsite because the result `45` is inlined into `main()`. :)
- flohofwoe 6y agoThanks for the clarification (also to archgoon). To your last point, I should have used -O1 instead of -O2, this also makes it clear what happens on the callsite.
- nikki93 6y agoAnother approach is to use a `volatile` function pointer: https://www.godbolt.org/z/e6GM93 https://www.godbolt.org/z/e6GM93 -- That way you get to keep other optimizations from `-O3` without inlining the function call itself.
- ddalcino 6y agoYou don’t need to lower the optimization level to see what happens at the call site: you can turn the function ‘blub’ into a function prototype. The optimizer cannot in-line your code if it isn’t available. https://www.godbolt.org/z/sj11rc https://www.godbolt.org/z/sj11rc