<div dir="ltr">Hi,<br><br>I've run into some interesting behaviour with how globals are aligned - it seems like the alignment preferences specified in the target datalayout are being ignored. Example:<br><br>$ cat test.c<br>

void f(const char *);<br>void g(const char *c) {<br>  const char *b = "jgksj";<br>  return f(b + 1);<br>}<br><br>$ clang -target arm-none-linux-gnueabi -mcpu=cortex-m0 test.c -S -emit-llvm -o - -O0<br>; ModuleID = 'test.c'<br>

target datalayout = "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i64:64-v128:64:128-a:0:32-n32-S64"<br>target triple = "thumbv6m-none-linux-gnueabi"<br><br>@.str = private unnamed_addr constant [6 x i8] c"jgksj\00", align 1<br>

<br>(...)<br><br>Shouldn't @.str be aligned to the preferred alignment of i8, i.e. 4?<br><br>If I set MinGlobalAlignment for my target to 32, @.str is aligned to 4 bytes (as expected). This leads me to believe that the behaviour is caused by ASTContext::getAlignOfGlobalVar, which in turn calls ASTContext::getTypeAlign - and from there gets the ABI alignment (which of course is 8 bits / 1 in this case).<br>

<br>I imagine a fix would do something like<br><br>unsigned ASTContext::getAlignOfGlobalVar(QualType T) const {<br>  return std::max(getPreferredTypeAlign(T.getTypePtr()), getTargetInfo().getMinGlobalAlign());<br>}<br><br>

However, I'd then also need to change getPreferredTypeAlign so it actually considers what the target specifies as the preferred alignment, not just the ABI align and type size. Is this the right place to make changes? Or is all this intended behaviour?<br>

<br>Any opinions would be greatly appreciated.<br><br>Thanks<br>Moritz<br></div>