[llvm] [AtomicExpandPass][NVPTX] Preserve atomic and volatile nature of emulated operations (PR #188361)

Akshay Deodhar via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 18:07:59 PDT 2026


================
@@ -1163,6 +1164,22 @@ bool AtomicExpandImpl::expandPartwordCmpXchg(AtomicCmpXchgInst *CI) {
   PHINode *Loaded_MaskOut = Builder.CreatePHI(PMV.WordType, 2);
   Loaded_MaskOut->addIncoming(InitLoaded_MaskOut, BB);
 
+  // The initial load must be atomic with the same synchronization scope
+  // to avoid a data race with concurrent stores. If the instruction being
+  // emulated is volatile, issue a volatile load.
+  // addIncoming is done first so that expandAtomicLoadToLibcall's
+  // replaceAllUsesWith correctly updates the PHI incoming value.
+  InitLoaded->setVolatile(CI->isVolatile());
+  if (TLI->issueAtomicInitLoadForAtomicEmulation()) {
+    // TODO: Get rid of the target hook once all backends start issuing
+    // atomic loads.
----------------
akshayrdeodhar wrote:

Got rid of the hooks, and found 3 kinds of failures:
- Caused by change to codegen: updated these either using the scripts, or mechanically (using claude) to match the new codegen. 
- Crashes caused by newly created atomic loads not being processed by AtomicExpandPass. (The atomic load if tested in an independent test does not cause a crash). To fix these, added recursive calls to `processAtomicInstr` on the newly created atomic loads. These calls convert the loads to libcalls, or cast them to integer types.
- Crashes in X86, AMDGPU, and AArch64 caused by unhandled vector types. These loads crash even with upstream LLVM. Fixed these by adding `shouldCastAtomicLoadInIR` to start casting vector types to integers. These backends support vector types for atomicrmw. Ideally, these should be supported for atomic loads as well. 

Added @madhur13490 @sjoerdmeijer @phoebewang for reviews for the other 2 backends. I have not touched these backends before, I would really appreciate assistance with the new codegen, and a critical review of whether my target hook overrides are correct. 

Perhaps the hooks need to be massaged to only support integer types that fit within supported atomic integer bitwidths? Or do we rely on libcalls?


https://github.com/llvm/llvm-project/pull/188361


More information about the llvm-commits mailing list