[llvm] [AtomicExpand] Add elementwise expansion (PR #196464)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 02:23:38 PDT 2026


================
@@ -83,100 +101,133 @@ def get_addrspace_cast(addrspace):
         return " addrspace({})".format(str(addrspace))
 
 
+def get_vector_datatype(datatype, size):
+    return "<{} x {}>".format(size, datatype)
+
+
+def get_vector_datatype_name(datatype, size):
+    return "v{}{}".format(size, DATATYPE_SUFFIXES.get(datatype, datatype))
+
+
+def print_atomicrmw(
+    fp, operation, datatype, ordering, addrspace, llvm_scope, vector_size=None
+):
+    if vector_size is None:
+        template = atomicrmw_func
+        ir_datatype = datatype
+        datatype_name = datatype
+    else:
+        template = atomicrmw_elementwise_func
+        ir_datatype = get_vector_datatype(datatype, vector_size)
+        datatype_name = get_vector_datatype_name(datatype, vector_size)
+
+    print(
+        template.substitute(
+            operation=operation,
+            ordering=ordering,
+            datatype=ir_datatype,
+            datatype_name=datatype_name,
+            addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
+            ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
+            llvm_scope=llvm_scope,
+            addrspace_cast=get_addrspace_cast(addrspace),
+        ),
+        file=fp,
+    )
+
+
 if __name__ == "__main__":
     for sm, ptx in TEST_SM_ARCH_PAIRS:
-        # Slice 1: Keep addrspace, llvm_scope, ordering fixed, generate all possible operations and sizes
-        with open("atomicrmw-sm{}.ll".format(str(sm)), "w") as fp:
-            print(run_statement.substitute(sm=sm, ptx=ptx, ptxfp=ptx / 10.0), file=fp)
-            # Integer operations
-            addrspace, llvm_scope, ordering = 1, "block", "acq_rel"
-            for operation, datatype in product(
-                INTEGER_OPERATIONS, ["i8", "i16", "i32", "i64"]
-            ):
+        for elementwise in [False, True]:
+            vector_sizes = VECTOR_SIZES if elementwise else [None]
+            filename_suffix = "-elementwise" if elementwise else ""
+            with open("atomicrmw{}-sm{}.ll".format(filename_suffix, sm), "w") as fp:
                 print(
-                    atomicrmw_func.substitute(
-                        operation=operation,
-                        ordering=ordering,
-                        datatype=datatype,
-                        addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
-                        ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
-                        llvm_scope=llvm_scope,
-                        addrspace_cast=get_addrspace_cast(addrspace),
-                    ),
+                    run_statement.substitute(sm=sm, ptx=ptx, ptxfp=ptx / 10.0),
                     file=fp,
                 )
 
-            # Floating point add
-            for datatype, operation in product(
-                ["float", "double", "half", "bfloat"], FLOATING_POINT_OPERATIONS
-            ):
-                print(
-                    atomicrmw_func.substitute(
-                        operation=operation,
-                        ordering=ordering,
-                        datatype=datatype,
-                        addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
-                        ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
-                        llvm_scope=llvm_scope,
-                        addrspace_cast=get_addrspace_cast(addrspace),
-                    ),
-                    file=fp,
-                )
-
-            # Slice 2: Keep addrspace, llvm_scope fixed, and generate all possible orderings for operations add and nand.
-            # add is natively supported for larger bitwidths, while nand is emulated always
-            addrspace, llvm_scope = 1, "block"
-            for operation, datatype, ordering in product(
-                ["add", "nand"], ["i8", "i32"], ORDERINGS
-            ):
-                if addrspace == 1 and llvm_scope == "block" and ordering == "acq_rel":
-                    # These are a part of Slice 1
-                    continue
-                print(
-                    atomicrmw_func.substitute(
-                        operation=operation,
-                        ordering=ordering,
-                        datatype=datatype,
-                        addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
-                        addrspace_cast=get_addrspace_cast(addrspace),
-                        ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
-                        llvm_scope=llvm_scope,
-                    ),
-                    file=fp,
-                )
+                # Slice 1: Keep addrspace, llvm_scope, and ordering fixed while
+                # generating every operation and type combination.
+                addrspace, llvm_scope, ordering = 1, "block", "acq_rel"
+                for operation, datatype, vector_size in product(
+                    INTEGER_OPERATIONS,
+                    ["i8", "i16", "i32", "i64"],
+                    vector_sizes,
+                ):
+                    print_atomicrmw(
+                        fp,
+                        operation,
+                        datatype,
+                        ordering,
+                        addrspace,
+                        llvm_scope,
+                        vector_size,
+                    )
 
-            # Slice 3: Keep addrspace (global) and ordering fixed, vary the scope
-            # qualifier. block/global is already covered by Slice 1.
-            addrspace, ordering = 1, "acq_rel"
-            for llvm_scope in [s for s in SCOPE_LLVM_TO_PTX if s != "block"]:
-                for operation, datatype in REPRESENTATIVE_OPS:
-                    print(
-                        atomicrmw_func.substitute(
-                            operation=operation,
-                            ordering=ordering,
-                            datatype=datatype,
-                            addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
-                            addrspace_cast=get_addrspace_cast(addrspace),
-                            ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
-                            llvm_scope=llvm_scope,
-                        ),
-                        file=fp,
+                # Floating-point operations.
+                for datatype, operation, vector_size in product(
+                    ["float", "double", "half", "bfloat"],
+                    FLOATING_POINT_OPERATIONS,
+                    vector_sizes,
+                ):
+                    print_atomicrmw(
+                        fp,
+                        operation,
+                        datatype,
+                        ordering,
+                        addrspace,
+                        llvm_scope,
+                        vector_size,
                     )
 
-            # Slice 4: Keep scope (block) and ordering fixed, vary the address-space
-            # qualifier. global is already covered by Slice 1.
-            llvm_scope, ordering = "block", "acq_rel"
-            for addrspace in [a for a in ADDRSPACE_NUM_TO_ADDRSPACE if a != 1]:
-                for operation, datatype in REPRESENTATIVE_OPS:
-                    print(
-                        atomicrmw_func.substitute(
-                            operation=operation,
-                            ordering=ordering,
-                            datatype=datatype,
-                            addrspace=ADDRSPACE_NUM_TO_ADDRSPACE[addrspace],
-                            addrspace_cast=get_addrspace_cast(addrspace),
-                            ptx_scope=SCOPE_LLVM_TO_PTX[llvm_scope],
-                            llvm_scope=llvm_scope,
-                        ),
-                        file=fp,
+                # Slice 2: Keep addrspace and llvm_scope fixed, and generate all
+                # orderings for add and nand. add is natively supported for larger
+                # bitwidths, while nand is always emulated.
+                addrspace, llvm_scope = 1, "block"
+                for operation, datatype, ordering, vector_size in product(
+                    ["add", "nand"], ["i8", "i32"], ORDERINGS, vector_sizes
+                ):
+                    if ordering == "acq_rel":
+                        # These cases are part of Slice 1.
+                        continue
+                    print_atomicrmw(
+                        fp,
+                        operation,
+                        datatype,
+                        ordering,
+                        addrspace,
+                        llvm_scope,
+                        vector_size,
                     )
+
+                if elementwise:
+                    continue
----------------
antoniofrighetto wrote:

Nit:
```suggestion
                if elementwise and ordering == "seq_cst":
                    continue
```
Not sure if we should regenerate the tests though.

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


More information about the llvm-commits mailing list