Skip to content

Commit dbd17f2

Browse files
ghehgxlauko
authored andcommitted
[CIR][CIRGen][Builtin][Neon] Lower neon_vpadd_v and neon_vpaddq_v into llvm intrinsic (llvm#960)
This PR refactored Neon Built in code in clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp a bit to make it cleaner. Also changed RUNOption of test file clang/test/CIR/CodeGen/AArch64/neon-arith.c to make test more concise, and easy to compare against OG (to compare, just remove -fclangir from llvm gen part of RUN, and the test should still pass)
1 parent dce9d59 commit dbd17f2

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,7 +3192,7 @@ static mlir::Value buildArmLdrexNon128Intrinsic(unsigned int builtinID,
31923192
}
31933193
}
31943194

3195-
mlir::Value buildNeonCall(unsigned int builtinID, CIRGenFunction &cgf,
3195+
mlir::Value buildNeonCall(CIRGenBuilderTy &builder,
31963196
llvm::SmallVector<mlir::Type> argTypes,
31973197
llvm::SmallVectorImpl<mlir::Value> &args,
31983198
llvm::StringRef intrinsicName, mlir::Type funcResTy,
@@ -3209,7 +3209,6 @@ mlir::Value buildNeonCall(unsigned int builtinID, CIRGenFunction &cgf,
32093209
if (shift > 0)
32103210
llvm_unreachable("Argument shift NYI");
32113211

3212-
CIRGenBuilderTy &builder = cgf.getBuilder();
32133212
for (unsigned j = 0; j < argTypes.size(); ++j) {
32143213
if (isConstrainedFPIntrinsic) {
32153214
assert(!MissingFeatures::buildConstrainedFPCall());
@@ -3239,6 +3238,24 @@ static int64_t getIntValueFromConstOp(mlir::Value val) {
32393238
.getSExtValue();
32403239
}
32413240

3241+
/// This function `buildCommonNeonCallPattern0` implements a common way
3242+
// to generate neon intrinsic call that has following pattern:
3243+
// 1. There is a need to cast result of the intrinsic call back to
3244+
// expression type.
3245+
// 2. Function arg types are given, not deduced from actual arg types.
3246+
static mlir::Value
3247+
buildCommonNeonCallPattern0(CIRGenFunction &cgf, std::string &intrincsName,
3248+
llvm::SmallVector<mlir::Type> argTypes,
3249+
llvm::SmallVectorImpl<mlir::Value> &ops,
3250+
mlir::Type funcResTy, const clang::CallExpr *e) {
3251+
CIRGenBuilderTy &builder = cgf.getBuilder();
3252+
mlir::Value res =
3253+
buildNeonCall(builder, std::move(argTypes), ops, intrincsName, funcResTy,
3254+
cgf.getLoc(e->getExprLoc()));
3255+
mlir::Type resultType = cgf.ConvertType(e->getType());
3256+
return builder.createBitcast(res, resultType);
3257+
}
3258+
32423259
mlir::Value CIRGenFunction::buildCommonNeonBuiltinExpr(
32433260
unsigned builtinID, unsigned llvmIntrinsic, unsigned altLLVMIntrinsic,
32443261
const char *nameHint, unsigned modifier, const CallExpr *e,
@@ -3303,18 +3320,25 @@ mlir::Value CIRGenFunction::buildCommonNeonBuiltinExpr(
33033320
default:
33043321
llvm::errs() << getAArch64SIMDIntrinsicString(builtinID) << " ";
33053322
llvm_unreachable("NYI");
3306-
case NEON::BI__builtin_neon_vqadd_v:
3307-
mlir::Value res = buildNeonCall(builtinID, *this, {vTy, vTy}, ops,
3308-
(intrinicId != altLLVMIntrinsic)
3309-
? "llvm.aarch64.neon.uqadd"
3310-
: "llvm.aarch64.neon.sqadd",
3311-
vTy, getLoc(e->getExprLoc()));
3312-
mlir::Type resultType = ConvertType(e->getType());
3313-
// AArch64 intrinsic one-element vector type cast to
3314-
// scalar type expected by the builtin
3315-
return builder.createBitcast(res, resultType);
3323+
3324+
case NEON::BI__builtin_neon_vpadd_v:
3325+
case NEON::BI__builtin_neon_vpaddq_v: {
3326+
std::string intrincsName = mlir::isa<mlir::FloatType>(vTy.getEltType())
3327+
? "llvm.aarch64.neon.faddp"
3328+
: "llvm.aarch64.neon.addp";
3329+
return buildCommonNeonCallPattern0(*this, intrincsName, {vTy, vTy}, ops,
3330+
vTy, e);
33163331
break;
33173332
}
3333+
case NEON::BI__builtin_neon_vqadd_v: {
3334+
std::string intrincsName = (intrinicId != altLLVMIntrinsic)
3335+
? "llvm.aarch64.neon.uqadd"
3336+
: "llvm.aarch64.neon.sqadd";
3337+
return buildCommonNeonCallPattern0(*this, intrincsName, {vTy, vTy}, ops,
3338+
vTy, e);
3339+
break;
3340+
}
3341+
}
33183342
return nullptr;
33193343
}
33203344

0 commit comments

Comments
 (0)