Why does Clang-Tidy suggest a larger alignment?
Asked Answered
E

2

9

Given the following c language struct definition:

typedef struct PackTest {
    long long a;
    int b;
    int c;
} PackTest;

Clang-Tidy gives the following message:

Accessing fields in struct 'PackTest' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes

I know why the struct is aligned to 8 bytes, but I don't know if the suggestion is valid and why.

Ehtelehud answered 27/10, 2020 at 3:8 Comment(2)
Can yo upost a complete program that gives the message?Witter
This is a really, really irresponsible warning. There is nothing "untidy" about having standard structs that are not ridiculously over-aligned. For very specialized purposes you might want an over-aligned object, but if it's flagging this then most things it's flagging are just wrong, and encouraging you to write code that's inefficient and gratuitously nonstandard.Baier
B
7

Some particular specialized assembly instructions might have alignment requirements (for example, x86 non-scalar SSE instructions strictly require alignment to 16 bytes boundaries). Other instructions might have lower throughput when used on data that is not aligned to 16 byte boundaries (for example, x86 SSE2).

These kind of instructions are usually used to perform aggressive optimizations based on the hardware features of the processor. Overall, the message you get is only useful in those scenarios (i.e. if you are actually planning to take advantage of such instructions).

See also:


Finally I'll just quote Rich from the above comment since they make a really good point:

There is nothing "untidy" about having standard structs that are not ridiculously over-aligned. For very specialized purposes you might want an over-aligned object, but if it's flagging this then most things it's flagging are just wrong, and encouraging you to write code that's inefficient and gratuitously nonstandard.

Beige answered 27/10, 2020 at 3:26 Comment(2)
In defence of clang-tidy this suggestion appears only if altera flag is set which relates to FPGA development.Kruse
@SpyrosMourelatos, in clang tidy 14 it is active right after *.Vaios
T
4

you can add -altera-struct-pack-align for Clang-Tidy to disable this warning

source: https://www.mail-archive.com/[email protected]/msg171275.html

Theophilus answered 2/4, 2021 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.