public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115201] New: Recursive binary search is incorrectly inlined
@ 2024-05-23  1:41 llvm at rifkin dot dev
  2024-05-23  1:54 ` [Bug ipa/115201] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: llvm at rifkin dot dev @ 2024-05-23  1:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115201

            Bug ID: 115201
           Summary: Recursive binary search is incorrectly inlined
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: llvm at rifkin dot dev
  Target Milestone: ---

I was sent the following code 

template<typename T, typename I, typename C>
constexpr I binarySearch(const T& item, const I start, const I end, C&& cmp) {
    if (std::next(start) == end) {
        return start;
    }

    const auto mid = std::next(start, std::distance(start, end) / 2);
    if (cmp(item, *mid)) {
        return binarySearch(item, start, mid, std::forward<C>(cmp));
    }
    return binarySearch(item, mid, end, std::forward<C>(cmp));
}

auto foo(const std::vector<int>& items) {
    return *binarySearch(5, items.begin(), items.end(), std::less{});
}

This causes gcc to aggressively inline the recursive function into itself
eventually emitting 1600 instructions whereas clang emits about 30. Textual
assembly length of course means little as far as performance goes however in
this case it is indicative of something going wrong, even if a TCO'd loop were
somehow unrolled.

It seems that the tail recursion is not transformed during the tailr1 pass and
instead binarySearch is inlined into itself during inline, and additional
inlining happens with foo. https://godbolt.org/z/45r7dvbxr

Imho this sort of inlining makes little sense and either it should be properly
TCO'd or not inlined at all.

This happen going back to at least gcc 7.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-05-23  3:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-23  1:41 [Bug tree-optimization/115201] New: Recursive binary search is incorrectly inlined llvm at rifkin dot dev
2024-05-23  1:54 ` [Bug ipa/115201] " pinskia at gcc dot gnu.org
2024-05-23  2:04 ` llvm at rifkin dot dev
2024-05-23  2:10 ` pinskia at gcc dot gnu.org
2024-05-23  2:11 ` pinskia at gcc dot gnu.org
2024-05-23  2:13 ` [Bug tree-optimization/115201] Recursive binary search with struct iterator is not handled by tail Recursion pinskia at gcc dot gnu.org
2024-05-23  3:01 ` pinskia at gcc dot gnu.org
2024-05-23  3:26 ` [Bug tree-optimization/115201] Recursion not optimized for structs arguments pinskia at gcc dot gnu.org
2024-05-23  3:29 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).