Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] [NATIVECPU] Implement missing math builtins for scalar data types #11321

Merged
merged 12 commits into from
Oct 24, 2023
1 change: 1 addition & 0 deletions libclc/x86_64-unknown-linux/libspirv/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ math/native_sqrt.cl
math/rint.cl
math/round.cl
math/trunc.cl
shared/helpers.ll
9 changes: 9 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/integer/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "func.h"

#define GEN_UNARY_BUILTIN_T(NAME, TYPE) \
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE); \
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE n) { return __##NAME##_helper(n); }

#define GEN_UNARY_BUILTIN(NAME) \
GEN_UNARY_BUILTIN_T(NAME, int) \
GEN_UNARY_BUILTIN_T(NAME, signed char)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/integer/popcount.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(popcount)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/ceil.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(ceil)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/fabs.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(fabs)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/floor.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(floor)
4 changes: 4 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/fma.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "helpers.h"

GEN_TERNARY_BUILTIN(fma);

19 changes: 19 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "func.h"

#define GEN_UNARY_BUILTIN_T(NAME, TYPE) \
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE); \
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE n) { return __##NAME##_helper(n); }

#define GEN_TERNARY_BUILTIN_T(NAME, TYPE) \
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE, TYPE, TYPE); \
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE a, TYPE b, TYPE c) { \
return __##NAME##_helper(a, b, c); \
}

#define GEN_UNARY_BUILTIN(NAME) \
GEN_UNARY_BUILTIN_T(NAME, float) \
GEN_UNARY_BUILTIN_T(NAME, double)

#define GEN_TERNARY_BUILTIN(NAME) \
GEN_TERNARY_BUILTIN_T(NAME, float) \
GEN_TERNARY_BUILTIN_T(NAME, double)\
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_cos.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_cos)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_exp.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_exp)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_exp2.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_exp2)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_log.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_log)
4 changes: 4 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_log10.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_log10)

4 changes: 4 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_log2.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_log2)

4 changes: 4 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_sin.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_sin)

3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/native_sqrt.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(native_sqrt)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/rint.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(rint)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/round.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(round)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/sqrt.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(sqrt)
3 changes: 3 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/math/trunc.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

GEN_UNARY_BUILTIN(trunc)
270 changes: 270 additions & 0 deletions libclc/x86_64-unknown-linux/libspirv/shared/helpers.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
declare float @llvm.sqrt.f32(float %n)
declare double @llvm.sqrt.f64(double %n)
declare float @llvm.fabs.f32(float %n)
declare double @llvm.fabs.f64(double %n)
declare float @llvm.trunc.f32(float %n)
declare double @llvm.trunc.f64(double %n)
declare float @llvm.ceil.f32(float %n)
declare double @llvm.ceil.f64(double %n)
declare float @llvm.floor.f32(float %n)
declare double @llvm.floor.f64(double %n)
declare float @llvm.round.f32(float %n)
declare double @llvm.round.f64(double %n)
declare float @llvm.rint.f32(float %n)
declare double @llvm.rint.f64(double %n)
declare float @llvm.cos.f32(float %n)
declare double @llvm.cos.f64(double %n)
declare float @llvm.sin.f32(float %n)
declare double @llvm.sin.f64(double %n)
declare float @llvm.exp2.f32(float %n)
declare double @llvm.exp2.f64(double %n)
declare float @llvm.exp.f32(float %n)
declare double @llvm.exp.f64(double %n)
declare float @llvm.log10.f32(float %n)
declare double @llvm.log10.f64(double %n)
declare float @llvm.log.f32(float %n)
declare double @llvm.log.f64(double %n)
declare float @llvm.log2.f32(float %n)
declare double @llvm.log2.f64(double %n)
declare float @llvm.fma.f32(float %n1, float %n2, float %n3)
declare double @llvm.fma.f64(double %n1, double %n2, double %n3)
declare i32 @llvm.ctpop.i32(i32 %n)
declare i8 @llvm.ctpop.i8(i8 %n)
npmiller marked this conversation as resolved.
Show resolved Hide resolved

define dso_local float @_Z13__sqrt_helperf(float %x) {
entry:
%call = call float @llvm.sqrt.f32(float %x)
ret float %call
}


define dso_local double @_Z13__sqrt_helperd(double %x) {
entry:
%call = call double @llvm.sqrt.f64(double %x)
ret double %call
}


define dso_local float @_Z13__fabs_helperf(float %x) {
entry:
%call = call float @llvm.fabs.f32(float %x)
ret float %call
}


define dso_local double @_Z13__fabs_helperd(double %x) {
entry:
%call = call double @llvm.fabs.f64(double %x)
ret double %call
}


define dso_local float @_Z14__trunc_helperf(float %x) {
entry:
%call = call float @llvm.trunc.f32(float %x)
ret float %call
}


define dso_local double @_Z14__trunc_helperd(double %x) {
entry:
%call = call double @llvm.trunc.f64(double %x)
ret double %call
}


define dso_local float @_Z13__ceil_helperf(float %x) {
entry:
%call = call float @llvm.ceil.f32(float %x)
ret float %call
}


define dso_local double @_Z13__ceil_helperd(double %x) {
entry:
%call = call double @llvm.ceil.f64(double %x)
ret double %call
}


define dso_local float @_Z14__floor_helperf(float %x) {
entry:
%call = call float @llvm.floor.f32(float %x)
ret float %call
}


define dso_local double @_Z14__floor_helperd(double %x) {
entry:
%call = call double @llvm.floor.f64(double %x)
ret double %call
}


define dso_local float @_Z14__round_helperf(float %x) {
entry:
%call = call float @llvm.round.f32(float %x)
ret float %call
}


define dso_local double @_Z14__round_helperd(double %x) {
entry:
%call = call double @llvm.round.f64(double %x)
ret double %call
}


define dso_local float @_Z13__rint_helperf(float %x) {
entry:
%call = call float @llvm.rint.f32(float %x)
ret float %call
}


define dso_local double @_Z13__rint_helperd(double %x) {
entry:
%call = call double @llvm.rint.f64(double %x)
ret double %call
}


define dso_local float @_Z20__native_sqrt_helperf(float %x) {
entry:
%call = call float @llvm.sqrt.f32(float %x)
ret float %call
}


define dso_local double @_Z20__native_sqrt_helperd(double %x) {
entry:
%call = call double @llvm.sqrt.f64(double %x)
ret double %call
}


define dso_local float @_Z19__native_cos_helperf(float %x) {
entry:
%call = call float @llvm.cos.f32(float %x)
ret float %call
}


define dso_local double @_Z19__native_cos_helperd(double %x) {
entry:
%call = call double @llvm.cos.f64(double %x)
ret double %call
}


define dso_local float @_Z19__native_sin_helperf(float %x) {
entry:
%call = call float @llvm.sin.f32(float %x)
ret float %call
}


define dso_local double @_Z19__native_sin_helperd(double %x) {
entry:
%call = call double @llvm.sin.f64(double %x)
ret double %call
}


define dso_local float @_Z20__native_exp2_helperf(float %x) {
entry:
%call = call float @llvm.exp2.f32(float %x)
ret float %call
}


define dso_local double @_Z20__native_exp2_helperd(double %x) {
entry:
%call = call double @llvm.exp2.f64(double %x)
ret double %call
}


define dso_local float @_Z19__native_exp_helperf(float %x) {
entry:
%call = call float @llvm.exp.f32(float %x)
ret float %call
}


define dso_local double @_Z19__native_exp_helperd(double %x) {
entry:
%call = call double @llvm.exp.f64(double %x)
ret double %call
}


define dso_local float @_Z21__native_log10_helperf(float %x) {
entry:
%call = call float @llvm.log10.f32(float %x)
ret float %call
}


define dso_local double @_Z21__native_log10_helperd(double %x) {
entry:
%call = call double @llvm.log10.f64(double %x)
ret double %call
}


define dso_local float @_Z19__native_log_helperf(float %x) {
entry:
%call = call float @llvm.log.f32(float %x)
ret float %call
}


define dso_local double @_Z19__native_log_helperd(double %x) {
entry:
%call = call double @llvm.log.f64(double %x)
ret double %call
}


define dso_local float @_Z20__native_log2_helperf(float %x) {
entry:
%call = call float @llvm.log2.f32(float %x)
ret float %call
}


define dso_local double @_Z20__native_log2_helperd(double %x) {
entry:
%call = call double @llvm.log2.f64(double %x)
ret double %call
}


define dso_local float @_Z12__fma_helperfff(float %a, float %b, float %c) {
entry:
%call = call float @llvm.fma.f32(float %a, float %b, float %c)
ret float %call
}


define dso_local double @_Z12__fma_helperddd(double %a, double %b, double %c) {
entry:
%call = call double @llvm.fma.f64(double %a, double %b, double %c)
ret double %call
}


define dso_local i32 @_Z17__popcount_helperi(i32 %x) {
entry:
%call = call i32 @llvm.ctpop.i32(i32 %x)
ret i32 %call
}


define dso_local i8 @_Z17__popcount_helpera(i8 %x) {
entry:
%call = call i8 @llvm.ctpop.i8(i8 %x)
ret i8 %call
}

2 changes: 0 additions & 2 deletions llvm/lib/SYCLLowerIR/PrepareSYCLNativeCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ Value *getStateArg(const Function *F) {
return F->getArg(FT->getNumParams() - 1);
}

static constexpr unsigned int NativeCPUGlobalAS = 1;

} // namespace

PreservedAnalyses PrepareSYCLNativeCPUPass::run(Module &M,
Expand Down
Loading