From b0eab9a9f0131db7e20cfb47c5b4a4a35e9e7fe7 Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Tue, 1 Apr 2025 22:58:18 +0800 Subject: [PATCH] [CIR][CIRGen][Builtin] Add `__builtin_elementwise_{log, log2, log10}` --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 6 +- clang/test/CIR/CodeGen/builtins-elementwise.c | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index a80e5f721666..225ff73899f1 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1457,11 +1457,11 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, case Builtin::BI__builtin_elementwise_exp2: llvm_unreachable("BI__builtin_elementwise_exp2 NYI"); case Builtin::BI__builtin_elementwise_log: - llvm_unreachable("BI__builtin_elementwise_log NYI"); + return emitUnaryFPBuiltin(*this, *E); case Builtin::BI__builtin_elementwise_log2: - llvm_unreachable("BI__builtin_elementwise_log2 NYI"); + return emitUnaryFPBuiltin(*this, *E); case Builtin::BI__builtin_elementwise_log10: - llvm_unreachable("BI__builtin_elementwise_log10 NYI"); + return emitUnaryFPBuiltin(*this, *E); case Builtin::BI__builtin_elementwise_pow: llvm_unreachable("BI__builtin_elementwise_pow NYI"); case Builtin::BI__builtin_elementwise_bitreverse: diff --git a/clang/test/CIR/CodeGen/builtins-elementwise.c b/clang/test/CIR/CodeGen/builtins-elementwise.c index 38b6e24f9d14..1c632794111b 100644 --- a/clang/test/CIR/CodeGen/builtins-elementwise.c +++ b/clang/test/CIR/CodeGen/builtins-elementwise.c @@ -120,3 +120,66 @@ void test_builtin_elementwise_exp(float f, double d, vfloat4 vf4, // LLVM: {{%.*}} = call <4 x double> @llvm.exp.v4f64(<4 x double> {{%.*}}) vd4 = __builtin_elementwise_exp(vd4); } + +void test_builtin_elementwise_log(float f, double d, vfloat4 vf4, + vdouble4 vd4) { + // CIR-LABEL: test_builtin_elementwise_log + // LLVM-LABEL: test_builtin_elementwise_log + // CIR: {{%.*}} = cir.log {{%.*}} : !cir.float + // LLVM: {{%.*}} = call float @llvm.log.f32(float {{%.*}}) + f = __builtin_elementwise_log(f); + + // CIR: {{%.*}} = cir.log {{%.*}} : !cir.double + // LLVM: {{%.*}} = call double @llvm.log.f64(double {{%.*}}) + d = __builtin_elementwise_log(d); + + // CIR: {{%.*}} = cir.log {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x float> @llvm.log.v4f32(<4 x float> {{%.*}}) + vf4 = __builtin_elementwise_log(vf4); + + // CIR: {{%.*}} = cir.log {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x double> @llvm.log.v4f64(<4 x double> {{%.*}}) + vd4 = __builtin_elementwise_log(vd4); +} + +void test_builtin_elementwise_log2(float f, double d, vfloat4 vf4, + vdouble4 vd4) { + // CIR-LABEL: test_builtin_elementwise_log2 + // LLVM-LABEL: test_builtin_elementwise_log2 + // CIR: {{%.*}} = cir.log2 {{%.*}} : !cir.float + // LLVM: {{%.*}} = call float @llvm.log2.f32(float {{%.*}}) + f = __builtin_elementwise_log2(f); + + // CIR: {{%.*}} = cir.log2 {{%.*}} : !cir.double + // LLVM: {{%.*}} = call double @llvm.log2.f64(double {{%.*}}) + d = __builtin_elementwise_log2(d); + + // CIR: {{%.*}} = cir.log2 {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x float> @llvm.log2.v4f32(<4 x float> {{%.*}}) + vf4 = __builtin_elementwise_log2(vf4); + + // CIR: {{%.*}} = cir.log2 {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x double> @llvm.log2.v4f64(<4 x double> {{%.*}}) + vd4 = __builtin_elementwise_log2(vd4); +} + +void test_builtin_elementwise_log10(float f, double d, vfloat4 vf4, + vdouble4 vd4) { + // CIR-LABEL: test_builtin_elementwise_log10 + // LLVM-LABEL: test_builtin_elementwise_log10 + // CIR: {{%.*}} = cir.log10 {{%.*}} : !cir.float + // LLVM: {{%.*}} = call float @llvm.log10.f32(float {{%.*}}) + f = __builtin_elementwise_log10(f); + + // CIR: {{%.*}} = cir.log10 {{%.*}} : !cir.double + // LLVM: {{%.*}} = call double @llvm.log10.f64(double {{%.*}}) + d = __builtin_elementwise_log10(d); + + // CIR: {{%.*}} = cir.log10 {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x float> @llvm.log10.v4f32(<4 x float> {{%.*}}) + vf4 = __builtin_elementwise_log10(vf4); + + // CIR: {{%.*}} = cir.log10 {{%.*}} : !cir.vector + // LLVM: {{%.*}} = call <4 x double> @llvm.log10.v4f64(<4 x double> {{%.*}}) + vd4 = __builtin_elementwise_log10(vd4); +}