<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Sorry for chiming in so late in this.<div class=""><br class=""></div><div class="">So I agree that negative indices are UB, I don’t think thats contentious.</div><div class=""><br class=""></div><div class="">However, I think the issue here is the DAG expansion.  That is the point at which we go from UB which would just be hidden in the instruction to something which can crash.  I think its at that point where we should mask to ensure that the in memory expansion doesn’t read out of bounds.  On architectures which do the variable extract in an instruction, they won’t be penalized by a mask, only the memory expansion will be which should be rarer,</div><div class=""><br class=""></div><div class="">The point about speculation at the IR level is interesting.  Personally i’m ok with constant indices being speculated and variable not.  If we later want to find a good way to ask TTI whether a variable extract is cheap then we can find a way to do so.</div><div class=""><br class=""></div><div class="">Anyway, just my 2c.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Pete<br class=""><div><blockquote type="cite" class=""><div class="">On Jun 30, 2015, at 2:07 PM, Paweł Bylica <<a href="mailto:chfast@gmail.com" class="">chfast@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Jun 30, 2015 at 11:03 PM Hal Finkel <<a href="mailto:hfinkel@anl.gov" class="">hfinkel@anl.gov</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">----- Original Message -----<br class="">
> From: "Paweł Bylica" <<a href="mailto:chfast@gmail.com" target="_blank" class="">chfast@gmail.com</a>><br class="">
> To: "David Majnemer" <<a href="mailto:david.majnemer@gmail.com" target="_blank" class="">david.majnemer@gmail.com</a>><br class="">
> Cc: "LLVMdev" <<a href="mailto:llvmdev@cs.uiuc.edu" target="_blank" class="">llvmdev@cs.uiuc.edu</a>><br class="">
> Sent: Tuesday, June 30, 2015 5:42:24 AM<br class="">
> Subject: Re: [LLVMdev] extractelement causes memory access violation - what   to do?<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> On Fri, Jun 26, 2015 at 5:42 PM David Majnemer <<br class="">
> <a href="mailto:david.majnemer@gmail.com" target="_blank" class="">david.majnemer@gmail.com</a> > wrote:<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> On Fri, Jun 26, 2015 at 7:00 AM, Paweł Bylica < <a href="mailto:chfast@gmail.com" target="_blank" class="">chfast@gmail.com</a> ><br class="">
> wrote:<br class="">
><br class="">
><br class="">
><br class="">
> Hi,<br class="">
><br class="">
><br class="">
> Let's have a simple program:<br class="">
><br class="">
> define i32 @main(i32 %n, i64 %idx) {<br class="">
> %idxSafe = trunc i64 %idx to i5<br class="">
> %r = extractelement <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, i64<br class="">
> %idx<br class="">
> ret i32 %r<br class="">
> }<br class="">
><br class="">
><br class="">
> The assembly of that would be:<br class="">
><br class="">
> pcmpeqd %xmm0, %xmm0<br class="">
> movdqa %xmm0, -24(%rsp)<br class="">
> movl -24(%rsp,%rsi,4), %eax<br class="">
> retq<br class="">
><br class="">
><br class="">
> The language reference states that the extractelement instruction<br class="">
> produces undefined value in case the index argument is invalid (our<br class="">
> case). But the implementation simply dumps the vector to the stack<br class="">
> memory, calculates the memory offset out of the index value and<br class="">
> tries to access the memory. That causes the crash.<br class="">
><br class="">
><br class="">
> The workaround is to trunc the index value before extractelement (see<br class="">
> %idxSafe). But what should be the ultimate solution?<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> We could fix this by specifying that out of bounds access on an<br class="">
> extractelement leads to full-on undefined behavior, no need to force<br class="">
> everyone to eat the cost of a mask.<br class="">
><br class="">
><br class="">
> I don't have preference for any of the solutions.<br class="">
><br class="">
><br class="">
> I have a side question. It is not stated explicitly in the reference<br class="">
> but I would assume the index of extractelement is processed as an<br class="">
> unsigned value. However, the DAG Builder extends the index with<br class="">
> sext. Is it correct?<br class="">
<br class="">
Hrmm. Given that only (small) positive numbers are valid, it shouldn't matter. Unless we can find a reason that it works better to be sext, it seems conceptually cleaner to make it zext.<br class=""></blockquote><div class=""><br class=""></div><div class="">I have tried to change it to zext. 2 Mips test have failed. I haven't checked the details though.</div><div class="">sext looks pretty wrong to me because i5 -1 does not mean 31 any more.</div><div class=""><br class=""></div><div class="">- PB</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="">
 -Hal<br class="">
<br class="">
><br class="">
><br class="">
> - PB<br class="">
> _______________________________________________<br class="">
> LLVM Developers mailing list<br class="">
> <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank" class="">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu/" rel="noreferrer" target="_blank" class="">http://llvm.cs.uiuc.edu</a><br class="">
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" rel="noreferrer" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br class="">
><br class="">
<br class="">
--<br class="">
Hal Finkel<br class="">
Assistant Computational Scientist<br class="">
Leadership Computing Facility<br class="">
Argonne National Laboratory<br class="">
</blockquote></div></div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:LLVMdev@cs.uiuc.edu" class="">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" class="">http://llvm.cs.uiuc.edu</a><br class=""><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br class=""></div></blockquote></div><br class=""></div></body></html>