This repository was archived by the owner on Mar 28, 2023. It is now read-only.
forked from llvm/llvm-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 130
[SYCL] Add tests for some half builtins #880
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7aa4c9
[SYCL] added tests for some half builtins
t4c1 2e797c1
disabled for OpenCL backend on CPU
t4c1 02f653c
format
t4c1 ac29a61
actually check relative error
t4c1 2f7fc03
format
t4c1 20dd111
addressed review comments and refactored into a single kernel
t4c1 f0dbb4a
Update SYCL/Basic/half_builtins.cpp
t4c1 bf1912d
format
t4c1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out | ||
// RUN: %HOST_RUN_PLACEHOLDER %t.out | ||
// RUN: %CPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %GPU_RUN_PLACEHOLDER %t.out | ||
// RUN: %ACC_RUN_PLACEHOLDER %t.out | ||
|
||
// OpenCL CPU driver does not support cl_khr_fp16 extension | ||
// UNSUPPORTED: cpu && opencl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: would it be better to test for the extension support in the source code itself? |
||
|
||
#include <sycl/sycl.hpp> | ||
|
||
#include <cmath> | ||
#include <limits> | ||
|
||
using namespace sycl; | ||
|
||
constexpr int SZ_max = 16; | ||
|
||
bool check(float a, float b) { | ||
return fabs(2 * (a - b) / (a + b)) < std::numeric_limits<half>::epsilon() || | ||
a < std::numeric_limits<half>::min(); | ||
} | ||
|
||
template <int N> bool check(vec<float, N> a, vec<float, N> b) { | ||
for (int i = 0; i < N; i++) { | ||
if (!check(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
#define TEST_BUILTIN_1_VEC_IMPL(NAME, SZ) \ | ||
{ \ | ||
float##SZ *a = (float##SZ *)&A[0]; \ | ||
float##SZ *b = (float##SZ *)&B[0]; \ | ||
if (i < SZ_max / SZ) { \ | ||
if (!check(NAME(a[i]), NAME(a[i].convert<half>()).convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
// vectors of size 3 need separate test, as they actually have the size of 4 | ||
// elements | ||
#define TEST_BUILTIN_1_VEC3_IMPL(NAME) \ | ||
{ \ | ||
float3 *a = (float3 *)&A[0]; \ | ||
float3 *b = (float3 *)&B[0]; \ | ||
if (i < SZ_max / 4) { \ | ||
if (!check(NAME(a[i]), NAME(a[i].convert<half>()).convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_1_SCAL_IMPL(NAME) \ | ||
{ \ | ||
float *a = (float *)&A[0]; \ | ||
float *b = (float *)&B[0]; \ | ||
if (!check(NAME(a[i]), (float)NAME((half)a[i]))) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_1(NAME) \ | ||
TEST_BUILTIN_1_SCAL_IMPL(NAME) \ | ||
TEST_BUILTIN_1_VEC_IMPL(NAME, 2) \ | ||
TEST_BUILTIN_1_VEC3_IMPL(NAME) \ | ||
TEST_BUILTIN_1_VEC_IMPL(NAME, 4) \ | ||
TEST_BUILTIN_1_VEC_IMPL(NAME, 8) \ | ||
TEST_BUILTIN_1_VEC_IMPL(NAME, 16) | ||
|
||
#define TEST_BUILTIN_2_VEC_IMPL(NAME, SZ) \ | ||
{ \ | ||
float##SZ *a = (float##SZ *)&A[0]; \ | ||
float##SZ *b = (float##SZ *)&B[0]; \ | ||
if (i < SZ_max / SZ) { \ | ||
if (!check(NAME(a[i], b[i]), \ | ||
NAME(a[i].convert<half>(), b[i].convert<half>()) \ | ||
.convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_2_VEC3_IMPL(NAME) \ | ||
{ \ | ||
float3 *a = (float3 *)&A[0]; \ | ||
float3 *b = (float3 *)&B[0]; \ | ||
if (i < SZ_max / 4) { \ | ||
if (!check(NAME(a[i], b[i]), \ | ||
NAME(a[i].convert<half>(), b[i].convert<half>()) \ | ||
.convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_2_SCAL_IMPL(NAME) \ | ||
{ \ | ||
float *a = (float *)&A[0]; \ | ||
float *b = (float *)&B[0]; \ | ||
if (!check(NAME(a[i], b[i]), (float)NAME((half)a[i], (half)b[i]))) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_2(NAME) \ | ||
TEST_BUILTIN_2_SCAL_IMPL(NAME) \ | ||
TEST_BUILTIN_2_VEC_IMPL(NAME, 2) \ | ||
TEST_BUILTIN_2_VEC3_IMPL(NAME) \ | ||
TEST_BUILTIN_2_VEC_IMPL(NAME, 4) \ | ||
TEST_BUILTIN_2_VEC_IMPL(NAME, 8) \ | ||
TEST_BUILTIN_2_VEC_IMPL(NAME, 16) | ||
|
||
#define TEST_BUILTIN_3_VEC_IMPL(NAME, SZ) \ | ||
{ \ | ||
float##SZ *a = (float##SZ *)&A[0]; \ | ||
float##SZ *b = (float##SZ *)&B[0]; \ | ||
float##SZ *c = (float##SZ *)&C[0]; \ | ||
if (i < SZ_max / SZ) { \ | ||
if (!check(NAME(a[i], b[i], c[i]), \ | ||
NAME(a[i].convert<half>(), b[i].convert<half>(), \ | ||
c[i].convert<half>()) \ | ||
.convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_3_VEC3_IMPL(NAME) \ | ||
{ \ | ||
float3 *a = (float3 *)&A[0]; \ | ||
float3 *b = (float3 *)&B[0]; \ | ||
float3 *c = (float3 *)&C[0]; \ | ||
if (i < SZ_max / 4) { \ | ||
if (!check(NAME(a[i], b[i], c[i]), \ | ||
NAME(a[i].convert<half>(), b[i].convert<half>(), \ | ||
c[i].convert<half>()) \ | ||
.convert<float>())) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_3_SCAL_IMPL(NAME) \ | ||
{ \ | ||
float *a = (float *)&A[0]; \ | ||
float *b = (float *)&B[0]; \ | ||
float *c = (float *)&C[0]; \ | ||
if (!check(NAME(a[i], b[i], c[i]), \ | ||
(float)NAME((half)a[i], (half)b[i], (half)c[i]))) { \ | ||
err[0] = 1; \ | ||
} \ | ||
} | ||
|
||
#define TEST_BUILTIN_3(NAME) \ | ||
TEST_BUILTIN_3_SCAL_IMPL(NAME) \ | ||
TEST_BUILTIN_3_VEC_IMPL(NAME, 2) \ | ||
TEST_BUILTIN_3_VEC3_IMPL(NAME) \ | ||
TEST_BUILTIN_3_VEC_IMPL(NAME, 4) \ | ||
TEST_BUILTIN_3_VEC_IMPL(NAME, 8) \ | ||
TEST_BUILTIN_3_VEC_IMPL(NAME, 16) | ||
|
||
int main() { | ||
queue q; | ||
float16 a, b, c, d; | ||
for (int i = 0; i < SZ_max; i++) { | ||
a[i] = i / (float)SZ_max; | ||
b[i] = (SZ_max - i) / (float)SZ_max; | ||
c[i] = (float)(3 * i); | ||
} | ||
int err = 0; | ||
{ | ||
buffer<float16> a_buf(&a, 1); | ||
buffer<float16> b_buf(&b, 1); | ||
buffer<float16> c_buf(&c, 1); | ||
buffer<int> err_buf(&err, 1); | ||
q.submit([&](handler &cgh) { | ||
auto A = a_buf.get_access<access::mode::read>(cgh); | ||
auto B = b_buf.get_access<access::mode::read>(cgh); | ||
auto C = c_buf.get_access<access::mode::read>(cgh); | ||
auto err = err_buf.get_access<access::mode::write>(cgh); | ||
cgh.parallel_for(SZ_max, [=](item<1> index) { | ||
size_t i = index.get_id(0); | ||
TEST_BUILTIN_1(fabs); | ||
TEST_BUILTIN_2(fmin); | ||
TEST_BUILTIN_2(fmax); | ||
TEST_BUILTIN_3(fma); | ||
}); | ||
}); | ||
} | ||
assert(err == 0); | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.