Skip to content

Commit 836249b

Browse files
committed
Add codegen for llvm log2/log10 elementwise builtins
Add codegen for llvm log2 / log10 elementwise builtin The log2/log10 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 builtins are restricted to floating point types only. Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D143207
1 parent 056af48 commit 836249b

File tree

10 files changed

+126
-0
lines changed

10 files changed

+126
-0
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±in
636636
T __builtin_elementwise_cos(T x) return the cosine of x interpreted as an angle in radians floating point types
637637
T __builtin_elementwise_floor(T x) return the largest integral value less than or equal to x floating point types
638638
T __builtin_elementwise_log(T x) return the natural logarithm of x floating point types
639+
T __builtin_elementwise_log2(T x) return the base 2 logarithm of x floating point types
640+
T __builtin_elementwise_log10(T x) return the base 10 logarithm of x floating point types
639641
T __builtin_elementwise_roundeven(T x) round x to the nearest integer value in floating point format, floating point types
640642
rounding halfway cases to even (that is, to the nearest value
641643
that is an even integer), regardless of the current rounding

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Arm and AArch64 Support in Clang
184184
Floating Point Support in Clang
185185
-------------------------------
186186
- Add ``__builtin_elementwise_log`` builtin for floating point types only.
187+
- Add ``__builtin_elementwise_log10`` builtin for floating point types only.
188+
- Add ``__builtin_elementwise_log2`` builtin for floating point types only.
187189

188190
Internal API Changes
189191
--------------------

clang/include/clang/Basic/Builtins.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ BUILTIN(__builtin_elementwise_ceil, "v.", "nct")
664664
BUILTIN(__builtin_elementwise_cos, "v.", "nct")
665665
BUILTIN(__builtin_elementwise_floor, "v.", "nct")
666666
BUILTIN(__builtin_elementwise_log, "v.", "nct")
667+
BUILTIN(__builtin_elementwise_log2, "v.", "nct")
668+
BUILTIN(__builtin_elementwise_log10, "v.", "nct")
667669
BUILTIN(__builtin_elementwise_roundeven, "v.", "nct")
668670
BUILTIN(__builtin_elementwise_sin, "v.", "nct")
669671
BUILTIN(__builtin_elementwise_trunc, "v.", "nct")

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
30913091
case Builtin::BI__builtin_elementwise_log:
30923092
return RValue::get(
30933093
emitUnaryBuiltin(*this, E, llvm::Intrinsic::log, "elt.log"));
3094+
case Builtin::BI__builtin_elementwise_log2:
3095+
return RValue::get(
3096+
emitUnaryBuiltin(*this, E, llvm::Intrinsic::log2, "elt.log2"));
3097+
case Builtin::BI__builtin_elementwise_log10:
3098+
return RValue::get(
3099+
emitUnaryBuiltin(*this, E, llvm::Intrinsic::log10, "elt.log10"));
30943100
case Builtin::BI__builtin_elementwise_cos:
30953101
return RValue::get(
30963102
emitUnaryBuiltin(*this, E, llvm::Intrinsic::cos, "elt.cos"));

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,8 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
26132613
case Builtin::BI__builtin_elementwise_cos:
26142614
case Builtin::BI__builtin_elementwise_floor:
26152615
case Builtin::BI__builtin_elementwise_log:
2616+
case Builtin::BI__builtin_elementwise_log2:
2617+
case Builtin::BI__builtin_elementwise_log10:
26162618
case Builtin::BI__builtin_elementwise_roundeven:
26172619
case Builtin::BI__builtin_elementwise_sin:
26182620
case Builtin::BI__builtin_elementwise_trunc:

clang/test/CodeGen/builtins-elementwise-math.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,38 @@ void test_builtin_elementwise_log(float f1, float f2, double d1, double d2,
383383
vf2 = __builtin_elementwise_log(vf1);
384384
}
385385

386+
void test_builtin_elementwise_log10(float f1, float f2, double d1, double d2,
387+
float4 vf1, float4 vf2) {
388+
// CHECK-LABEL: define void @test_builtin_elementwise_log10(
389+
// CHECK: [[F1:%.+]] = load float, ptr %f1.addr, align 4
390+
// CHECK-NEXT: call float @llvm.log10.f32(float [[F1]])
391+
f2 = __builtin_elementwise_log10(f1);
392+
393+
// CHECK: [[D1:%.+]] = load double, ptr %d1.addr, align 8
394+
// CHECK-NEXT: call double @llvm.log10.f64(double [[D1]])
395+
d2 = __builtin_elementwise_log10(d1);
396+
397+
// CHECK: [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16
398+
// CHECK-NEXT: call <4 x float> @llvm.log10.v4f32(<4 x float> [[VF1]])
399+
vf2 = __builtin_elementwise_log10(vf1);
400+
}
401+
402+
void test_builtin_elementwise_log2(float f1, float f2, double d1, double d2,
403+
float4 vf1, float4 vf2) {
404+
// CHECK-LABEL: define void @test_builtin_elementwise_log2(
405+
// CHECK: [[F1:%.+]] = load float, ptr %f1.addr, align 4
406+
// CHECK-NEXT: call float @llvm.log2.f32(float [[F1]])
407+
f2 = __builtin_elementwise_log2(f1);
408+
409+
// CHECK: [[D1:%.+]] = load double, ptr %d1.addr, align 8
410+
// CHECK-NEXT: call double @llvm.log2.f64(double [[D1]])
411+
d2 = __builtin_elementwise_log2(d1);
412+
413+
// CHECK: [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16
414+
// CHECK-NEXT: call <4 x float> @llvm.log2.v4f32(<4 x float> [[VF1]])
415+
vf2 = __builtin_elementwise_log2(vf1);
416+
}
417+
386418
void test_builtin_elementwise_roundeven(float f1, float f2, double d1, double d2,
387419
float4 vf1, float4 vf2) {
388420
// CHECK-LABEL: define void @test_builtin_elementwise_roundeven(

clang/test/Sema/aarch64-sve-vector-log-ops.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ svfloat32_t test_log_vv_i8mf8(svfloat32_t v) {
1010
return __builtin_elementwise_log(v);
1111
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
1212
}
13+
14+
svfloat32_t test_log10_vv_i8mf8(svfloat32_t v) {
15+
16+
return __builtin_elementwise_log10(v);
17+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
18+
}
19+
20+
svfloat32_t test_log2_vv_i8mf8(svfloat32_t v) {
21+
22+
return __builtin_elementwise_log2(v);
23+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
24+
}

clang/test/Sema/builtins-elementwise-math.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,48 @@ void test_builtin_elementwise_log(int i, float f, double d, float4 v, int3 iv, u
351351
// expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}}
352352
}
353353

354+
void test_builtin_elementwise_log10(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
355+
356+
struct Foo s = __builtin_elementwise_log10(f);
357+
// expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}}
358+
359+
i = __builtin_elementwise_log10();
360+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
361+
362+
i = __builtin_elementwise_log10(i);
363+
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
364+
365+
i = __builtin_elementwise_log10(f, f);
366+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
367+
368+
u = __builtin_elementwise_log10(u);
369+
// expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}}
370+
371+
uv = __builtin_elementwise_log10(uv);
372+
// expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}}
373+
}
374+
375+
void test_builtin_elementwise_log2(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
376+
377+
struct Foo s = __builtin_elementwise_log2(f);
378+
// expected-error@-1 {{initializing 'struct Foo' with an expression of incompatible type 'float'}}
379+
380+
i = __builtin_elementwise_log2();
381+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
382+
383+
i = __builtin_elementwise_log2(i);
384+
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
385+
386+
i = __builtin_elementwise_log2(f, f);
387+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
388+
389+
u = __builtin_elementwise_log2(u);
390+
// expected-error@-1 {{1st argument must be a floating point type (was 'unsigned int')}}
391+
392+
uv = __builtin_elementwise_log2(uv);
393+
// expected-error@-1 {{1st argument must be a floating point type (was 'unsigned4' (vector of 4 'unsigned int' values))}}
394+
}
395+
354396
void test_builtin_elementwise_roundeven(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
355397

356398
struct Foo s = __builtin_elementwise_roundeven(f);

clang/test/Sema/riscv-sve-vector-log-ops.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ vfloat32mf2_t test_log_vv_i8mf8(vfloat32mf2_t v) {
1111
return __builtin_elementwise_log(v);
1212
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
1313
}
14+
15+
vfloat32mf2_t test_log10_vv_i8mf8(vfloat32mf2_t v) {
16+
17+
return __builtin_elementwise_log10(v);
18+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
19+
}
20+
21+
vfloat32mf2_t test_log2_vv_i8mf8(vfloat32mf2_t v) {
22+
23+
return __builtin_elementwise_log2(v);
24+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type}}
25+
}

clang/test/SemaCXX/builtins-elementwise-math.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,17 @@ void test_builtin_elementwise_log() {
8080
static_assert(!is_const<decltype(__builtin_elementwise_log(a))>::value);
8181
static_assert(!is_const<decltype(__builtin_elementwise_log(b))>::value);
8282
}
83+
84+
void test_builtin_elementwise_log10() {
85+
const float a = 42.0;
86+
float b = 42.3;
87+
static_assert(!is_const<decltype(__builtin_elementwise_log10(a))>::value);
88+
static_assert(!is_const<decltype(__builtin_elementwise_log10(b))>::value);
89+
}
90+
91+
void test_builtin_elementwise_log2() {
92+
const float a = 42.0;
93+
float b = 42.3;
94+
static_assert(!is_const<decltype(__builtin_elementwise_log2(a))>::value);
95+
static_assert(!is_const<decltype(__builtin_elementwise_log2(b))>::value);
96+
}

0 commit comments

Comments
 (0)