Skip to content

Commit 659b25d

Browse files
slawekptakKornevNikita
authored andcommitted
[SYCL] Optimize getSyclObjImpl return values (#17491)
Use references as local variables to assign the return value of getSyclObjImpl.
1 parent 25a70b6 commit 659b25d

File tree

8 files changed

+29
-30
lines changed

8 files changed

+29
-30
lines changed

sycl/source/backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ kernel make_kernel(const context &TargetContext,
319319
backend Backend) {
320320
const auto &Adapter = getAdapter(Backend);
321321
const auto &ContextImpl = getSyclObjImpl(TargetContext);
322-
const auto KernelBundleImpl = getSyclObjImpl(KernelBundle);
322+
const auto &KernelBundleImpl = getSyclObjImpl(KernelBundle);
323323

324324
// For Level-Zero expect exactly one device image in the bundle. This is
325325
// natural for interop kernel to get created out of a single native

sycl/source/detail/image_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ image_channel_type convertChannelType(ur_image_channel_type_t Type) {
259259
}
260260

261261
template <typename T>
262-
static void getImageInfo(const ContextImplPtr Context, ur_image_info_t Info,
262+
static void getImageInfo(const ContextImplPtr &Context, ur_image_info_t Info,
263263
T &Dest, ur_mem_handle_t InteropMemObject) {
264264
const AdapterPtr &Adapter = Context->getAdapter();
265265
Adapter->call<UrApiKind::urMemImageGetInfo>(InteropMemObject, Info, sizeof(T),
@@ -274,7 +274,7 @@ image_impl::image_impl(cl_mem MemObject, const context &SyclContext,
274274
std::move(Allocator)),
275275
MDimensions(Dimensions), MRange({0, 0, 0}) {
276276
ur_mem_handle_t Mem = ur::cast<ur_mem_handle_t>(BaseT::MInteropMemObject);
277-
const ContextImplPtr Context = getSyclObjImpl(SyclContext);
277+
const ContextImplPtr &Context = getSyclObjImpl(SyclContext);
278278
const AdapterPtr &Adapter = Context->getAdapter();
279279
Adapter->call<UrApiKind::urMemGetInfo>(Mem, UR_MEM_INFO_SIZE, sizeof(size_t),
280280
&(BaseT::MSizeInBytes), nullptr);

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class kernel_bundle_impl {
255255
removeDuplicateImages();
256256

257257
for (const kernel_bundle<bundle_state::object> &Bundle : ObjectBundles) {
258-
const KernelBundleImplPtr BundlePtr = getSyclObjImpl(Bundle);
258+
const KernelBundleImplPtr &BundlePtr = getSyclObjImpl(Bundle);
259259
for (const std::pair<const std::string, std::vector<unsigned char>>
260260
&SpecConst : BundlePtr->MSpecConstValues) {
261261
MSpecConstValues[SpecConst.first] = SpecConst.second;
@@ -449,7 +449,7 @@ class kernel_bundle_impl {
449449
const std::string &SourceStr,
450450
ur_program_handle_t &UrProgram) {
451451
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
452-
ContextImplPtr ContextImpl = getSyclObjImpl(MContext);
452+
const ContextImplPtr &ContextImpl = getSyclObjImpl(MContext);
453453
const AdapterPtr &Adapter = ContextImpl->getAdapter();
454454

455455
std::string UserArgs = syclex::detail::userArgsAsString(BuildOptions);
@@ -494,13 +494,13 @@ class kernel_bundle_impl {
494494
"bundle_state::ext_oneapi_source required");
495495

496496
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
497-
ContextImplPtr ContextImpl = getSyclObjImpl(MContext);
497+
const ContextImplPtr &ContextImpl = getSyclObjImpl(MContext);
498498
const AdapterPtr &Adapter = ContextImpl->getAdapter();
499499

500500
std::vector<ur_device_handle_t> DeviceVec;
501501
DeviceVec.reserve(Devices.size());
502502
for (const auto &SyclDev : Devices) {
503-
DeviceImplPtr DevImpl = getSyclObjImpl(SyclDev);
503+
const DeviceImplPtr &DevImpl = getSyclObjImpl(SyclDev);
504504
if (!ContextImpl->hasDevice(DevImpl)) {
505505
throw sycl::exception(make_error_code(errc::invalid),
506506
"device not part of kernel_bundle context");
@@ -785,7 +785,7 @@ class kernel_bundle_impl {
785785
std::transform(MDeviceGlobalNames.begin(), MDeviceGlobalNames.end(),
786786
std::back_inserter(DeviceGlobalIDs),
787787
[&](const std::string &DGName) { return MPrefix + DGName; });
788-
auto ContextImpl = getSyclObjImpl(MContext);
788+
const auto &ContextImpl = getSyclObjImpl(MContext);
789789
for (DeviceGlobalMapEntry *Entry :
790790
ProgramManager::getInstance().getDeviceGlobalEntries(
791791
DeviceGlobalIDs)) {
@@ -840,7 +840,7 @@ class kernel_bundle_impl {
840840
const std::shared_ptr<detail::device_image_impl> &DeviceImageImpl =
841841
detail::getSyclObjImpl(MDeviceImages[0].getMain());
842842
ur_program_handle_t UrProgram = DeviceImageImpl->get_ur_program_ref();
843-
ContextImplPtr ContextImpl = getSyclObjImpl(MContext);
843+
const ContextImplPtr &ContextImpl = getSyclObjImpl(MContext);
844844
const AdapterPtr &Adapter = ContextImpl->getAdapter();
845845
ur_kernel_handle_t UrKernel = nullptr;
846846
Adapter->call<UrApiKind::urKernelCreate>(UrProgram, AdjustedName.c_str(),

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ ProgramManager::createURProgram(const RTDeviceBinaryImage &Img,
233233
const auto &ProgMetadata = Img.getProgramMetadataUR();
234234

235235
// Load the image
236-
const ContextImplPtr Ctx = getSyclObjImpl(Context);
236+
const ContextImplPtr &Ctx = getSyclObjImpl(Context);
237237
std::vector<const uint8_t *> Binaries(
238238
Devices.size(), const_cast<uint8_t *>(RawImg.BinaryStart));
239239
std::vector<size_t> Lengths(Devices.size(), ImgSize);
@@ -2696,7 +2696,8 @@ void ProgramManager::bringSYCLDeviceImagesToState(
26962696

26972697
for (DevImgPlainWithDeps &ImgWithDeps : DeviceImages) {
26982698
device_image_plain &MainImg = ImgWithDeps.getMain();
2699-
const bundle_state DevImageState = getSyclObjImpl(MainImg)->get_state();
2699+
const DeviceImageImplPtr &MainImgImpl = getSyclObjImpl(MainImg);
2700+
const bundle_state DevImageState = MainImgImpl->get_state();
27002701
// At this time, there is no circumstance where a device image should ever
27012702
// be in the source state. That not good.
27022703
assert(DevImageState != bundle_state::ext_oneapi_source);
@@ -2712,9 +2713,8 @@ void ProgramManager::bringSYCLDeviceImagesToState(
27122713
break;
27132714
case bundle_state::object:
27142715
if (DevImageState == bundle_state::input) {
2715-
ImgWithDeps =
2716-
compile(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2717-
/*PropList=*/{});
2716+
ImgWithDeps = compile(ImgWithDeps, MainImgImpl->get_devices(),
2717+
/*PropList=*/{});
27182718
break;
27192719
}
27202720
// Device image is expected to be object state then.
@@ -2728,7 +2728,7 @@ void ProgramManager::bringSYCLDeviceImagesToState(
27282728
assert(DevImageState != bundle_state::ext_oneapi_source);
27292729
break;
27302730
case bundle_state::input:
2731-
ImgWithDeps = build(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2731+
ImgWithDeps = build(ImgWithDeps, MainImgImpl->get_devices(),
27322732
/*PropList=*/{});
27332733
break;
27342734
case bundle_state::object: {
@@ -2742,7 +2742,7 @@ void ProgramManager::bringSYCLDeviceImagesToState(
27422742
break;
27432743
}
27442744
case bundle_state::executable:
2745-
ImgWithDeps = build(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2745+
ImgWithDeps = build(ImgWithDeps, MainImgImpl->get_devices(),
27462746
/*PropList=*/{});
27472747
break;
27482748
}
@@ -2884,7 +2884,8 @@ static void mergeImageData(const std::vector<device_image_plain> &Imgs,
28842884
std::vector<unsigned char> &NewSpecConstBlob,
28852885
device_image_impl::SpecConstMapT &NewSpecConstMap) {
28862886
for (const device_image_plain &Img : Imgs) {
2887-
std::shared_ptr<device_image_impl> DeviceImageImpl = getSyclObjImpl(Img);
2887+
const std::shared_ptr<device_image_impl> &DeviceImageImpl =
2888+
getSyclObjImpl(Img);
28882889
// Duplicates are not expected here, otherwise urProgramLink should fail
28892890
if (DeviceImageImpl->get_kernel_ids_ptr())
28902891
KernelIDs.insert(KernelIDs.end(),
@@ -2946,16 +2947,15 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
29462947
std::string LinkOptionsStr;
29472948
applyLinkOptionsFromEnvironment(LinkOptionsStr);
29482949
const device_image_plain &MainImg = ImgWithDeps.getMain();
2950+
const std::shared_ptr<device_image_impl> &InputImpl = getSyclObjImpl(MainImg);
29492951
if (LinkOptionsStr.empty()) {
2950-
const std::shared_ptr<device_image_impl> &InputImpl =
2951-
getSyclObjImpl(MainImg);
29522952
appendLinkOptionsFromImage(LinkOptionsStr,
29532953
*(InputImpl->get_bin_image_ref()));
29542954
}
29552955
// Should always come last!
29562956
appendLinkEnvironmentVariablesThatAppend(LinkOptionsStr);
2957-
const context &Context = getSyclObjImpl(MainImg)->get_context();
2958-
const ContextImplPtr ContextImpl = getSyclObjImpl(Context);
2957+
const context &Context = InputImpl->get_context();
2958+
const ContextImplPtr &ContextImpl = getSyclObjImpl(Context);
29592959
const AdapterPtr &Adapter = ContextImpl->getAdapter();
29602960

29612961
ur_program_handle_t LinkedProg = nullptr;
@@ -2981,8 +2981,7 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
29812981

29822982
if (Error != UR_RESULT_SUCCESS) {
29832983
if (LinkedProg) {
2984-
const std::string ErrorMsg =
2985-
getProgramBuildLog(LinkedProg, std::move(ContextImpl));
2984+
const std::string ErrorMsg = getProgramBuildLog(LinkedProg, ContextImpl);
29862985
throw sycl::exception(make_error_code(errc::build), ErrorMsg);
29872986
}
29882987
throw set_ur_error(exception(make_error_code(errc::build), "link() failed"),
@@ -3008,7 +3007,7 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
30083007
}
30093008
}
30103009

3011-
auto BinImg = getSyclObjImpl(MainImg)->get_bin_image_ref();
3010+
auto BinImg = InputImpl->get_bin_image_ref();
30123011
DeviceImageImplPtr ExecutableImpl =
30133012
std::make_shared<detail::device_image_impl>(
30143013
BinImg, Context, Devs, bundle_state::executable, std::move(KernelIDs),
@@ -3037,7 +3036,6 @@ ProgramManager::build(const DevImgPlainWithDeps &DevImgWithDeps,
30373036
getSyclObjImpl(DevImgWithDeps.getMain());
30383037

30393038
const context Context = MainInputImpl->get_context();
3040-
const ContextImplPtr ContextImpl = getSyclObjImpl(Context);
30413039

30423040
std::vector<const RTDeviceBinaryImage *> BinImgs;
30433041
BinImgs.reserve(DevImgWithDeps.size());
@@ -3089,7 +3087,7 @@ ProgramManager::getOrCreateKernel(const context &Context,
30893087
PropList, NoAllowedPropertiesCheck, NoAllowedPropertiesCheck);
30903088
}
30913089

3092-
const ContextImplPtr Ctx = getSyclObjImpl(Context);
3090+
const ContextImplPtr &Ctx = getSyclObjImpl(Context);
30933091

30943092
KernelProgramCache &Cache = Ctx->getKernelProgramCache();
30953093

sycl/source/detail/queue_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ sycl::detail::optional<event> queue_impl::getLastEvent() {
303303
}
304304

305305
void queue_impl::addEvent(const event &Event) {
306-
EventImplPtr EImpl = getSyclObjImpl(Event);
306+
const EventImplPtr &EImpl = getSyclObjImpl(Event);
307307
assert(EImpl && "Event implementation is missing");
308308
auto *Cmd = static_cast<Command *>(EImpl->getCommand());
309309
if (!Cmd) {

sycl/source/detail/queue_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ class queue_impl {
848848
Handler.depends_on(Deps.LastBarrier);
849849

850850
auto EventRet = Handler.finalize();
851-
EventImplPtr EventRetImpl = getSyclObjImpl(EventRet);
851+
const EventImplPtr &EventRetImpl = getSyclObjImpl(EventRet);
852852
if (Type == CGType::CodeplayHostTask)
853853
Deps.UnenqueuedCmdEvents.push_back(std::move(EventRetImpl));
854854
else if (Type == CGType::Barrier || Type == CGType::BarrierWaitlist) {

sycl/source/detail/ur.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace pi {
4949
void contextSetExtendedDeleter(const sycl::context &context,
5050
pi_context_extended_deleter func,
5151
void *user_data) {
52-
auto impl = getSyclObjImpl(context);
52+
const auto &impl = getSyclObjImpl(context);
5353
const auto &Adapter = impl->getAdapter();
5454
Adapter->call<UrApiKind::urContextSetExtendedDeleter>(
5555
impl->getHandleRef(),

sycl/source/kernel_bundle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ exe_kb build_from_source(
481481
LogPtr = &Log;
482482
std::vector<device> UniqueDevices =
483483
sycl::detail::removeDuplicateDevices(Devices);
484-
std::shared_ptr<kernel_bundle_impl> sourceImpl = getSyclObjImpl(SourceKB);
484+
const std::shared_ptr<kernel_bundle_impl> &sourceImpl =
485+
getSyclObjImpl(SourceKB);
485486
std::shared_ptr<kernel_bundle_impl> KBImpl = sourceImpl->build_from_source(
486487
UniqueDevices, Options, LogPtr, KernelNames);
487488
auto result = sycl::detail::createSyclObjFromImpl<exe_kb>(std::move(KBImpl));

0 commit comments

Comments
 (0)