[all-commits] [llvm/llvm-project] b08cb3: [AtomicExpand] Implement SUB → ADD(-x) and FSUB → ...

Yonah Goldberg via All-commits all-commits at lists.llvm.org
Thu Oct 1 17:53:38 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b08cb38bd7bcacb1a740f2dd566eef19c2b9ae40
      https://github.com/llvm/llvm-project/commit/b08cb38bd7bcacb1a740f2dd566eef19c2b9ae40
  Author: Yonah Goldberg <ygoldberg at nvidia.com>
  Date:   2026-10-02 (Fri, 02 Oct 2026)

  Changed paths:
    M llvm/lib/CodeGen/AtomicExpandPass.cpp
    M llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
    M llvm/test/CodeGen/NVPTX/atomicrmw-sm60.ll
    M llvm/test/CodeGen/NVPTX/atomicrmw-sm70.ll
    M llvm/test/CodeGen/NVPTX/atomicrmw-sm90.ll
    M llvm/test/Transforms/AtomicExpand/NVPTX/atomicrmw-i64-sm20.ll
    A llvm/test/Transforms/AtomicExpand/NVPTX/atomicrmw-sub.ll

  Log Message:
  -----------
  [AtomicExpand] Implement SUB → ADD(-x) and FSUB → FADD(-x) (#221425)

Implement ATOMIC_SUB → ATOMIC_ADD(-x) and ATOMIC_FSUB → ATOMIC_FADD(-x).
Many targets have atomic adds, but I don't think any have atomic subs.
This transform prevents cmpxchg expansion of atomic SUB. Enable these
transformations in NVPTX.

The integer variant exists in many different places currently. For
example, NVPTX currently expands it in DAG legalization.

begin AI generated

- SelectionDAG generic legalization:
[LegalizeDAG.cpp:3410](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp#L3410)
- GlobalISel generic legalization:
[LegalizerHelper.cpp:5117](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp#L5117)
- GlobalISel outlined-atomic libcall path, mapping SUB to negated
`LDADD`:
[LegalizerHelper.cpp:919](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp#L919)
and
[LegalizerHelper.cpp:973](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp#L973)
- X86 SelectionDAG, when the result is used:
[X86ISelLowering.cpp:33966](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/X86/X86ISelLowering.cpp#L33966)
- SystemZ i32/i64:
[SystemZISelLowering.cpp:5281](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp#L5281)
- SystemZ i8/i16, constant RHS only:
[SystemZISelLowering.cpp:5238](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp#L5238)
- BPF i32 and i64 instruction-selection patterns:
[BPFInstrInfo.td:1040](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/BPF/BPFInstrInfo.td#L1040)
and
[BPFInstrInfo.td:1062](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/BPF/BPFInstrInfo.td#L1062)
- LoongArch i8/i16 patterns:
[LoongArchInstrInfo.td:2314](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td#L2314)
- LoongArch i32/i64 patterns:
[LoongArchInstrInfo.td:2371](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td#L2371)
- AArch64 GlobalISel/LSE pattern:
[AArch64InstrAtomics.td:546](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/AArch64/AArch64InstrAtomics.td#L546),
with the negating pattern machinery in
[AArch64InstrFormats.td:12625](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/AArch64/AArch64InstrFormats.td#L12625)

- AMDGPU changes only `atomic sub 0` to `atomic add 0`:
[SIISelLowering.cpp:21345](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/AMDGPU/SIISelLowering.cpp#L21345)

end AI generated

It looks like the only place that does the float transformation is
SPIRV:

begin AI generated

- SPIR-V converts `G_ATOMICRMW_FSUB` into `OpAtomicFAddEXT` with an
`OpFNegate`:
[SPIRVInstructionSelector.cpp:1413](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L1413),
with operand-negation machinery at
[SPIRVInstructionSelector.cpp:2614](https://github.com/llvm/llvm-project/blob/4977a0c815c39fd90bb67c270174f2356ee9b5a7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L2614)

end AI generated

I guess less targets have a native atomic fadd, but it looks like
Aarch64 and AMDGPU do, so this could be useful there.

AI assisted.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list