Skip to content

Commit 1b130e3

Browse files
authored
[CIR] Upstream FPToFPBuiltin ACosOp (llvm#156356)
Upstream support for FPToFPBuiltin ACosOp
1 parent df9965c commit 1b130e3

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,12 +3752,23 @@ class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
37523752
let llvmOp = llvmOpName;
37533753
}
37543754

3755+
def CIR_ACosOp : CIR_UnaryFPToFPBuiltinOp<"acos", "ACosOp"> {
3756+
let summary = "Computes the arcus cosine of the specified value";
3757+
let description = [{
3758+
`cir.acos`computes the arcus cosine of a given value and
3759+
returns a result of the same type.
3760+
3761+
Floating-point exceptions are ignored, and it does not set `errno`.
3762+
}];
3763+
}
3764+
37553765
def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
37563766
let summary = "Computes the floating-point absolute value";
37573767
let description = [{
37583768
`cir.fabs` computes the absolute value of a floating-point operand
3759-
and returns a result of the same type, ignoring floating-point
3760-
exceptions. It does not set `errno`.
3769+
and returns a result of the same type.
3770+
3771+
Floating-point exceptions are ignored, and it does not set `errno`.
37613772
}];
37623773
}
37633774

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 {
@@ -2396,6 +2405,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
23962405
dl);
23972406
patterns.add<
23982407
// clang-format off
2408+
CIRToLLVMACosOpLowering,
23992409
CIRToLLVMAssumeOpLowering,
24002410
CIRToLLVMAssumeAlignedOpLowering,
24012411
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)