[cfe-dev] Question about alignment when the packed struct type's field is accessed.

jingu jingu at codeplay.com
Sun Jun 23 04:09:17 PDT 2013


Hi all,

I have a question about alignment when the packed struct type's field is
accessed.

My colleague Fraser tested a code and the generated IR from clang is as
following:

******* test source code *******
typedef unsigned char uint8_t;
typedef int int32_t;

#pragma pack(push)
#pragma pack(1)
struct S0 {
const uint8_t f_0;
int32_t f_1;
};
#pragma pack(pop)

static struct S0 g_var = {255UL, 1L};

int main ()
{
int32_t *ptr = &g_var.f_1;
int32_t test = *ptr;

return 0;
}

******* generated IR from clang *******
%struct.S0 = type <{ i8, i32 }>

@g_var = internal global %struct.S0 <{ i8 -1, i32 1 }>, align 1

; Function Attrs: nounwind
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%ptr = alloca i32*, align 4
%test = alloca i32, align 4
store i32 0, i32* %retval
store i32* getelementptr inbounds (%struct.S0* @g_var, i32 0, i32 1),
i32** %ptr, align 4
%0 = load i32** %ptr, align 4
%1 = load i32* %0, align 4
store i32 %1, i32* %test, align 4
ret i32 0
}

We are wondering whether the alignment of this instruction "%1 = load
i32* %0, align 4" is correct or not. We thought "align 1" is correct
because g_var is packed structype's variable. How do you feel about
this? Is it problem or not? After Mem2Reg pass, this problem is
disappeared. If clang considers this code as problem, we would like to
make a patch to fix it.

Thanks,
JinGu Kang





More information about the cfe-dev mailing list