<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 - Loop unroll may be too aggressive to unroll constant-trip-count loops, which could disable LSR"
   href="https://bugs.llvm.org/show_bug.cgi?id=51084">51084</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Loop unroll may be too aggressive to unroll constant-trip-count loops, which could disable LSR
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>zsrkmyn@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>We found that loop unroll pass may be too aggressive to unroll
constant-trip-count loops, which can disable LSR pass and generate inefficient
code.


Reproducer:

// test.c
#define SIZE (16*1024+4)
float a[SIZE];
void bar() {
#ifdef UNROLL64
# pragma unroll(64)
#else
# pragma unroll
#endif
  for (int i = 0; i < SIZE - 4; i++)
    a[i] = a[i + 4];
}


Command:
$ clang -O2 -fno-slp-vectorize -fno-vectorize -S -DUNROLL64 test.c -o -
$ clang -O2 -fno-slp-vectorize -fno-vectorize -S test.c -o -


When checking the output, we noticed that there's a lot unnecessary mov+or
instructions to calculate the index of the array when UNROLL64 is undefined.
See below.


# w/ UNROLL64
movss   -140(%rdi,%rax,4), %xmm0        # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, -156(%rdi,%rax,4)
movss   -136(%rdi,%rax,4), %xmm0        # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, -152(%rdi,%rax,4)
movss   -132(%rdi,%rax,4), %xmm0        # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, -148(%rdi,%rax,4)
movss   -128(%rdi,%rax,4), %xmm0        # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, -144(%rdi,%rax,4)
...

# w/o UNROLL64
movq    %rsi, %rcx
orq     $256, %rcx                      # imm = 0x100
movss   (%rdi,%rcx), %xmm0              # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, (%rdi,%rdx)
movq    %rsi, %rdx
orq     $260, %rdx                      # imm = 0x104
movss   (%rdi,%rdx), %xmm0              # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, (%rdi,%rbx)
movq    %rsi, %rbx
orq     $264, %rbx                      # imm = 0x108
movss   (%rdi,%rbx), %xmm0              # xmm0 = mem[0],zero,zero,zero
movss   %xmm0, (%rdi,%r9)
...



Triage note:

We noticed that these mov/or insts are eliminated in LSR pass, however, when
the unroll factor isn't specified, the loop is unrolled with count 2048 because
of a big partial unroll threshold (16*1024).

  // llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
  static cl::opt<unsigned> PragmaUnrollThreshold(
      "pragma-unroll-threshold", cl::init(16 * 1024),
  ...
  if (ExplicitUnroll && TripCount != 0) {
    // If the loop has an unrolling pragma, we want to be more aggressive with
    // unrolling limits. Set thresholds to at least the PragmaUnrollThreshold
    // value which is larger than the default limits.
    UP.Threshold = std::max<unsigned>(UP.Threshold, PragmaUnrollThreshold);
    UP.PartialThreshold =
        std::max<unsigned>(UP.PartialThreshold, PragmaUnrollThreshold);
  }


Then LSR just rejected to optimize the unrolled loop.

$ clang -O2 -fno-slp-vectorize -fno-vectorize -S test.c -emit-llvm -o - | opt
-loop-reduce -debug-only=loop-reduce -disable-output
LSR skipping loop, too many IV Users in 0x62c2f50</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>