[llvm-bugs] [Bug 42499] New: Missed Optimization with for_each, transform and raw loop with small std::array

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 3 08:58:15 PDT 2019


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

            Bug ID: 42499
           Summary: Missed Optimization with for_each, transform and raw
                    loop with small std::array
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: nathanoliver60097 at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

The compiler fails to optimize for_each, transform and a raw for loop into the
same code for small std::array's.  With the following code

#include <array>
#include <algorithm>


static void ForEach(benchmark::State& state) {
  std::array<bool, sizeof(short) * 8> a;
  std::fill(a.begin(), a.end(), true);

  for (auto _ : state) {
    std::for_each(a.begin(), a.end(), [](auto & arg) { arg = !arg; });
    benchmark::DoNotOptimize(a);
  }
}
BENCHMARK(ForEach);

static void Transform(benchmark::State& state) {
  std::array<bool, sizeof(short) * 8> a;
  std::fill(a.begin(), a.end(), true);

  for (auto _ : state) {
    std::transform(a.begin(), a.end(), a.begin(), [](auto arg) { return !arg;
});
    benchmark::DoNotOptimize(a);
  }
}
BENCHMARK(Transform);


static void RawLoop(benchmark::State& state) {
  std::array<bool, sizeof(short) * 8> a;
  std::fill(a.begin(), a.end(), true);

  for (auto _ : state) {
    for (int i = 0; i < a.size(); i++) {
      a[i] = !a[i];
    }
    benchmark::DoNotOptimize(a);
  }
}
BENCHMARK(RawLoop);

Running on quick-bench the raw loop is about 5 times faster
(http://quick-bench.com/F0C__3bEIG0maZh_vQddQNWQUUQ).  If the array size is
increased then the compiler gets the same results for all 3 functions
(http://quick-bench.com/eC1fp41UYz7YK8WQOFfJokqfduw).  GCC on the other hand
will optimize the small array case to the same performance for all 3 functions
(http://quick-bench.com/UCmqDfqqWtlHOMscX01RTIHqVHU)

-- 
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/20190703/38299d6f/attachment.html>


More information about the llvm-bugs mailing list