[llvm-dev] [CodeGen][AtomicExpandPass] Initial load not atomic?

Itay Bookstein via llvm-dev llvm-dev at lists.llvm.org
Sun Jan 24 08:42:22 PST 2021


Hey all,

While reading through AtomicExpand::insertRMWCmpXchgLoop I noticed
that the initial load is documented to be atomic but the code below
generates a non-atomic load. I'm not sure whether it's an actual
problem or not - could that be mis-compiled or mis-optimized?

Inline documentation comment here
https://github.com/llvm/llvm-project/blob/8b9df70bf7e7b812715a3dc9772719188e0df06c/llvm/lib/CodeGen/AtomicExpandPass.cpp#L1394

Example:

define dso_local zeroext i16 @foo(i16* %0, i16 zeroext %1, i16 zeroext
%2) local_unnamed_addr #0 {
%4 = atomicrmw volatile nand i16* %0, i16 %1 seq_cst
ret i16 %4
}

Running opt -atomic-expand gives:

define dso_local zeroext i16 @foo(i16* %0, i16 zeroext %1, i16 zeroext
%2) local_unnamed_addr #0 {
%4 = load i16, i16* %0, align 2
br label %atomicrmw.start

atomicrmw.start: ; preds = %atomicrmw.start, %3
%loaded = phi i16 [ %4, %3 ], [ %newloaded, %atomicrmw.start ]
%5 = and i16 %loaded, %1
%new = xor i16 %5, -1
%6 = cmpxchg i16* %0, i16 %loaded, i16 %new seq_cst seq_cst
%success = extractvalue { i16, i1 } %6, 1
%newloaded = extractvalue { i16, i1 } %6, 0
br i1 %success, label %atomicrmw.end, label %atomicrmw.start

atomicrmw.end: ; preds = %atomicrmw.start
ret i16 %newloaded
}


More information about the llvm-dev mailing list