[llvm-bugs] [Bug 44969] New: Failure to remove monotonic atomic reads

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Feb 19 16:54:55 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=44969

            Bug ID: 44969
           Summary: Failure to remove monotonic atomic reads
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: hfinkel at anl.gov
                CC: jdoerfert at anl.gov, llvm-bugs at lists.llvm.org

#include <atomic>
#include <memory>

int *convert(std::atomic<int> *in, int n) {
    int *out = (int *) in;
    for (int i = 0; i < n; ++i) {
      int v = in[i].load(std::memory_order_relaxed);
      new (&out[i]) int(v);
    }

    return out;
}

currently generates IR, using clang -O3 -emit-llvm -g0 -fno-unroll-loops
-std=c++17, that looks like this:

define dso_local i32* @_Z7convertPSt6atomicIiEi(%"struct.std::atomic"* %0, i32
%1) local_unnamed_addr #0 personality i32 (...)* @__gxx_personality_v0 {
  %3 = getelementptr %"struct.std::atomic", %"struct.std::atomic"* %0, i64 0,
i32 0, i32 0
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %5, label %7

5:                                                ; preds = %2
  %6 = zext i32 %1 to i64
  br label %8

7:                                                ; preds = %8, %2
  ret i32* %3

8:                                                ; preds = %8, %5
  %9 = phi i64 [ 0, %5 ], [ %12, %8 ]
  %10 = getelementptr inbounds %"struct.std::atomic", %"struct.std::atomic"*
%0, i64 %9, i32 0, i32 0
  %11 = load atomic i32, i32* %10 monotonic, align 4
  %12 = add nuw nsw i64 %9, 1
  %13 = icmp eq i64 %12, %6
  br i1 %13, label %7, label %8
}

and so we successfully remove the store from the placement new, but not the
atomic. AFAIK, there's no reason why we cannot remove it, and we should do so
in order to make it possible for the compiler to remove loops converting atomic
data to non-atomic data (which actually do nothing at all).

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200220/df29b670/attachment-0001.html>


More information about the llvm-bugs mailing list