This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Use the non-virtual alignment when emitting the base class subobject constructor
ClosedPublic

Authored by ahatanak on Jan 25 2018, 12:08 AM.

Details

Summary

This patch fixes what looks like a bug in IRGen's member initialization where it uses the alignment of a complete object instead of its non-virtual alignment when emitting the base constructor.

For example, when emitting the base constructor for B, IRGen uses a 16-byte alignment to initialize B::x because B's alignment is 16-byte (which is because A is 16-byte aligned). This is not correct since A is a virtual base of B and therefore we cannot guarantee that the pointer to B that is passed to the base constructor is always 16-byte aligned.

struct A {
  __attribute__((aligned(16))) double data1;
};

struct B : public virtual A {
  B() : x(123) {}
  double a;
  int x;
};

struct C : public virtual B {};

void test() { B b; C c; }

To fix the bug, this patch calls CGF.MakeNaturalAlignPointeeAddrLValue in EmitMemberInitializer to create an LValue that uses the non-virtual alignment of the class.

rdar://problem/36382481

Diff Detail

Repository
rC Clang

Event Timeline

ahatanak created this revision.Jan 25 2018, 12:08 AM
This revision is now accepted and ready to land.Jan 25 2018, 11:17 AM
This revision was automatically updated to reflect the committed changes.