[clang] [CIR] Add bit reverse and byte reverse operations (PR #147200)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 10 10:05:04 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.byte_swap", CIR_UIntOfWidths<[16, 32, 64]>> {
+ let summary = "Reverse the bytes in the object representation of the operand";
+ let description = [{
+ The `cir.bswap` operation takes an integer as operand, reverse the bytes in
+ the object representation of the operand integer, and returns the result.
+
+ The operand integer must be an unsigned integer. Its widths must be either
+ 16, 32, or 64.
+
+ Example:
+
+ ```mlir
+ // %0 = 0x12345678
+ %0 = cir.const #cir.int<305419896> : !u32i
+
+ // %1 should be 0x78563412
+ %1 = cir.bit.bswap(%0 : !u32i) : !u32i
----------------
andykaylor wrote:
This is out of sync now.
@xlauko Based on your other comments, I got the impression that you would like these to be `cir.bitreverse` and `cir.bswap`. Is that correct? If so, that makes sense to me, and I don't see a reason to merge these with the `.bit` prefix only to remove it in a subsequent change when the prefix is removed from other bit operations.
https://github.com/llvm/llvm-project/pull/147200
More information about the cfe-commits
mailing list