Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-34983

Some std::atomic::fetch_or() or fetch_and() is being avoided unnecessarily

Details

    Description

      There are some compiler bugs around some single-bit test-and-set or test-and-reset operations, mostly affecting the most significant bit of a word.
      https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102566
      https://github.com/llvm/llvm-project/issues/37322

      That is, when the compilers generate code for the IA-32 or AMD64 ISA, they would emit a loop around the LOCK CMPXCHG instruction, instead of emitting LOCK BTS, LOCK BTR, LOCK OR or LOCK AND. Here is a test program:

      #include <atomic>
      uint32_t fa(std::atomic<uint32_t>&a) { return a.fetch_and(~(1U<<31)) & 1U<<31;}
      uint32_t fo(std::atomic<uint32_t>&a) { return a.fetch_or(1U<<31) & 1U<<31;}
      

      Starting with clang++-15 or GCC 7.1 (the first GA release of that GCC series), no LOCK CMPXCHG will be generated for the above.

      Fortunately, the oldest major GCC version that is included in any currently supported GNU/Linux distribution is 7. Also, FreeBSD 14 is using clang version 16. So, there should not be any need to worry about older versions of these compilers anymore.

      However, even the most recent version of MSVC would generate LOCK CMPXCHG for these. We would be forced to use _interlockedbittestandset() or _interlockedbittestandreset() on that platform in order to avoid a performance regression.

      Attachments

        Activity

          marko Marko Mäkelä created issue -
          marko Marko Mäkelä made changes -
          Field Original Value New Value
          Status Open [ 1 ] In Progress [ 3 ]
          marko Marko Mäkelä made changes -
          Assignee Marko Mäkelä [ marko ] Debarun Banerjee [ JIRAUSER54513 ]
          Status In Progress [ 3 ] In Review [ 10002 ]
          debarun Debarun Banerjee made changes -
          Assignee Debarun Banerjee [ JIRAUSER54513 ] Marko Mäkelä [ marko ]
          Status In Review [ 10002 ] Stalled [ 10000 ]
          marko Marko Mäkelä made changes -
          issue.field.resolutiondate 2024-09-24 07:16:19.0 2024-09-24 07:16:19.141
          marko Marko Mäkelä made changes -
          Fix Version/s 10.6.20 [ 29903 ]
          Fix Version/s 10.11.10 [ 29904 ]
          Fix Version/s 11.2.6 [ 29906 ]
          Fix Version/s 11.4.4 [ 29907 ]
          Fix Version/s 11.6.2 [ 29908 ]
          Fix Version/s 10.6 [ 24028 ]
          Fix Version/s 10.11 [ 27614 ]
          Fix Version/s 11.4 [ 29301 ]
          Resolution Fixed [ 1 ]
          Status Stalled [ 10000 ] Closed [ 6 ]

          People

            marko Marko Mäkelä
            marko Marko Mäkelä
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.