Skip to content

Commit 82020de

Browse files
junairefhahn
authored andcommitted
Recommit "[Clang] Extend emitUnaryBuiltin to avoid duplicate logic.""
This reverts the revert commit f552ba6. Recommit with fixed author name.
1 parent f552ba6 commit 82020de

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,13 @@ static Value *emitCallMaybeConstrainedFPBuiltin(CodeGenFunction &CGF,
532532

533533
// Emit a simple mangled intrinsic that has 1 argument and a return type
534534
// matching the argument type.
535-
static Value *emitUnaryBuiltin(CodeGenFunction &CGF,
536-
const CallExpr *E,
537-
unsigned IntrinsicID) {
535+
static Value *emitUnaryBuiltin(CodeGenFunction &CGF, const CallExpr *E,
536+
unsigned IntrinsicID,
537+
llvm::StringRef Name = "") {
538538
llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
539539

540540
Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
541-
return CGF.Builder.CreateCall(F, Src0);
541+
return CGF.Builder.CreateCall(F, Src0, Name);
542542
}
543543

544544
// Emit an intrinsic that has 2 operands of the same type as its result.
@@ -3122,24 +3122,25 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
31223122
}
31233123

31243124
case Builtin::BI__builtin_elementwise_abs: {
3125-
Value *Op0 = EmitScalarExpr(E->getArg(0));
31263125
Value *Result;
3127-
if (Op0->getType()->isIntOrIntVectorTy())
3126+
QualType QT = E->getArg(0)->getType();
3127+
3128+
if (auto *VecTy = QT->getAs<VectorType>())
3129+
QT = VecTy->getElementType();
3130+
if (QT->isIntegerType())
31283131
Result = Builder.CreateBinaryIntrinsic(
3129-
llvm::Intrinsic::abs, Op0, Builder.getFalse(), nullptr, "elt.abs");
3132+
llvm::Intrinsic::abs, EmitScalarExpr(E->getArg(0)),
3133+
Builder.getFalse(), nullptr, "elt.abs");
31303134
else
3131-
Result = Builder.CreateUnaryIntrinsic(llvm::Intrinsic::fabs, Op0, nullptr,
3132-
"elt.abs");
3133-
return RValue::get(Result);
3134-
}
3135+
Result = emitUnaryBuiltin(*this, E, llvm::Intrinsic::fabs, "elt.abs");
31353136

3136-
case Builtin::BI__builtin_elementwise_ceil: {
3137-
Value *Op0 = EmitScalarExpr(E->getArg(0));
3138-
Value *Result = Builder.CreateUnaryIntrinsic(llvm::Intrinsic::ceil, Op0,
3139-
nullptr, "elt.ceil");
31403137
return RValue::get(Result);
31413138
}
31423139

3140+
case Builtin::BI__builtin_elementwise_ceil:
3141+
return RValue::get(
3142+
emitUnaryBuiltin(*this, E, llvm::Intrinsic::ceil, "elt.ceil"));
3143+
31433144
case Builtin::BI__builtin_elementwise_max: {
31443145
Value *Op0 = EmitScalarExpr(E->getArg(0));
31453146
Value *Op1 = EmitScalarExpr(E->getArg(1));
@@ -3174,50 +3175,40 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
31743175
}
31753176

31763177
case Builtin::BI__builtin_reduce_max: {
3177-
auto GetIntrinsicID = [](QualType QT, llvm::Type *IrTy) {
3178-
if (IrTy->isIntOrIntVectorTy()) {
3179-
if (auto *VecTy = QT->getAs<VectorType>())
3180-
QT = VecTy->getElementType();
3181-
if (QT->isSignedIntegerType())
3182-
return llvm::Intrinsic::vector_reduce_smax;
3183-
else
3184-
return llvm::Intrinsic::vector_reduce_umax;
3185-
}
3178+
auto GetIntrinsicID = [](QualType QT) {
3179+
if (auto *VecTy = QT->getAs<VectorType>())
3180+
QT = VecTy->getElementType();
3181+
if (QT->isSignedIntegerType())
3182+
return llvm::Intrinsic::vector_reduce_smax;
3183+
if (QT->isUnsignedIntegerType())
3184+
return llvm::Intrinsic::vector_reduce_umax;
3185+
assert(QT->isFloatingType() && "must have a float here");
31863186
return llvm::Intrinsic::vector_reduce_fmax;
31873187
};
3188-
Value *Op0 = EmitScalarExpr(E->getArg(0));
3189-
Value *Result = Builder.CreateUnaryIntrinsic(
3190-
GetIntrinsicID(E->getArg(0)->getType(), Op0->getType()), Op0, nullptr,
3191-
"rdx.min");
3192-
return RValue::get(Result);
3188+
return RValue::get(emitUnaryBuiltin(
3189+
*this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
31933190
}
31943191

31953192
case Builtin::BI__builtin_reduce_min: {
3196-
auto GetIntrinsicID = [](QualType QT, llvm::Type *IrTy) {
3197-
if (IrTy->isIntOrIntVectorTy()) {
3198-
if (auto *VecTy = QT->getAs<VectorType>())
3199-
QT = VecTy->getElementType();
3200-
if (QT->isSignedIntegerType())
3201-
return llvm::Intrinsic::vector_reduce_smin;
3202-
else
3203-
return llvm::Intrinsic::vector_reduce_umin;
3204-
}
3193+
auto GetIntrinsicID = [](QualType QT) {
3194+
if (auto *VecTy = QT->getAs<VectorType>())
3195+
QT = VecTy->getElementType();
3196+
if (QT->isSignedIntegerType())
3197+
return llvm::Intrinsic::vector_reduce_smin;
3198+
if (QT->isUnsignedIntegerType())
3199+
return llvm::Intrinsic::vector_reduce_umin;
3200+
assert(QT->isFloatingType() && "must have a float here");
32053201
return llvm::Intrinsic::vector_reduce_fmin;
32063202
};
3207-
Value *Op0 = EmitScalarExpr(E->getArg(0));
3208-
Value *Result = Builder.CreateUnaryIntrinsic(
3209-
GetIntrinsicID(E->getArg(0)->getType(), Op0->getType()), Op0, nullptr,
3210-
"rdx.min");
3211-
return RValue::get(Result);
3212-
}
32133203

3214-
case Builtin::BI__builtin_reduce_xor: {
3215-
Value *Op0 = EmitScalarExpr(E->getArg(0));
3216-
Value *Result = Builder.CreateUnaryIntrinsic(
3217-
llvm::Intrinsic::vector_reduce_xor, Op0, nullptr, "rdx.xor");
3218-
return RValue::get(Result);
3204+
return RValue::get(emitUnaryBuiltin(
3205+
*this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
32193206
}
32203207

3208+
case Builtin::BI__builtin_reduce_xor:
3209+
return RValue::get(emitUnaryBuiltin(
3210+
*this, E, llvm::Intrinsic::vector_reduce_xor, "rdx.xor"));
3211+
32213212
case Builtin::BI__builtin_matrix_transpose: {
32223213
const auto *MatrixTy = E->getArg(0)->getType()->getAs<ConstantMatrixType>();
32233214
Value *MatValue = EmitScalarExpr(E->getArg(0));

0 commit comments

Comments
 (0)