public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "thiago at kde dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/104475] [12/13 Regression] Wstringop-overflow + atomics incorrect warning on dynamic object
Date: Tue, 06 Dec 2022 18:03:33 +0000	[thread overview]
Message-ID: <bug-104475-4-fnWzrcxdYh@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104475-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #19 from Thiago Macieira <thiago at kde dot org> ---
(In reply to Richard Biener from comment #15)
> Thanks, it's still the same reason - we isolate a nullptr case and end up
> with
> 
> __atomic_or_fetch_4 (184B, 64, 0); [tail call]
> 
> The path we isolate is d->m_mutex == nullptr && !enable in
> 
> void QFutureInterfaceBase::setThrottled(bool enable)
> {
>     QMutexLocker lock(&d->m_mutex);

Thank you for the analysis, Richard. But do note that it's &d->m_mutex, not
d->m_mutex that is passed to the locker. C++ says that if you do d-> then d !=
nullptr, so &d->m_mutex can't be nullptr either.

However, I guess GCC thinks it can be because the offset of m_mutex in QFIBP is
zero. pahole says:

public:
        void QFutureInterfaceBasePrivate(class QFutureInterfaceBasePrivate *,
enum State);
        void ~QFutureInterfaceBasePrivate(class QFutureInterfaceBasePrivate *,
int);

        class QMutex              m_mutex;               /*     0     8 */
        class QBasicMutex         continuationMutex;     /*     8     8 */

So there's a missed optimisation here. But it doesn't look like GCC is the only
one to miss it, see https://gcc.godbolt.org/z/WW5hbW6sW. Maybe it's an
intentional choice?

> we predict the path to be unlikely but the adjustment to the threader
> covered probably never executed paths (with probability zero).  The
> threading opportunity arises because the DTOR calls
> 
>     inline void unlock() noexcept
>     {   
>         if (!isLocked)
>             return;
>         m->unlock();
>         isLocked = false;
>     }
> 
> and we know isLocked on the nullptr path.

We know it can't be true.

> I thought we could maybe enhance prediction to look for nullptr based
> accesses but at the time we estimate probabilities the QMutexLocker
> CTOR isn't yet inlined (the DTOR is partially inlined, exposing the
> isLocked check).
> 
> Note the "impossible" path is actually in the sources - so there might
> be a missing conditional somewhere.

I don't see it, but that's probably because I'm looking at it from the C++
side. If the mutex pointer that was passed is null, then isLocked is never set
to true. What you're saying is that the unlock() function above was inlined and
that GCC knew m to be nullptr, but didn't know isLocked's value... which makes
no sense to me. If the constructor wasn't inlined, it couldn't know the value
of m either. If the constructor was inlined, then it should know the value of
both.

Anyway, this discussion made me realise there's a series of changes to
QMutexLocker ending in "QMutexLocker: strenghten the locking operations"
(https://code.qt.io/cgit/qt/qtbase.git/commit/?id=1b1456975347b044c11169458b53c9f6083dbc59).
This probably did change how the optimiser works, explaining why the warnings
went away.

But it shouldn't have. We went from

    inline ~QMutexLocker() {
        unlock();
    }
    inline void unlock() noexcept
    {
        if (!isLocked)
            return;
        m->unlock();
        isLocked = false;
    }

to

    inline ~QMutexLocker()
    {
        if (m_isLocked)
            unlock();
    }
    inline void unlock() noexcept
    {
        Q_ASSERT(m_isLocked);
        m_mutex->unlock();
        m_isLocked = false;
    }

with the Q_ASSERT expanding to nothing in release builds, it should be
effectively identical code.

  parent reply	other threads:[~2022-12-06 18:03 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-09 19:37 [Bug c++/104475] New: " thiago at kde dot org
2022-02-09 20:14 ` [Bug tree-optimization/104475] [12 Regression] " pinskia at gcc dot gnu.org
2022-02-09 23:48 ` msebor at gcc dot gnu.org
2022-02-16 14:56 ` jakub at gcc dot gnu.org
2022-03-03 14:00 ` aldyh at gcc dot gnu.org
2022-03-03 16:09 ` amacleod at redhat dot com
2022-03-03 17:55 ` amacleod at redhat dot com
2022-03-04 14:47 ` amacleod at redhat dot com
2022-03-09 13:13 ` rguenth at gcc dot gnu.org
2022-03-12  9:39 ` aldyh at gcc dot gnu.org
2022-03-23 12:47 ` rguenth at gcc dot gnu.org
2022-03-23 17:23 ` aldyh at gcc dot gnu.org
2022-05-06  8:32 ` [Bug tree-optimization/104475] [12/13 " jakub at gcc dot gnu.org
2022-07-26 12:59 ` rguenth at gcc dot gnu.org
2022-12-05 15:50 ` rguenth at gcc dot gnu.org
2022-12-05 16:36 ` thiago at kde dot org
2022-12-06  8:17 ` rguenth at gcc dot gnu.org
2022-12-06  9:11 ` rguenth at gcc dot gnu.org
2022-12-06 10:22 ` cvs-commit at gcc dot gnu.org
2022-12-06 10:26 ` rguenth at gcc dot gnu.org
2022-12-06 18:03 ` thiago at kde dot org [this message]
2022-12-07  9:46 ` rguenth at gcc dot gnu.org
2022-12-07  9:48 ` rguenth at gcc dot gnu.org
2022-12-07  9:49 ` rguenth at gcc dot gnu.org
2022-12-07  9:54 ` rguenth at gcc dot gnu.org
2022-12-07 11:25 ` rguenth at gcc dot gnu.org
2023-01-17 17:37 ` jason at gcc dot gnu.org
2023-05-08 12:23 ` [Bug tree-optimization/104475] [12/13/14 " rguenth at gcc dot gnu.org
2023-09-18  9:19 ` aph at gcc dot gnu.org
2023-09-18 10:04 ` rguenth 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-104475-4-fnWzrcxdYh@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: link
Be 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).