Skip to content

Commit cf9576d

Browse files
authored
[CIR] Upstream FPToFPBuiltin CosOp (#158342)
Upstream support for FPToFPBuiltin CosOp
1 parent 2a3c9f9 commit cf9576d

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,16 @@ def CIR_ATanOp : CIR_UnaryFPToFPBuiltinOp<"atan", "ATanOp"> {
38473847
}];
38483848
}
38493849

3850+
def CIR_CosOp : CIR_UnaryFPToFPBuiltinOp<"cos", "CosOp"> {
3851+
let summary = "Computes the floating-point cosine value";
3852+
let description = [{
3853+
`cir.cos` computes the cosine of a floating-point operand and returns
3854+
a result of the same type.
3855+
3856+
Floating-point exceptions are ignored, and it does not set `errno`.
3857+
}];
3858+
}
3859+
38503860
def CIR_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> {
38513861
let summary = "Computes the floating-point absolute value";
38523862
let description = [{

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
200200
builder.createBitcast(allocaAddr, builder.getVoidPtrTy()));
201201
}
202202

203+
case Builtin::BIcos:
204+
case Builtin::BIcosf:
205+
case Builtin::BIcosl:
206+
case Builtin::BI__builtin_cos:
207+
case Builtin::BI__builtin_cosf:
208+
case Builtin::BI__builtin_cosf16:
209+
case Builtin::BI__builtin_cosl:
210+
case Builtin::BI__builtin_cosf128:
211+
assert(!cir::MissingFeatures::fastMathFlags());
212+
return emitUnaryMaybeConstrainedFPBuiltin<cir::CosOp>(*this, *e);
213+
203214
case Builtin::BIfabs:
204215
case Builtin::BIfabsf:
205216
case Builtin::BIfabsl:
@@ -415,6 +426,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
415426
return emitUnaryFPBuiltin<cir::ASinOp>(*this, *e);
416427
case Builtin::BI__builtin_elementwise_atan:
417428
return emitUnaryFPBuiltin<cir::ATanOp>(*this, *e);
429+
case Builtin::BI__builtin_elementwise_cos:
430+
return emitUnaryFPBuiltin<cir::CosOp>(*this, *e);
418431
}
419432

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ mlir::LogicalResult CIRToLLVMCopyOpLowering::matchAndRewrite(
185185
return mlir::success();
186186
}
187187

188+
mlir::LogicalResult CIRToLLVMCosOpLowering::matchAndRewrite(
189+
cir::CosOp op, OpAdaptor adaptor,
190+
mlir::ConversionPatternRewriter &rewriter) const {
191+
mlir::Type resTy = typeConverter->convertType(op.getType());
192+
rewriter.replaceOpWithNewOp<mlir::LLVM::CosOp>(op, resTy, adaptor.getSrc());
193+
return mlir::success();
194+
}
195+
188196
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
189197
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
190198
bool isUnsigned, uint64_t cirSrcWidth,
@@ -2498,6 +2506,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
24982506
CIRToLLVMComplexRealPtrOpLowering,
24992507
CIRToLLVMComplexSubOpLowering,
25002508
CIRToLLVMCopyOpLowering,
2509+
CIRToLLVMCosOpLowering,
25012510
CIRToLLVMConstantOpLowering,
25022511
CIRToLLVMExpectOpLowering,
25032512
CIRToLLVMFAbsOpLowering,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ class CIRToLLVMCopyOpLowering : public mlir::OpConversionPattern<cir::CopyOp> {
189189
mlir::ConversionPatternRewriter &) const override;
190190
};
191191

192+
class CIRToLLVMCosOpLowering : public mlir::OpConversionPattern<cir::CosOp> {
193+
public:
194+
using mlir::OpConversionPattern<cir::CosOp>::OpConversionPattern;
195+
196+
mlir::LogicalResult
197+
matchAndRewrite(cir::CosOp op, OpAdaptor,
198+
mlir::ConversionPatternRewriter &) const override;
199+
};
200+
192201
class CIRToLLVMExpectOpLowering
193202
: public mlir::OpConversionPattern<cir::ExpectOp> {
194203
public:

clang/test/CIR/CodeGen/builtins-elementwise.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,30 @@ void test_builtin_elementwise_atan(float f, double d, vfloat4 vf4,
8989
// OGCG: %{{.*}} = call <4 x double> @llvm.atan.v4f64(<4 x double> %{{.*}})
9090
vd4 = __builtin_elementwise_atan(vd4);
9191
}
92+
93+
void test_builtin_elementwise_cos(float f, double d, vfloat4 vf4,
94+
vdouble4 vd4) {
95+
// CIR-LABEL: test_builtin_elementwise_cos
96+
// LLVM-LABEL: test_builtin_elementwise_cos
97+
// OGCG-LABEL: test_builtin_elementwise_cos
98+
99+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.float
100+
// LLVM: {{%.*}} = call float @llvm.cos.f32(float {{%.*}})
101+
// OGCG: {{%.*}} = call float @llvm.cos.f32(float {{%.*}})
102+
f = __builtin_elementwise_cos(f);
103+
104+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.double
105+
// LLVM: {{%.*}} = call double @llvm.cos.f64(double {{%.*}})
106+
// OGCG: {{%.*}} = call double @llvm.cos.f64(double {{%.*}})
107+
d = __builtin_elementwise_cos(d);
108+
109+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.vector<4 x !cir.float>
110+
// LLVM: {{%.*}} = call <4 x float> @llvm.cos.v4f32(<4 x float> {{%.*}})
111+
// OGCG: {{%.*}} = call <4 x float> @llvm.cos.v4f32(<4 x float> {{%.*}})
112+
vf4 = __builtin_elementwise_cos(vf4);
113+
114+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.vector<4 x !cir.double>
115+
// LLVM: {{%.*}} = call <4 x double> @llvm.cos.v4f64(<4 x double> {{%.*}})
116+
// OGCG: {{%.*}} = call <4 x double> @llvm.cos.v4f64(<4 x double> {{%.*}})
117+
vd4 = __builtin_elementwise_cos(vd4);
118+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
float cosf(float f) {
9+
return __builtin_cosf(f);
10+
// CHECK: %{{.*}} = cir.cos {{.*}} : !cir.float
11+
// LLVM: %{{.*}} = call float @llvm.cos.f32(float %{{.*}})
12+
// OGCG: %{{.*}} = call float @llvm.cos.f32(float %{{.*}})
13+
}
14+
15+
double cos(double f) {
16+
return __builtin_cos(f);
17+
// CIR: {{.+}} = cir.cos {{.+}} : !cir.double
18+
// LLVM: %{{.*}} = call double @llvm.cos.f64(double %{{.*}})
19+
// OGCG: %{{.*}} = call double @llvm.cos.f64(double %{{.*}})
20+
}

0 commit comments

Comments
 (0)