Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions src/dmd/backend/cgcod.d
Original file line number Diff line number Diff line change
Expand Up @@ -291,32 +291,6 @@ tryagain:
regcon.params &= ~noparams;
}

// See if we need to enforce a particular stack alignment
foreach (i; 0 .. globsym.top)
{
Symbol *s = globsym.tab[i];

switch (s.Sclass)
{
case SCregister:
case SCauto:
case SCfastpar:
if (s.Sfl == FLreg)
break;

const sz = type_alignsize(s.Stype);
if (sz > STACKALIGN && (I64 || config.exe == EX_OSX))
{
STACKALIGN = sz;
enforcealign = true;
}
break;

default:
break;
}
}

if (config.flags4 & CFG4optimized)
{
if (nretblocks == 0 && // if no return blocks in function
Expand Down Expand Up @@ -376,6 +350,35 @@ tryagain:
cgcod_eh();
}

// See if we need to enforce a particular stack alignment
foreach (i; 0 .. globsym.top)
{
Symbol *s = globsym.tab[i];

if (Symbol_Sisdead(s, anyiasm))
continue;

switch (s.Sclass)
{
case SCregister:
case SCauto:
case SCfastpar:
if (s.Sfl == FLreg)
break;

const sz = type_alignsize(s.Stype);
if (sz > STACKALIGN && (I64 || config.exe == EX_OSX))
{
STACKALIGN = sz;
enforcealign = true;
}
break;

default:
break;
}
}

stackoffsets(1); // compute addresses of stack variables
cod5_prol_epi(); // see where to place prolog/epilog

Expand Down Expand Up @@ -1003,7 +1006,14 @@ else
}

/* Determine if we need BP set up */
if (config.flags & CFGalwaysframe)
if (enforcealign)
{
// we need BP to reset the stack before return
// otherwise the return address is lost
needframe = 1;

}
else if (config.flags & CFGalwaysframe)
needframe = 1;
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/backend/cod3.d
Original file line number Diff line number Diff line change
Expand Up @@ -4129,7 +4129,7 @@ void epilog(block *b)
{
L4:
assert(hasframe);
if (xlocalsize)
if (xlocalsize || enforcealign)
{
if (config.flags2 & CFG2stomp)
{ /* MOV ECX,0xBEAF
Expand Down
33 changes: 32 additions & 1 deletion test/runnable/testxmm.d
Original file line number Diff line number Diff line change
Expand Up @@ -1933,17 +1933,34 @@ void refIntrinsics()

/*****************************************/

void test6()
void test6a()
{
version (D_AVX2)
{
// stack occasionally misaligned
float f = 0;
long4 v;
assert((cast(size_t)&v) % 32 == 0);
v += 1;
}
}

void test6b()
{
version (D_AVX2)
{
struct S {long4 v;}
S s;
assert((cast(size_t)&s) % 32 == 0);
}
}

void test6()
{
test6a();
test6b();
}

/*****************************************/

version (D_AVX)
Expand All @@ -1968,6 +1985,19 @@ void test7()

/*****************************************/


auto test20052()
{
version (D_AVX2)
{
struct S { long4 v; }
S s;
return s;
}
}

/*****************************************/

int main()
{
test1();
Expand Down Expand Up @@ -2007,6 +2037,7 @@ int main()
test10447();
test17344();
test17356();
test20052();

test6();
test7();
Expand Down