Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
887 changes: 6 additions & 881 deletions sycl/include/sycl/__spirv/spirv_ops.hpp

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions sycl/include/sycl/__spirv/spirv_ops_atomic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//==------- spirv_ops_atomic.hpp --- SPIRV atomic operations --------------==//
//
// 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
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/__spirv/spirv_ops_builtin_decls.hpp>

#include <type_traits>

#ifdef __SYCL_DEVICE_ONLY__

// Atomic SPIR-V builtins
// TODO: drop these forward-declarations.
// As of now, compiler does not forward-declare long long overloads for
// these and as such we can't drop anything from here. But ideally, we should
// rely on the compiler to generate those - that would allow to drop
// spirv_ops.hpp include from more files.
Comment on lines +18 to +22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Implementing this TODO comment will reduce compile time much better than the header split.
  2. Why this header declares built-ins for the types other than long long? I expect https://github.com/intel/llvm/blob/sycl/clang/lib/Sema/SPIRVBuiltins.td#L1031-L1093 to automatically declare necessary functions for all the types except long long.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment and suggestion, that makes sense.

Just to clarify the scope of this PR: this change is intended as a split of the
existing declarations in spirv_ops.hpp into category-specific headers, not as
a functional change to the atomic declaration set itself. The primary benefit from this
PR is the avoidance of the inclusion of utility (std::pair) and some of the parsing/expansion cost.

The declarations currently in spirv_ops_atomic.hpp were moved over from the
existing umbrella header without changing their contents. For reference, they
come from the original spirv_ops.hpp

So, I agree, your point is complementary to this refactor relying on the
compiler-provided SPIR-V declarations for the non-long long cases and keeping
only the missing declarations would likely be a better compile-time improvement
for this area.

That looks like a worthwhile follow-up cleanup on top of this
header split.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Instead of splitting spirv_ops.hpp, you can just remove all declarations which clang already provides. It's much better change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I want to be explicit about why I am still confused about the direction here.

The old umbrella spirv_ops.hpp costs about 45.8 ms to parse in the measurement
I took, and some of the heaviest transitive contributors on that path are exactly
the standard-library utility/type-traits machinery (30ms) that the split helps avoid
on narrower include paths.

So I agree your suggestion is a stronger optimization for the atomic slice
itself, but I do not understand it as making this split directionally wrong.
From my perspective, this PR still reduces unnecessary parsing for non-atomic
consumers, while your proposal reduces cost inside the atomic declarations.

Do you view this PR as moving in the wrong direction overall?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. From my perspective we should remove declarations for ALL consumer, not just "non-atomic".
This will also remove the "the standard-library utility/type-traits machinery (30ms) that the split helps avoid
on narrower include paths".

Are you up to exploring this option?
If not, I'm not against making the split, I just saying that we can do much better that limiting the scope for __spirv_* functions declarations. I'll let @againull to decide.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that removing the redundant declarations Clang already provides is a worthwhile direction, and it may well deliver a larger win for this area than the split alone.

My hesitation is only that I do not think it fully addresses all of the current utility / type_traits cost in spirv_ops.hpp.

For example, __spirv_IAddCarry and __spirv_ISubBorrow still use std::pair, and there are also type_traits-based helpers such as __spirv_ConvertPtrToU and the atomic min/max helper dispatch. So even after pruning the redundant atomic declarations, some broader refactoring would still be needed to remove those dependencies from all consumers.

I’m happy to explore that as follow-up work (later), but for this PR I’d prefer to keep the scope to the header split, since it is part of a larger refactoring series already in progress.

@againull let me know.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like removing all header declarations might require more effort/time, so I am not against the split.

#define __SPIRV_ATOMIC_LOAD(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicLoad(AS Type *P, int S, \
int O) noexcept;
#define __SPIRV_ATOMIC_STORE(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL void __spirv_AtomicStore( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_EXCHANGE(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicExchange( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_CMP_EXCHANGE(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicCompareExchange( \
AS Type *P, int S, int E, int U, Type V, Type C) noexcept;
#define __SPIRV_ATOMIC_IADD(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicIAdd( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_ISUB(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicISub( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_FADD(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicFAddEXT( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_SMIN(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicSMin( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_UMIN(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicUMin( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_FMIN(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicFMinEXT( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_SMAX(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicSMax( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_UMAX(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicUMax( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_FMAX(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicFMaxEXT( \
AS Type *P, int S, int O, Type V) noexcept;
#define __SPIRV_ATOMIC_AND(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicAnd(AS Type *P, int S, \
int O, Type V) noexcept;
#define __SPIRV_ATOMIC_OR(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicOr(AS Type *P, int S, int O, \
Type V) noexcept;
#define __SPIRV_ATOMIC_XOR(AS, Type) \
extern __DPCPP_SYCL_EXTERNAL Type __spirv_AtomicXor(AS Type *P, int S, \
int O, Type V) noexcept;

#define __SPIRV_ATOMIC_FLOAT(AS, Type) \
__SPIRV_ATOMIC_FADD(AS, Type) \
__SPIRV_ATOMIC_FMIN(AS, Type) \
__SPIRV_ATOMIC_FMAX(AS, Type) \
__SPIRV_ATOMIC_LOAD(AS, Type) \
__SPIRV_ATOMIC_STORE(AS, Type) \
__SPIRV_ATOMIC_EXCHANGE(AS, Type)

#define __SPIRV_ATOMIC_BASE(AS, Type) \
__SPIRV_ATOMIC_FLOAT(AS, Type) \
__SPIRV_ATOMIC_CMP_EXCHANGE(AS, Type) \
__SPIRV_ATOMIC_IADD(AS, Type) \
__SPIRV_ATOMIC_ISUB(AS, Type) \
__SPIRV_ATOMIC_AND(AS, Type) \
__SPIRV_ATOMIC_OR(AS, Type) \
__SPIRV_ATOMIC_XOR(AS, Type)

#define __SPIRV_ATOMIC_SIGNED(AS, Type) \
__SPIRV_ATOMIC_BASE(AS, Type) \
__SPIRV_ATOMIC_SMIN(AS, Type) \
__SPIRV_ATOMIC_SMAX(AS, Type)

#define __SPIRV_ATOMIC_UNSIGNED(AS, Type) \
__SPIRV_ATOMIC_BASE(AS, Type) \
__SPIRV_ATOMIC_UMIN(AS, Type) \
__SPIRV_ATOMIC_UMAX(AS, Type)

// Helper atomic operations which select correct signed/unsigned version
// of atomic min/max based on the type
#define __SPIRV_ATOMIC_MINMAX(AS, Op) \
template <typename T> \
typename std::enable_if_t< \
std::is_integral<T>::value && std::is_signed<T>::value, T> \
__spirv_Atomic##Op(AS T *Ptr, int Memory, int Semantics, T Value) noexcept { \
return __spirv_AtomicS##Op(Ptr, Memory, Semantics, Value); \
} \
template <typename T> \
typename std::enable_if_t< \
std::is_integral<T>::value && !std::is_signed<T>::value, T> \
__spirv_Atomic##Op(AS T *Ptr, int Memory, int Semantics, T Value) noexcept { \
return __spirv_AtomicU##Op(Ptr, Memory, Semantics, Value); \
} \
template <typename T> \
typename std::enable_if_t<std::is_floating_point<T>::value, T> \
__spirv_Atomic##Op(AS T *Ptr, int Memory, int Semantics, T Value) noexcept { \
return __spirv_AtomicF##Op##EXT(Ptr, Memory, Semantics, Value); \
}

#define __SPIRV_ATOMICS(macro, Arg) \
macro(__attribute__((opencl_global)), Arg) \
macro(__attribute__((opencl_local)), Arg) macro(, Arg)

__SPIRV_ATOMICS(__SPIRV_ATOMIC_FLOAT, _Float16)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_FLOAT, float)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_FLOAT, double)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_SIGNED, int)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_SIGNED, long)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_SIGNED, long long)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_UNSIGNED, unsigned int)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_UNSIGNED, unsigned long)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_UNSIGNED, unsigned long long)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_MINMAX, Min)
__SPIRV_ATOMICS(__SPIRV_ATOMIC_MINMAX, Max)

#undef __SPIRV_ATOMICS
#undef __SPIRV_ATOMIC_AND
#undef __SPIRV_ATOMIC_BASE
#undef __SPIRV_ATOMIC_CMP_EXCHANGE
#undef __SPIRV_ATOMIC_EXCHANGE
#undef __SPIRV_ATOMIC_FADD
#undef __SPIRV_ATOMIC_FLOAT
#undef __SPIRV_ATOMIC_FMAX
#undef __SPIRV_ATOMIC_FMIN
#undef __SPIRV_ATOMIC_IADD
#undef __SPIRV_ATOMIC_ISUB
#undef __SPIRV_ATOMIC_LOAD
#undef __SPIRV_ATOMIC_MINMAX
#undef __SPIRV_ATOMIC_OR
#undef __SPIRV_ATOMIC_SIGNED
#undef __SPIRV_ATOMIC_SMAX
#undef __SPIRV_ATOMIC_SMIN
#undef __SPIRV_ATOMIC_STORE
#undef __SPIRV_ATOMIC_UMAX
#undef __SPIRV_ATOMIC_UMIN
#undef __SPIRV_ATOMIC_UNSIGNED
#undef __SPIRV_ATOMIC_XOR

#endif
20 changes: 20 additions & 0 deletions sycl/include/sycl/__spirv/spirv_ops_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//==-------- spirv_ops_base.hpp --- SPIRV operation common support --------==//
//
// 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
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/__spirv/spirv_types.hpp> // for Scope, __ocl_event_t
#include <sycl/detail/defines_elementary.hpp> // for __DPCPP_SYCL_EXTERNAL
#include <sycl/detail/export.hpp> // for __SYCL_EXPORT

// Convergent attribute
#ifdef __SYCL_DEVICE_ONLY__
#define __SYCL_CONVERGENT__ __attribute__((convergent))
#else
#define __SYCL_CONVERGENT__
#endif
18 changes: 18 additions & 0 deletions sycl/include/sycl/__spirv/spirv_ops_builtin_decls.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//==--- spirv_ops_builtin_decls.hpp --- SPIRV built-in declaration guard --==//
//
// 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
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/__spirv/spirv_ops_base.hpp>

#ifdef __SYCL_DEVICE_ONLY__
#ifndef __SPIRV_BUILTIN_DECLARATIONS__
#error \
"SPIR-V built-ins are not available. Please set -fdeclare-spirv-builtins flag."
#endif
#endif
81 changes: 81 additions & 0 deletions sycl/include/sycl/__spirv/spirv_ops_image.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//==-------- spirv_ops_image.hpp --- SPIRV image operations ---------------==//
//
// 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
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/__spirv/spirv_ops_builtin_decls.hpp>

#ifdef __SYCL_DEVICE_ONLY__

template <typename RetT, typename ImageT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageQueryFormat(ImageT);

template <typename RetT, typename ImageT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageQueryOrder(ImageT);

template <typename RetT, typename ImageT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageQuerySize(ImageT);

template <typename ImageT, typename CoordT, typename ValT>
extern __DPCPP_SYCL_EXTERNAL void __spirv_ImageWrite(ImageT, CoordT, ValT);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageRead(ImageT, TempArgT);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageFetch(ImageT, TempArgT);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_SampledImageFetch(ImageT, TempArgT);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageArrayFetch(ImageT, TempArgT,
int);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_SampledImageArrayFetch(ImageT,
TempArgT, int);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_SampledImageGather(ImageT, TempArgT,
unsigned);

template <class RetT, typename ImageT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ImageArrayRead(ImageT, TempArgT, int);

template <typename ImageT, typename CoordT, typename ValT>
extern __DPCPP_SYCL_EXTERNAL void __spirv_ImageArrayWrite(ImageT, CoordT, int,
ValT);

template <typename ImageT, typename SampledType>
extern __DPCPP_SYCL_EXTERNAL SampledType __spirv_SampledImage(ImageT,
__ocl_sampler_t);

template <typename SampledType, typename TempRetT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL TempRetT
__spirv_ImageSampleExplicitLod(SampledType, TempArgT, int, float);

template <typename SampledType, typename TempRetT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL TempRetT
__spirv_ImageSampleExplicitLod(SampledType, TempArgT, int, TempArgT, TempArgT);

template <typename SampledType, typename TempRetT, typename TempArgT>
extern __DPCPP_SYCL_EXTERNAL TempRetT __spirv_ImageSampleCubemap(SampledType,
TempArgT);

template <typename RetT, class HandleT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ConvertHandleToImageINTEL(HandleT);

template <typename RetT, class HandleT>
extern __DPCPP_SYCL_EXTERNAL RetT __spirv_ConvertHandleToSamplerINTEL(HandleT);

template <typename RetT, class HandleT>
extern __DPCPP_SYCL_EXTERNAL
RetT __spirv_ConvertHandleToSampledImageINTEL(HandleT);

#endif
Loading
Loading