问题
I know why GCC doesn\'t re-order members of a structure by default, but I seldom write code that relies on the order of the structure, so is there some way I can flag my structures to be automaticly reordered?
回答1:
Previous GCC versions have the -fipa-struct-reorg option to allow structure reordering in -fwhole-program
+ -combine
mode.
-fipa-struct-reorg
Perform structure reorganization optimization, that change C-like structures layout in order to better utilize spatial locality. This transformation is affective for programs containing arrays of structures. Available in two compilation modes: profile-based (enabled with
-fprofile-generate
) or static (which uses built-in heuristics). Require-fipa-type-escape
to provide the safety of this transformation. It works only in whole program mode, so it requires-fwhole-program
and-combine
to be enabled. Structures considered'cold'
by this transformation are not affected (see--param struct-reorg-cold-struct-ratio=value
).
It was removed since GCC 4.8.x due to the below reasons in the release note
The struct reorg and matrix reorg optimizations (command-line options
-fipa-struct-reorg
and-fipa-matrix-reorg
) have been removed. They did not always work correctly, nor did they work with link-time optimization (LTO), hence were only applicable to programs consisting of a single translation unit.
However you can still try the struct-reorg-branch
on GCC SVN or the github mirror out on your own risk as it's still in active development.
You can also reorder the fields with the clang-reorder-fields tool in clang-tools-extra
See also
- Automated field re-ordering in C structs to avoid padding
回答2:
There is no such option in GCC. And, I sure, it can not be introduced in any sensible fashion. About padding optimizations please look at this discussion.
The only exception I know is hot/cold structure fields splitting, that can be done in some cases (still I am not sure, that GCC can do it even in profile-guided mode, I know ICC can). This feature is not under user control and is performed on call-graphs where conservativeness of such transformation over data-flow is provable.
回答3:
I think it is possible to reorganize/split elements of struct when you are compiling the whole program (lto mode, use -flto flag). In that case you have complete picture of the program, and for the symbols which do not escape it should be possible to reorder them for better cache behavior etc.
In the gcc trunk this is under active development. This was presented in GNU cauldron 2015. You might want to try gcc trunk or the struct-reorg-branch.
https://gcc.gnu.org/wiki/cauldron2015?action=AttachFile&do=view&target=Olga+Golovanevsky_+Memory+Layout+Optimizations+of+Structures+and+Objects.pdf
来源:https://stackoverflow.com/questions/14671253/is-there-a-gcc-keyword-to-allow-structure-reordering