In this case, I used #pragma pack to set the structure alignment to 1. Then, I loaded the first int member of the structure. When compiled with -O2, the align attribute in the IR was converted to 4.
// align.c
#pragma pack(1)
struct a {
volatile int b;
int c;
long d;
long e;
short f;
unsigned : 25;
volatile long g;
int h;
} i[];
static volatile struct a k = {1}, l = {2}, m = {3}, n = {4}, p = {5}, q = {6};
static int foo() {
int s = m.b; // load m with align 4
m.c = k.c = l.c = n.c = p.c = q.c = 1;
return s;
}
int main() {
foo();
return 0;
}
The pass responsible for increasing the alignment attribute is InferAlignmentPass. Since the user code loaded the struct object m (Or m.b, the first int member of m) with a 4-byte alignment once, the alignment attribute for m was increased from 1 to 4. However, the struct's actual memory is still 1-byte alignment, and this inconsistency has caused issues in my development. Should llvm add a check in InferAlignmentPass for this case?
In this case, I used
#pragma pack
to set the structure alignment to 1. Then, I loaded the first int member of the structure. When compiled with-O2
, the align attribute in the IR was converted to 4.compile with main branch
-O2 -emit-llvm -S
The pass responsible for increasing the alignment attribute is
InferAlignmentPass
. Since the user code loaded the struct object m (Or m.b, the first int member of m) with a 4-byte alignment once, the alignment attribute for m was increased from 1 to 4. However, the struct's actual memory is still 1-byte alignment, and this inconsistency has caused issues in my development. Should llvm add a check inInferAlignmentPass
for this case?