<div dir="ltr"><div><span style="font-family:arial,sans-serif;font-size:13px">Hi Rafael,</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">First of all thanks for your input -</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><span style="font-family:arial,sans-serif;font-size:13px">> I think it is safe, but changing getAlignOfGlobalVar to use the</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">> preferred alignment would be a big change for all targets, so it would</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> need a really large benchmarking run. </span><div>

<span style="font-family:arial,sans-serif;font-size:13px">Agreed, this would be a big change. Perhaps it</span><font face="arial, sans-serif"><span style="white-space:pre"> should be target-specific - i.e modifying </span></font><span style="font-family:arial,sans-serif">ASTContext::get[Preferred]TypeAlign so the behaviour only changes for certain targets -</span><span style="white-space:pre;font-family:arial,sans-serif"> although at the moment the front-end is quite limited in that regard as it only has access to ASTContext::TargetInfo, which doesn't contain much relevant data.</span></div>

<div><font face="arial, sans-serif"><span style="white-space:pre"><br></span></font></div><div><span style="font-size:13px;font-family:arial,sans-serif">> I think it would also prevent </span><span style="font-size:13px;font-family:arial,sans-serif">the linker from merging global strings.</span><font face="arial, sans-serif"><span style="white-space:pre"><br>

</span></font></div><div><span style="font-size:13px;font-family:arial,sans-serif">Due to the alignment change it would at least be restricted (e.g. in Thumb1, the maximum offset from a global is </span><font face="arial, sans-serif">127). On the other hand, allowing an increased alignment allows other optimisations in the back-end such as inlining calls to strcpy/memcpy - </font><span style="color:rgb(0,0,0)">LDM/IA has 32-bit alignment requirements. </span></div>

<div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">I think this issue is only really relevant to architectures which don't have extensive support for unaligned memory accesses, and thus a solution shouldn't change the behaviour on most platforms. For now, it might be best to leave this alone and simply set a minimum alignment for the specific target, and then investigate further from there.</span></div>

<div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">Cheers</span></div><div><span style="color:rgb(0,0,0)">Moritz</span></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

2014-04-22 15:52 GMT+01:00 Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="HOEnZb"><div class="h5">On 15 April 2014 13:29, Moritz Roth <<a href="mailto:moritz.roth11@gmail.com">moritz.roth11@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> I've run into some interesting behaviour with how globals are aligned - it<br>
> seems like the alignment preferences specified in the target datalayout are<br>
> 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<br>
> -o - -O0<br>
> ; ModuleID = 'test.c'<br>
> target datalayout =<br>
> "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<br>
> (as expected). This leads me to believe that the behaviour is caused by<br>
> ASTContext::getAlignOfGlobalVar, which in turn calls<br>
> ASTContext::getTypeAlign - and from there gets the ABI alignment (which of<br>
> 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()),<br>
> getTargetInfo().getMinGlobalAlign());<br>
> }<br>
><br>
> However, I'd then also need to change getPreferredTypeAlign so it actually<br>
> considers what the target specifies as the preferred alignment, not just the<br>
> ABI align and type size. Is this the right place to make changes? Or is all<br>
> this intended behaviour?<br>
<br>
</div></div>I think it is safe, but changing getAlignOfGlobalVar to use the<br>
preferred alignment would be a big change for all targets, so it would<br>
need a really large benchmarking run. I think it would also prevent<br>
the linker from merging global strings.<br>
<div class=""><br>
> Any opinions would be greatly appreciated.<br>
><br>
> Thanks<br>
> Moritz<br>
<br>
</div>Cheers,<br>
Rafael<br>
</blockquote></div><br></div>