Skip to content

Commit f917f3b

Browse files
authored
[CIR][CIRGen][Neon] Make vrndns emit RoundEvenOp directly (#1434)
1 parent 7f66a20 commit f917f3b

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4787,6 +4787,7 @@ def Log2Op : UnaryFPToFPBuiltinOp<"log2", "Log2Op">;
47874787
def NearbyintOp : UnaryFPToFPBuiltinOp<"nearbyint", "NearbyintOp">;
47884788
def RintOp : UnaryFPToFPBuiltinOp<"rint", "RintOp">;
47894789
def RoundOp : UnaryFPToFPBuiltinOp<"round", "RoundOp">;
4790+
def RoundEvenOp : UnaryFPToFPBuiltinOp<"roundeven", "RoundEvenOp">;
47904791
def SinOp : UnaryFPToFPBuiltinOp<"sin", "SinOp">;
47914792
def SqrtOp : UnaryFPToFPBuiltinOp<"sqrt", "SqrtOp">;
47924793
def TruncOp : UnaryFPToFPBuiltinOp<"trunc", "FTruncOp">;

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,13 +2226,13 @@ static mlir::Value vecReduceIntValue(CIRGenFunction &cgf, mlir::Value val,
22262226
loc, val, builder.getConstInt(loc, cgf.SizeTy, 0));
22272227
}
22282228

2229-
mlir::Value emitNeonCall(CIRGenBuilderTy &builder,
2230-
llvm::SmallVector<mlir::Type> argTypes,
2231-
llvm::SmallVectorImpl<mlir::Value> &args,
2232-
llvm::StringRef intrinsicName, mlir::Type funcResTy,
2233-
mlir::Location loc,
2234-
bool isConstrainedFPIntrinsic = false,
2235-
unsigned shift = 0, bool rightshift = false) {
2229+
template <typename Operation>
2230+
static mlir::Value emitNeonCallToOp(
2231+
CIRGenBuilderTy &builder, llvm::SmallVector<mlir::Type> argTypes,
2232+
llvm::SmallVectorImpl<mlir::Value> &args,
2233+
std::optional<llvm::StringRef> intrinsicName, mlir::Type funcResTy,
2234+
mlir::Location loc, bool isConstrainedFPIntrinsic = false,
2235+
unsigned shift = 0, bool rightshift = false) {
22362236
// TODO: Consider removing the following unreachable when we have
22372237
// emitConstrainedFPCall feature implemented
22382238
assert(!cir::MissingFeatures::emitConstrainedFPCall());
@@ -2255,10 +2255,26 @@ mlir::Value emitNeonCall(CIRGenBuilderTy &builder,
22552255
assert(!cir::MissingFeatures::emitConstrainedFPCall());
22562256
return nullptr;
22572257
}
2258-
return builder
2259-
.create<cir::LLVMIntrinsicCallOp>(
2260-
loc, builder.getStringAttr(intrinsicName), funcResTy, args)
2261-
.getResult();
2258+
if constexpr (std::is_same_v<Operation, cir::LLVMIntrinsicCallOp>) {
2259+
return builder
2260+
.create<Operation>(loc, builder.getStringAttr(intrinsicName.value()),
2261+
funcResTy, args)
2262+
.getResult();
2263+
} else {
2264+
return builder.create<Operation>(loc, funcResTy, args).getResult();
2265+
}
2266+
}
2267+
2268+
static mlir::Value emitNeonCall(CIRGenBuilderTy &builder,
2269+
llvm::SmallVector<mlir::Type> argTypes,
2270+
llvm::SmallVectorImpl<mlir::Value> &args,
2271+
llvm::StringRef intrinsicName,
2272+
mlir::Type funcResTy, mlir::Location loc,
2273+
bool isConstrainedFPIntrinsic = false,
2274+
unsigned shift = 0, bool rightshift = false) {
2275+
return emitNeonCallToOp<cir::LLVMIntrinsicCallOp>(
2276+
builder, std::move(argTypes), args, intrinsicName, funcResTy, loc,
2277+
isConstrainedFPIntrinsic, shift, rightshift);
22622278
}
22632279

22642280
/// This function `emitCommonNeonCallPattern0` implements a common way
@@ -4139,8 +4155,9 @@ CIRGenFunction::emitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E,
41394155
case NEON::BI__builtin_neon_vrndns_f32: {
41404156
mlir::Value arg0 = emitScalarExpr(E->getArg(0));
41414157
args.push_back(arg0);
4142-
return emitNeonCall(builder, {arg0.getType()}, args, "roundeven.f32",
4143-
getCIRGenModule().FloatTy, getLoc(E->getExprLoc()));
4158+
return emitNeonCallToOp<cir::RoundEvenOp>(
4159+
builder, {arg0.getType()}, args, std::nullopt,
4160+
getCIRGenModule().FloatTy, getLoc(E->getExprLoc()));
41444161
}
41454162
case NEON::BI__builtin_neon_vrndph_f16: {
41464163
llvm_unreachable("NEON::BI__builtin_neon_vrndph_f16 NYI");

clang/test/CIR/CodeGen/AArch64/neon-arith.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ float32_t test_vrndns_f32(float32_t a) {
2323
// CIR: cir.func internal private @vrndns_f32(%arg0: !cir.float {{.*}}) -> !cir.float
2424
// CIR: cir.store %arg0, [[ARG_SAVE:%.*]] : !cir.float, !cir.ptr<!cir.float>
2525
// CIR: [[INTRIN_ARG:%.*]] = cir.load [[ARG_SAVE]] : !cir.ptr<!cir.float>, !cir.float
26-
// CIR: {{%.*}} = cir.llvm.intrinsic "roundeven.f32" [[INTRIN_ARG]] : (!cir.float)
26+
// CIR: {{%.*}} = cir.roundeven [[INTRIN_ARG]] : !cir.float
2727
// CIR: cir.return {{%.*}} : !cir.float
2828

2929
// CIR-LABEL: test_vrndns_f32

0 commit comments

Comments
 (0)