<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Clang doesn't honor unroll #pragma to disable loop unrolling"
   href="https://bugs.llvm.org/show_bug.cgi?id=50682">50682</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang doesn't honor unroll #pragma to disable loop unrolling
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kobalicek.petr@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Clang doesn't honor unroll #pragma to disable loop unrolling. I tried the
following 3 approaches to disable loop unrolling, but it's never honored. The
code is always autovectorized and unrolled regardless of #pragmas used.

Is there any way to make clang not unroll loops? Because in many cases I know
the loops will have very few iterations and I mark them with compiler specific
pragmas to not unroll, however, clang doesn't really honor them.

Compile flags:
  -O2 -fno-math-errno -mavx2 -std=c++2a

The problem happens with -O2, -O3, and even -Os.

Sample code:

#include <stdint.h>

uint32_t countBits1(uint32_t* x, uint32_t n) {
    uint32_t count = 0;
    #pragma clang loop unroll(disable)
    for (uint32_t i = 0; i < n; i++) {
        count += __builtin_popcount(x[i]);
    }
    return count;
}

uint32_t countBits2(uint32_t* x, uint32_t n) {
    uint32_t count = 0;
    #pragma nounroll
    for (uint32_t i = 0; i < n; i++) {
        count += __builtin_popcount(x[i]);
    }
    return count;
}

uint32_t countBits3(uint32_t* x, uint32_t n) {
    uint32_t count = 0;
    #pragma unroll(1)
    for (uint32_t i = 0; i < n; i++) {
        count += __builtin_popcount(x[i]);
    }
    return count;
}

Compile explorer:
  <a href="https://godbolt.org/z/EcoGqoaYG">https://godbolt.org/z/EcoGqoaYG</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>