Skip to content

Commit fe8b9ca

Browse files
authored
[CIR][NFC] Backport upstream bit operations changes (#1744)
This patch backports changes made to the bit operations in the upstream PR llvm/llvm-project#148378. Namely, this patch includes the following changes: - This patch removes the `bit.` prefix in the op mnemonic. The operation names now directly correspond to the builtin function names except for bswap which is represented by `cir.byte_swap` for more clarity. - Since all bit operations are `SameOperandsAndResultType`, this patch updates their assembly format and avoids spelling out the operand type twice.
1 parent aa1692a commit fe8b9ca

File tree

8 files changed

+137
-144
lines changed

8 files changed

+137
-144
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,28 +1608,28 @@ class CIR_BitOp<string mnemonic, TypeConstraint inputTy> : CIR_Op<mnemonic, [
16081608
let results = (outs inputTy:$result);
16091609

16101610
let assemblyFormat = [{
1611-
`(` $input `:` type($input) `)` `:` type($result) attr-dict
1611+
$input `:` type($result) attr-dict
16121612
}];
16131613
}
16141614

16151615
class CIR_CountZerosBitOp<string mnemonic, TypeConstraint inputTy>
16161616
: CIR_BitOp<mnemonic, inputTy> {
16171617
let arguments = (ins inputTy:$input, UnitAttr:$is_zero_poison);
16181618
let assemblyFormat = [{
1619-
`(` $input `:` type($input) `)` (`zero_poison` $is_zero_poison^)?
1619+
$input (`zero_poison` $is_zero_poison^)?
16201620
`:` type($result) attr-dict
16211621
}];
16221622
}
16231623

1624-
def CIR_BitClrsbOp : CIR_BitOp<"bit.clrsb", CIR_SIntOfWidths<[32, 64]>> {
1624+
def CIR_BitClrsbOp : CIR_BitOp<"clrsb", CIR_SIntOfWidths<[32, 64]>> {
16251625
let summary = "Get the number of leading redundant sign bits in the input";
16261626
let description = [{
16271627
Compute the number of leading redundant sign bits in the input integer.
16281628

16291629
The input integer must be a signed integer. The most significant bit of the
1630-
input integer is the sign bit. The `cir.bit.clrsb` operation returns the
1631-
number of redundant sign bits in the input, that is, the number of bits
1632-
following the most significant bit that are identical to it.
1630+
input integer is the sign bit. The `cir.clrsb` operation returns the number
1631+
of redundant sign bits in the input, that is, the number of bits following
1632+
the most significant bit that are identical to it.
16331633

16341634
The bit width of the input integer must be either 32 or 64.
16351635

@@ -1642,24 +1642,22 @@ def CIR_BitClrsbOp : CIR_BitOp<"bit.clrsb", CIR_SIntOfWidths<[32, 64]>> {
16421642
%0 = cir.const #cir.int<3735928559> : !s32i
16431643
// %1 will be 1 because there is 1 bit following the most significant bit
16441644
// that is identical to it.
1645-
%1 = cir.bit.clrsb(%0 : !s32i) : !s32i
1645+
%1 = cir.clrsb(%0 : !s32i) : !s32i
16461646

16471647
// %2 = 1, 0b0000_0000_0000_0000_0000_0000_0000_0001
16481648
%2 = cir.const #cir.int<1> : !s32i
16491649
// %3 will be 30
1650-
%3 = cir.bit.clrsb(%2 : !s32i) : !s32i
1650+
%3 = cir.clrsb(%2 : !s32i) : !s32i
16511651
```
16521652
}];
16531653
}
16541654

1655-
def CIR_BitClzOp : CIR_CountZerosBitOp<"bit.clz",
1656-
CIR_UIntOfWidths<[16, 32, 64]>
1657-
> {
1655+
def CIR_BitClzOp : CIR_CountZerosBitOp<"clz", CIR_UIntOfWidths<[16, 32, 64]>> {
16581656
let summary = "Get the number of leading 0-bits in the input";
16591657
let description = [{
16601658
Compute the number of leading 0-bits in the input.
16611659

1662-
The input integer must be an unsigned integer. The `cir.bit.clz` operation
1660+
The input integer must be an unsigned integer. The `cir.clz` operation
16631661
returns the number of consecutive 0-bits at the most significant bit
16641662
position in the input.
16651663

@@ -1674,19 +1672,17 @@ def CIR_BitClzOp : CIR_CountZerosBitOp<"bit.clz",
16741672
// %0 = 0b0000_0000_0000_0000_0000_0000_0000_1000
16751673
%0 = cir.const #cir.int<8> : !u32i
16761674
// %1 will be 28
1677-
%1 = cir.bit.clz(%0 : !u32i) zero_poison : !u32i
1675+
%1 = cir.clz(%0 : !u32i) zero_poison : !u32i
16781676
```
16791677
}];
16801678
}
16811679

1682-
def CIR_BitCtzOp : CIR_CountZerosBitOp<"bit.ctz",
1683-
CIR_UIntOfWidths<[16, 32, 64]>
1684-
> {
1680+
def CIR_BitCtzOp : CIR_CountZerosBitOp<"ctz", CIR_UIntOfWidths<[16, 32, 64]>> {
16851681
let summary = "Get the number of trailing 0-bits in the input";
16861682
let description = [{
16871683
Compute the number of trailing 0-bits in the input.
16881684

1689-
The input integer must be an unsigned integer. The `cir.bit.ctz` operation
1685+
The input integer must be an unsigned integer. The `cir.ctz` operation
16901686
returns the number of consecutive 0-bits at the least significant bit
16911687
position in the input.
16921688

@@ -1702,20 +1698,19 @@ def CIR_BitCtzOp : CIR_CountZerosBitOp<"bit.ctz",
17021698
// %0 = 0b1000
17031699
%0 = cir.const #cir.int<8> : !u32i
17041700
// %1 will be 3
1705-
%1 = cir.bit.ctz(%0 : !u32i) : !u32i
1701+
%1 = cir.ctz(%0 : !u32i) : !u32i
17061702
```
17071703
}];
17081704
}
17091705

1710-
def CIR_BitFfsOp : CIR_BitOp<"bit.ffs", CIR_SIntOfWidths<[32, 64]>> {
1706+
def CIR_BitFfsOp : CIR_BitOp<"ffs", CIR_SIntOfWidths<[32, 64]>> {
17111707
let summary = "Get the position of the least significant 1-bit of input";
17121708
let description = [{
17131709
Compute the position of the least significant 1-bit of the input.
17141710

1715-
The input integer must be a signed integer. The `cir.bit.ffs` operation
1716-
returns one plus the index of the least significant 1-bit of the input
1717-
signed integer. As a special case, if the input integer is 0, `cir.bit.ffs`
1718-
returns 0.
1711+
The input integer must be a signed integer. The `cir.ffs` operation returns
1712+
one plus the index of the least significant 1-bit of the input signed
1713+
integer. As a special case, if the input integer is 0, `cir.ffs` returns 0.
17191714

17201715
Example:
17211716

@@ -1725,12 +1720,12 @@ def CIR_BitFfsOp : CIR_BitOp<"bit.ffs", CIR_SIntOfWidths<[32, 64]>> {
17251720
// %0 = 0x0010_1000
17261721
%0 = cir.const #cir.int<40> : !s32i
17271722
// #1 will be 4 since the 4th least significant bit is 1.
1728-
%1 = cir.bit.ffs(%0 : !s32i) : !s32i
1723+
%1 = cir.ffs(%0 : !s32i) : !s32i
17291724
```
17301725
}];
17311726
}
17321727

1733-
def CIR_BitParityOp : CIR_BitOp<"bit.parity", CIR_UIntOfWidths<[32, 64]>> {
1728+
def CIR_BitParityOp : CIR_BitOp<"parity", CIR_UIntOfWidths<[32, 64]>> {
17341729
let summary = "Get the parity of input";
17351730
let description = [{
17361731
Compute the parity of the input. The parity of an integer is the number of
@@ -1747,14 +1742,12 @@ def CIR_BitParityOp : CIR_BitOp<"bit.parity", CIR_UIntOfWidths<[32, 64]>> {
17471742
// %0 = 0x0110_1000
17481743
%0 = cir.const #cir.int<104> : !u32i
17491744
// %1 will be 1 since there are 3 1-bits in %0
1750-
%1 = cir.bit.parity(%0 : !u32i) : !u32i
1745+
%1 = cir.parity(%0 : !u32i) : !u32i
17511746
```
17521747
}];
17531748
}
17541749

1755-
def CIR_BitPopcountOp : CIR_BitOp<"bit.popcount",
1756-
CIR_UIntOfWidths<[16, 32, 64]>
1757-
> {
1750+
def CIR_BitPopcountOp : CIR_BitOp<"popcount", CIR_UIntOfWidths<[16, 32, 64]>> {
17581751
let summary = "Get the number of 1-bits in input";
17591752
let description = [{
17601753
Compute the number of 1-bits in the input.
@@ -1769,7 +1762,7 @@ def CIR_BitPopcountOp : CIR_BitOp<"bit.popcount",
17691762
// %0 = 0x0110_1000
17701763
%0 = cir.const #cir.int<104> : !u32i
17711764
// %1 will be 3 since there are 3 1-bits in %0
1772-
%1 = cir.bit.popcount(%0 : !u32i) : !u32i
1765+
%1 = cir.popcount(%0 : !u32i) : !u32i
17731766
```
17741767
}];
17751768
}
@@ -1778,11 +1771,11 @@ def CIR_BitPopcountOp : CIR_BitOp<"bit.popcount",
17781771
// ByteswapOp
17791772
//===----------------------------------------------------------------------===//
17801773

1781-
def CIR_ByteswapOp : CIR_Op<"bswap", [Pure, SameOperandsAndResultType]> {
1774+
def CIR_ByteswapOp : CIR_Op<"byte_swap", [Pure, SameOperandsAndResultType]> {
17821775
let summary = "Reverse the bytes that constitute the operand integer";
17831776
let description = [{
1784-
The `cir.bswap` operation takes an integer as operand, and returns it with
1785-
the order of bytes that constitute the operand reversed.
1777+
The `cir.byte_swap` operation takes an integer as operand, and returns it
1778+
with the order of bytes that constitute the operand reversed.
17861779

17871780
The operand integer must be an unsigned integer. Its widths must be either
17881781
16, 32, or 64.
@@ -1796,15 +1789,15 @@ def CIR_ByteswapOp : CIR_Op<"bswap", [Pure, SameOperandsAndResultType]> {
17961789
%0 = cir.const #cir.int<305419896> : !u32i
17971790

17981791
// %1 should be 0x78563412
1799-
%1 = cir.bswap(%0 : !u32i) : !u32i
1792+
%1 = cir.byte_swap(%0 : !u32i) : !u32i
18001793
```
18011794
}];
18021795

18031796
let results = (outs CIR_IntType:$result);
18041797
let arguments = (ins CIR_UIntOfWidths<[16, 32, 64]>:$input);
18051798

18061799
let assemblyFormat = [{
1807-
`(` $input `:` type($input) `)` `:` type($result) attr-dict
1800+
$input `:` type($result) attr-dict
18081801
}];
18091802
}
18101803

clang/test/CIR/CodeGen/bswap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ u16 bswap_u16(u16 x) {
1010
}
1111

1212
// CHECK: cir.func dso_local @_Z9bswap_u16t
13-
// CHECK: %{{.+}} = cir.bswap(%{{.+}} : !u16i) : !u16i
13+
// CHECK: %{{.+}} = cir.byte_swap %{{.+}} : !u16i
1414
// CHECK: }
1515

1616
u32 bswap_u32(u32 x) {
1717
return __builtin_bswap32(x);
1818
}
1919

2020
// CHECK: cir.func dso_local @_Z9bswap_u32j
21-
// CHECK: %{{.+}} = cir.bswap(%{{.+}} : !u32i) : !u32i
21+
// CHECK: %{{.+}} = cir.byte_swap %{{.+}} : !u32i
2222
// CHECK: }
2323

2424
u64 bswap_u64(u64 x) {
2525
return __builtin_bswap64(x);
2626
}
2727

2828
// CHECK: cir.func dso_local @_Z9bswap_u64y
29-
// CHECK: %{{.+}} = cir.bswap(%{{.+}} : !u64i) : !u64i
29+
// CHECK: %{{.+}} = cir.byte_swap %{{.+}} : !u64i
3030
// CHECK: }

0 commit comments

Comments
 (0)