Skip to content

Commit 44d0ce4

Browse files
committed
[CIR] Upstream builtin ACos op
1 parent 3ed91d8 commit 44d0ce4

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,6 +3732,15 @@ class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
37323732
let llvmOp = llvmOpName;
37333733
}
37343734

3735+
def CIR_ACosOp : CIR_UnaryFPToFPBuiltinOp<"acos", "ACosOp"> {
3736+
let summary = "Computes the arcus cosine of the specified value";
3737+
let description = [{
3738+
`cir.acos`computes the arcus cosine of a given value and
3739+
returns a result of the same type. ignoring floating-point
3740+
exceptions. It does not set `errno`.
3741+
}];
3742+
}
3743+
37353744
def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
37363745
let summary = "Computes the floating-point absolute value";
37373746
let description = [{

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ static RValue emitUnaryMaybeConstrainedFPBuiltin(CIRGenFunction &cgf,
8585
return RValue::get(call->getResult(0));
8686
}
8787

88+
template <class Operation>
89+
static RValue emitUnaryFPBuiltin(CIRGenFunction &cgf, const CallExpr &e) {
90+
mlir::Value arg = cgf.emitScalarExpr(e.getArg(0));
91+
auto call =
92+
Operation::create(cgf.getBuilder(), arg.getLoc(), arg.getType(), arg);
93+
return RValue::get(call->getResult(0));
94+
}
95+
8896
RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
8997
const CallExpr *e,
9098
ReturnValueSlot returnValue) {
@@ -349,6 +357,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
349357
case Builtin::BI__builtin_unreachable:
350358
emitUnreachable(e->getExprLoc(), /*createNewBlock=*/true);
351359
return RValue::get(nullptr);
360+
361+
case Builtin::BI__builtin_elementwise_acos:
362+
return emitUnaryFPBuiltin<cir::ACosOp>(*this, *e);
352363
}
353364

354365
// If this is an alias for a lib function (e.g. __builtin_sin), emit

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,15 @@ struct ConvertCIRToLLVMPass
577577
StringRef getArgument() const override { return "cir-flat-to-llvm"; }
578578
};
579579

580+
mlir::LogicalResult CIRToLLVMACosOpLowering::matchAndRewrite(
581+
cir::ACosOp op, OpAdaptor adaptor,
582+
mlir::ConversionPatternRewriter &rewriter) const {
583+
mlir::Type resTy = typeConverter->convertType(op.getType());
584+
rewriter.replaceOpWithNewOp<mlir::LLVM::ACosOp>(op, resTy,
585+
adaptor.getOperands()[0]);
586+
return mlir::success();
587+
}
588+
580589
mlir::LogicalResult CIRToLLVMAssumeOpLowering::matchAndRewrite(
581590
cir::AssumeOp op, OpAdaptor adaptor,
582591
mlir::ConversionPatternRewriter &rewriter) const {
@@ -2395,6 +2404,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
23952404
dl);
23962405
patterns.add<
23972406
// clang-format off
2407+
CIRToLLVMACosOpLowering,
23982408
CIRToLLVMAssumeOpLowering,
23992409
CIRToLLVMAssumeAlignedOpLowering,
24002410
CIRToLLVMAssumeSepStorageOpLowering,

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,15 @@ class CIRToLLVMFAbsOpLowering : public mlir::OpConversionPattern<cir::FAbsOp> {
718718
mlir::ConversionPatternRewriter &) const override;
719719
};
720720

721+
class CIRToLLVMACosOpLowering : public mlir::OpConversionPattern<cir::ACosOp> {
722+
public:
723+
using mlir::OpConversionPattern<cir::ACosOp>::OpConversionPattern;
724+
725+
mlir::LogicalResult
726+
matchAndRewrite(cir::ACosOp op, OpAdaptor,
727+
mlir::ConversionPatternRewriter &) const override;
728+
};
729+
721730
class CIRToLLVMInlineAsmOpLowering
722731
: public mlir::OpConversionPattern<cir::InlineAsmOp> {
723732
mlir::DataLayout const &dataLayout;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -Wno-unused-value -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
typedef int vint4 __attribute__((ext_vector_type(4)));
9+
typedef float vfloat4 __attribute__((ext_vector_type(4)));
10+
typedef double vdouble4 __attribute__((ext_vector_type(4)));
11+
12+
void test_builtin_elementwise_acos(float f, double d, vfloat4 vf4,
13+
vdouble4 vd4) {
14+
// CIR-LABEL: test_builtin_elementwise_acos
15+
// LLVM-LABEL: test_builtin_elementwise_acos
16+
// OGCG-LABEL: test_builtin_elementwise_acos
17+
18+
// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.float
19+
// LLVM: %{{.*}} = call float @llvm.acos.f32(float %{{.*}})
20+
// OGCG: %{{.*}} = call float @llvm.acos.f32(float %{{.*}})
21+
f = __builtin_elementwise_acos(f);
22+
23+
// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.double
24+
// LLVM: %{{.*}} = call double @llvm.acos.f64(double %{{.*}})
25+
// OGCG: %{{.*}} = call double @llvm.acos.f64(double %{{.*}})
26+
d = __builtin_elementwise_acos(d);
27+
28+
// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.vector<4 x !cir.float>
29+
// LLVM: %{{.*}} = call <4 x float> @llvm.acos.v4f32(<4 x float> %{{.*}})
30+
// OGCG: %{{.*}} = call <4 x float> @llvm.acos.v4f32(<4 x float> %{{.*}})
31+
vf4 = __builtin_elementwise_acos(vf4);
32+
33+
// CIR: %{{.*}} = cir.acos %{{.*}} : !cir.vector<4 x !cir.double>
34+
// LLVM: %{{.*}} = call <4 x double> @llvm.acos.v4f64(<4 x double> %{{.*}})
35+
// OGCG: %{{.*}} = call <4 x double> @llvm.acos.v4f64(<4 x double> %{{.*}})
36+
vd4 = __builtin_elementwise_acos(vd4);
37+
}
38+

0 commit comments

Comments
 (0)