Add builtin_elementwise_log
authorJoshua Batista <jbatista@microsoft.com>
Thu, 2 Feb 2023 19:10:53 +0000 (11:10 -0800)
committerJoshua Batista <jbatista@microsoft.com>
Thu, 2 Feb 2023 19:36:22 +0000 (11:36 -0800)
Add codegen for llvm log elementwise builtin
The log elementwise builtin is 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 builtin is restricted to floating point types only.

Reviewed By: beanz

Differential Revision: https://reviews.llvm.org/D140489

clang/docs/LanguageExtensions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/Builtins.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/builtins-elementwise-math.c
clang/test/Sema/aarch64-sve-vector-log-ops.c [new file with mode: 0644]
clang/test/Sema/builtins-elementwise-math.c
clang/test/Sema/riscv-sve-vector-log-ops.c [new file with mode: 0644]
clang/test/SemaCXX/builtins-elementwise-math.cpp

index 0b533a2..90e7943 100644 (file)
@@ -635,6 +635,7 @@ Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±in
  T __builtin_elementwise_sin(T x)            return the sine of x interpreted as an angle in radians          floating point types
  T __builtin_elementwise_cos(T x)            return the cosine of x interpreted as an angle in radians        floating point types
  T __builtin_elementwise_floor(T x)          return the largest integral value less than or equal to x        floating point types
+ T __builtin_elementwise_log(T x)            return the natural logarithm of x                                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
index 3ed971e..8e93be1 100644 (file)
@@ -183,6 +183,7 @@ Arm and AArch64 Support in Clang
 
 Floating Point Support in Clang
 -------------------------------
+- Add ``__builtin_elementwise_log``builtin for floating point types only.
 
 Internal API Changes
 --------------------
index 6b54ff7..07e0a2d 100644 (file)
@@ -662,6 +662,7 @@ BUILTIN(__builtin_elementwise_min, "v.", "nct")
 BUILTIN(__builtin_elementwise_ceil, "v.", "nct")
 BUILTIN(__builtin_elementwise_cos, "v.", "nct")
 BUILTIN(__builtin_elementwise_floor, "v.", "nct")
+BUILTIN(__builtin_elementwise_log, "v.", "nct")
 BUILTIN(__builtin_elementwise_roundeven, "v.", "nct")
 BUILTIN(__builtin_elementwise_sin, "v.", "nct")
 BUILTIN(__builtin_elementwise_trunc, "v.", "nct")
index bf1c9ac..8f733d7 100644 (file)
@@ -3079,6 +3079,9 @@ 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_log:
+    return RValue::get(
+        emitUnaryBuiltin(*this, E, llvm::Intrinsic::log, "elt.log"));
   case Builtin::BI__builtin_elementwise_cos:
     return RValue::get(
         emitUnaryBuiltin(*this, E, llvm::Intrinsic::cos, "elt.cos"));
index 4602284..f1c4aab 100644 (file)
@@ -2609,6 +2609,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
   case Builtin::BI__builtin_elementwise_ceil:
   case Builtin::BI__builtin_elementwise_cos:
   case Builtin::BI__builtin_elementwise_floor:
+  case Builtin::BI__builtin_elementwise_log:
   case Builtin::BI__builtin_elementwise_roundeven:
   case Builtin::BI__builtin_elementwise_sin:
   case Builtin::BI__builtin_elementwise_trunc:
index cc70a19..0f8c422 100644 (file)
@@ -367,6 +367,22 @@ void test_builtin_elementwise_floor(float f1, float f2, double d1, double d2,
   vf2 = __builtin_elementwise_floor(vf1);
 }
 
+void test_builtin_elementwise_log(float f1, float f2, double d1, double d2,
+                                  float4 vf1, float4 vf2) {
+  // CHECK-LABEL: define void @test_builtin_elementwise_log(
+  // CHECK:      [[F1:%.+]] = load float, ptr %f1.addr, align 4
+  // CHECK-NEXT:  call float @llvm.log.f32(float [[F1]])
+  f2 = __builtin_elementwise_log(f1);
+
+  // CHECK:      [[D1:%.+]] = load double, ptr %d1.addr, align 8
+  // CHECK-NEXT: call double @llvm.log.f64(double [[D1]])
+  d2 = __builtin_elementwise_log(d1);
+
+  // CHECK:      [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16
+  // CHECK-NEXT: call <4 x float> @llvm.log.v4f32(<4 x float> [[VF1]])
+  vf2 = __builtin_elementwise_log(vf1);
+}
+
 void test_builtin_elementwise_roundeven(float f1, float f2, double d1, double d2,
                                         float4 vf1, float4 vf2) {
   // CHECK-LABEL: define void @test_builtin_elementwise_roundeven(
diff --git a/clang/test/Sema/aarch64-sve-vector-log-ops.c b/clang/test/Sema/aarch64-sve-vector-log-ops.c
new file mode 100644 (file)
index 0000000..f38c185
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple aarch64 -target-feature +f -target-feature +d \\r
+// RUN:   -target-feature +v -target-feature +zfh  -target-feature +sve -target-feature +experimental-zvfh \\r
+// RUN:   -disable-O0-optnone -o - -fsyntax-only %s -verify \r
+// REQUIRES: aarch64-registered-target\r
+\r
+#include <arm_sve.h>\r
+\r
+svfloat32_t test_log_vv_i8mf8(svfloat32_t v) {\r
+\r
+  return __builtin_elementwise_log(v);\r
+  // expected-error@-1 {{1st argument must be a vector, integer or floating point type}}\r
+}\r
index 72cc1e2..cea5b19 100644 (file)
@@ -330,6 +330,27 @@ void test_builtin_elementwise_floor(int i, float f, double d, float4 v, int3 iv,
   // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}}
 }
 
+void test_builtin_elementwise_log(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
+
+  struct Foo s = __builtin_elementwise_log(f);
+  // expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}}
+
+  i = __builtin_elementwise_log();
+  // expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
+
+  i = __builtin_elementwise_log(i);
+  // expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
+
+  i = __builtin_elementwise_log(f, f);
+  // expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
+
+  u = __builtin_elementwise_log(u);
+  // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}}
+
+  uv = __builtin_elementwise_log(uv);
+  // expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}}
+}
+
 void test_builtin_elementwise_roundeven(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
 
   struct Foo s = __builtin_elementwise_roundeven(f);
diff --git a/clang/test/Sema/riscv-sve-vector-log-ops.c b/clang/test/Sema/riscv-sve-vector-log-ops.c
new file mode 100644 (file)
index 0000000..c245b24
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple riscv64 -target-feature +f -target-feature +d \\r
+// RUN:   -target-feature +v -target-feature +zfh -target-feature +experimental-zvfh \\r
+// RUN:   -disable-O0-optnone -o - -fsyntax-only %s -verify \r
+// REQUIRES: riscv-registered-target\r
+\r
+#include <riscv_vector.h>\r
+\r
+\r
+vfloat32mf2_t test_log_vv_i8mf8(vfloat32mf2_t v) {\r
+\r
+  return __builtin_elementwise_log(v);\r
+  // expected-error@-1 {{1st argument must be a vector, integer or floating point type}}\r
+}\r
index c79ff7c..30f6bf2 100644 (file)
@@ -73,3 +73,10 @@ void test_builtin_elementwise_sin() {
   static_assert(!is_const<decltype(__builtin_elementwise_sin(a))>::value);
   static_assert(!is_const<decltype(__builtin_elementwise_sin(b))>::value);
 }
+
+void test_builtin_elementwise_log() {
+  const float a = 42.0;
+  float b = 42.3;
+  static_assert(!is_const<decltype(__builtin_elementwise_log(a))>::value);
+  static_assert(!is_const<decltype(__builtin_elementwise_log(b))>::value);
+}