Permalink
Browse files

Merge pull request #2919 from kinke/classAlignment

Fix alignment of scope-allocated class instances
  • Loading branch information...
kinke committed Nov 20, 2018
2 parents 402ebc4 + 8dc3376 commit 78d6fb3bb80c020a24f0e52144dfd5cf3bca1602
Showing with 54 additions and 3 deletions.
  1. +8 −1 dmd/argtypes_sysv_x64.d
  2. +4 −1 gen/classes.cpp
  3. +41 −0 tests/codegen/align_class.d
  4. +1 −1 tests/d2/dmd-testsuite
@@ -147,7 +147,7 @@ Classification classify(Type t, size_t size)
extern (C++) final class ToClassesVisitor : Visitor
{
const size_t size;
const int numEightbytes;
int numEightbytes;
Class[4] result = Class.NoClass;
this(size_t size)
@@ -390,5 +390,12 @@ extern (C++) final class ToClassesVisitor : Visitor
if (c != Class.SSEUp)
return memory();
}
// Undocumented special case for aggregates with the 2nd eightbyte
// consisting of padding only (`struct S { align(16) int a; }`).
// clang only passes the first eightbyte in that case, so let's do the
// same.
if (numEightbytes == 2 && result[1] == Class.NoClass)
numEightbytes = 1;
}
}
@@ -83,7 +83,10 @@ DValue *DtoNewClass(Loc &loc, TypeClass *tc, NewExp *newexp) {
// allocate
LLValue *mem;
if (newexp->onstack) {
mem = DtoRawAlloca(DtoType(tc)->getContainedType(0), DtoAlignment(tc),
unsigned alignment = tc->sym->alignsize;
if (alignment == STRUCTALIGN_DEFAULT)
alignment = 0;
mem = DtoRawAlloca(DtoType(tc)->getContainedType(0), alignment,
".newclass_alloca");
}
// custom allocator
@@ -0,0 +1,41 @@
// RUN: %ldc -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
// RUN: %ldc -run %s
struct S16 { align(16) int a; }
class D { align(32) int d = 0xDD; }
// CHECK: %align_class.D = type { [5 x i8*]*, i8*, [{{(16|24)}} x i8], i32 }
class E : D { S16 s16 = S16(0xEE); }
// CHECK: %align_class.E = type { [5 x i8*]*, i8*, [{{(16|24)}} x i8], i32, [12 x i8], %align_class.S16 }
class F : D { align(64) int f = 0xFF; }
// CHECK: %align_class.F = type { [5 x i8*]*, i8*, [{{(16|24)}} x i8], i32, [28 x i8], i32 }
extern(C++) class CppClass { align(32) int a = 0xAA; }
// CHECK: %align_class.CppClass = type { [0 x i8*]*, [{{(24|28)}} x i8], i32 }
void main()
{
scope d = new D;
// CHECK: = alloca %align_class.D, align 32
static assert(D.d.offsetof == 32);
assert(d.d == 0xDD);
scope e = new E;
// CHECK: = alloca %align_class.E, align 32
static assert(E.d.offsetof == 32);
assert(e.d == 0xDD);
static assert(E.s16.offsetof == 48);
assert(e.s16.a == 0xEE);
scope f = new F;
// CHECK: = alloca %align_class.F, align 64
static assert(F.d.offsetof == 32);
assert(f.d == 0xDD);
static assert(F.f.offsetof == 64);
assert(f.f == 0xFF);
scope cppClass = new CppClass;
// CHECK: = alloca %align_class.CppClass, align 32
static assert(CppClass.a.offsetof == 32);
assert(cppClass.a == 0xAA);
}

0 comments on commit 78d6fb3

Please sign in to comment.