Skip to content

Commit 17557ab

Browse files
committed
Redesign LzcntOp
1 parent c9383d8 commit 17557ab

File tree

5 files changed

+136
-118
lines changed

5 files changed

+136
-118
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,9 +1559,9 @@ def ComplexBinOp : CIR_Op<"complex.binop",
15591559
//===----------------------------------------------------------------------===//
15601560

15611561
class CIR_BitOp<string mnemonic, TypeConstraint inputTy>
1562-
: CIR_Op<mnemonic, [Pure, SameOperandsAndResultType]> {
1562+
: CIR_Op<mnemonic, [Pure]> {
15631563
let arguments = (ins inputTy:$input);
1564-
let results = (outs inputTy:$result);
1564+
let results = (outs SInt32:$result);
15651565

15661566
let assemblyFormat = [{
15671567
`(` $input `:` type($input) `)` `:` type($result) attr-dict
@@ -1715,7 +1715,7 @@ def BitPopcountOp
17151715
}];
17161716
}
17171717

1718-
def BitLzcntOp : CIR_BitOp<"bit.lzcnt", AnyTypeOf<[UInt16, UInt32, UInt64]>> {
1718+
def BitLzcntOp : CIR_Op<"bit.lzcnt", [Pure, SameOperandsAndResultType]> {
17191719
let summary = "Get the number of leading 0-bits in the input";
17201720
let description = [{
17211721
Compute the number of leading 0-bits in the input.
@@ -1737,6 +1737,13 @@ def BitLzcntOp : CIR_BitOp<"bit.lzcnt", AnyTypeOf<[UInt16, UInt32, UInt64]>> {
17371737
%1 = cir.bit.lzcnt(%0 : !u32i) : !u32i
17381738
```
17391739
}];
1740+
1741+
let arguments = (ins AnyTypeOf<[UInt16, UInt32, UInt64]>:$input);
1742+
let results = (outs AnyTypeOf<[UInt16, UInt32, UInt64]>:$result);
1743+
1744+
let assemblyFormat = [{
1745+
`(` $input `:` type($input) `)` `:` type($result) attr-dict
1746+
}];
17401747
}
17411748

17421749
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,8 @@ emitBuiltinBitOp(CIRGenFunction &CGF, const CallExpr *E,
137137
else
138138
arg = CGF.emitScalarExpr(E->getArg(0));
139139

140-
auto op = CGF.getBuilder().create<Op>(CGF.getLoc(E->getExprLoc()),
141-
arg.getType(), arg);
142-
143-
if constexpr (std::is_same_v<Op, cir::BitLzcntOp>) {
144-
return RValue::get(op);
145-
} else {
146-
return RValue::get(CGF.getBuilder().createIntCast(
147-
op->getResult(0),
148-
CGF.convertType(E->getCallReturnType(CGF.getContext()))));
149-
}
140+
auto op = CGF.getBuilder().create<Op>(CGF.getLoc(E->getExprLoc()),arg);
141+
return RValue::get(op);
150142
}
151143

152144
// Initialize the alloca with the given size and alignment according to the lang

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,7 +3049,9 @@ mlir::Value createLLVMBitOp(mlir::Location loc,
30493049
operand.getType(), operand);
30503050
}
30513051

3052-
return op->getResult(0);
3052+
return getLLVMIntCast(
3053+
rewriter, op->getResult(0), mlir::cast<mlir::IntegerType>(resultTy),
3054+
/*isUnsigned=*/true, operandIntTy.getWidth(), resultIntTy.getWidth());
30533055
}
30543056

30553057
mlir::LogicalResult CIRToLLVMBitClrsbOpLowering::matchAndRewrite(
@@ -3184,9 +3186,8 @@ mlir::LogicalResult CIRToLLVMBitLzcntOpLowering::matchAndRewrite(
31843186
cir::BitLzcntOp op, OpAdaptor adaptor,
31853187
mlir::ConversionPatternRewriter &rewriter) const {
31863188
auto resTy = getTypeConverter()->convertType(op.getType());
3187-
auto llvmOp =
3188-
createLLVMBitOp(op.getLoc(), "llvm.ctlz", resTy, adaptor.getInput(),
3189-
/*poisonZeroInputFlag=*/false, rewriter);
3189+
auto llvmOp = rewriter.create<mlir::LLVM::CountLeadingZerosOp>(op.getLoc(), resTy, adaptor.getInput(), false);
3190+
31903191
rewriter.replaceOp(op, llvmOp);
31913192
return mlir::LogicalResult::success();
31923193
}

clang/test/CIR/CodeGen/builtin-bits.cpp

Lines changed: 92 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,158 +4,183 @@
44
int test_builtin_clrsb(int x) {
55
return __builtin_clrsb(x);
66
}
7-
// CIR-LABEL: test_builtin_clrsb
8-
// CIR: %{{.+}} = cir.bit.clrsb(%{{.+}} : !s32i) : !s32i
7+
8+
// CIR: cir.func @_Z18test_builtin_clrsbi
9+
// CIR: %{{.+}} = cir.bit.clrsb(%{{.+}} : !s32i) : !s32i
10+
// CIR: }
911

1012
int test_builtin_clrsbl(long x) {
1113
return __builtin_clrsbl(x);
1214
}
13-
// CIR-LABEL: test_builtin_clrsbl
14-
// CIR: [[tmp:%.+]] = cir.bit.clrsb({{%.+}} : !s64i) : !s64i
15-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !s64i), !s32i
15+
16+
// CIR: cir.func @_Z19test_builtin_clrsbll
17+
// CIR: %{{.+}} = cir.bit.clrsb(%{{.+}} : !s64i) : !s32i
18+
// CIR: }
1619

1720
int test_builtin_clrsbll(long long x) {
1821
return __builtin_clrsbll(x);
1922
}
20-
// CIR-LABEL: test_builtin_clrsbll
21-
// CIR: [[tmp:%.+]] = cir.bit.clrsb(%{{.+}} : !s64i) : !s64i
22-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !s64i), !s32i
23+
24+
// CIR: cir.func @_Z20test_builtin_clrsbllx
25+
// CIR: %{{.+}} = cir.bit.clrsb(%{{.+}} : !s64i) : !s32i
26+
// CIR: }
2327

2428
int test_builtin_ctzs(unsigned short x) {
2529
return __builtin_ctzs(x);
2630
}
27-
// CIR-LABEL: test_builtin_ctzs
28-
// CIR: [[tmp:%.+]] = cir.bit.ctz(%{{.+}} : !u16i) : !u16i
29-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u16i), !s32i
31+
32+
// CIR: cir.func @_Z17test_builtin_ctzst
33+
// CIR: %{{.+}} = cir.bit.ctz(%{{.+}} : !u16i) : !s32i
34+
// CHEKC: }
3035

3136
int test_builtin_ctz(unsigned x) {
3237
return __builtin_ctz(x);
3338
}
34-
// CIR-LABEL: test_builtin_ctz
35-
// CIR: [[tmp:%.+]] = cir.bit.ctz(%{{.+}} : !u32i) : !u32i
36-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
39+
40+
// CIR: cir.func @_Z16test_builtin_ctzj
41+
// CIR: %{{.+}} = cir.bit.ctz(%{{.+}} : !u32i) : !s32i
42+
// CIR: }
3743

3844
int test_builtin_ctzl(unsigned long x) {
3945
return __builtin_ctzl(x);
4046
}
41-
// CIR-LABEL: test_builtin_ctzl
42-
// CIR: [[tmp:%.+]] = cir.bit.ctz(%{{.+}} : !u64i) : !u64i
43-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
47+
48+
// CIR: cir.func @_Z17test_builtin_ctzlm
49+
// CIR: %{{.+}} = cir.bit.ctz(%{{.+}} : !u64i) : !s32i
50+
// CIR: }
4451

4552
int test_builtin_ctzll(unsigned long long x) {
4653
return __builtin_ctzll(x);
4754
}
48-
// CIR-LABEL: test_builtin_ctzll
49-
// CIR: [[tmp:%.+]] = cir.bit.ctz(%{{.+}} : !u64i) : !u64i
50-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
55+
56+
// CIR: cir.func @_Z18test_builtin_ctzlly
57+
// CIR: %{{.+}} = cir.bit.ctz(%{{.+}} : !u64i) : !s32i
58+
// CIR: }
5159

5260
int test_builtin_ctzg(unsigned x) {
5361
return __builtin_ctzg(x);
5462
}
55-
// CIR-LABEL: test_builtin_ctzg
56-
// CIR: [[tmp:%.+]] = cir.bit.ctz(%{{.+}} : !u32i) : !u32i
57-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
63+
64+
// CIR: cir.func @_Z17test_builtin_ctzgj
65+
// CIR: %{{.+}} = cir.bit.ctz(%{{.+}} : !u32i) : !s32i
66+
// CIR: }
5867

5968
int test_builtin_clzs(unsigned short x) {
6069
return __builtin_clzs(x);
6170
}
62-
// CIR-LABEL: test_builtin_clzs
63-
// CIR: [[tmp:%.+]] = cir.bit.clz(%{{.+}} : !u16i) : !u16i
64-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u16i), !s32i
71+
72+
// CIR: cir.func @_Z17test_builtin_clzst
73+
// CIR: %{{.+}} = cir.bit.clz(%{{.+}} : !u16i) : !s32i
74+
// CIR: }
6575

6676
int test_builtin_clz(unsigned x) {
6777
return __builtin_clz(x);
6878
}
69-
// CIR-LABEL: cir.func @_Z16test_builtin_clz
70-
// CIR: [[tmp:%.+]] = cir.bit.clz(%{{.+}} : !u32i) : !u32i
71-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
79+
80+
// CIR: cir.func @_Z16test_builtin_clzj
81+
// CIR: %{{.+}} = cir.bit.clz(%{{.+}} : !u32i) : !s32i
82+
// CIR: }
7283

7384
int test_builtin_clzl(unsigned long x) {
7485
return __builtin_clzl(x);
7586
}
76-
// CIR-LABEL: test_builtin_clzl
77-
// CIR: [[tmp:%.+]] = cir.bit.clz(%{{.+}} : !u64i) : !u64i
78-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
87+
88+
// CIR: cir.func @_Z17test_builtin_clzlm
89+
// CIR: %{{.+}} = cir.bit.clz(%{{.+}} : !u64i) : !s32i
90+
// CIR: }
7991

8092
int test_builtin_clzll(unsigned long long x) {
8193
return __builtin_clzll(x);
8294
}
83-
// CIR-LABEL: test_builtin_clzll
84-
// CIR: [[tmp:%.+]] = cir.bit.clz(%{{.+}} : !u64i) : !u64i
85-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
95+
96+
// CIR: cir.func @_Z18test_builtin_clzlly
97+
// CIR: %{{.+}} = cir.bit.clz(%{{.+}} : !u64i) : !s32i
98+
// CIR: }
8699

87100
int test_builtin_clzg(unsigned x) {
88101
return __builtin_clzg(x);
89102
}
90-
// CIR-LABEL: test_builtin_clz
91-
// CIR: [[tmp:%.+]] = cir.bit.clz(%{{.+}} : !u32i) : !u32i
92-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
103+
104+
// CIR: cir.func @_Z17test_builtin_clzgj
105+
// CIR: %{{.+}} = cir.bit.clz(%{{.+}} : !u32i) : !s32i
106+
// CIR: }
93107

94108
int test_builtin_ffs(int x) {
95109
return __builtin_ffs(x);
96110
}
97-
// CIR-LABEL: test_builtin_ffs
98-
// CIR: [[tmp:%.+]] = cir.bit.ffs(%{{.+}} : !s32i) : !s32i
111+
112+
// CIR: cir.func @_Z16test_builtin_ffsi
113+
// CIR: %{{.+}} = cir.bit.ffs(%{{.+}} : !s32i) : !s32i
114+
// CIR: }
99115

100116
int test_builtin_ffsl(long x) {
101117
return __builtin_ffsl(x);
102118
}
103-
// CIR-LABEL: test_builtin_ffsl
104-
// CIR: [[tmp:%.+]] = cir.bit.ffs(%{{.+}} : !s64i) : !s64i
105-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !s64i), !s32i
119+
120+
// CIR: cir.func @_Z17test_builtin_ffsll
121+
// CIR: %{{.+}} = cir.bit.ffs(%{{.+}} : !s64i) : !s32i
122+
// CIR: }
106123

107124
int test_builtin_ffsll(long long x) {
108125
return __builtin_ffsll(x);
109126
}
110-
// CIR-LABEL: test_builtin_ffsll
111-
// CIR: [[tmp:%.+]] = cir.bit.ffs(%{{.+}} : !s64i) : !s64i
112-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !s64i), !s32i
127+
128+
// CIR: cir.func @_Z18test_builtin_ffsllx
129+
// CIR: %{{.+}} = cir.bit.ffs(%{{.+}} : !s64i) : !s32i
130+
// CIR: }
113131

114132
int test_builtin_parity(unsigned x) {
115133
return __builtin_parity(x);
116134
}
117-
// CIR-LABEL: test_builtin_parity
118-
// CIR: [[tmp:%.+]] = cir.bit.parity(%{{.+}} : !u32i) : !u32i
119-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
135+
136+
// CIR: cir.func @_Z19test_builtin_parityj
137+
// CIR: %{{.+}} = cir.bit.parity(%{{.+}} : !u32i) : !s32i
138+
// CIR: }
120139

121140
int test_builtin_parityl(unsigned long x) {
122141
return __builtin_parityl(x);
123142
}
124-
// CIR-LABEL: test_builtin_parityl
125-
// CIR: [[tmp:%.+]] = cir.bit.parity(%{{.+}} : !u64i) : !u64i
126-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
143+
144+
// CIR: cir.func @_Z20test_builtin_paritylm
145+
// CIR: %{{.+}} = cir.bit.parity(%{{.+}} : !u64i) : !s32i
146+
// CIR: }
127147

128148
int test_builtin_parityll(unsigned long long x) {
129149
return __builtin_parityll(x);
130150
}
131-
// CIR-LABEL: test_builtin_parityll
132-
// CIR: [[tmp:%.+]] = cir.bit.parity(%{{.+}} : !u64i) : !u64i
133-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
151+
152+
// CIR: cir.func @_Z21test_builtin_paritylly
153+
// CIR: %{{.+}} = cir.bit.parity(%{{.+}} : !u64i) : !s32i
154+
// CIR: }
134155

135156
int test_builtin_popcount(unsigned x) {
136157
return __builtin_popcount(x);
137158
}
138-
// CIR-LABEL: test_builtin_popcount
139-
// CIR: [[tmp:%.+]] = cir.bit.popcount(%{{.+}} : !u32i) : !u32i
140-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
159+
160+
// CIR: cir.func @_Z21test_builtin_popcountj
161+
// CIR: %{{.+}} = cir.bit.popcount(%{{.+}} : !u32i) : !s32i
162+
// CIR: }
141163

142164
int test_builtin_popcountl(unsigned long x) {
143165
return __builtin_popcountl(x);
144166
}
145-
// CIR-LABEL: test_builtin_popcountl
146-
// CIR: [[tmp:%.+]] = cir.bit.popcount(%{{.+}} : !u64i) : !u64i
147-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
167+
168+
// CIR: cir.func @_Z22test_builtin_popcountlm
169+
// CIR: %{{.+}} = cir.bit.popcount(%{{.+}} : !u64i) : !s32i
170+
// CIR: }
148171

149172
int test_builtin_popcountll(unsigned long long x) {
150173
return __builtin_popcountll(x);
151174
}
152-
// CIR-LABEL: test_builtin_popcountll
153-
// CIR: [[tmp:%.+]] = cir.bit.popcount(%{{.+}} : !u64i) : !u64i
154-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u64i), !s32i
175+
176+
// CIR: cir.func @_Z23test_builtin_popcountlly
177+
// CIR: %{{.+}} = cir.bit.popcount(%{{.+}} : !u64i) : !s32i
178+
// CIR: }
155179

156180
int test_builtin_popcountg(unsigned x) {
157181
return __builtin_popcountg(x);
158182
}
159-
// CIR-LABEL: test_builtin_popcountg
160-
// CIR: [[tmp:%.+]] = cir.bit.popcount(%{{.+}} : !u32i) : !u32i
161-
// CIR: {{%.*}} = cir.cast(integral, [[tmp]] : !u32i), !s32i
183+
184+
// CIR: cir.func @_Z22test_builtin_popcountgj
185+
// CIR: %{{.+}} = cir.bit.popcount(%{{.+}} : !u32i) : !s32i
186+
// CIR: }

0 commit comments

Comments
 (0)