<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 18, 2016, at 2:43 PM, Rail Shafigulin <<a href="mailto:rail@esenciatech.com" class="">rail@esenciatech.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><span class=""><div class="">I see vectorization happening on this example (see below).<br class=""></div></span><span class=""><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">Any help is appreciated.</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><span class=""><font color="#888888" class=""><div class=""><br class=""></div><span class=""><font color="#888888" class=""><div class="">-- </div><div class="">Mehdi</div></font></span></font></span></div></div></blockquote></div><span class=""><br class=""><br clear="all" class=""><div class=""><br class=""></div>--<span class="Apple-converted-space"> </span><br class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">Rail Shafigulin<br class=""></div>Software Engineer<span class="Apple-converted-space"> </span><br class="">Esencia Technologies<br class=""></div></div></div></div></span></div></div></blockquote></div><br class="">Forgot to attach a C file. Here it is:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"><div class="gmail_extra">#define N 32</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">int main () {</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>int  a[N], b[N];</div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>int c[N];</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>for (int i = 0; i < N; ++i)</div><div class="gmail_extra">       c[i] = a[i] + b[i];</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>int sum=0;</div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>for (int i = 0; i < N; ++i)</div><div class="gmail_extra">       sum += c[i];</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"> <span class="Apple-converted-space"> </span>return sum;</div><div class="gmail_extra">}</div><div class=""><br class=""></div></div></div></div></blockquote><div class=""><br class=""></div></span><div class="">This will be vectorized without any insertelement, here is a few lines extracted from the output of clang on this code:</div><div class=""><br class=""></div><div class="">  %wide.load8.6 = load <4 x i32>* %48, align 16, !tbaa !2<br class="">  %49 = add nsw <4 x i32> %wide.load8.6, %wide.load.6<br class="">  %50 = getelementptr inbounds [32 x i32]* %c, i64 0, i64 24<br class="">  %51 = bitcast i32* %50 to <4 x i32>*<br class="">  store <4 x i32> %49, <4 x i32>* %51, align 16, !tbaa !2<br class=""><br class=""></div><div class="">Because you didn't write the example as I described it, i.e. taking integer, doing a few arithmetic and writing result to contiguous memory, the vectorizer will be able to load directly vectors from memory, operates on them, and store the results. For example try with the following C code:</div><div class=""><br class="">void foo (int a1, int a2, int a3, int a4, int b1, int b2, int b3, int b4, int *res) {<br class="">  res[0] = a1 + b1 * 2;<br class="">  res[1] = a2 + b2 * 2;<br class="">  res[2] = a3 + b3 * 2;<br class="">  res[3] = a4 + b4 * 2;<br class="">}<br class=""><br class=""></div><div class=""><br class=""></div><div class="">That's for the clang part, you can look at the vectorizer lit test to have examples of IR before/after vectorization.</div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">-- </div><div class="">Mehdi</div></font></span></div></blockquote></div><br class="">Just out of curiosity how did you know that your foo code will produce vectorized code? </div></div></div></blockquote><div><br class=""></div><div>I read the source code for the SLP Vectorizer ;)</div><div>(other than looking at unit tests, this is another good way of learning of LLVM works)</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra">I tried code similar to yours without any multiplication and no vectors were generated.<br clear="all" class=""></div></div></div></blockquote><div><br class=""></div><div>It is a matter of cost model: there need to be a few arithmetic instruction to balance the cost of building a vector.</div><div><br class=""></div><div>-- </div><div>Mehdi</div><div><br class=""></div><div><br class=""></div></div></body></html>