[llvm-bugs] [Bug 25268] New: Performance regression in simple linear search

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 20 21:24:12 PDT 2015


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

            Bug ID: 25268
           Summary: Performance regression in simple linear search
           Product: clang
           Version: 3.7
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: chris at detrino.org
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

struct Ret {
  std::size_t index;
  std::size_t pos;
};

Ret Test1(std::size_t const *array, std::size_t pos) {
  std::size_t index = 0;
  while (true) {
    auto next = array[index];
    if (pos < next) break;
    pos -= next;
    ++index;
  }
  return {index, pos};
}

This generates the following IR with clang 3.7 -O2 -S -c -std=c++11 -emit-llvm:

; Function Attrs: nounwind readonly uwtable
define { i64, i64 } @_Z5Test1PKmm(i64* nocapture readonly %array, i64 %pos) #3
{
  br label %1

; <label>:1                                       ; preds = %1, %0
  %index.0 = phi i64 [ 0, %0 ], [ %index.1, %1 ]
  %.01 = phi i64 [ %pos, %0 ], [ %.1, %1 ]
  %2 = getelementptr inbounds i64, i64* %array, i64 %index.0
  %3 = load i64, i64* %2, align 8, !tbaa !1
  %4 = icmp ult i64 %.01, %3
  %5 = zext i1 %4 to i64
  %6 = xor i64 %5, 1
  %index.1 = add i64 %6, %index.0
  %7 = select i1 %4, i64 0, i64 %3
  %.1 = sub i64 %.01, %7
  br i1 %4, label %8, label %1

; <label>:8                                       ; preds = %1
  %.1.lcssa = phi i64 [ %.1, %1 ]
  %index.1.lcssa = phi i64 [ %index.1, %1 ]
  %9 = insertvalue { i64, i64 } undef, i64 %index.1.lcssa, 0
  %10 = insertvalue { i64, i64 } %9, i64 %.1.lcssa, 1
  ret { i64, i64 } %10
}

And using Clang 3.5 -O2 -S -c -std=c++11 -emit-llvm:

; Function Attrs: nounwind readonly uwtable
define { i64, i64 } @_Z5Test1PKmm(i64* nocapture readonly %array, i64 %pos) #3
{
  %1 = load i64* %array, align 8, !tbaa !1
  %2 = icmp ugt i64 %1, %pos
  br i1 %2, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader,
%.lr.ph
  %3 = phi i64 [ %7, %.lr.ph ], [ %1, %.lr.ph.preheader ]
  %.02 = phi i64 [ %4, %.lr.ph ], [ %pos, %.lr.ph.preheader ]
  %index.01 = phi i64 [ %5, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %4 = sub i64 %.02, %3
  %5 = add i64 %index.01, 1
  %6 = getelementptr inbounds i64* %array, i64 %5
  %7 = load i64* %6, align 8, !tbaa !1
  %8 = icmp ult i64 %4, %7
  br i1 %8, label %._crit_edge.loopexit, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.lcssa6 = phi i64 [ %5, %.lr.ph ]
  %.lcssa = phi i64 [ %4, %.lr.ph ]
  br label %._crit_edge

._crit_edge:                                      ; preds =
%._crit_edge.loopexit, %0
  %.0.lcssa = phi i64 [ %pos, %0 ], [ %.lcssa, %._crit_edge.loopexit ]
  %index.0.lcssa = phi i64 [ 0, %0 ], [ %.lcssa6, %._crit_edge.loopexit ]
  %9 = insertvalue { i64, i64 } undef, i64 %index.0.lcssa, 0
  %10 = insertvalue { i64, i64 } %9, i64 %.0.lcssa, 1
  ret { i64, i64 } %10
}

I have measured 3.7 to be 7x slower than 3.5 on my computer for this function.

This is a reduced testcase from a B+Tree library that caused the overall
implementation to be 2x slower.

I can recover the same assembly using 3.7 that I get in 3.5 by writing it this
way instead:

Ret Test2(std::size_t const *array, std::size_t pos) {
  std::size_t index = 0;
  auto next = array[index];
  while (pos >= next) {
    pos -= next;
    ++index;
    next = array[index];
  }
  return {index, pos};
}

The problem still exists in trunk.

-- 
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/20151021/e78173ef/attachment.html>


More information about the llvm-bugs mailing list