Detection of poor data layout / packing.

Hey,

I asked about this a while back but got no answers. And after seeing the GN talks and specially also after watching Andrei talking about data layout I’m tempted to ask again.

Are there any tools within clang/llvm that detect poor data packing in classes?

I saw that there exist some flags that can print out the current data structure and the memory offset of each member variable. But it seemed not to work that well given anything else outside of some testing code and trying to parse it seemed to be hard (but not impossible)

Would it make sense to try to have a tool or in some way emit the structure and point out places where packing could be improved? If it would, and no tool exists, I would be interested in helping out or doing something on my own.

Cheers,
Olafur Waage

Clang’s -Wpadding may be sort of like what you want.

– Sean Silva

Thanks for the reply.

The flag is -Wpadded and there seems to be very little information about it.

I tried it with a few tests.

struct Test
{
bool a;
int b;
};

Gives the warning

“test.cpp:6:9: warning: padding struct ‘Test’ with 3 bytes to align ‘b’ [-Wpadded]”

And it also spots

struct Test
{
int b;
bool a;
};

struct Test2
{
Test t;
int a;
};

With the warning.

“test.cpp:3:8: warning: padding size of ‘Test’ with 3 bytes to alignment boundary”

And it seems to work just fine. Need to look better into this, if it’s catching all of the cases.

Cheers!