Skip to content

Commit b788a3f

Browse files
authored
[SYCL][Fusion][NoSTL] Reimplement Indices class without std::array (#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 <[email protected]>
1 parent 6eac61a commit b788a3f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

sycl-fusion/common/include/Kernel.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,45 @@ struct SYCLArgumentDescriptor {
147147
/// List of SYCL/OpenCL kernel attributes.
148148
using AttributeList = std::vector<SYCLKernelAttribute>;
149149

150-
using Indices = std::array<size_t, 3>;
150+
///
151+
/// Class to model a three-dimensional index.
152+
class Indices {
153+
public:
154+
static constexpr size_t size() { return Size; }
155+
156+
constexpr Indices() : Values{0, 0, 0} {}
157+
constexpr Indices(size_t V1, size_t V2, size_t V3) : Values{V1, V2, V3} {}
158+
159+
constexpr const size_t *begin() const { return Values; }
160+
constexpr const size_t *end() const { return Values + Size; }
161+
constexpr size_t *begin() { return Values; }
162+
constexpr size_t *end() { return Values + Size; }
163+
164+
constexpr const size_t &operator[](int Idx) const { return Values[Idx]; }
165+
constexpr size_t &operator[](int Idx) { return Values[Idx]; }
166+
167+
friend bool operator==(const Indices &A, const Indices &B) {
168+
return std::equal(A.begin(), A.end(), B.begin());
169+
}
170+
171+
friend bool operator!=(const Indices &A, const Indices &B) {
172+
return !(A == B);
173+
}
174+
175+
friend bool operator<(const Indices &A, const Indices &B) {
176+
return std::lexicographical_compare(A.begin(), A.end(), B.begin(), B.end(),
177+
std::less<size_t>{});
178+
}
179+
180+
friend bool operator>(const Indices &A, const Indices &B) {
181+
return std::lexicographical_compare(A.begin(), A.end(), B.begin(), B.end(),
182+
std::greater<size_t>{});
183+
}
184+
185+
private:
186+
static constexpr size_t Size = 3;
187+
size_t Values[Size];
188+
};
151189

152190
///
153191
/// Class to model SYCL nd_range

sycl-fusion/jit-compiler/lib/fusion/FusionHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Expected<std::unique_ptr<Module>> helper::FusionHelper::addFusedKernel(
9292
{
9393
const auto MDFromND = [&LLVMCtx](const auto &ND) {
9494
auto MDFromIndices = [&LLVMCtx](const auto &Ind) -> Metadata * {
95-
std::array<Metadata *, jit_compiler::Indices{}.size()> MD{nullptr};
95+
std::array<Metadata *, jit_compiler::Indices::size()> MD{nullptr};
9696
std::transform(
9797
Ind.begin(), Ind.end(), MD.begin(),
9898
[&LLVMCtx](auto I) { return getConstantIntMD(LLVMCtx, I); });

sycl-fusion/jit-compiler/lib/fusion/Hashing.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inline llvm::hash_code hash_value(const ParameterIdentity &IP) {
3434
return llvm::hash_combine(IP.LHS, IP.RHS);
3535
}
3636

37+
inline llvm::hash_code hash_value(const Indices &I) {
38+
return llvm::hash_combine_range(I.begin(), I.end());
39+
}
40+
3741
inline llvm::hash_code hash_value(const NDRange &ND) {
3842
return llvm::hash_combine(ND.getDimensions(), ND.getGlobalSize(),
3943
ND.getLocalSize(), ND.getOffset());

0 commit comments

Comments
 (0)