diff --git a/sycl/include/sycl/builtins_scalar.hpp b/sycl/include/sycl/builtins_scalar.hpp new file mode 100644 index 0000000000000..760e63689a48b --- /dev/null +++ b/sycl/include/sycl/builtins_scalar.hpp @@ -0,0 +1,31 @@ +//==------- builtins_scalar.hpp - SYCL scalar built-in functions -----------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Lightweight alternative to that provides only the +// scalar (float / double / half / integer fundamental-type) overloads of all +// SYCL built-in functions, without pulling in sycl::vec<> or sycl::marray<> +// template machinery. +// +// Trade-offs vs : +// - Significantly less front-end parse work (no vector.hpp, multi_ptr.hpp, +// marray.hpp, vector_convert.hpp) +// - Covers: sycl::sin(float), sycl::native::exp(float), +// sycl::fmax(float,float), sycl::abs(int), +// sycl::isequal(float,float), etc. +// - Does NOT cover: sycl::sin(sycl::float4), +// sycl::native::exp(sycl::marray), etc. +// Use for those. +// +// Including both this header and in the same translation +// unit is safe: the scalar inline functions are identical and ODR-compliant. +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include diff --git a/sycl/include/sycl/detail/builtins/builtins.hpp b/sycl/include/sycl/detail/builtins/builtins.hpp index d69c0b93ce7c1..0eed93ef6fb6f 100644 --- a/sycl/include/sycl/detail/builtins/builtins.hpp +++ b/sycl/include/sycl/detail/builtins/builtins.hpp @@ -63,103 +63,15 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include + +#include namespace sycl { inline namespace _V1 { namespace detail { -#ifdef __FAST_MATH__ -template -struct use_fast_math - : std::is_same>, float> {}; -#else -template struct use_fast_math : std::false_type {}; -#endif -template constexpr bool use_fast_math_v = use_fast_math::value; - -// Utility trait for getting the decoration of a multi_ptr. -template struct get_multi_ptr_decoration; -template -struct get_multi_ptr_decoration< - multi_ptr> { - static constexpr access::decorated value = DecorateAddress; -}; - -template -constexpr access::decorated get_multi_ptr_decoration_v = - get_multi_ptr_decoration::value; - -// Utility trait for checking if a multi_ptr has a "writable" address space, -// i.e. global, local, private or generic. -template struct has_writeable_addr_space : std::false_type {}; -template -struct has_writeable_addr_space> - : std::bool_constant {}; - -template -constexpr bool has_writeable_addr_space_v = has_writeable_addr_space::value; - -// Utility trait for changing the element type of a type T. If T is a scalar, -// the new type replaces T completely. -template -struct change_elements { - using type = NewElemT; -}; -template -struct change_elements>> { - using type = - marray::type, - T::size()>; -}; -template -struct change_elements>> { - using type = - vec::type, - T::size()>; -}; - -template -using change_elements_t = typename change_elements::type; - -template -inline constexpr bool builtin_same_shape_v = - ((... && is_scalar_arithmetic_v) || (... && is_marray_v) || - (... && is_vec_or_swizzle_v)) && - (... && (num_elements::value == - num_elements::type>::value)); - -template -inline constexpr bool builtin_same_or_swizzle_v = - // Use builtin_same_shape_v to filter out types unrelated to builtins. - builtin_same_shape_v && all_same_v...>; - -// Utility functions for converting to/from vec/marray. -template vec to_vec2(marray X, size_t Start) { - return {X[Start], X[Start + 1]}; -} -template vec to_vec(marray X) { - vec Vec; - for (size_t I = 0; I < N; I++) - Vec[I] = X[I]; - return Vec; -} -template marray to_marray(vec X) { - marray Marray; - for (size_t I = 0; I < N; I++) - Marray[I] = X[I]; - return Marray; -} namespace builtins { #ifdef __SYCL_DEVICE_ONLY__ @@ -187,34 +99,6 @@ template auto convert_arg(T &&x) { #endif } // namespace builtins -template -auto builtin_marray_impl(FuncTy F, const Ts &...x) { - using ret_elem_type = decltype(F(x[0]...)); - using T = typename first_type::type; - marray Res; - constexpr auto N = T::size(); - for (size_t I = 0; I < N / 2; ++I) { - auto PartialRes = [&]() { - using elem_ty = get_elem_type_t; - if constexpr (std::is_integral_v) - return F( - to_vec2(x, I * 2) - .template as, - fixed_width_signed, - fixed_width_unsigned>, - 2>>()...); - else - return F(to_vec2(x, I * 2)...); - }(); - sycl::detail::memcpy_no_adl(&Res[I * 2], &PartialRes, - sizeof(decltype(PartialRes))); - } - if (N % 2) - Res[N - 1] = F(x[N - 1]...); - return Res; -} - template auto builtin_default_host_impl(FuncTy F, const Ts &...x) { // We implement support for marray/swizzle in the headers and export symbols @@ -229,80 +113,6 @@ auto builtin_default_host_impl(FuncTy F, const Ts &...x) { return F(simplify_if_swizzle_t{x}...); } } - -template -auto builtin_delegate_to_scalar(FuncTy F, const Ts &...x) { - using T = typename first_type::type; - static_assert(is_vec_or_swizzle_v || is_marray_v); - - constexpr auto Size = T::size(); - using ret_elem_type = decltype(F(x[0]...)); - std::conditional_t, marray, - vec> - r{}; - - if constexpr (is_marray_v) { - for (size_t i = 0; i < Size; ++i) - r[i] = F(x[i]...); - } else { - loop([&](auto idx) { r[idx] = F(x[idx]...); }); - } - - return r; -} - -template -struct fp_elem_type - : std::bool_constant< - check_type_in_v, float, double, half>> {}; -template -struct float_elem_type - : std::bool_constant, float>> {}; - -template -struct same_basic_shape : std::bool_constant> {}; - -template -struct same_elem_type : std::bool_constant::value && - all_same_v...>> { -}; - -template struct any_shape : std::true_type {}; - -template -struct scalar_only : std::bool_constant> {}; - -template -struct non_scalar_only : std::bool_constant> {}; - -template struct default_ret_type { - using type = T; -}; - -template struct scalar_ret_type { - using type = get_elem_type_t; -}; - -template