Clang has different behaviors in different cases, when add padding to struct, and what's the rule?
For below c++ code:
struct CT1 {
char c1 = 'c';
double d1;
char c2;
};
struct CT2 {
char c1;
double d1;
char c2;
};
struct CT3 {
char c1 = 'c';
double d1;
};
int main() {
CT1 ct1;
CT2 ct2;
CT3 ct3;
return 0;
}
Clang will generate IR code:
%struct.CT1 = type <{ i8, [7 x i8], double, i8, [7 x i8] }>
%struct.CT2 = type { i8, double, i8 }
%struct.CT3 = type { i8, double }
I have several questions:
- CT1 has padding, while CT2 has not. CT1 is only different with CT2 by having a initial value of
c1
. Why? - CT3 hasn't padding, which is only different with CT1 by reduced a property named
c2
. Why? - IR Code of CT2 doesn't add padding explicitly, but in log (using
-Wpadded
) it seems to have added padding already, is this true? And does it means that we can not add padding by hand?
warning: padding struct 'CT3' with 7 bytes to align 'd1' [-Wpadded]
double d1;
- How to add padding to struct by IRBuilder of LLVM?
I need to generate IR code by hand, so i need to know when should i add padding to struct.
Really thanks for any reply.