[llvm-bugs] [Bug 47020] New: Reduction loop repeated due to loop invariant variable

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 6 11:39:12 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47020

            Bug ID: 47020
           Summary: Reduction loop repeated due to loop invariant variable
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm-dev at redking.me.uk
                CC: florian_hahn at apple.com, listmail at philipreames.com,
                    llvm-bugs at lists.llvm.org, spatel+llvm at rotateright.com

Pulled out of a code example discussed here:

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

#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

https://godbolt.org/z/x79oPx

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;
    }
}

https://godbolt.org/z/168GMz

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200806/e57e8b49/attachment.html>


More information about the llvm-bugs mailing list