Skip to content

Commit 90299cb

Browse files
author
liuzhenya
committed
fix: review
1 parent b0163a6 commit 90299cb

File tree

6 files changed

+237
-142
lines changed

6 files changed

+237
-142
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
137137
if (auto floatType = mlir::dyn_cast<cir::FPTypeInterface>(eltTy))
138138
return floatType.getWidth();
139139

140-
llvm_unreachable("Wrong type passed in or Non-CIR type passed in");
140+
llvm_unreachable("Unsupported type in getCIRIntOrFloatBitWidth");
141141
}
142142
cir::IntType getSIntNTy(int n) {
143143
return cir::IntType::get(getContext(), n, true);

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,13 @@ namespace clang::CIRGen {
2727

2828
class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
2929
const CIRGenTypeCache &typeCache;
30-
bool isFPConstrained = false;
3130
llvm::StringMap<unsigned> recordNames;
3231
llvm::StringMap<unsigned> globalsVersioning;
3332

3433
public:
3534
CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc)
3635
: CIRBaseBuilderTy(mlirContext), typeCache(tc) {}
3736

38-
//
39-
// Floating point specific helpers
40-
// -------------------------------
41-
//
42-
43-
/// Query for the use of constrained floating point math
44-
bool getisFPConstrained() {
45-
if (isFPConstrained)
46-
llvm_unreachable("Constrained FP NYI");
47-
return isFPConstrained;
48-
}
49-
5037
/// Get a cir::ConstArrayAttr for a string literal.
5138
/// Note: This is different from what is returned by
5239
/// mlir::Builder::getStringAttr() which is an mlir::StringAttr.

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,17 +631,17 @@ CIRGenFunction::emitTargetBuiltinExpr(unsigned builtinID, const CallExpr *e,
631631
// a ConstantInt. Otherwise we emit it as a normal scalar value.
632632
mlir::Value CIRGenFunction::emitScalarOrConstFoldImmArg(unsigned iceArguments,
633633
unsigned idx,
634-
const CallExpr *expr) {
634+
const Expr *argExpr) {
635635
mlir::Value arg = {};
636636
if ((iceArguments & (1 << idx)) == 0) {
637-
arg = emitScalarExpr(expr->getArg(idx));
637+
arg = emitScalarExpr(argExpr);
638638
} else {
639639
// If this is required to be a constant, constant fold it so that we
640640
// know that the generated intrinsic gets a ConstantInt.
641641
std::optional<llvm::APSInt> result =
642-
expr->getArg(idx)->getIntegerConstantExpr(getContext());
642+
argExpr->getIntegerConstantExpr(getContext());
643643
assert(result && "Expected argument to be a constant");
644-
arg = builder.getConstInt(getLoc(expr->getSourceRange()), *result);
644+
arg = builder.getConstInt(getLoc(argExpr->getSourceRange()), *result);
645645
}
646646
return arg;
647647
}

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@
2121
using namespace clang;
2222
using namespace clang::CIRGen;
2323

24+
// OG has unordered comparison as a form of optimization in addition to
25+
// ordered comparison, while CIR doesn't.
26+
//
27+
// This means that we can't encode the comparison code of UGT (unordered
28+
// greater than), at least not at the CIR level.
29+
//
30+
// The boolean shouldInvert compensates for this.
31+
// For example: to get to the comparison code UGT, we pass in
32+
// emitVectorFCmp (OLE, shouldInvert = true) since OLE is the inverse of UGT.
33+
34+
// There are several ways to support this otherwise:
35+
// - register extra CmpOpKind for unordered comparison types and build the
36+
// translation code for
37+
// to go from CIR -> LLVM dialect. Notice we get this naturally with
38+
// shouldInvert, benefiting from existing infrastructure, albeit having to
39+
// generate an extra `not` at CIR).
40+
// - Just add extra comparison code to a new VecCmpOpKind instead of
41+
// cluttering CmpOpKind.
42+
// - Add a boolean in VecCmpOp to indicate if it's doing unordered or ordered
43+
// comparison
44+
// - Just emit the intrinsics call instead of calling this helper, see how the
45+
// LLVM lowering handles this.
46+
static mlir::Value emitVectorFCmp(CIRGenBuilderTy &builder,
47+
llvm::SmallVector<mlir::Value> &ops,
48+
mlir::Location loc, cir::CmpOpKind pred,
49+
bool shouldInvert) {
50+
assert(!cir::MissingFeatures::cgFPOptionsRAII());
51+
// TODO(cir): Add isSignaling boolean once emitConstrainedFPCall implemented
52+
assert(!cir::MissingFeatures::emitConstrainedFPCall());
53+
mlir::Value cmp = builder.createVecCompare(loc, pred, ops[0], ops[1]);
54+
mlir::Value bitCast = builder.createBitcast(
55+
shouldInvert ? builder.createNot(cmp) : cmp, ops[0].getType());
56+
return bitCast;
57+
}
58+
2459
mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
2560
const CallExpr *expr) {
2661
if (builtinID == Builtin::BI__builtin_cpu_is) {
@@ -52,45 +87,9 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
5287
assert(error == ASTContext::GE_None &&
5388
"builtinID should be checked before calling emitX86BuiltinExpr");
5489
for (auto [idx, arg] : llvm::enumerate(expr->arguments())) {
55-
ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, expr));
90+
ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, arg));
5691
}
5792

58-
// OG has unordered comparison as a form of optimization in addition to
59-
// ordered comparison, while CIR doesn't.
60-
//
61-
// This means that we can't encode the comparison code of UGT (unordered
62-
// greater than), at least not at the CIR level.
63-
//
64-
// The boolean shouldInvert compensates for this.
65-
// For example: to get to the comparison code UGT, we pass in
66-
// emitVectorFCmp (OLE, shouldInvert = true) since OLE is the inverse of UGT.
67-
68-
// There are several ways to support this otherwise:
69-
// - register extra CmpOpKind for unordered comparison types and build the
70-
// translation code for
71-
// to go from CIR -> LLVM dialect. Notice we get this naturally with
72-
// shouldInvert, benefiting from existing infrastructure, albeit having to
73-
// generate an extra `not` at CIR).
74-
// - Just add extra comparison code to a new VecCmpOpKind instead of
75-
// cluttering CmpOpKind.
76-
// - Add a boolean in VecCmpOp to indicate if it's doing unordered or ordered
77-
// comparison
78-
// - Just emit the intrinsics call instead of calling this helper, see how the
79-
// LLVM lowering handles this.
80-
auto emitVectorFCmp = [this, &ops, &expr](cir::CmpOpKind pred,
81-
bool shouldInvert,
82-
bool isSignaling) {
83-
assert(!cir::MissingFeatures::cgFPOptionsRAII());
84-
auto loc = getLoc(expr->getExprLoc());
85-
// TODO: Add isSignaling boolean once emitConstrainedFPCall implemented
86-
assert(!cir::MissingFeatures::emitConstrainedFPCall() &&
87-
"constrained FP compare NYI");
88-
mlir::Value cmp = builder.createVecCompare(loc, pred, ops[0], ops[1]);
89-
mlir::Value bitCast = builder.createBitcast(
90-
shouldInvert ? builder.createNot(cmp) : cmp, ops[0].getType());
91-
return bitCast;
92-
};
93-
9493
switch (builtinID) {
9594
default:
9695
return {};
@@ -764,12 +763,12 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
764763
return {};
765764
case X86::BI__builtin_ia32_cmpnltps:
766765
case X86::BI__builtin_ia32_cmpnltpd:
767-
return emitVectorFCmp(cir::CmpOpKind::lt, /*shouldInvert=*/true,
768-
/*isSignaling=*/true);
766+
return emitVectorFCmp(builder, ops, getLoc(expr->getExprLoc()),
767+
cir::CmpOpKind::lt, /*shouldInvert=*/true);
769768
case X86::BI__builtin_ia32_cmpnleps:
770769
case X86::BI__builtin_ia32_cmpnlepd:
771-
return emitVectorFCmp(cir::CmpOpKind::le, /*shouldInvert=*/true,
772-
/*isSignaling=*/true);
770+
return emitVectorFCmp(builder, ops, getLoc(expr->getExprLoc()),
771+
cir::CmpOpKind::le, /*shouldInvert=*/true);
773772
case X86::BI__builtin_ia32_cmpordps:
774773
case X86::BI__builtin_ia32_cmpordpd:
775774
case X86::BI__builtin_ia32_cmpph128_mask:

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ class CIRGenFunction : public CIRGenTypeCache {
17001700
LValue lvalue, bool capturedByInit = false);
17011701

17021702
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned idx,
1703-
const CallExpr *expr);
1703+
const Expr *argExpr);
17041704

17051705
void emitStaticVarDecl(const VarDecl &d, cir::GlobalLinkageKind linkage);
17061706

0 commit comments

Comments
 (0)