Skip to content

Commit 2c7609a

Browse files
Make some offset indexer classes constexpr
Making NoOpIndexer constexpr allows compiler to completely inline its definition at earlier optimization stages. Also replaced stray py::ssize with ssize_t
1 parent e2d9640 commit 2c7609a

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

dpctl/tensor/libtensor/include/utils/offset_utils.hpp

+53-48
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,15 @@ device_allocate_and_pack(sycl::queue &q,
125125

126126
struct NoOpIndexer
127127
{
128-
size_t operator()(size_t gid) const
128+
constexpr NoOpIndexer() {}
129+
constexpr size_t operator()(size_t gid) const
129130
{
130131
return gid;
131132
}
132133
};
133134

135+
using dpctl::tensor::ssize_t;
136+
134137
/* @brief Indexer with shape and strides arrays of same size are packed */
135138
struct StridedIndexer
136139
{
@@ -204,7 +207,7 @@ struct UnpackedStridedIndexer
204207
using dpctl::tensor::strides::CIndexer_vector;
205208

206209
CIndexer_vector _ind(nd);
207-
py::ssize_t relative_offset(0);
210+
ssize_t relative_offset(0);
208211
_ind.get_displacement<const ssize_t *, const ssize_t *>(
209212
gid,
210213
shape, // shape ptr
@@ -253,18 +256,18 @@ struct Strided1DCyclicIndexer
253256

254257
template <typename displacementT> struct TwoOffsets
255258
{
256-
TwoOffsets() : first_offset(0), second_offset(0) {}
257-
TwoOffsets(const displacementT &first_offset_,
258-
const displacementT &second_offset_)
259+
constexpr TwoOffsets() : first_offset(0), second_offset(0) {}
260+
constexpr TwoOffsets(const displacementT &first_offset_,
261+
const displacementT &second_offset_)
259262
: first_offset(first_offset_), second_offset(second_offset_)
260263
{
261264
}
262265

263-
displacementT get_first_offset() const
266+
constexpr displacementT get_first_offset() const
264267
{
265268
return first_offset;
266269
}
267-
displacementT get_second_offset() const
270+
constexpr displacementT get_second_offset() const
268271
{
269272
return second_offset;
270273
}
@@ -323,9 +326,9 @@ struct TwoOffsets_StridedIndexer
323326

324327
struct TwoZeroOffsets_Indexer
325328
{
326-
TwoZeroOffsets_Indexer() {}
329+
constexpr TwoZeroOffsets_Indexer() {}
327330

328-
TwoOffsets<ssize_t> operator()(ssize_t) const
331+
constexpr TwoOffsets<ssize_t> operator()(ssize_t) const
329332
{
330333
return TwoOffsets<ssize_t>();
331334
}
@@ -339,39 +342,41 @@ struct TwoOffsets_CombinedIndexer
339342
SecondIndexerT second_indexer_;
340343

341344
public:
342-
TwoOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
343-
const SecondIndexerT &second_indexer)
345+
constexpr TwoOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
346+
const SecondIndexerT &second_indexer)
344347
: first_indexer_(first_indexer), second_indexer_(second_indexer)
345348
{
346349
}
347350

348-
TwoOffsets<py::ssize_t> operator()(py::ssize_t gid) const
351+
constexpr TwoOffsets<ssize_t> operator()(ssize_t gid) const
349352
{
350-
return TwoOffsets<py::ssize_t>(first_indexer_(gid),
351-
second_indexer_(gid));
353+
return TwoOffsets<ssize_t>(first_indexer_(gid), second_indexer_(gid));
352354
}
353355
};
354356

355357
template <typename displacementT> struct ThreeOffsets
356358
{
357-
ThreeOffsets() : first_offset(0), second_offset(0), third_offset(0) {}
358-
ThreeOffsets(const displacementT &first_offset_,
359-
const displacementT &second_offset_,
360-
const displacementT &third_offset_)
359+
constexpr ThreeOffsets()
360+
: first_offset(0), second_offset(0), third_offset(0)
361+
{
362+
}
363+
constexpr ThreeOffsets(const displacementT &first_offset_,
364+
const displacementT &second_offset_,
365+
const displacementT &third_offset_)
361366
: first_offset(first_offset_), second_offset(second_offset_),
362367
third_offset(third_offset_)
363368
{
364369
}
365370

366-
displacementT get_first_offset() const
371+
constexpr displacementT get_first_offset() const
367372
{
368373
return first_offset;
369374
}
370-
displacementT get_second_offset() const
375+
constexpr displacementT get_second_offset() const
371376
{
372377
return second_offset;
373378
}
374-
displacementT get_third_offset() const
379+
constexpr displacementT get_third_offset() const
375380
{
376381
return third_offset;
377382
}
@@ -438,11 +443,11 @@ struct ThreeOffsets_StridedIndexer
438443

439444
struct ThreeZeroOffsets_Indexer
440445
{
441-
ThreeZeroOffsets_Indexer() {}
446+
constexpr ThreeZeroOffsets_Indexer() {}
442447

443-
ThreeOffsets<py::ssize_t> operator()(py::ssize_t) const
448+
constexpr ThreeOffsets<ssize_t> operator()(ssize_t) const
444449
{
445-
return ThreeOffsets<py::ssize_t>();
450+
return ThreeOffsets<ssize_t>();
446451
}
447452
};
448453

@@ -457,15 +462,15 @@ struct ThreeOffsets_CombinedIndexer
457462
ThirdIndexerT third_indexer_;
458463

459464
public:
460-
ThreeOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
461-
const SecondIndexerT &second_indexer,
462-
const ThirdIndexerT &third_indexer)
465+
constexpr ThreeOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
466+
const SecondIndexerT &second_indexer,
467+
const ThirdIndexerT &third_indexer)
463468
: first_indexer_(first_indexer), second_indexer_(second_indexer),
464469
third_indexer_(third_indexer)
465470
{
466471
}
467472

468-
ThreeOffsets<ssize_t> operator()(ssize_t gid) const
473+
constexpr ThreeOffsets<ssize_t> operator()(ssize_t gid) const
469474
{
470475
return ThreeOffsets<ssize_t>(first_indexer_(gid), second_indexer_(gid),
471476
third_indexer_(gid));
@@ -474,32 +479,32 @@ struct ThreeOffsets_CombinedIndexer
474479

475480
template <typename displacementT> struct FourOffsets
476481
{
477-
FourOffsets()
482+
constexpr FourOffsets()
478483
: first_offset(0), second_offset(0), third_offset(0), fourth_offset(0)
479484
{
480485
}
481-
FourOffsets(const displacementT &first_offset_,
482-
const displacementT &second_offset_,
483-
const displacementT &third_offset_,
484-
const displacementT &fourth_offset_)
486+
constexpr FourOffsets(const displacementT &first_offset_,
487+
const displacementT &second_offset_,
488+
const displacementT &third_offset_,
489+
const displacementT &fourth_offset_)
485490
: first_offset(first_offset_), second_offset(second_offset_),
486491
third_offset(third_offset_), fourth_offset(fourth_offset_)
487492
{
488493
}
489494

490-
displacementT get_first_offset() const
495+
constexpr displacementT get_first_offset() const
491496
{
492497
return first_offset;
493498
}
494-
displacementT get_second_offset() const
499+
constexpr displacementT get_second_offset() const
495500
{
496501
return second_offset;
497502
}
498-
displacementT get_third_offset() const
503+
constexpr displacementT get_third_offset() const
499504
{
500505
return third_offset;
501506
}
502-
displacementT get_fourth_offset() const
507+
constexpr displacementT get_fourth_offset() const
503508
{
504509
return fourth_offset;
505510
}
@@ -513,12 +518,12 @@ template <typename displacementT> struct FourOffsets
513518

514519
struct FourOffsets_StridedIndexer
515520
{
516-
FourOffsets_StridedIndexer(int common_nd,
517-
ssize_t first_offset_,
518-
ssize_t second_offset_,
519-
ssize_t third_offset_,
520-
ssize_t fourth_offset_,
521-
ssize_t const *_packed_shape_strides)
521+
constexpr FourOffsets_StridedIndexer(int common_nd,
522+
ssize_t first_offset_,
523+
ssize_t second_offset_,
524+
ssize_t third_offset_,
525+
ssize_t fourth_offset_,
526+
ssize_t const *_packed_shape_strides)
522527
: nd(common_nd), starting_first_offset(first_offset_),
523528
starting_second_offset(second_offset_),
524529
starting_third_offset(third_offset_),
@@ -527,12 +532,12 @@ struct FourOffsets_StridedIndexer
527532
{
528533
}
529534

530-
FourOffsets<ssize_t> operator()(ssize_t gid) const
535+
constexpr FourOffsets<ssize_t> operator()(ssize_t gid) const
531536
{
532537
return compute_offsets(gid);
533538
}
534539

535-
FourOffsets<ssize_t> operator()(size_t gid) const
540+
constexpr FourOffsets<ssize_t> operator()(size_t gid) const
536541
{
537542
return compute_offsets(static_cast<ssize_t>(gid));
538543
}
@@ -545,7 +550,7 @@ struct FourOffsets_StridedIndexer
545550
ssize_t starting_fourth_offset;
546551
ssize_t const *shape_strides;
547552

548-
FourOffsets<py::ssize_t> compute_offsets(ssize_t gid) const
553+
FourOffsets<ssize_t> compute_offsets(ssize_t gid) const
549554
{
550555
using dpctl::tensor::strides::CIndexer_vector;
551556

@@ -573,9 +578,9 @@ struct FourOffsets_StridedIndexer
573578

574579
struct FourZeroOffsets_Indexer
575580
{
576-
FourZeroOffsets_Indexer() {}
581+
constexpr FourZeroOffsets_Indexer() {}
577582

578-
FourOffsets<ssize_t> operator()(ssize_t) const
583+
constexpr FourOffsets<ssize_t> operator()(ssize_t) const
579584
{
580585
return FourOffsets<ssize_t>();
581586
}

0 commit comments

Comments
 (0)