<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi, <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>This patch contains code to unroll loops that contain a run-time trip count.  It extends the existing code, in the LoopUnroll and LoopUnrollPass classes, that unrolls loops with compile-time trip counts.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The ability to unroll loops with run-time trip counts using this patch is turned off by default.  To enable the transformation, I added an option, –unroll-runtime.  It’s probably best to keep it disabled for now since some programs may degrade in performance with the option enabled.  We’re hoping that some more tuning will help minimize any performance regressions.  Of course, we do see performance improvements as well.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I tested llvm with the patch and it passes ‘make check-all’ and there are no regressions in test-suite when using the ‘simple’ test target.  I have also enabled the option and tested using test-suite, and there are no regressions with the option enabled.    With the option enabled, there will be some errors with the tests when running ‘make check-all’ for two reasons.  The existing tests assume that there is no loop unrolling for run-time trip counts, and my implementation requires that loop simplify is run afterward (I am planning on fixing this issue).  I’ve also tested the code on a different test suite for ARM and our soon to be added backend.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>This implementation works by using the existing loop unrolling code to actually unroll the loop by some unroll factor (the default is 8 iterations).   This patch generates code prior to the loop to compute the number of extra iterations to execute before entering the unrolled loop.  The number of extra iterations is the run-time trip count modulo the unroll factor.  We generate code to check the number of extra iterations and branch to the extra copies of the loop body that  execute the extra iterations before entering the unrolled loop.  We generate an if-then-else sequence, which may get converted to a switch statement by LLVM.  The patch generates ‘unroll factor – 1’ copies of the loop body prior to the loop to execute these extra iterations.  <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>This implementation only allow unroll factors that are a power to 2 to reduce the cost of computing the number of extra iterations.  There are other limitations in implementation including only allowing loops with a single exit that occurs in the latch block.  There is certainly some room for improving the code, but it works and improves performance on some actual benchmarks.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The patch includes changes to the following files:<o:p></o:p></p><p class=MsoNormal>   lib/Transforms/Scalar/LoopUnrollPass.cpp<o:p></o:p></p><p class=MsoNormal>   lib/Transfoms/Utils/LoopUnroll.cpp<o:p></o:p></p><p class=MsoNormal>   include/llvm/Transforms/Utils/UnrollLoop.h<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>And new files in the test directory: test/Transforms/LoopUnroll/runtime-loop[1-3].ll<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I appreciate any comments, questions, etc. on the code.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks,<o:p></o:p></p><p class=MsoNormal>-- Brendon Cahoon<o:p></o:p></p><p class=MsoNormal><span style='font-size:10.5pt;font-family:Consolas'>--<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.5pt;font-family:Consolas'>Qualcomm Innovation Center, Inc is a member of Code Aurora Forum<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>