Skip to content

Commit 7e4213f

Browse files
authored
[CIR][CIRGen][builtin] handle __popcnt (#1479)
This PR removes a useless argument `convertToInt` and removes hardcoded `Sint32Type`. I realized I committed a new file with CRLF before. Really sorry about that >_<
1 parent f72a1bd commit 7e4213f

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ static mlir::Value emitBinaryMaybeConstrainedFPBuiltin(CIRGenFunction &CGF,
130130
template <typename Op>
131131
static RValue emitBuiltinBitOp(
132132
CIRGenFunction &CGF, const CallExpr *E,
133-
std::optional<CIRGenFunction::BuiltinCheckKind> CK = std::nullopt,
134-
bool isZeroPoison = false, bool convertToInt = true) {
133+
std::optional<CIRGenFunction::BuiltinCheckKind> checkKind = std::nullopt,
134+
bool isZeroPoison = false) {
135135
mlir::Value arg;
136-
if (CK.has_value())
137-
arg = CGF.emitCheckedArgForBuiltin(E->getArg(0), *CK);
136+
if (checkKind.has_value())
137+
arg = CGF.emitCheckedArgForBuiltin(E->getArg(0), *checkKind);
138138
else
139139
arg = CGF.emitScalarExpr(E->getArg(0));
140140

@@ -146,12 +146,12 @@ static RValue emitBuiltinBitOp(
146146
} else {
147147
op = CGF.getBuilder().create<Op>(CGF.getLoc(E->getExprLoc()), arg);
148148
}
149-
const mlir::Value bitResult = op.getResult();
150-
if (const auto si32Ty = CGF.getBuilder().getSInt32Ty();
151-
convertToInt && arg.getType() != si32Ty) {
152-
return RValue::get(CGF.getBuilder().createIntCast(bitResult, si32Ty));
149+
const mlir::Value result = op.getResult();
150+
if (const mlir::Type resultType = CGF.convertType(E->getType());
151+
op.getResult().getType() != resultType) {
152+
return RValue::get(CGF.getBuilder().createIntCast(result, resultType));
153153
}
154-
return RValue::get(bitResult);
154+
return RValue::get(result);
155155
}
156156

157157
// Initialize the alloca with the given size and alignment according to the lang
@@ -1098,8 +1098,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
10981098
case Builtin::BI__lzcnt16:
10991099
case Builtin::BI__lzcnt:
11001100
case Builtin::BI__lzcnt64: {
1101-
return emitBuiltinBitOp<cir::BitClzOp>(*this, E, BCK_CLZPassedZero, false,
1102-
false);
1101+
return emitBuiltinBitOp<cir::BitClzOp>(*this, E, BCK_CLZPassedZero, false);
11031102
}
11041103

11051104
case Builtin::BI__popcnt16:
+55-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
1-
// RUN: %clang_cc1 -ffreestanding -fms-extensions -Wno-implicit-function-declaration -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2-
// RUN: FileCheck %s --check-prefix=CIR --input-file=%t.cir
3-
// RUN: %clang_cc1 -ffreestanding -fms-extensions -Wno-implicit-function-declaration -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4-
// RUN: FileCheck %s --check-prefix=LLVM --input-file=%t.ll
5-
6-
unsigned short test__lzcnt16(unsigned short x) {
7-
return __lzcnt16(x);
8-
}
9-
// CIR-LABEL: test__lzcnt16
10-
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u16i) : !u16i
11-
12-
// LLVM-LABEL: test__lzcnt16
13-
// LLVM: {{%.*}} = call i16 @llvm.ctlz.i16(i16 {{%.*}}, i1 false)
14-
15-
unsigned int test__lzcnt(unsigned int x) {
16-
return __lzcnt(x);
17-
}
18-
// CIR-LABEL: test__lzcnt
19-
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u32i) : !u32i
20-
21-
// LLVM-LABEL: test__lzcnt
22-
// LLVM: {{%.*}} = call i32 @llvm.ctlz.i32(i32 {{%.*}}, i1 false)
23-
24-
unsigned __int64 test__lzcnt64(unsigned __int64 x) {
25-
return __lzcnt64(x);
26-
}
27-
// CIR-LABEL: test__lzcnt64
28-
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u64i) : !u64i
29-
30-
// LLVM-LABEL: test__lzcnt64
31-
// LLVM: {{%.*}} = call i64 @llvm.ctlz.i64(i64 {{%.*}}, i1 false)
1+
// RUN: %clang_cc1 -ffreestanding -fms-extensions -Wno-implicit-function-declaration -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck %s --check-prefix=CIR --input-file=%t.cir
3+
// RUN: %clang_cc1 -ffreestanding -fms-extensions -Wno-implicit-function-declaration -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck %s --check-prefix=LLVM --input-file=%t.ll
5+
6+
// This test mimics clang/test/CodeGen/ms-intrinsics-other.c, which eventually
7+
// CIR shall be able to support fully.
8+
9+
unsigned short test__lzcnt16(unsigned short x) {
10+
return __lzcnt16(x);
11+
}
12+
// CIR-LABEL: test__lzcnt16
13+
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u16i) : !u16i
14+
// LLVM-LABEL: test__lzcnt16
15+
// LLVM: {{%.*}} = call i16 @llvm.ctlz.i16(i16 {{%.*}}, i1 false)
16+
17+
unsigned int test__lzcnt(unsigned int x) {
18+
return __lzcnt(x);
19+
}
20+
// CIR-LABEL: test__lzcnt
21+
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u32i) : !u32i
22+
// LLVM-LABEL: test__lzcnt
23+
// LLVM: {{%.*}} = call i32 @llvm.ctlz.i32(i32 {{%.*}}, i1 false)
24+
25+
unsigned __int64 test__lzcnt64(unsigned __int64 x) {
26+
return __lzcnt64(x);
27+
}
28+
// CIR-LABEL: test__lzcnt64
29+
// CIR: {{%.*}} = cir.bit.clz({{%.*}} : !u64i) : !u64i
30+
// LLVM-LABEL: test__lzcnt64
31+
// LLVM: {{%.*}} = call i64 @llvm.ctlz.i64(i64 {{%.*}}, i1 false)
32+
33+
unsigned short test__popcnt16(unsigned short x) {
34+
return __popcnt16(x);
35+
}
36+
// CIR-LABEL: test__popcnt16
37+
// CIR: {{%.*}} = cir.bit.popcount({{%.*}} : !u16i) : !u16i
38+
// LLVM-LABEL: test__popcnt16
39+
// LLVM: {{%.*}} = call i16 @llvm.ctpop.i16(i16 {{%.*}})
40+
41+
unsigned int test__popcnt(unsigned int x) {
42+
return __popcnt(x);
43+
}
44+
// CIR-LABEL: test__popcnt
45+
// CIR: {{%.*}} = cir.bit.popcount({{%.*}} : !u32i) : !u32i
46+
// LLVM-LABEL: test__popcnt
47+
// LLVM: {{%.*}} = call i32 @llvm.ctpop.i32(i32 {{%.*}})
48+
49+
unsigned __int64 test__popcnt64(unsigned __int64 x) {
50+
return __popcnt64(x);
51+
}
52+
// CIR-LABEL: test__popcnt64
53+
// CIR: {{%.*}} = cir.bit.popcount({{%.*}} : !u64i) : !u64i
54+
// LLVM-LABEL: test__popcnt64
55+
// LLVM: {{%.*}} = call i64 @llvm.ctpop.i64(i64 {{%.*}})

0 commit comments

Comments
 (0)