Skip to content

Commit 600096c

Browse files
committed
[SYCL] Addition of suggestions.
Signed-off-by: Garima Gupta <[email protected]>
1 parent 945dd85 commit 600096c

File tree

10 files changed

+70
-57
lines changed

10 files changed

+70
-57
lines changed

sycl/include/CL/sycl/detail/pi.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#define _PI_H_VERSION_MAJOR 1
2424
#define _PI_H_VERSION_MINOR 1
2525

26+
#define _PI_STRING_HELPER(a) #a
27+
#define _PI_CONCAT(a, b) _PI_STRING_HELPER(a.b)
28+
#define _PI_H_VERSION_STRING \
29+
_PI_CONCAT(_PI_H_VERSION_MAJOR, _PI_H_VERSION_MINOR)
2630
// TODO: we need a mapping of PI to OpenCL somewhere, and this can be done
2731
// elsewhere, e.g. in the pi_opencl, but constants/enums mapping is now
2832
// done here, for efficiency and simplicity.
@@ -914,9 +918,6 @@ pi_result piEnqueueMemUnmap(
914918
pi_event * event);
915919

916920

917-
#define STRING_HELPER(a) #a
918-
#define STRINGIZE(a,b) STRING_HELPER(a.b)
919-
920921
struct _pi_plugin {
921922
// PI version supported by host passed to the plugin. The Plugin
922923
// checks and writes the appropriate Function Pointers in
@@ -925,19 +926,16 @@ struct _pi_plugin {
925926
// Some choices are:
926927
// - Use of integers to keep major and minor version.
927928
// - Keeping char* Versions.
928-
const char PiVersion[4] = STRINGIZE(_PI_H_VERSION_MAJOR,_PI_H_VERSION_MINOR);
929+
const char PiVersion[4] = _PI_H_VERSION_STRING;
929930
// Plugin edits this.
930-
char PluginVersion[4] = STRINGIZE(_PI_H_VERSION_MAJOR, _PI_H_VERSION_MINOR);
931+
char PluginVersion[4] = _PI_H_VERSION_STRING;
931932
char *Targets;
932933
struct FunctionPointers {
933934
#define _PI_API(api) decltype(::api) *api;
934935
#include <CL/sycl/detail/pi.def>
935936
} PiFunctionTable;
936937
};
937938

938-
#undef STRING_HELPER
939-
#undef STRINGIZE
940-
941939
#ifdef __cplusplus
942940
} // extern "C"
943941
#endif // __cplusplus

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,45 +120,68 @@ void printArgs(Arg0 arg0, Args... args) {
120120
// Utility function to check return from pi calls.
121121
// Throws if pi_result is not a PI_SUCCESS.
122122
template <typename Exception = cl::sycl::runtime_error>
123-
inline void piCheckResult(PiResult pi_result) {
123+
inline void checkPiResult(PiResult pi_result) {
124124
CHECK_OCL_CODE_THROW(pi_result, Exception);
125125
}
126126

127-
#define PI_TRACE_ENABLED (std::getenv("SYCL_PI_TRACE") != nullptr)
128-
129-
template <typename FnType, size_t FnOffset> class Trace {
127+
// Class to call PI API, trace and get the result.
128+
// To Trace : Set SYCL_PI_TRACE environment variable.
129+
// Template Arguments:
130+
// FnType - Type of Function pointer to the PI API.
131+
// FnOffset- Offset to the Function Pointer in the piPlugin::FunctionPointers
132+
// structure.Used to differentiate between APIs with same pointer type, Eg:
133+
// piDeviceRelease and piDeviceRetain. Differentiation needed to avoid
134+
// duplicate instantiation of class in pi.cpp.
135+
// Members: Initialiaed in default constructor in Class Template Specialization.
136+
// Usage:
137+
// Operator() - Call, Trace and Get result
138+
// Use Macro PI_CALL_NOCHECK call the constructor directly.
139+
template <typename FnType, size_t FnOffset> class CallPi {
130140
private:
131-
FnType m_FnPtr;
132-
std::string m_FnName;
141+
FnType MFnPtr;
142+
std::string MFnName;
143+
static bool MEnableTrace;
133144

134145
public:
135-
Trace();
146+
CallPi();
136147
template <typename... Args> PiResult operator()(Args... args) {
137-
bool enableTrace = PI_TRACE_ENABLED;
138-
if (enableTrace) {
139-
std::cout << "---> " << m_FnName << "(";
148+
if (MEnableTrace) {
149+
std::cout << "---> " << MFnName << "(";
140150
printArgs(args...);
141151
}
142152

143-
PiResult r = m_FnPtr(args...);
153+
PiResult r = MFnPtr(args...);
144154

145-
if (enableTrace) {
155+
if (MEnableTrace) {
146156
std::cout << ") ---> ";
147157
std::cout << (print(r), "") << std::endl;
148158
}
149159
return r;
150160
}
151161
};
152162

163+
template <typename FnType, size_t FnOffset>
164+
bool CallPi<FnType, FnOffset>::MEnableTrace = (std::getenv("SYCL_PI_TRACE") !=
165+
nullptr);
166+
167+
// Class to call PI API, trace, check the return result and throw Exception.
168+
// To Trace : Set SYCL_PI_TRACE environment variable.
169+
// Template Arguments:
170+
// FnType, FnOffset - for CallPi Class.
171+
// Exception - The type of exception to throw if PiResult of a call is not
172+
// PI_SUCCESS. default is cl::sycl::runtime_error.
173+
// Usage:
174+
// Operator() - Call, Trace, check Result and Throw Exception.
175+
// Use Macro PI_CALL and PI_CALL_THROW to call the constructor directly.
153176
template <typename FnType, size_t FnOffset,
154177
typename Exception = cl::sycl::runtime_error>
155-
class TraceCheck : private Trace<FnType, FnOffset> {
178+
class CallPiAndCheck : private CallPi<FnType, FnOffset> {
156179
public:
157-
TraceCheck() : Trace<FnType, FnOffset>(){};
180+
CallPiAndCheck() : CallPi<FnType, FnOffset>(){};
158181

159182
template <typename... Args> void operator()(Args... args) {
160-
PiResult Err = (Trace<FnType, FnOffset>::operator()(args...));
161-
piCheckResult<Exception>(Err);
183+
PiResult Err = (CallPi<FnType, FnOffset>::operator()(args...));
184+
checkPiResult<Exception>(Err);
162185
}
163186
};
164187

@@ -167,39 +190,39 @@ class TraceCheck : private Trace<FnType, FnOffset> {
167190
// api.
168191
#define _PI_API(api) \
169192
template <> \
170-
Trace<decltype(&::api), \
171-
(offsetof(pi_plugin::FunctionPointers, api))>::Trace();
193+
CallPi<decltype(&::api), \
194+
(offsetof(pi_plugin::FunctionPointers, api))>::CallPi();
172195

173196
#include <CL/sycl/detail/pi.def>
174197

175198
} // namespace pi
176199

177200
namespace RT = cl::sycl::detail::pi;
178201

179-
#define PI_ASSERT(cond, msg) RT::assertion((cond), "assert: " msg);
180-
181202
// Use this macro to call the API, trace the call, check the return and throw a
182203
// runtime_error exception.
183204
// Usage: PI_CALL(pi)(Args);
184205
#define PI_CALL(pi) \
185-
RT::TraceCheck<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi))>()
206+
RT::CallPiAndCheck<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi))>()
186207

187208
// Use this macro to call the API, trace the call and return the result.
188-
// To check the result use piCheckResult.
209+
// To check the result use checkPiResult.
189210
// Usage:
190211
// PiResult Err = PI_CALL_NOCHECK(pi)(args);
191-
// RT::piCheckResult(Err); <- Checks Result and throws a runtime error
212+
// RT::checkPiResult(Err); <- Checks Result and throws a runtime error
192213
// exception.
193214
#define PI_CALL_NOCHECK(pi) \
194-
RT::Trace<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi))>()
215+
RT::CallPi<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi))>()
195216

196217
// Use this macro to call the API, trace the call, check the return and throw a
197218
// Exception as given in the MACRO.
198219
// Usage: PI_CALL_THROW(pi, compile_program_error)(args);
199220
#define PI_CALL_THROW(pi, Exception) \
200-
RT::TraceCheck<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi)), \
221+
RT::CallPiAndCheck<decltype(&::pi), (offsetof(pi_plugin::FunctionPointers, pi)), \
201222
Exception>()
202223

224+
#define PI_ASSERT(cond, msg) RT::assertion((cond), "assert: " msg);
225+
203226
// Want all the needed casts be explicit, do not define conversion
204227
// operators.
205228
template <class To, class From> To pi::cast(From value) {

sycl/include/CL/sycl/detail/program_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ class program_impl {
448448
throw invalid_object_error(
449449
"This instance of program does not contain the kernel requested");
450450
}
451-
RT::piCheckResult(Err);
451+
RT::checkPiResult(Err);
452452
}
453453

454454
return Kernel;

sycl/include/CL/sycl/detail/queue_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class queue_impl {
170170
m_SupportOOO = false;
171171
Queue = createQueue(QueueOrder::Ordered);
172172
} else {
173-
RT::piCheckResult(Error);
173+
RT::checkPiResult(Error);
174174
}
175175

176176
return Queue;

sycl/plugins/opencl/pi_opencl.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
return cast<pi_result>(reterr); \
2121
}
2222

23-
#define STRING_HELPER(a) #a
24-
#define STRINGIZE(a,b) STRING_HELPER(a.b)
25-
const char SupportedVersion[] = STRINGIZE(_PI_H_VERSION_MAJOR,_PI_H_VERSION_MINOR);
26-
#undef STRING_HELPER
27-
#undef STRINGIZE
28-
23+
const char SupportedVersion[] = _PI_H_VERSION_STRING;
2924

3025
// Want all the needed casts be explicit, do not define conversion operators.
3126
template <class To, class From> To cast(From value) {

sycl/source/detail/memory_manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ void MemoryManager::copy_usm(const void *SrcMem, QueueImplPtr SrcQueue,
489489

490490
std::shared_ptr<usm::USMDispatcher> USMDispatch =
491491
getSyclObjImpl(Context)->getUSMDispatch();
492-
RT::piCheckResult(USMDispatch->enqueueMemcpy(Queue,
492+
RT::checkPiResult(USMDispatch->enqueueMemcpy(Queue,
493493
/* blocking */ false, DstMem,
494494
SrcMem, Len, DepEvents.size(),
495495
&DepEvents[0], &OutEvent));
@@ -506,7 +506,7 @@ void MemoryManager::fill_usm(void *Mem, QueueImplPtr Queue, size_t Length,
506506
} else {
507507
std::shared_ptr<usm::USMDispatcher> USMDispatch =
508508
getSyclObjImpl(Context)->getUSMDispatch();
509-
RT::piCheckResult(
509+
RT::checkPiResult(
510510
USMDispatch->enqueueMemset(Queue->getHandleRef(), Mem, Pattern, Length,
511511
DepEvents.size(), &DepEvents[0], &OutEvent));
512512
}
@@ -522,7 +522,7 @@ void MemoryManager::prefetch_usm(void *Mem, QueueImplPtr Queue, size_t Length,
522522
} else {
523523
std::shared_ptr<usm::USMDispatcher> USMDispatch =
524524
getSyclObjImpl(Context)->getUSMDispatch();
525-
RT::piCheckResult(USMDispatch->enqueuePrefetch(Queue->getHandleRef(), Mem,
525+
RT::checkPiResult(USMDispatch->enqueuePrefetch(Queue->getHandleRef(), Mem,
526526
Length, DepEvents.size(),
527527
&DepEvents[0], &OutEvent));
528528
}

sycl/source/detail/pi.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,14 @@ void assertion(bool Condition, const char *Message) {
138138
// TODO: Pass platform object to constructor which will contain the
139139
// PluginInformation class. Platform class with Plugin information is not
140140
// implemented yet.
141-
// Note: offsetof is used to differentiate between apis that have the same
142-
// signature. Eg: piDeviceRelease and piDeviceRetain.
143-
// offsetof(pi_plugin.PiFunctionTable,api)>
144141

145142
#define _PI_API(api) \
146143
template <> \
147-
Trace<decltype(&::api), \
148-
(offsetof(pi_plugin::FunctionPointers, api))>::Trace() { \
144+
CallPi<decltype(&::api), \
145+
(offsetof(pi_plugin::FunctionPointers, api))>::CallPi() { \
149146
initialize(); \
150-
m_FnPtr = (RT::PluginInformation.PiFunctionTable.api); \
151-
m_FnName = #api; \
147+
MFnPtr = (RT::PluginInformation.PiFunctionTable.api); \
148+
MFnName = #api; \
152149
}
153150
#include <CL/sycl/detail/pi.def>
154151

sycl/source/detail/sampler_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ RT::PiSampler sampler_impl::getOrCreateSampler(const context &Context) {
6262
if (errcode_ret == PI_INVALID_OPERATION)
6363
throw feature_not_supported("Images are not supported by this device.");
6464

65-
RT::piCheckResult(errcode_ret);
65+
RT::checkPiResult(errcode_ret);
6666
m_contextToSampler[Context] = resultSampler;
6767

6868
return m_contextToSampler[Context];

sycl/source/detail/usm/usm_dispatch.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pi_result USMDispatcher::enqueueMigrateMem(pi_queue Queue, const void *Ptr,
301301
CLQueue, NumEventsInWaitList,
302302
reinterpret_cast<const cl_event *>(EventWaitList),
303303
reinterpret_cast<cl_event *>(Event)));
304-
pi::piCheckResult(RetVal);
304+
pi::checkPiResult(RetVal);
305305
} else {
306306
RetVal = pi::cast<pi_result>(pfn_clEnqueueMigrateMemINTEL(
307307
CLQueue, Ptr, Size, Flags, NumEventsInWaitList,
@@ -349,19 +349,19 @@ void USMDispatcher::memAdvise(pi_queue Queue, const void *Ptr, size_t Length,
349349
if (mEmulated) {
350350
// memAdvise does nothing here
351351
// TODO: Implement a PI call for this openCL API
352-
RT::piCheckResult(RT::cast<RT::PiResult>(clEnqueueMarkerWithWaitList(
352+
RT::checkPiResult(RT::cast<RT::PiResult>(clEnqueueMarkerWithWaitList(
353353
CLQueue, 0, nullptr, reinterpret_cast<cl_event *>(Event))));
354354
} else {
355355
// Temporary until driver supports
356356
// memAdvise doesn't do anything on an iGPU anyway
357357
// TODO: Implement a PI call for this openCL API
358-
RT::piCheckResult(RT::cast<RT::PiResult>(clEnqueueMarkerWithWaitList(
358+
RT::checkPiResult(RT::cast<RT::PiResult>(clEnqueueMarkerWithWaitList(
359359
CLQueue, 0, nullptr, reinterpret_cast<cl_event *>(Event))));
360360
/*
361361
// Enable once this is supported in the driver
362362
auto CLAdvice = *reinterpret_cast<cl_mem_advice_intel *>(&Advice);
363363
// TODO: Implement a PI call for this openCL API
364-
RT::piCheckResult(RT::cast<RT::PiResult>(pfn_clEnqueueMemAdviseINTEL(
364+
RT::checkPiResult(RT::cast<RT::PiResult>(pfn_clEnqueueMemAdviseINTEL(
365365
CLQueue, Ptr, Length, CLAdvice, 0, nullptr,
366366
reinterpret_cast<cl_event *>(Event))));
367367
*/

sycl/source/detail/usm/usm_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void free(void *Ptr, const context &Ctxt) {
129129
pi_context C = CtxImpl->getHandleRef();
130130
pi_result Error = Dispatch->memFree(C, Ptr);
131131

132-
RT::piCheckResult(Error);
132+
RT::checkPiResult(Error);
133133
}
134134
}
135135

0 commit comments

Comments
 (0)