From 4bf02de0821fe07eb82e9abab2cb1abc62ffa60d Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Sat, 1 Nov 2025 20:50:36 +0100 Subject: [PATCH] [CIR] Upstream FPToFP Builtin CeilOp --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 10 ++++++++++ clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 11 +++++++++++ clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 8 ++++++++ clang/test/CIR/CodeGen/builtins-floating-point.c | 11 +++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 2b361ed0982c6..dc56db1bbd4ea 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -4171,6 +4171,16 @@ def CIR_ATanOp : CIR_UnaryFPToFPBuiltinOp<"atan", "ATanOp"> { }]; } +def CIR_CeilOp : CIR_UnaryFPToFPBuiltinOp<"ceil", "FCeilOp"> { + let summary = "Computes the ceiling of the specified value"; + let description = [{ + `cir.ceil` computes the ceiling of a given value and returns a result + of the same type. + + Floating-point exceptions are ignored, and it does not set `errno`. + }]; +} + def CIR_CosOp : CIR_UnaryFPToFPBuiltinOp<"cos", "CosOp"> { let summary = "Computes the floating-point cosine value"; let description = [{ diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index e35100ffe4b6b..d9b9e3b877b50 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -211,6 +211,17 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, assert(!cir::MissingFeatures::fastMathFlags()); return emitUnaryMaybeConstrainedFPBuiltin(*this, *e); + case Builtin::BIceil: + case Builtin::BIceilf: + case Builtin::BIceill: + case Builtin::BI__builtin_ceil: + case Builtin::BI__builtin_ceilf: + case Builtin::BI__builtin_ceilf16: + case Builtin::BI__builtin_ceill: + case Builtin::BI__builtin_ceilf128: + assert(!cir::MissingFeatures::fastMathFlags()); + return emitUnaryMaybeConstrainedFPBuiltin(*this, *e); + case Builtin::BIfabs: case Builtin::BIfabsf: case Builtin::BIfabsl: diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 5a6193fa8d840..d94108294a9a3 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1336,6 +1336,14 @@ mlir::LogicalResult CIRToLLVMATanOpLowering::matchAndRewrite( return mlir::success(); } +mlir::LogicalResult CIRToLLVMCeilOpLowering::matchAndRewrite( + cir::CeilOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + mlir::Type resTy = typeConverter->convertType(op.getType()); + rewriter.replaceOpWithNewOp(op, resTy, adaptor.getSrc()); + return mlir::success(); +} + mlir::LogicalResult CIRToLLVMAllocaOpLowering::matchAndRewrite( cir::AllocaOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { diff --git a/clang/test/CIR/CodeGen/builtins-floating-point.c b/clang/test/CIR/CodeGen/builtins-floating-point.c index 193cc172d37d2..8bdc43c59dc6f 100644 --- a/clang/test/CIR/CodeGen/builtins-floating-point.c +++ b/clang/test/CIR/CodeGen/builtins-floating-point.c @@ -7,14 +7,21 @@ float cosf(float f) { return __builtin_cosf(f); - // CHECK: %{{.*}} = cir.cos {{.*}} : !cir.float + // CIR: %{{.*}} = cir.cos %{{.*}} : !cir.float // LLVM: %{{.*}} = call float @llvm.cos.f32(float %{{.*}}) // OGCG: %{{.*}} = call float @llvm.cos.f32(float %{{.*}}) } double cos(double f) { return __builtin_cos(f); - // CIR: {{.+}} = cir.cos {{.+}} : !cir.double + // CIR: %{{.*}} = cir.cos %{{.*}} : !cir.double // LLVM: %{{.*}} = call double @llvm.cos.f64(double %{{.*}}) // OGCG: %{{.*}} = call double @llvm.cos.f64(double %{{.*}}) } + +float ceil(float f) { + return __builtin_ceilf(f); + // CIR: %{{.*}} = cir.ceil %{{.*}} : !cir.float + // LLVM: %{{.*}} = call float @llvm.ceil.f32(float %{{.*}}) + // OGCG: %{{.*}} = call float @llvm.ceil.f32(float %{{.*}}) +}