Skip to content

Commit

Permalink
[SYCL][Fusion][NoSTL] Reimplement Indices class without std::array (
Browse files Browse the repository at this point in the history
intel#12340)

The `jit_compiler::NDRange` class stores global/local sizes and an
offset as instances of `jit_compiler::Indices`, which previously was
just an alias to `std::array<size_t, 3>`. To elide this use of the STL
class, I implemented a drop-in replacement which is backed by a C array
and provides a minimal set of accessors and operators.

*This PR is part of a series of changes to remove uses of STL classes in
the kernel fusion interface to prevent ABI issues in the future.*

---------

Signed-off-by: Julian Oppermann <julian.oppermann@codeplay.com>
  • Loading branch information
jopperm authored Jan 12, 2024
1 parent 6eac61a commit b788a3f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
40 changes: 39 additions & 1 deletion sycl-fusion/common/include/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,45 @@ struct SYCLArgumentDescriptor {
/// List of SYCL/OpenCL kernel attributes.
using AttributeList = std::vector<SYCLKernelAttribute>;

using Indices = std::array<size_t, 3>;
///
/// Class to model a three-dimensional index.
class Indices {
public:
static constexpr size_t size() { return Size; }

constexpr Indices() : Values{0, 0, 0} {}
constexpr Indices(size_t V1, size_t V2, size_t V3) : Values{V1, V2, V3} {}

constexpr const size_t *begin() const { return Values; }
constexpr const size_t *end() const { return Values + Size; }
constexpr size_t *begin() { return Values; }
constexpr size_t *end() { return Values + Size; }

constexpr const size_t &operator[](int Idx) const { return Values[Idx]; }
constexpr size_t &operator[](int Idx) { return Values[Idx]; }

friend bool operator==(const Indices &A, const Indices &B) {
return std::equal(A.begin(), A.end(), B.begin());
}

friend bool operator!=(const Indices &A, const Indices &B) {
return !(A == B);
}

friend bool operator<(const Indices &A, const Indices &B) {
return std::lexicographical_compare(A.begin(), A.end(), B.begin(), B.end(),
std::less<size_t>{});
}

friend bool operator>(const Indices &A, const Indices &B) {
return std::lexicographical_compare(A.begin(), A.end(), B.begin(), B.end(),
std::greater<size_t>{});
}

private:
static constexpr size_t Size = 3;
size_t Values[Size];
};

///
/// Class to model SYCL nd_range
Expand Down
2 changes: 1 addition & 1 deletion sycl-fusion/jit-compiler/lib/fusion/FusionHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Expected<std::unique_ptr<Module>> helper::FusionHelper::addFusedKernel(
{
const auto MDFromND = [&LLVMCtx](const auto &ND) {
auto MDFromIndices = [&LLVMCtx](const auto &Ind) -> Metadata * {
std::array<Metadata *, jit_compiler::Indices{}.size()> MD{nullptr};
std::array<Metadata *, jit_compiler::Indices::size()> MD{nullptr};
std::transform(
Ind.begin(), Ind.end(), MD.begin(),
[&LLVMCtx](auto I) { return getConstantIntMD(LLVMCtx, I); });
Expand Down
4 changes: 4 additions & 0 deletions sycl-fusion/jit-compiler/lib/fusion/Hashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ inline llvm::hash_code hash_value(const ParameterIdentity &IP) {
return llvm::hash_combine(IP.LHS, IP.RHS);
}

inline llvm::hash_code hash_value(const Indices &I) {
return llvm::hash_combine_range(I.begin(), I.end());
}

inline llvm::hash_code hash_value(const NDRange &ND) {
return llvm::hash_combine(ND.getDimensions(), ND.getGlobalSize(),
ND.getLocalSize(), ND.getOffset());
Expand Down

0 comments on commit b788a3f

Please sign in to comment.