public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "gcc at rabensky dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/104515] New: trivially-destructible destructors interfere with loop optimization - maybe related to lifetime-dse. Date: Sat, 12 Feb 2022 20:16:20 +0000 [thread overview] Message-ID: <bug-104515-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104515 Bug ID: 104515 Summary: trivially-destructible destructors interfere with loop optimization - maybe related to lifetime-dse. Product: gcc Version: og11 (devel/omp/gcc-11) Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: gcc at rabensky dot com Target Milestone: --- This issue started in GCC-9.1, but a change in GCC-11 made it worse. It didn't exist in GCC-7.1-GCC-8.5 Short description: ----------------- When we have a loop that can be optimized out, calling the destructor for a trivially-destructible type will prevent the optimization starting from GCC-9.1 These are loops that correctly optimized out in GCC-7.1 to GCC-8.5 This bug doesn't happen if we set -fno-lifetime-dse Interestingly enough - a non-trivially-destructible destructor doesn't necessarily prevent the optimization. How this became worse in GCC-11: ------------------------------- In GCC-11 this also applies to calling the destructor of basic types (int, long etc.) So loops that optimized in GCC-7.1 to GCC-10.3 no longer optimize. Short reproducing example: ------------------------- NOTE: No `include`s are needed ``` using T = int; struct Vec { T* end; }; void pop_back_many(Vec& v, unsigned n) { for (unsigned i = 0; i < n; ++i) { --v.end; v.end->~T(); } } ``` compiled with `-O3 -Wall` In GCC-7 to GCC-10, `pop_back_many` optimizes out the loop (becomes `v.end-=n`). In GCC-11, the loop remains. See https://godbolt.org/z/vTexxhxP9 NOTE that adding `-fno-lifetime-dse` will re-enable the loop optimization. Why this matters ---------------- This prevents optimization of a loop over `std::vector<int>::pop_back()`, which is a very common usecase! Loops that optimize out in GCC-7.1 to GCC-10.3 will suddenly not optimize in GCC-11.1/2, making existing code run MUCH slower! (O(n) instead of O(1)) NOTE: std::vector<int>::resize is a lot slower than loop over pop_back. A loop over pop_back is currently the most efficient way to do pop_back_many! More complete reproducing example: --------------------------------- - We can replace the type `T` with a class that is trivially destructible. **In that case, the problem exists in previous versions of GCC as well** - We can replace the type `T` with a class that had user-supplied destructor. **In that case, the loop correctly optimizes out if possible** Actual examples: https://godbolt.org/z/7WqTPq3cE compiled with `-O3 -Wall` ``` template <typename T> struct Vec { T* end; }; template <typename T> void pop_back_many(Vec<T>& v, unsigned n) { for (unsigned i = 0; i < n; ++i) { --v.end; v.end->~T(); } } struct TrivialDestruct { ~TrivialDestruct()=default; }; struct NoopDestruct { ~NoopDestruct(){} }; unsigned count=0; struct CountDestruct { ~CountDestruct(){++count;} }; // Here loop optimization fails in GCC-11.1-11.2 // But succeeds in GCC 7.1-10.3 // // NOTE that adding -fno-lifetime-dse re-enabled the optimization template void pop_back_many(Vec<int>&, unsigned); // Here loop optimization fails in GCC-9.1-11.2 // But succeeds in GCC 7.1-8.5 // // NOTE that adding -fno-lifetime-dse re-enabled the optimization template void pop_back_many(Vec<TrivialDestruct>&, unsigned); // Here loop optimization succeeds in all versions // // NOTE that it's surprising that a no-op destructor can be optimized // but a trivial destructor can't template void pop_back_many(Vec<NoopDestruct>&, unsigned); // Here loop optimization succeeds in all version // // NOTE that it's surprising that a destructor with an action // can be optimized, but a trivial destructor can't template void pop_back_many(Vec<CountDestruct>&, unsigned); ```
next reply other threads:[~2022-02-12 20:16 UTC|newest] Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-02-12 20:16 gcc at rabensky dot com [this message] 2022-02-14 9:32 ` [Bug tree-optimization/104515] [11/12 Regression] " rguenth at gcc dot gnu.org 2022-02-15 20:28 ` gcc at rabensky dot com 2022-02-15 21:02 ` redi at gcc dot gnu.org 2022-02-23 18:22 ` gcc at rabensky dot com 2022-04-21 7:51 ` rguenth at gcc dot gnu.org 2023-04-12 14:05 ` [Bug tree-optimization/104515] [11/12/13 " rguenth at gcc dot gnu.org 2023-04-17 13:02 ` rguenth at gcc dot gnu.org 2023-05-29 10:06 ` [Bug tree-optimization/104515] [11/12/13/14 " jakub at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-104515-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).