From 4c82050c56926d840e4ccf253ad10e6ae3ee6cc7 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Thu, 9 Mar 2023 11:31:31 -0800 Subject: [PATCH] Add codegen for llvm exp/exp2 elementwise builtins Add codegen for llvm exp/exp2 elementwise builtin The exp/exp2 elementwise builtins are necessary for HLSL codegen. Tests were added to make sure that the expected errors are encountered when these functions are given inputs of incompatible types. The new builtins are restricted to floating point types only. Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D145270 --- clang/docs/LanguageExtensions.rst | 2 ++ clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/Basic/Builtins.def | 2 ++ clang/lib/CodeGen/CGBuiltin.cpp | 6 ++++ clang/lib/Sema/SemaChecking.cpp | 2 ++ clang/test/CodeGen/builtins-elementwise-math.c | 33 ++++++++++++++++++ clang/test/Sema/aarch64-sve-vector-exp-ops.c | 18 ++++++++++ clang/test/Sema/builtins-elementwise-math.c | 43 ++++++++++++++++++++++++ clang/test/Sema/riscv-sve-vector-exp-ops.c | 19 +++++++++++ clang/test/SemaCXX/builtins-elementwise-math.cpp | 14 ++++++++ 10 files changed, 141 insertions(+) create mode 100644 clang/test/Sema/aarch64-sve-vector-exp-ops.c create mode 100644 clang/test/Sema/riscv-sve-vector-exp-ops.c diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index c0ea8af..413ad8d 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -639,6 +639,8 @@ Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±in T __builtin_elementwise_log(T x) return the natural logarithm of x floating point types T __builtin_elementwise_log2(T x) return the base 2 logarithm of x floating point types T __builtin_elementwise_log10(T x) return the base 10 logarithm of x floating point types + T __builtin_elementwise_exp(T x) returns the base-e exponential, e^x, of the specified value floating point types + T __builtin_elementwise_exp2(T x) returns the base-2 exponential, 2^x, of the specified value floating point types T __builtin_elementwise_roundeven(T x) round x to the nearest integer value in floating point format, floating point types rounding halfway cases to even (that is, to the nearest value that is an even integer), regardless of the current rounding diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 59c8c5c..332431e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -270,6 +270,8 @@ Floating Point Support in Clang - Add ``__builtin_elementwise_log`` builtin for floating point types only. - Add ``__builtin_elementwise_log10`` builtin for floating point types only. - Add ``__builtin_elementwise_log2`` builtin for floating point types only. +- Add ``__builtin_elementwise_exp`` builtin for floating point types only. +- Add ``__builtin_elementwise_exp2`` builtin for floating point types only. AST Matchers ------------ diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def index 478eb59..8144616 100644 --- a/clang/include/clang/Basic/Builtins.def +++ b/clang/include/clang/Basic/Builtins.def @@ -667,6 +667,8 @@ BUILTIN(__builtin_elementwise_max, "v.", "nct") BUILTIN(__builtin_elementwise_min, "v.", "nct") BUILTIN(__builtin_elementwise_ceil, "v.", "nct") BUILTIN(__builtin_elementwise_cos, "v.", "nct") +BUILTIN(__builtin_elementwise_exp, "v.", "nct") +BUILTIN(__builtin_elementwise_exp2, "v.", "nct") BUILTIN(__builtin_elementwise_floor, "v.", "nct") BUILTIN(__builtin_elementwise_log, "v.", "nct") BUILTIN(__builtin_elementwise_log2, "v.", "nct") diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index cea4727..e4d65fe 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -3100,6 +3100,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, case Builtin::BI__builtin_elementwise_ceil: return RValue::get( emitUnaryBuiltin(*this, E, llvm::Intrinsic::ceil, "elt.ceil")); + case Builtin::BI__builtin_elementwise_exp: + return RValue::get( + emitUnaryBuiltin(*this, E, llvm::Intrinsic::exp, "elt.exp")); + case Builtin::BI__builtin_elementwise_exp2: + return RValue::get( + emitUnaryBuiltin(*this, E, llvm::Intrinsic::exp2, "elt.exp2")); case Builtin::BI__builtin_elementwise_log: return RValue::get( emitUnaryBuiltin(*this, E, llvm::Intrinsic::log, "elt.log")); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 485351f..01f73f6 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2614,6 +2614,8 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, // types only. case Builtin::BI__builtin_elementwise_ceil: case Builtin::BI__builtin_elementwise_cos: + case Builtin::BI__builtin_elementwise_exp: + case Builtin::BI__builtin_elementwise_exp2: case Builtin::BI__builtin_elementwise_floor: case Builtin::BI__builtin_elementwise_log: case Builtin::BI__builtin_elementwise_log2: diff --git a/clang/test/CodeGen/builtins-elementwise-math.c b/clang/test/CodeGen/builtins-elementwise-math.c index 1b48a12..deb518b 100644 --- a/clang/test/CodeGen/builtins-elementwise-math.c +++ b/clang/test/CodeGen/builtins-elementwise-math.c @@ -355,6 +355,39 @@ void test_builtin_elementwise_cos(float f1, float f2, double d1, double d2, vf2 = __builtin_elementwise_cos(vf1); } +void test_builtin_elementwise_exp(float f1, float f2, double d1, double d2, + float4 vf1, float4 vf2) { + // CHECK-LABEL: define void @test_builtin_elementwise_exp( + // CHECK: [[F1:%.+]] = load float, ptr %f1.addr, align 4 + // CHECK-NEXT: call float @llvm.exp.f32(float [[F1]]) + f2 = __builtin_elementwise_exp(f1); + + // CHECK: [[D1:%.+]] = load double, ptr %d1.addr, align 8 + // CHECK-NEXT: call double @llvm.exp.f64(double [[D1]]) + d2 = __builtin_elementwise_exp(d1); + + // CHECK: [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16 + // CHECK-NEXT: call <4 x float> @llvm.exp.v4f32(<4 x float> [[VF1]]) + vf2 = __builtin_elementwise_exp(vf1); +} + +void test_builtin_elementwise_exp2(float f1, float f2, double d1, double d2, + float4 vf1, float4 vf2) { + // CHECK-LABEL: define void @test_builtin_elementwise_exp2( + // CHECK: [[F1:%.+]] = load float, ptr %f1.addr, align 4 + // CHECK-NEXT: call float @llvm.exp2.f32(float [[F1]]) + f2 = __builtin_elementwise_exp2(f1); + + // CHECK: [[D1:%.+]] = load double, ptr %d1.addr, align 8 + // CHECK-NEXT: call double @llvm.exp2.f64(double [[D1]]) + d2 = __builtin_elementwise_exp2(d1); + + // CHECK: [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16 + // CHECK-NEXT: call <4 x float> @llvm.exp2.v4f32(<4 x float> [[VF1]]) + vf2 = __builtin_elementwise_exp2(vf1); +} + + void test_builtin_elementwise_floor(float f1, float f2, double d1, double d2, float4 vf1, float4 vf2) { // CHECK-LABEL: define void @test_builtin_elementwise_floor( diff --git a/clang/test/Sema/aarch64-sve-vector-exp-ops.c b/clang/test/Sema/aarch64-sve-vector-exp-ops.c new file mode 100644 index 0000000..b64bd91 --- /dev/null +++ b/clang/test/Sema/aarch64-sve-vector-exp-ops.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple aarch64 -target-feature +f -target-feature +d \ +// RUN: -target-feature +v -target-feature +zfh -target-feature +sve -target-feature +experimental-zvfh \ +// RUN: -disable-O0-optnone -o - -fsyntax-only %s -verify +// REQUIRES: aarch64-registered-target + +#include + +svfloat32_t test_exp_vv_i8mf8(svfloat32_t v) { + + return __builtin_elementwise_exp(v); + // expected-error@-1 {{1st argument must be a vector, integer or floating point type}} +} + +svfloat32_t test_exp2_vv_i8mf8(svfloat32_t v) { + + return __builtin_elementwise_exp2(v); + // expected-error@-1 {{1st argument must be a vector, integer or floating point type}} +} diff --git a/clang/test/Sema/builtins-elementwise-math.c b/clang/test/Sema/builtins-elementwise-math.c index c803fce..35b0654 100644 --- a/clang/test/Sema/builtins-elementwise-math.c +++ b/clang/test/Sema/builtins-elementwise-math.c @@ -311,6 +311,49 @@ void test_builtin_elementwise_cos(int i, float f, double d, float4 v, int3 iv, u // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}} } +void test_builtin_elementwise_exp(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) { + + struct Foo s = __builtin_elementwise_exp(f); + // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}} + + i = __builtin_elementwise_exp(); + // expected-error@-1 {{too few arguments to function call, expected 1, have 0}} + + i = __builtin_elementwise_exp(i); + // expected-error@-1 {{1st argument must be a floating point type (was 'int')}} + + i = __builtin_elementwise_exp(f, f); + // expected-error@-1 {{too many arguments to function call, expected 1, have 2}} + + u = __builtin_elementwise_exp(u); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}} + + uv = __builtin_elementwise_exp(uv); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}} +} + +void test_builtin_elementwise_exp2(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) { + + struct Foo s = __builtin_elementwise_exp2(f); + // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}} + + i = __builtin_elementwise_exp2(); + // expected-error@-1 {{too few arguments to function call, expected 1, have 0}} + + i = __builtin_elementwise_exp2(i); + // expected-error@-1 {{1st argument must be a floating point type (was 'int')}} + + i = __builtin_elementwise_exp2(f, f); + // expected-error@-1 {{too many arguments to function call, expected 1, have 2}} + + u = __builtin_elementwise_exp2(u); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}} + + uv = __builtin_elementwise_exp2(uv); + // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}} +} + + void test_builtin_elementwise_floor(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) { struct Foo s = __builtin_elementwise_floor(f); diff --git a/clang/test/Sema/riscv-sve-vector-exp-ops.c b/clang/test/Sema/riscv-sve-vector-exp-ops.c new file mode 100644 index 0000000..f76c225 --- /dev/null +++ b/clang/test/Sema/riscv-sve-vector-exp-ops.c @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple riscv64 -target-feature +f -target-feature +d \ +// RUN: -target-feature +v -target-feature +zfh -target-feature +experimental-zvfh \ +// RUN: -disable-O0-optnone -o - -fsyntax-only %s -verify +// REQUIRES: riscv-registered-target + +#include + + +vfloat32mf2_t test_exp_vv_i8mf8(vfloat32mf2_t v) { + + return __builtin_elementwise_exp(v); + // expected-error@-1 {{1st argument must be a vector, integer or floating point type}} +} + +vfloat32mf2_t test_exp2_vv_i8mf8(vfloat32mf2_t v) { + + return __builtin_elementwise_exp2(v); + // expected-error@-1 {{1st argument must be a vector, integer or floating point type}} +} diff --git a/clang/test/SemaCXX/builtins-elementwise-math.cpp b/clang/test/SemaCXX/builtins-elementwise-math.cpp index 8e3e5d6..78cfcac 100644 --- a/clang/test/SemaCXX/builtins-elementwise-math.cpp +++ b/clang/test/SemaCXX/builtins-elementwise-math.cpp @@ -67,6 +67,20 @@ void test_builtin_elementwise_cos() { static_assert(!is_const::value); } +void test_builtin_elementwise_exp() { + const float a = 42.0; + float b = 42.3; + static_assert(!is_const::value); + static_assert(!is_const::value); +} + +void test_builtin_elementwise_exp2() { + const float a = 42.0; + float b = 42.3; + static_assert(!is_const::value); + static_assert(!is_const::value); +} + void test_builtin_elementwise_sin() { const float a = 42.0; float b = 42.3; -- 2.7.4