From 764cd36f0b1ff4405616c126ac799e602a182f89 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Sun, 7 Sep 2025 18:58:07 +0200 Subject: [PATCH 1/3] [CIR] Upstream FPToFPBuiltin ASinOp --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 10 +++++++ clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 2 ++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 10 +++++++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.h | 9 +++++++ clang/test/CIR/CodeGen/builtins-elementwise.c | 26 +++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 4592078af966b..6534104703182 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -3808,6 +3808,16 @@ def CIR_ACosOp : CIR_UnaryFPToFPBuiltinOp<"acos", "ACosOp"> { }]; } +def CIR_ASinOp : CIR_UnaryFPToFPBuiltinOp<"asin", "ASinOp"> { + let summary = "Computes the arcus sine of the specified value"; + let description = [{ + `cir.asin`computes the arcus sine 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_FAbsOp : CIR_UnaryFPToFPBuiltinOp<"fabs", "FAbsOp"> { let summary = "Computes the floating-point absolute value"; let description = [{ diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index b68e91f64dc84..eb524ea41f1ea 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -360,6 +360,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BI__builtin_elementwise_acos: return emitUnaryFPBuiltin(*this, *e); + case Builtin::BI__builtin_elementwise_asin: + return emitUnaryFPBuiltin(*this, *e); } // If this is an alias for a lib function (e.g. __builtin_sin), emit diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index ee9f58c829ca9..4069656434f07 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -598,6 +598,15 @@ mlir::LogicalResult CIRToLLVMACosOpLowering::matchAndRewrite( return mlir::success(); } +mlir::LogicalResult CIRToLLVMASinOpLowering::matchAndRewrite( + cir::ASinOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + mlir::Type resTy = typeConverter->convertType(op.getType()); + rewriter.replaceOpWithNewOp(op, resTy, + adaptor.getOperands()[0]); + return mlir::success(); +} + mlir::LogicalResult CIRToLLVMAssumeOpLowering::matchAndRewrite( cir::AssumeOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { @@ -2427,6 +2436,7 @@ void ConvertCIRToLLVMPass::runOnOperation() { patterns.add< // clang-format off CIRToLLVMACosOpLowering, + CIRToLLVMASinOpLowering, CIRToLLVMAssumeOpLowering, CIRToLLVMAssumeAlignedOpLowering, CIRToLLVMAssumeSepStorageOpLowering, diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h index 2c2aede09b0b2..e2966f20d2efc 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h @@ -736,6 +736,15 @@ class CIRToLLVMACosOpLowering : public mlir::OpConversionPattern { mlir::ConversionPatternRewriter &) const override; }; +class CIRToLLVMASinOpLowering : public mlir::OpConversionPattern { +public: + using mlir::OpConversionPattern::OpConversionPattern; + + mlir::LogicalResult + matchAndRewrite(cir::ASinOp op, OpAdaptor, + mlir::ConversionPatternRewriter &) const override; +}; + class CIRToLLVMInlineAsmOpLowering : public mlir::OpConversionPattern { mlir::DataLayout const &dataLayout; diff --git a/clang/test/CIR/CodeGen/builtins-elementwise.c b/clang/test/CIR/CodeGen/builtins-elementwise.c index 1898f56a33628..431558d7e9697 100644 --- a/clang/test/CIR/CodeGen/builtins-elementwise.c +++ b/clang/test/CIR/CodeGen/builtins-elementwise.c @@ -36,3 +36,29 @@ void test_builtin_elementwise_acos(float f, double d, vfloat4 vf4, vd4 = __builtin_elementwise_acos(vd4); } +void test_builtin_elementwise_asin(float f, double d, vfloat4 vf4, + vdouble4 vd4) { + // CIR-LABEL: test_builtin_elementwise_asin + // LLVM-LABEL: test_builtin_elementwise_asin + // OGCG-LABEL: test_builtin_elementwise_asin + + // CIR: %{{.*}} = cir.asin %{{.*}} : !cir.float + // LLVM: %{{.*}} = call float @llvm.asin.f32(float %{{.*}}) + // OGCG: %{{.*}} = call float @llvm.asin.f32(float %{{.*}}) + f = __builtin_elementwise_asin(f); + + // CIR: %{{.*}} = cir.asin %{{.*}} : !cir.double + // LLVM: %{{.*}} = call double @llvm.asin.f64(double %{{.*}}) + // OGCG: %{{.*}} = call double @llvm.asin.f64(double %{{.*}}) + d = __builtin_elementwise_asin(d); + + // CIR: %{{.*}} = cir.asin %{{.*}} : !cir.vector<4 x !cir.float> + // LLVM: %{{.*}} = call <4 x float> @llvm.asin.v4f32(<4 x float> %{{.*}}) + // OGCG: %{{.*}} = call <4 x float> @llvm.asin.v4f32(<4 x float> %{{.*}}) + vf4 = __builtin_elementwise_asin(vf4); + + // CIR: %{{.*}} = cir.asin %{{.*}} : !cir.vector<4 x !cir.double> + // LLVM: %{{.*}} = call <4 x double> @llvm.asin.v4f64(<4 x double> %{{.*}}) + // OGCG: %{{.*}} = call <4 x double> @llvm.asin.v4f64(<4 x double> %{{.*}}) + vd4 = __builtin_elementwise_asin(vd4); +} From edbdcb1ceedc5017e309b4d49aa70deb9814e3b7 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Tue, 9 Sep 2025 18:35:29 +0200 Subject: [PATCH 2/3] Address code review comments --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 4069656434f07..3a7af69af8e3d 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -603,7 +603,7 @@ mlir::LogicalResult CIRToLLVMASinOpLowering::matchAndRewrite( mlir::ConversionPatternRewriter &rewriter) const { mlir::Type resTy = typeConverter->convertType(op.getType()); rewriter.replaceOpWithNewOp(op, resTy, - adaptor.getOperands()[0]); + adaptor.getSrc()); return mlir::success(); } From 65cb179daf1713b519256755af6b66df078b0b06 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Tue, 9 Sep 2025 18:57:22 +0200 Subject: [PATCH 3/3] Format the code --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 3a7af69af8e3d..243c83fdfe7d0 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -602,8 +602,7 @@ mlir::LogicalResult CIRToLLVMASinOpLowering::matchAndRewrite( cir::ASinOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { mlir::Type resTy = typeConverter->convertType(op.getType()); - rewriter.replaceOpWithNewOp(op, resTy, - adaptor.getSrc()); + rewriter.replaceOpWithNewOp(op, resTy, adaptor.getSrc()); return mlir::success(); }