<div dir="ltr">Thanks again. <div><br></div><div>So this is actually a problem in the test case, and I can assume that I can trust alignment of the storeinst :) </div><div><br></div><div>Let me try to modify that testcase.</div><div><br></div><div>Thanks</div><div>Hongbin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 16, 2016 at 12:48 PM, Tim Northover <span dir="ltr"><<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Hongbin,<br>
<span class=""><br>
On 16 December 2016 at 12:22, Hongbin Zheng <<a href="mailto:etherzhhb@gmail.com">etherzhhb@gmail.com</a>> wrote:<br>
> Thanks for the explanation. The above code snippets is actually extracted by<br>
> bugpoint from MultiSource/Applications/ALAC/<wbr>encode/alacconvert-encode.<wbr>test.<br>
> I get a different result if I cast the pointer to int and mask away the<br>
> lower two bits and cast the int back to pointer. While casting pointer to<br>
> int and back to pointer do not cause any problem.<br>
<br>
</span>It's quite possible there's some undefined behaviour in ALAC that's<br>
only recently become a problem. On almost all targets I'm aware of<br>
32-bit integers should be aligned to 32-bits. There's a bit more<br>
variation for 64-bit ints.<br>
<br>
Programs that work on buffers of bytes often cast pointers into those<br>
buffers inappropriately. E.g.<br>
<br>
char *Buf = ...;<br>
[...]<br>
return doSomethingWithInts((int32_t *)&Buf[Idx]);<br>
<br>
For example I'd be very suspicious of ALACEncoder.cpp lines 340 and<br>
352. I expect there's more, those are just the first two I found by<br>
grepping for "(int32_t *)".<br>
<br>
Everything seems to work fine until the compiler becomes clever enough<br>
to realise it can use a slightly faster store since the pointer is<br>
aligned (or one of any number of other optimizations), and then things<br>
blow up.<br>
<br>
Generally you need to memcpy part of the buffer into a properly<br>
aligned variable and pass that, or use a carefully under-aligned type.<br>
In Clang you can create a specifically under-aligned integer type, and<br>
as long as all *accesses* go through that type it should work. So<br>
something like this:<br>
<br>
typedef int32_t __attribute__((aligned(1))) unaligned_int32_t;<br>
[...]<br>
int32_t doSomethingWithInts(unaligned_<wbr>int32_t *Ints) {<br>
return Ints[0] + Ints[1];<br>
}<br>
<br>
But that's obviously a non-standard extension (GCC supports it, and I<br>
believe MSVC has a similar but incompatible concept). It's also really<br>
easy to mess that up, the attribute gets dropped in some really<br>
surprising situations you (or at least I) wouldn't expect.<br>
<br>
Cheers.<br>
<span class="HOEnZb"><font color="#888888"><br>
Tim.<br>
</font></span></blockquote></div><br></div>