<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">test.cpp:<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">int main() {<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">int ret = 0;<o:p></o:p></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++) {<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">    ret += i;<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">}<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">return ret;<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">}<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><o:p style="background-color: rgba(255, 255, 255, 0);"> </o:p></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">Hi,<o:p></o:p></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.<o:p></o:p></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.<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">Here are my attempts:<o:p></o:p></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)<o:p></o:p></span></li></ol><p class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 0.5in;"><span style="background-color: rgba(255, 255, 255, 0);">It did not unroll the loop.<o:p></o:p></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<o:p></o:p></span></li></ol><p class="MsoListParagraph" 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<o:p></o:p></span></p><p class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 0.5in;"><span style="background-color: rgba(255, 255, 255, 0);">It did not unroll the loop.<o:p></o:p></span></p><p class="MsoListParagraph" 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…..”<o:p></o:p></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<o:p></o:p></span></li></ol><p class="MsoListParagraph" 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).<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><o:p style="background-color: rgba(255, 255, 255, 0);"> </o:p></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.<o:p></o:p></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?<o:p></o:p></span></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><o:p style="background-color: rgba(255, 255, 255, 0);"> </o:p></p><p class="MsoNormal" style="margin: 0in 0in 0.0001pt;"><span style="background-color: rgba(255, 255, 255, 0);">Thanks,<o:p></o:p></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></body></html>