<div dir="ltr">It may be that clang added the `optnone` attribute to main when you built it in (b): <a href="https://godbolt.org/g/WhM1UA" target="_blank">https://godbolt.org/g/WhM1UA</a> . optnone, as the name implies, requests that optimizations are skipped for a given function. If this is the case, removing the optnone might make (b) do what you want.<div><br></div><div>For (c), I'd introduce something that may have side-effects, so LLVM can't easily model+eliminate the entire loop. Examples of this being an external function call (<a href="https://godbolt.org/g/toqR6b" target="_blank">https://godbolt.org/g/toqR6b</a>) or making your int volatile (<a href="https://godbolt.org/g/4svW7F" target="_blank">https://godbolt.org/g/4svW7F</a>).</div><div><br></div><div>George</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 26, 2018 at 12:48 PM luck.caile via cfe-users <<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@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 dir="auto"><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">test.cpp:<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">int main() {<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">int ret = 0;<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">for (int i = 0; i < 100; i++) {<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">    ret += i;<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">}<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">return ret;<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">}<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><u></u> <u></u></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">Hi,<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">I’m new to clang/llvm recently and interested in doing some stuff in optimization passes.<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">I tried to play above simple test with clang and see if I could get expected llvm IR by enabling llvm loop unrolling of a count 2.<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">Here are my attempts:<u></u><u></u></span></p><ol start="1" type="a" style="margin-bottom:0in;margin-top:0in"><li class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">adding pragma before loop in test.cpp: #pragma clang loop unroll_count(2)<u></u><u></u></span></li></ol><p class="m_292017446779459898m_-8582521041933957463m_3949082192492340922MsoListParagraph" style="margin:0in 0in 0.0001pt 0.5in"><span style="background-color:rgba(255,255,255,0)">It did not unroll the loop.<u></u><u></u></span></p><ol start="2" type="a" style="margin-bottom:0in;margin-top:0in"><li class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">clang++ -c -emit-llvm -S -std=c++11 test.cpp<u></u><u></u></span></li></ol><p class="m_292017446779459898m_-8582521041933957463m_3949082192492340922MsoListParagraph" style="margin:0in 0in 0.0001pt 0.5in"><span style="background-color:rgba(255,255,255,0)">opt test.ll -mem2reg -loop-unroll -unroll-count=2 -unroll-allow-partial -S<u></u><u></u></span></p><p class="m_292017446779459898m_-8582521041933957463m_3949082192492340922MsoListParagraph" style="margin:0in 0in 0.0001pt 0.5in"><span style="background-color:rgba(255,255,255,0)">It did not unroll the loop.<u></u><u></u></span></p><p class="m_292017446779459898m_-8582521041933957463m_3949082192492340922MsoListParagraph" style="margin:0in 0in 0.0001pt 0.5in"><span style="background-color:rgba(255,255,255,0)">In addition, by enabling -debug, I saw message “Skipping ‘Unroll Loops’ pass…..”<u></u><u></u></span></p><ol start="3" type="a" style="margin-bottom:0in;margin-top:0in"><li class="MsoNormal" style="text-align:start;margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">clang++ -c -emit-llvm -S -std=c++11 -O1 test.cpp<u></u><u></u></span></li></ol><p class="m_292017446779459898m_-8582521041933957463m_3949082192492340922MsoListParagraph" style="margin:0in 0in 0.0001pt 0.5in"><span style="background-color:rgba(255,255,255,0)">It unrolled the loop completely and directly return the final result(4950).<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><u></u> <u></u></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">I assume that my llvm and clang are latest and installed correctly.<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">Could someone please let me know what I am missing here?<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><u></u> <u></u></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">Thanks,<u></u><u></u></span></p><p class="MsoNormal" style="margin:0in 0in 0.0001pt"><span style="background-color:rgba(255,255,255,0)">Kai</span></p><div></div></div>_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><br>
</blockquote></div>