Skip to content

Commit 8c13580

Browse files
authored
[SYCL][NFC] Small refactoring (#2204)
1. Use C++ cast instead of C casts 2. Move implementation of several handler methods to source directory
1 parent 32bf607 commit 8c13580

File tree

5 files changed

+76
-60
lines changed

5 files changed

+76
-60
lines changed

sycl/include/CL/sycl/detail/aligned_allocator.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <CL/sycl/detail/os_util.hpp>
1313
#include <CL/sycl/range.hpp>
1414

15-
#include <algorithm>
1615
#include <cstdlib>
1716
#include <cstring>
1817
#include <memory>

sycl/include/CL/sycl/handler.hpp

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <CL/sycl/sampler.hpp>
2929
#include <CL/sycl/stl.hpp>
3030

31-
#include <algorithm>
3231
#include <functional>
3332
#include <limits>
3433
#include <memory>
@@ -250,7 +249,7 @@ class __SYCL_EXPORT handler {
250249
typename std::remove_reference<T>::type>::type>
251250
F *storePlainArg(T &&Arg) {
252251
MArgsStorage.emplace_back(sizeof(T));
253-
F *Storage = (F *)MArgsStorage.back().data();
252+
auto Storage = reinterpret_cast<F *>(MArgsStorage.back().data());
254253
*Storage = Arg;
255254
return Storage;
256255
}
@@ -308,8 +307,8 @@ class __SYCL_EXPORT handler {
308307
/// Streams are then forwarded to command group and flushed in the scheduler.
309308
///
310309
/// \param Stream is a pointer to SYCL stream.
311-
void addStream(shared_ptr_class<detail::stream_impl> Stream) {
312-
MStreamStorage.push_back(std::move(Stream));
310+
void addStream(const shared_ptr_class<detail::stream_impl> &Stream) {
311+
MStreamStorage.push_back(Stream);
313312
}
314313

315314
/// Saves buffers created by handling reduction feature in handler.
@@ -318,28 +317,16 @@ class __SYCL_EXPORT handler {
318317
/// The 'MSharedPtrStorage' suits that need.
319318
///
320319
/// @param ReduObj is a pointer to object that must be stored.
321-
void addReduction(shared_ptr_class<const void> ReduObj) {
322-
MSharedPtrStorage.push_back(std::move(ReduObj));
320+
void addReduction(const shared_ptr_class<const void> &ReduObj) {
321+
MSharedPtrStorage.push_back(ReduObj);
323322
}
324323

325324
~handler() = default;
326325

327326
bool is_host() { return MIsHost; }
328327

329328
void associateWithHandler(detail::AccessorBaseHost *AccBase,
330-
access::target AccTarget) {
331-
detail::AccessorImplPtr AccImpl = detail::getSyclObjImpl(*AccBase);
332-
detail::Requirement *Req = AccImpl.get();
333-
// Add accessor to the list of requirements.
334-
MRequirements.push_back(Req);
335-
// Store copy of the accessor.
336-
MAccStorage.push_back(std::move(AccImpl));
337-
// Add an accessor to the handler list of associated accessors.
338-
// For associated accessors index does not means nothing.
339-
MAssociatedAccesors.emplace_back(detail::kernel_param_kind_t::kind_accessor,
340-
Req, static_cast<int>(AccTarget),
341-
/*index*/ 0);
342-
}
329+
access::target AccTarget);
343330

344331
// Recursively calls itself until arguments pack is fully processed.
345332
// The version for regular(standard layout) argument.
@@ -387,7 +374,7 @@ class __SYCL_EXPORT handler {
387374
}
388375

389376
template <typename T> void setArgHelper(int ArgIndex, T &&Arg) {
390-
void *StoredArg = (void *)storePlainArg(Arg);
377+
auto StoredArg = static_cast<void *>(storePlainArg(Arg));
391378

392379
if (!std::is_same<cl_mem, T>::value && std::is_pointer<T>::value) {
393380
MArgs.emplace_back(detail::kernel_param_kind_t::kind_pointer, StoredArg,
@@ -399,7 +386,7 @@ class __SYCL_EXPORT handler {
399386
}
400387

401388
void setArgHelper(int ArgIndex, sampler &&Arg) {
402-
void *StoredArg = (void *)storePlainArg(Arg);
389+
auto StoredArg = static_cast<void *>(storePlainArg(Arg));
403390
MArgs.emplace_back(detail::kernel_param_kind_t::kind_sampler, StoredArg,
404391
sizeof(sampler), ArgIndex);
405392
}
@@ -791,8 +778,8 @@ class __SYCL_EXPORT handler {
791778
/// Registers event dependencies on this command group.
792779
///
793780
/// \param Events is a vector of valid SYCL events to wait on.
794-
void depends_on(vector_class<event> Events) {
795-
for (event &Event : Events) {
781+
void depends_on(const vector_class<event> &Events) {
782+
for (const event &Event : Events) {
796783
MEvents.push_back(detail::getSyclObjImpl(Event));
797784
}
798785
}
@@ -1572,8 +1559,8 @@ class __SYCL_EXPORT handler {
15721559
detail::AccessorImplPtr AccImpl = detail::getSyclObjImpl(*AccBase);
15731560

15741561
MRequirements.push_back(AccImpl.get());
1575-
MSrcPtr = (void *)AccImpl.get();
1576-
MDstPtr = (void *)Dst;
1562+
MSrcPtr = static_cast<void *>(AccImpl.get());
1563+
MDstPtr = static_cast<void *>(Dst);
15771564
// Store copy of accessor to the local storage to make sure it is alive
15781565
// until we finish
15791566
MAccStorage.push_back(std::move(AccImpl));
@@ -1679,7 +1666,7 @@ class __SYCL_EXPORT handler {
16791666
detail::AccessorBaseHost *AccBase = (detail::AccessorBaseHost *)&Acc;
16801667
detail::AccessorImplPtr AccImpl = detail::getSyclObjImpl(*AccBase);
16811668

1682-
MDstPtr = (void *)AccImpl.get();
1669+
MDstPtr = static_cast<void *>(AccImpl.get());
16831670
MRequirements.push_back(AccImpl.get());
16841671
MAccStorage.push_back(std::move(AccImpl));
16851672
}
@@ -1708,12 +1695,12 @@ class __SYCL_EXPORT handler {
17081695
detail::AccessorBaseHost *AccBase = (detail::AccessorBaseHost *)&Dst;
17091696
detail::AccessorImplPtr AccImpl = detail::getSyclObjImpl(*AccBase);
17101697

1711-
MDstPtr = (void *)AccImpl.get();
1698+
MDstPtr = static_cast<void *>(AccImpl.get());
17121699
MRequirements.push_back(AccImpl.get());
17131700
MAccStorage.push_back(std::move(AccImpl));
17141701

17151702
MPattern.resize(sizeof(T));
1716-
T *PatternPtr = (T *)MPattern.data();
1703+
auto PatternPtr = reinterpret_cast<T *>(MPattern.data());
17171704
*PatternPtr = Pattern;
17181705
} else {
17191706

@@ -1741,54 +1728,30 @@ class __SYCL_EXPORT handler {
17411728
///
17421729
/// \param WaitList is a vector of valid SYCL events that need to complete
17431730
/// before barrier command can be executed.
1744-
void barrier(const vector_class<event> &WaitList) {
1745-
throwIfActionIsCreated();
1746-
MCGType = detail::CG::BARRIER_WAITLIST;
1747-
MEventsWaitWithBarrier.resize(WaitList.size());
1748-
std::transform(
1749-
WaitList.begin(), WaitList.end(), MEventsWaitWithBarrier.begin(),
1750-
[](const event &Event) { return detail::getSyclObjImpl(Event); });
1751-
}
1731+
void barrier(const vector_class<event> &WaitList);
17521732

17531733
/// Copies data from one memory region to another, both pointed by
17541734
/// USM pointers.
17551735
///
17561736
/// \param Dest is a USM pointer to the destination memory.
17571737
/// \param Src is a USM pointer to the source memory.
17581738
/// \param Count is a number of bytes to copy.
1759-
void memcpy(void *Dest, const void *Src, size_t Count) {
1760-
throwIfActionIsCreated();
1761-
MSrcPtr = const_cast<void *>(Src);
1762-
MDstPtr = Dest;
1763-
MLength = Count;
1764-
MCGType = detail::CG::COPY_USM;
1765-
}
1739+
void memcpy(void *Dest, const void *Src, size_t Count);
17661740

17671741
/// Fills the memory pointed by a USM pointer with the value specified.
17681742
///
17691743
/// \param Dest is a USM pointer to the memory to fill.
17701744
/// \param Value is a value to be set. Value is cast as an unsigned char.
17711745
/// \param Count is a number of bytes to fill.
1772-
void memset(void *Dest, int Value, size_t Count) {
1773-
throwIfActionIsCreated();
1774-
MDstPtr = Dest;
1775-
MPattern.push_back((char)Value);
1776-
MLength = Count;
1777-
MCGType = detail::CG::FILL_USM;
1778-
}
1746+
void memset(void *Dest, int Value, size_t Count);
17791747

17801748
/// Provides hints to the runtime library that data should be made available
17811749
/// on a device earlier than Unified Shared Memory would normally require it
17821750
/// to be available.
17831751
///
17841752
/// \param Ptr is a USM pointer to the memory to be prefetched to the device.
17851753
/// \param Count is a number of bytes to be prefetched.
1786-
void prefetch(const void *Ptr, size_t Count) {
1787-
throwIfActionIsCreated();
1788-
MDstPtr = const_cast<void *>(Ptr);
1789-
MLength = Count;
1790-
MCGType = detail::CG::PREFETCH_USM;
1791-
}
1754+
void prefetch(const void *Ptr, size_t Count);
17921755

17931756
private:
17941757
shared_ptr_class<detail::queue_impl> MQueue;
@@ -1830,7 +1793,7 @@ class __SYCL_EXPORT handler {
18301793
unique_ptr_class<detail::HostTask> MHostTask;
18311794
detail::OSModuleHandle MOSModuleHandle = detail::OSUtil::ExeModuleHandle;
18321795
// Storage for a lambda or function when using InteropTasks
1833-
std::unique_ptr<detail::InteropTask> MInteropTask;
1796+
unique_ptr_class<detail::InteropTask> MInteropTask;
18341797
/// The list of events that order this operation.
18351798
vector_class<detail::EventImplPtr> MEvents;
18361799
/// The list of valid SYCL events that need to complete

sycl/source/detail/scheduler/graph_builder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ Command *Scheduler::GraphBuilder::insertMemoryMove(MemObjRecord *Record,
289289
// Since no alloca command for the sub buffer requirement was found in the
290290
// current context, need to find a parent alloca command for it (it must be
291291
// there)
292-
auto IsSuitableAlloca = [Record, Req](AllocaCommandBase *AllocaCmd) {
292+
auto IsSuitableAlloca = [Record](AllocaCommandBase *AllocaCmd) {
293293
bool Res = sameCtx(AllocaCmd->getQueue()->getContextImplPtr(),
294294
Record->MCurContext) &&
295295
// Looking for a parent buffer alloca command
@@ -455,7 +455,7 @@ Command *Scheduler::GraphBuilder::addHostAccessor(Requirement *Req) {
455455
Command *Scheduler::GraphBuilder::addCGUpdateHost(
456456
std::unique_ptr<detail::CG> CommandGroup, QueueImplPtr HostQueue) {
457457

458-
CGUpdateHost *UpdateHost = (CGUpdateHost *)CommandGroup.get();
458+
auto UpdateHost = static_cast<CGUpdateHost *>(CommandGroup.get());
459459
Requirement *Req = UpdateHost->getReqToUpdate();
460460

461461
MemObjRecord *Record = getOrInsertMemObjRecord(HostQueue, Req);

sycl/source/handler.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <algorithm>
10+
911
#include <CL/sycl/detail/common.hpp>
1012
#include <CL/sycl/detail/helpers.hpp>
1113
#include <CL/sycl/detail/kernel_desc.hpp>
@@ -113,6 +115,21 @@ event handler::finalize() {
113115
return MLastEvent;
114116
}
115117

118+
void handler::associateWithHandler(detail::AccessorBaseHost *AccBase,
119+
access::target AccTarget) {
120+
detail::AccessorImplPtr AccImpl = detail::getSyclObjImpl(*AccBase);
121+
detail::Requirement *Req = AccImpl.get();
122+
// Add accessor to the list of requirements.
123+
MRequirements.push_back(Req);
124+
// Store copy of the accessor.
125+
MAccStorage.push_back(std::move(AccImpl));
126+
// Add an accessor to the handler list of associated accessors.
127+
// For associated accessors index does not means nothing.
128+
MAssociatedAccesors.emplace_back(detail::kernel_param_kind_t::kind_accessor,
129+
Req, static_cast<int>(AccTarget),
130+
/*index*/ 0);
131+
}
132+
116133
void handler::processArg(void *Ptr, const detail::kernel_param_kind_t &Kind,
117134
const int Size, const size_t Index, size_t &IndexShift,
118135
bool IsKernelCreatedFromSource) {
@@ -277,5 +294,37 @@ void handler::extractArgsAndReqsFromLambda(
277294
string_class handler::getKernelName() {
278295
return MKernel->get_info<info::kernel::function_name>();
279296
}
297+
298+
void handler::barrier(const vector_class<event> &WaitList) {
299+
throwIfActionIsCreated();
300+
MCGType = detail::CG::BARRIER_WAITLIST;
301+
MEventsWaitWithBarrier.resize(WaitList.size());
302+
std::transform(
303+
WaitList.begin(), WaitList.end(), MEventsWaitWithBarrier.begin(),
304+
[](const event &Event) { return detail::getSyclObjImpl(Event); });
305+
}
306+
307+
void handler::memcpy(void *Dest, const void *Src, size_t Count) {
308+
throwIfActionIsCreated();
309+
MSrcPtr = const_cast<void *>(Src);
310+
MDstPtr = Dest;
311+
MLength = Count;
312+
MCGType = detail::CG::COPY_USM;
313+
}
314+
315+
void handler::memset(void *Dest, int Value, size_t Count) {
316+
throwIfActionIsCreated();
317+
MDstPtr = Dest;
318+
MPattern.push_back(static_cast<char>(Value));
319+
MLength = Count;
320+
MCGType = detail::CG::FILL_USM;
321+
}
322+
323+
void handler::prefetch(const void *Ptr, size_t Count) {
324+
throwIfActionIsCreated();
325+
MDstPtr = const_cast<void *>(Ptr);
326+
MLength = Count;
327+
MCGType = detail::CG::PREFETCH_USM;
328+
}
280329
} // namespace sycl
281330
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,8 +3819,13 @@ _ZN2cl4sycl7contextC2ESt10shared_ptrINS0_6detail12context_implEE
38193819
_ZN2cl4sycl7handler10processArgEPvRKNS0_6detail19kernel_param_kind_tEimRmb
38203820
_ZN2cl4sycl7handler13getKernelNameB5cxx11Ev
38213821
_ZN2cl4sycl7handler18extractArgsAndReqsEv
3822+
_ZN2cl4sycl7handler20associateWithHandlerEPNS0_6detail16AccessorBaseHostENS0_6access6targetE
38223823
_ZN2cl4sycl7handler28extractArgsAndReqsFromLambdaEPcmPKNS0_6detail19kernel_param_desc_tE
3824+
_ZN2cl4sycl7handler6memcpyEPvPKvm
3825+
_ZN2cl4sycl7handler6memsetEPvim
3826+
_ZN2cl4sycl7handler7barrierERKSt6vectorINS0_5eventESaIS3_EE
38233827
_ZN2cl4sycl7handler8finalizeEv
3828+
_ZN2cl4sycl7handler8prefetchEPKvm
38243829
_ZN2cl4sycl7program17build_with_sourceENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_
38253830
_ZN2cl4sycl7program19compile_with_sourceENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_
38263831
_ZN2cl4sycl7program22build_with_kernel_nameENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_l

0 commit comments

Comments
 (0)