<div dir="ltr">Hi, <div><br></div><div>I just tried llvm-3.8 (LLVM SVN Repository). With this version, -fno-rtti can help me to compile my code and -irce can help me to do a better job for loop unrolling. However, I still have one question. If I use Clang to compile a piece of c++ code to .bc and then use 'opt -loop-rotate -loop-unroll -irce', I can get what I want. I mean, there is no conditional branch at the end of each unrolled part. However, If I use LLVM API such as IRBuilder (CreateAdd, CreateGEP, CreateLoad and so on) to generate the .bc (I dump the two .bc files and they looks like almost same except the variable name), then 'opt -loop-rotate -loop-unroll -irce'I cannot get what I want. I mean, in this case, there is still loop boundary checking (add, compare, conditional branch) at the end of each unrolled part. </div><div><br></div><div>I'm really confused about this. Does Clang do something special? Or do I need to do something else to eliminate the unnecessary loop boundary checking at the end of each unrolled part?</div><div><br></div><div>Thanks for your help.</div><div><br></div><div>Xiangyang</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 21, 2015 at 11:29 AM, Xiangyang Guo <span dir="ltr"><<a href="mailto:xguo6@ncsu.edu" target="_blank">xguo6@ncsu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, Jeremy,<div><br></div><div>Thanks for your reply. I tried -fno-rtti yesterday and no luck.</div><div><br></div><div>Regards,</div><div><br></div><div>Xiangyang</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 21, 2015 at 11:05 AM, Jeremy Lakeman <span dir="ltr"><<a href="mailto:Jeremy.Lakeman@gmail.com" target="_blank">Jeremy.Lakeman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>There's been some recent noise on the mailing list about requiring -fno-rtti;<br><a href="http://lists.llvm.org/pipermail/llvm-dev/2015-August/089010.html" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2015-August/089010.html</a><br><br></div>Could that be it?<br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Aug 22, 2015 at 12:21 AM, Xiangyang Guo via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, James and Philip, Thanks for your help.<div><br></div><div>Based on your advice, I downloaded llvm-3.7. However, with this new version of LLVM, I have the following errors when I compile my previous code:</div><div><br></div><div><div>g++ -o parser main.o  `llvm-config --libs all` `llvm-config --ldflags --system-libs` -lpthread -ldl -rdynamic -ltinfo</div><div>main.o:(.data.rel.ro._ZTIN4llvm17GetElementPtrInstE[_ZTIN4llvm17GetElementPtrInstE]+0x10): undefined reference to `typeinfo for llvm::Instruction'</div><div>main.o:(.data.rel.ro._ZTIN4llvm8ICmpInstE[_ZTIN4llvm8ICmpInstE]+0x10): undefined reference to `typeinfo for llvm::CmpInst'</div></div><div><br></div><div>BTW, in my code, I use LLVM API (IRBuilder and so on) to generate one Module and then use PassManager to add several passes. And my Makefile is pretty simple, it looks like this:</div><div>***********************************************************************************************</div><div><div>all: parser</div><div><br></div><div>OBJS = main.o    \</div><div><br></div><div>LLVMCONFIG = llvm-config</div><div>CPPFLAGS = `$(LLVMCONFIG) --cxxflags` -std=c++11</div><div>LDFLAGS = `$(LLVMCONFIG) --ldflags --system-libs` -lpthread -ldl -rdynamic -ltinfo</div><div>LIBS = `$(LLVMCONFIG) --libs all`</div><div><br></div><div>clean:</div><div><span style="white-space:pre-wrap">       </span>$(RM) -rf parser $(OBJS)</div><div><br></div><div>%.o: %.cpp</div><div><span style="white-space:pre-wrap">       </span>g++ -g -c $(CPPFLAGS) -o $@ $<</div><div><br></div><div><br></div><div>parser: $(OBJS)</div><div><span style="white-space:pre-wrap">        </span>g++ -o $@ $(OBJS) $(LIBS) $(LDFLAGS)</div></div><div>**********************************************************************************************</div><div>Do you have any idea? Thanks a lot.</div><div><br></div><div>Regards,</div><div><br></div><div>Xiangyang </div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Aug 20, 2015 at 2:23 PM, James Molloy <span dir="ltr"><<a href="mailto:james@jamesmolloy.co.uk" target="_blank">james@jamesmolloy.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Xiangyang,<div><br></div><div>The algorithm for loop unrolling was changed post-3.5 to do more what you'd expect. If you use 3.6 or 3.7 you'll likely get better results.</div><div><br></div><div>Cheers,</div><div><br></div><div>James</div></div><div><div><br><div class="gmail_quote"><div dir="ltr">On Thu, 20 Aug 2015 at 18:09 Philip Reames via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
    <div>On 08/20/2015 07:38 AM, Xiangyang Guo
      via llvm-dev wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr"><span style="font-size:12.8000001907349px">Hi, </span>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">I want to use loop
          unrolling pass, however, I find that loop unrolling will
          introduces conditional branch at end of every "unrolled" part.
          For example, consider the following code</div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">
          <div><i>void foo( int n, int array_x[])</i></div>
          <div><i>{</i></div>
          <div><i>    for (int i=0; i < n; i++)</i></div>
          <div><i><span style="white-space:pre-wrap"> </span>  
               array_x[i] = i; </i></div>
          <div><i>}</i></div>
        </div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">Then I use this
          command "opt-3.5 try.bc -mem2reg -loops -loop-simplify
          -loop-rotate -lcssa -indvars -loop-unroll -unroll-count=3
          -simplifycfg -S", it gives me this IR:</div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">
          <div><i>define void @_Z3fooiPi(i32 %n, i32* %array_x) #0 {</i></div>
          <div><i>  %1 = icmp slt i32 0, %n</i></div>
          <div><i>  br i1 %1, label %.<a href="http://lr.ph/" target="_blank">lr.ph</a>, label
              %._crit_edge</i></div>
          <div><i><br>
            </i></div>
          <div><i>.<a href="http://lr.ph/" target="_blank">lr.ph</a>:                              
                          ; preds = %0, %7</i></div>
          <div><i>  %indvars.iv = phi i64 [ %indvars.iv.next.2, %7 ], [
              0, %0 ]</i></div>
          <div><i>  %2 = getelementptr inbounds i32* %array_x, i64
              %indvars.iv</i></div>
          <div><i>  %3 = trunc i64 %indvars.iv to i32</i></div>
          <div><i>  store i32 %3, i32* %2</i></div>
          <div><i>  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1</i></div>
          <div><i>  %lftr.wideiv = trunc i64 %indvars.iv.next to i32</i></div>
          <div><i>  %exitcond = icmp ne i32 %lftr.wideiv, %n</i></div>
          <div><i>  br i1 %exitcond, label %4, label %._crit_edge</i></div>
          <div><i><br>
            </i></div>
          <div><i>._crit_edge:                                      ;
              preds = %.<a href="http://lr.ph/" target="_blank">lr.ph</a>, %4, %7, %0</i></div>
          <div><i>  ret void</i></div>
          <div><i><br>
            </i></div>
          <div><i>; <label>:4                                    
                ; preds = %.<a href="http://lr.ph/" target="_blank">lr.ph</a></i></div>
          <div><i>  %5 = getelementptr inbounds i32* %array_x, i64
              %indvars.iv.next</i></div>
          <div><i>  %6 = trunc i64 %indvars.iv.next to i32</i></div>
          <div><i>  store i32 %6, i32* %5</i></div>
          <div><i>  %indvars.iv.next.1 = add nuw nsw i64
              %indvars.iv.next, 1</i></div>
          <div><i>  %lftr.wideiv.1 = trunc i64 %indvars.iv.next.1 to i32</i></div>
          <div><i>  %exitcond.1 = icmp ne i32 %lftr.wideiv.1, %n</i></div>
          <div><i>  br i1 %exitcond.1, label %7, label %._crit_edge</i></div>
          <div><i><br>
            </i></div>
          <div><i>; <label>:7                                    
                ; preds = %4</i></div>
          <div><i>  %8 = getelementptr inbounds i32* %array_x, i64
              %indvars.iv.next.1</i></div>
          <div><i>  %9 = trunc i64 %indvars.iv.next.1 to i32</i></div>
          <div><i>  store i32 %9, i32* %8</i></div>
          <div><i>  %indvars.iv.next.2 = add nuw nsw i64
              %indvars.iv.next.1, 1</i></div>
          <div><i>  %lftr.wideiv.2 = trunc i64 %indvars.iv.next.2 to i32</i></div>
          <div><i>  %exitcond.2 = icmp ne i32 %lftr.wideiv.2, %n</i></div>
          <div><i>  br i1 %exitcond.2, label %.<a href="http://lr.ph/" target="_blank">lr.ph</a>, label
              %._crit_edge</i></div>
          <div><i>}</i></div>
        </div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">As you can see, at the
          end of BB <label>4 and BB<label>7 there are "add",
          "icmp" and "br" instrcutions to check the boundary. I
          understand this is for the correctness. However, I would
          expect the loop unrolling can change my code to something like
          this:</div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">
          <div><i>void foo( int n, int array_x[])</i></div>
          <div><i>{</i></div>
          <div><i>    int j = n%3;</i></div>
          <div><i>    int m = n - j;</i></div>
          <div><i>    for (int i=0; i < m; i+=3){</i></div>
          <div><i><span style="white-space:pre-wrap"> </span>  
               array_x[i] = i;</i></div>
          <div><i><span style="white-space:pre-wrap"> </span>  
               array_x[i+1] = i+1;</i></div>
          <div><i><span style="white-space:pre-wrap"> </span>  
               array_x[i+2] = i+2; </i></div>
          <div><i>    }</i></div>
          <div><i>    for(i=m; i<n; i++)</i></div>
          <div><i><span style="white-space:pre-wrap"> </span>  
               array_x[i] = i;        </i></div>
          <div><i>}</i></div>
        </div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">In this case, the
          BB<label>4 and BB<label>7 will do not have the
          "add", "icmp" and "br" instructions because these BBs can be
          merged together.</div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">How can I achieve
          this? Thanks.</div>
      </div>
    </blockquote></div><div bgcolor="#FFFFFF" text="#000000">
    One - rather heavy weight - way to do this would be to add the -irce
    pass after the loop unroll step.  InductiveRangeCheckElimination
    will introduce a post loop so as to eliminate the range checks in
    the inner loop.  This might not be the ideal transformation for this
    code, but it might get you closer to what you want.<br>
    <br>
    A couple of caveats:<br>
    - 3.5 isn't recent enough to have a stable IRCE.  Download ToT.<br>
    - IRCE requires profiling information on the branches.  I'd start by
    manually annotating your IR to see if it works, then exploring a
    profile build if it does.  <br>
    <br>
    For the record, teaching the unroller to do this transformation (or
    a creating a new pass) would seem interesting.  You might check with
    Chandler and/or Michael (see recent review threads) for what their
    plans in this area are.  <br></div><div bgcolor="#FFFFFF" text="#000000">
    <blockquote type="cite">
      <div dir="ltr">
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">Regards,</div>
        <div style="font-size:12.8000001907349px"><br>
        </div>
        <div style="font-size:12.8000001907349px">Xiangyang</div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      <pre>_______________________________________________
LLVM Developers mailing list
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
    </blockquote>
    <br>
  </div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</div></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>