[clang] [CIR] Add bit reverse and byte reverse operations (PR #147200)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 9 23:23:36 PDT 2025


================
@@ -2661,6 +2661,45 @@ def BitPopcountOp : CIR_BitOpBase<"bit.popcnt",
   }];
 }
 
+def BitReverseOp : CIR_BitOpBase<"bit.reverse",
+                                 CIR_UIntOfWidths<[8, 16, 32, 64]>> {
+  let summary = "Reverse the bit pattern of the operand integer";
+  let description = [{
+    The `cir.bit.reverse` operation reverses the bits of the operand integer.
+    Its only argument must be of unsigned integer types of width 8, 16, 32, or
+    64.
+
+    This operation covers the C/C++ builtin function `__builtin_bitreverse`.
+
+    Example:
+
+    ```mlir
+    %1 = cir.bit.reverse(%0 : !u32i): !u32i
+    ```
+  }];
+}
+
+def ByteSwapOp : CIR_BitOpBase<"bit.bswap", CIR_UIntOfWidths<[16, 32, 64]>> {
----------------
xlauko wrote:

Same here — I’d also prefer to keep the names as close as possible to Clang builtins. Using similar names reduces cognitive load and makes it easier to trace things through the entire pipeline.

For the same reason, I’d like to eliminate the `bit.` prefix entirely and use the builtin names directly, like `bitreverse` (This can be, and probably should be done as separate PR). I tend to interpret dot-separated names as namespaces, so using them just for readability feels a bit off. Grouping things like `complex.` or `libc.` makes sense, as they are in a way subdialects, but when the goal is simply improved readability, I’d lean toward using underscores instead in general.

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


More information about the cfe-commits mailing list