If I have func1() func2() and func3() and I want the compiler to arrange the implementations of func1() before func2() and func2() before func3() in-memory for instruction cache locality reasons, does anyone know if any compilers have flags to do this?
|
C
C++ compiler flags for function implementation arrangement in object file |
|
Jun 20, 2019 03:42 AM
(This post was last modified: Jun 20, 2019 04:25 AM by YouthInAsia.)
The compiler should automatically do this as far as I'm aware but it might just be unspecified behavior. I do remember though marking functions with the "noinline" attribute seemed to keep them in place. So try just giving each function a noinline attribute and see if that does the trick. It might just vary compiler to compiler, but I honestly doubt anything like that would ever be standard.
So I tried this code Code #include <stdio.h>Their placement in memory regardless of which order they were called was still f1 first, f2 second f3 third Code 0x100000f48 <+8>: call 0x100000f20 ; f3 at main.cpp:9So that does seem to work but when I remove the noinline attribute I get the same result Code 0x100000f48 <+8>: call 0x100000f20 ; f3 at main.cpp:9Which means the code section should just get compiled in the order you wrote it like it did in my case. But I think if you want to be extra careful then yes place noinline attribute. Another thing that could do the trick is marking a function as volatile/const. (Jun 20, 2019 09:35 AM)6577657534735 Wrote: Thanks, it's worrying that they could randomly swap order if you switched compiler versions or modified optimization flags or something, but I will look into whether this applies to me Yes one things for certain, even with compiler optimisation turned on a function with noinline attribute will still be separated.
Jun 20, 2019 06:08 PM
(This post was last modified: Jun 20, 2019 06:09 PM by dracx.)
This gretly varies from compiler to compiler, plus I take note of the object files (even just of your headers), if your worried bout their placement too, you should try and research more. And judging from your subject, I suggest disabling ASLR and other protection mechanisms that makes your stuff go dynamic.
|
|
|

