<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 - Reduction loop repeated due to loop invariant variable"
   href="https://bugs.llvm.org/show_bug.cgi?id=47020">47020</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Reduction loop repeated due to loop invariant variable
          </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>Windows NT
          </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>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>florian_hahn@apple.com, listmail@philipreames.com, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Pulled out of a code example discussed here:

What Everyone Should Know About How Amazing Compilers Are - Matt Godbolt [C++
on Sea 2019]
<a href="https://www.youtube.com/watch?v=w0sz5WbS5AM">https://www.youtube.com/watch?v=w0sz5WbS5AM</a>

#include <vector>

typedef int T;

static T sumA = 0;
static T sumB = 0;

void v3(bool useA, const std::vector<T> &x) {
    for(auto v : x) {
      if (useA)
        sumA += v*v;
      else
        sumB += v*v;
    }
}

-g0 -O3 -march=haswell

<a href="https://godbolt.org/z/x79oPx">https://godbolt.org/z/x79oPx</a>

We repeat the entire reduction loop, with the sumA/sumB result variable
load/store being hoisted out to the start/end - it should be possible to merge
these loops and just select which reference to use before the loop.

If I perform this manually we loose vectorization entirely:

#include <vector>

typedef int T;

static T sumA = 0;
static T sumB = 0;

void v3(bool useA, const std::vector<T> &x) {
    T &sum = useA ? sumA : sumB;
    for(auto v : x) {
        sum += v*v;
    }
}

<a href="https://godbolt.org/z/168GMz">https://godbolt.org/z/168GMz</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>