From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BD71B385840B; Sat, 29 Oct 2022 10:53:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD71B385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667040828; bh=jAESNLFAeGXxhdS7JAcEDcdgqDcY7rRuuX0DZ61gf74=; h=From:To:Subject:Date:In-Reply-To:References:From; b=d3Ww72Xg8XPV9rZEf9DbOHPdl7BP5gzklMDzV7geClSytVuOjQ6bDEL/O6P0HILKV 9W2+X1VhF2pBof2ervfmXC7NR8mtYoZio3DKmpUaPoZO7TuprjbnthZln8bzi5XlLq jbhwGvS5UcVoI2z2YM7YxLZQA2H8DcrfTEORg+X8= From: "marko.makela at mariadb dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/102566] [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic Date: Sat, 29 Oct 2022 10:53:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: marko.makela at mariadb dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102566 Marko M=C3=A4kel=C3=A4 changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |marko.makela at mariadb do= t com --- Comment #31 from Marko M=C3=A4kel=C3=A4 --- Much of this seems to work in GCC 12.2.0 as well as in clang++-15. For clang there is a related ticket https://github.com/llvm/llvm-project/issues/37322 I noticed a missed optimization in both g++-12 and clang++-15: Some operati= ons involving bit 31 degrade to loops around lock cmpxchg. I compiled it with "= -c -O2" (AMD64) or "-c -O2 -m32 -march=3Di686" (IA-32). #include template void lock_bts(std::atomic &a) { while (!(a.fetch_or(b) & b)); } template void lock_btr(std::atomic &a) { while (a.fetch_and(~b) & b); } template void lock_btc(std::atomic &a) { while (a.fetch_xor(b) & b); } template void lock_bts<1U<<30>(std::atomic &a); template void lock_btr<1U<<30>(std::atomic &a); template void lock_btc<1U<<30>(std::atomic &a); // bug: uses lock cmpxchg template void lock_bts<1U<<31>(std::atomic &a); template void lock_btr<1U<<31>(std::atomic &a); template void lock_btc<1U<<31>(std::atomic &a);=