[llvm] [BPF] Use FETCH atomic instruction for monotonic atomicrmw when the result is used (PR #210371)

Gustas JanuĊĦonis via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 5 15:26:55 PDT 2026


================
@@ -1039,51 +1068,43 @@ let Predicates = [BPFHasALU32] in {
 
     // atomic_load_sub can be represented as a neg followed
     // by an atomic_load_add.
-    foreach P = [[atomic_load_sub_i32_monotonic,  XADDW32],
+    foreach P = [[atomic_load_sub_i32_monotonic_nu, XADDW32],
+                 [atomic_load_sub_i32_monotonic_hu, XFADDW32],
                  [atomic_load_sub_i32_acquire,   XFADDW32],
                  [atomic_load_sub_i32_release,   XFADDW32],
                  [atomic_load_sub_i32_acq_rel,   XFADDW32],
                  [atomic_load_sub_i32_seq_cst,   XFADDW32],
                 ] in {
       def : Pat<(P[0] ADDRri:$addr, GPR32:$val), (P[1]  ADDRri:$addr, (NEG_32 GPR32:$val))>;
     }
+}
 
-    foreach P = [// add
-                 [atomic_load_add_i64_monotonic,  XADDD],
-                 [atomic_load_add_i64_acquire,   XFADDD],
-                 [atomic_load_add_i64_release,   XFADDD],
-                 [atomic_load_add_i64_acq_rel,   XFADDD],
-                 [atomic_load_add_i64_seq_cst,   XFADDD],
-                ] in {
-      def : Pat<(P[0] ADDRri:$addr, GPR:$val), (P[1]  ADDRri:$addr, GPR:$val)>;
-    }
+let Predicates = [BPFHasALU32] in {
----------------
LeGusto wrote:

The guard was added, because non-ALU32 CPUs should not use fetch sub operations, which is tested in an existing [test case](https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/BPF/atomics_sub64_relaxed_v1.ll) (it failed in CI before these instructions were explicitly addressed in my next commit). However, this guard is ultimately redundant, as the monotonic pattern I added already lowers to non-fetch sub, thus I moved them like you suggested.

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


More information about the llvm-commits mailing list