Skip to content

Commit

Permalink
[SYCL][ESIMD] Replace use of intrinsics with spirv functions for addc…
Browse files Browse the repository at this point in the history
…/subb (#13093)
  • Loading branch information
fineg74 authored Jan 7, 2025
1 parent 68c0fcc commit 1603bbe
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 275 deletions.
8 changes: 8 additions & 0 deletions sycl/include/sycl/__spirv/spirv_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stddef.h> // for size_t
#include <stdint.h> // for uint32_t
#include <type_traits>
#include <utility> // for pair

// Convergent attribute
#ifdef __SYCL_DEVICE_ONLY__
Expand Down Expand Up @@ -1132,6 +1133,13 @@ extern __DPCPP_SYCL_EXTERNAL
std::enable_if_t<std::is_integral_v<to> && std::is_unsigned_v<to>, to>
__spirv_ConvertPtrToU(from val) noexcept;

template <typename T, int N>
extern __DPCPP_SYCL_EXTERNAL std::pair<__ocl_vec_t<T, N>, __ocl_vec_t<T, N>>
__spirv_IAddCarry(__ocl_vec_t<T, N> src0, __ocl_vec_t<T, N> src1);

template <typename T, int N>
extern __DPCPP_SYCL_EXTERNAL std::pair<__ocl_vec_t<T, N>, __ocl_vec_t<T, N>>
__spirv_ISubBorrow(__ocl_vec_t<T, N> src0, __ocl_vec_t<T, N> src1);
template <typename RetT, typename... ArgsT>
extern __DPCPP_SYCL_EXTERNAL __spv::__spirv_TaskSequenceINTEL *
__spirv_TaskSequenceCreateINTEL(RetT (*f)(ArgsT...), int Pipelined = -1,
Expand Down
214 changes: 89 additions & 125 deletions sycl/include/sycl/ext/intel/esimd/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1720,131 +1720,95 @@ bfn(T src0, T src1, T src2) {

/// @} sycl_esimd_logical

/// Performs add with carry of 2 unsigned 32-bit vectors.
/// @tparam N size of the vectors
/// @param carry vector that is going to hold resulting carry flag
/// @param src0 first term
/// @param src1 second term
/// @return sum of 2 terms, carry flag is returned through \c carry parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
addc(__ESIMD_NS::simd<uint32_t, N> &carry, __ESIMD_NS::simd<uint32_t, N> src0,
__ESIMD_NS::simd<uint32_t, N> src1) {
std::pair<__ESIMD_DNS::vector_type_t<uint32_t, N>,
__ESIMD_DNS::vector_type_t<uint32_t, N>>
Result = __esimd_addc<uint32_t, N>(src0.data(), src1.data());

carry = Result.first;
return Result.second;
}

/// Performs add with carry of a unsigned 32-bit vector and scalar.
/// @tparam N size of the vectors
/// @param carry vector that is going to hold resulting carry flag
/// @param src0 first term
/// @param src1 second term
/// @return sum of 2 terms, carry flag is returned through \c carry parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
addc(__ESIMD_NS::simd<uint32_t, N> &carry, __ESIMD_NS::simd<uint32_t, N> src0,
uint32_t src1) {
__ESIMD_NS::simd<uint32_t, N> Src1V = src1;
return addc(carry, src0, Src1V);
}

/// Performs add with carry of a unsigned 32-bit scalar and vector.
/// @tparam N size of the vectors
/// @param carry vector that is going to hold resulting carry flag
/// @param src0 first term
/// @param src1 second term
/// @return sum of 2 terms, carry flag is returned through \c carry parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
addc(__ESIMD_NS::simd<uint32_t, N> &carry, uint32_t src0,
__ESIMD_NS::simd<uint32_t, N> src1) {
__ESIMD_NS::simd<uint32_t, N> Src0V = src0;
return addc(carry, Src0V, src1);
}

/// Performs add with carry of a unsigned 32-bit scalars.
/// @tparam N size of the vectors
/// @param carry scalar that is going to hold resulting carry flag
/// @param src0 first term
/// @param src1 second term
/// @return sum of 2 terms, carry flag is returned through \c carry parameter
__ESIMD_API uint32_t addc(uint32_t &carry, uint32_t src0, uint32_t src1) {
__ESIMD_NS::simd<uint32_t, 1> CarryV = carry;
__ESIMD_NS::simd<uint32_t, 1> Src0V = src0;
__ESIMD_NS::simd<uint32_t, 1> Src1V = src1;
__ESIMD_NS::simd<uint32_t, 1> Res = addc(CarryV, Src0V, Src1V);
carry = CarryV[0];
return Res[0];
}

/// Performs substraction with borrow of 2 unsigned 32-bit vectors.
/// @tparam N size of the vectors
/// @param borrow vector that is going to hold resulting borrow flag
/// @param src0 first term
/// @param src1 second term
/// @return difference of 2 terms, borrow flag is returned through \c borrow
/// parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
subb(__ESIMD_NS::simd<uint32_t, N> &borrow, __ESIMD_NS::simd<uint32_t, N> src0,
__ESIMD_NS::simd<uint32_t, N> src1) {
std::pair<__ESIMD_DNS::vector_type_t<uint32_t, N>,
__ESIMD_DNS::vector_type_t<uint32_t, N>>
Result = __esimd_subb<uint32_t, N>(src0.data(), src1.data());

borrow = Result.first;
return Result.second;
}

/// Performs substraction with borrow of unsigned 32-bit vector and scalar.
/// @tparam N size of the vectors
/// @param borrow vector that is going to hold resulting borrow flag
/// @param src0 first term
/// @param src1 second term
/// @return difference of 2 terms, borrow flag is returned through \c borrow
/// parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
subb(__ESIMD_NS::simd<uint32_t, N> &borrow, __ESIMD_NS::simd<uint32_t, N> src0,
uint32_t src1) {
__ESIMD_NS::simd<uint32_t, N> Src1V = src1;
return subb(borrow, src0, Src1V);
}

/// Performs substraction with borrow of unsigned 32-bit scalar and vector.
/// @tparam N size of the vectors
/// @param borrow vector that is going to hold resulting borrow flag
/// @param src0 first term
/// @param src1 second term
/// @return difference of 2 terms, borrow flag is returned through \c borrow
/// parameter
template <int N>
__ESIMD_API __ESIMD_NS::simd<uint32_t, N>
subb(__ESIMD_NS::simd<uint32_t, N> &borrow, uint32_t src0,
__ESIMD_NS::simd<uint32_t, N> src1) {
__ESIMD_NS::simd<uint32_t, N> Src0V = src0;
return subb(borrow, Src0V, src1);
}

/// Performs substraction with borrow of 2 unsigned 32-bit scalars.
/// @tparam N size of the vectors
/// @param borrow scalar that is going to hold resulting borrow flag
/// @param src0 first term
/// @param src1 second term
/// @return difference of 2 terms, borrow flag is returned through \c borrow
/// parameter
__ESIMD_API uint32_t subb(uint32_t &borrow, uint32_t src0, uint32_t src1) {
__ESIMD_NS::simd<uint32_t, 1> BorrowV = borrow;
__ESIMD_NS::simd<uint32_t, 1> Src0V = src0;
__ESIMD_NS::simd<uint32_t, 1> Src1V = src1;
__ESIMD_NS::simd<uint32_t, 1> Res = subb(BorrowV, Src0V, Src1V);
borrow = BorrowV[0];
return Res[0];
}
#if defined(__SYCL_DEVICE_ONLY__)
#define __ESIMD_ADDC_IMPL(T) \
std::pair<__ESIMD_DNS::vector_type_t<T, N>, \
__ESIMD_DNS::vector_type_t<T, N>> \
Result = __spirv_IAddCarry<T, N>(src0.data(), src1.data()); \
carry = Result.second; \
return Result.first;
#else
#define __ESIMD_ADDC_IMPL(T) return 0;
#endif // __SYCL_DEVICE_ONLY__

#define __ESIMD_ADDC(T) \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> addc(__ESIMD_NS::simd<T, N> &carry, \
__ESIMD_NS::simd<T, N> src0, \
__ESIMD_NS::simd<T, N> src1) { \
__ESIMD_ADDC_IMPL(T) \
} \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> addc( \
__ESIMD_NS::simd<T, N> &carry, __ESIMD_NS::simd<T, N> src0, T src1) { \
__ESIMD_NS::simd<T, N> Src1V = src1; \
return addc(carry, src0, Src1V); \
} \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> addc( \
__ESIMD_NS::simd<T, N> &carry, T src0, __ESIMD_NS::simd<T, N> src1) { \
__ESIMD_NS::simd<T, N> Src0V = src0; \
return addc(carry, Src0V, src1); \
} \
__ESIMD_API T addc(T &carry, T src0, T src1) { \
__ESIMD_NS::simd<T, 1> CarryV = carry; \
__ESIMD_NS::simd<T, 1> Src0V = src0; \
__ESIMD_NS::simd<T, 1> Src1V = src1; \
__ESIMD_NS::simd<T, 1> Res = addc(CarryV, Src0V, Src1V); \
carry = CarryV[0]; \
return Res[0]; \
}

__ESIMD_ADDC(uint32_t)
__ESIMD_ADDC(uint64_t)

#undef __ESIMD_ADDC
#undef __ESIMD_ADDC_IMPL

#if defined(__SYCL_DEVICE_ONLY__)
#define __ESIMD_SUBB_IMPL(T) \
std::pair<__ESIMD_DNS::vector_type_t<T, N>, \
__ESIMD_DNS::vector_type_t<T, N>> \
Result = __spirv_ISubBorrow<T, N>(src0.data(), src1.data()); \
borrow = Result.second; \
return Result.first;
#else
#define __ESIMD_SUBB_IMPL(T) return 0;
#endif // __SYCL_DEVICE_ONLY__

#define __ESIMD_SUBB(T) \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> subb(__ESIMD_NS::simd<T, N> &borrow, \
__ESIMD_NS::simd<T, N> src0, \
__ESIMD_NS::simd<T, N> src1) { \
__ESIMD_SUBB_IMPL(T) \
} \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> subb( \
__ESIMD_NS::simd<T, N> &borrow, __ESIMD_NS::simd<T, N> src0, T src1) { \
__ESIMD_NS::simd<T, N> Src1V = src1; \
return subb(borrow, src0, Src1V); \
} \
template <int N> \
__ESIMD_API __ESIMD_NS::simd<T, N> subb( \
__ESIMD_NS::simd<T, N> &borrow, T src0, __ESIMD_NS::simd<T, N> src1) { \
__ESIMD_NS::simd<T, N> Src0V = src0; \
return subb(borrow, Src0V, src1); \
} \
__ESIMD_API T subb(T &borrow, T src0, T src1) { \
__ESIMD_NS::simd<T, 1> BorrowV = borrow; \
__ESIMD_NS::simd<T, 1> Src0V = src0; \
__ESIMD_NS::simd<T, 1> Src1V = src1; \
__ESIMD_NS::simd<T, 1> Res = subb(BorrowV, Src0V, Src1V); \
borrow = BorrowV[0]; \
return Res[0]; \
} // namespace ext::intel::esimd

__ESIMD_SUBB(uint32_t)
__ESIMD_SUBB(uint64_t)

#undef __ESIMD_SUBB
#undef __ESIMD_SUBB_IMPL

/// rdtsc - get the value of timestamp counter.
/// @return the current value of timestamp counter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ __ESIMD_INTRIN __ESIMD_DNS::vector_type_t<T, N> __esimd_dpasw_nosrc0(
__ESIMD_DNS::vector_type_t<T1, N1> src1,
__ESIMD_DNS::vector_type_t<T2, N2> src2) __ESIMD_INTRIN_END;

template <typename T, int N>
__ESIMD_INTRIN std::pair<__ESIMD_DNS::vector_type_t<T, N>,
__ESIMD_DNS::vector_type_t<T, N>>
__esimd_addc(__ESIMD_DNS::vector_type_t<T, N> src0,
__ESIMD_DNS::vector_type_t<T, N> src1) __ESIMD_INTRIN_END;

template <typename T, int N>
__ESIMD_INTRIN std::pair<__ESIMD_DNS::vector_type_t<T, N>,
__ESIMD_DNS::vector_type_t<T, N>>
__esimd_subb(__ESIMD_DNS::vector_type_t<T, N> src0,
__ESIMD_DNS::vector_type_t<T, N> src1) __ESIMD_INTRIN_END;

template <uint8_t FuncControl, typename T, int N>
__ESIMD_INTRIN __ESIMD_raw_vec_t(T, N)
__esimd_bfn(__ESIMD_raw_vec_t(T, N) src0, __ESIMD_raw_vec_t(T, N) src1,
Expand Down
Loading

0 comments on commit 1603bbe

Please sign in to comment.