public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97640] New: GCC doesn't optimize-out outside-affecting lambdas within y-combinator while clang does.
@ 2020-10-30 14:29 hotguest1 at hotmail dot com
  2020-10-30 14:39 ` [Bug c++/97640] " hotguest1 at hotmail dot com
  0 siblings, 1 reply; 2+ messages in thread
From: hotguest1 at hotmail dot com @ 2020-10-30 14:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97640
           Summary: GCC doesn't optimize-out outside-affecting lambdas
                    within y-combinator while clang does.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hotguest1 at hotmail dot com
  Target Milestone: ---

Suppose we have a y-combinator, so that we can write lambdas in recursive
fashion.

    template <class F>
    struct y_combinator {
        F f;
        template <class... Ts>
        constexpr decltype(auto) operator()(Ts&&... ts) const { return
f(std::ref(*this), std::forward<Ts>(ts)...); }

        template <class... Ts>
        constexpr decltype(auto) operator()(Ts&&... ts) { return
f(std::ref(*this), std::forward<Ts>(ts)...); }
    };


Let's have a constexpr-qualified lambda

    auto sum = y_combinator{[&](auto self,int a,int b,int res = 0) mutable {
        if (a>b) {
          return res;
        }
        res += a;
        return self(a+1,b,res);
    }};

This lambda is constexpr-qualified.
So both gcc and clang do optimize-out all the things in the name of constexpr
expansion.
https://compiler-explorer.com/z/qx4MGE

==============================================================================

Let's try an outside-affecting lambda

    auto sum = y_combinator{[&,res=0](auto self,int a,int b) mutable {
        if (a>b) {
          return res;
        }
        res += a;
        return self(a+1,b);
    }};

This non-constexpr-qualified lambda is optimized-out in clang but in gcc it is
not.
https://compiler-explorer.com/z/hPGWod

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

* [Bug c++/97640] GCC doesn't optimize-out outside-affecting lambdas within y-combinator while clang does.
  2020-10-30 14:29 [Bug c++/97640] New: GCC doesn't optimize-out outside-affecting lambdas within y-combinator while clang does hotguest1 at hotmail dot com
@ 2020-10-30 14:39 ` hotguest1 at hotmail dot com
  0 siblings, 0 replies; 2+ messages in thread
From: hotguest1 at hotmail dot com @ 2020-10-30 14:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from sandthorn <hotguest1 at hotmail dot com> ---
The y-combinator implementation is from Barry's and Mathemagician's answer on
SO.
https://stackoverflow.com/a/40873657/6370128

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

end of thread, other threads:[~2020-10-30 14:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-30 14:29 [Bug c++/97640] New: GCC doesn't optimize-out outside-affecting lambdas within y-combinator while clang does hotguest1 at hotmail dot com
2020-10-30 14:39 ` [Bug c++/97640] " hotguest1 at hotmail dot com

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).