Skip to content

Commit 732abd1

Browse files
authored
[SYCL] Optimize getSyclObjImpl return values (#17491)
Use references as local variables to assign the return value of getSyclObjImpl.
1 parent 015ad4b commit 732abd1

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);
@@ -2673,7 +2673,8 @@ void ProgramManager::bringSYCLDeviceImagesToState(
26732673

26742674
for (DevImgPlainWithDeps &ImgWithDeps : DeviceImages) {
26752675
device_image_plain &MainImg = ImgWithDeps.getMain();
2676-
const bundle_state DevImageState = getSyclObjImpl(MainImg)->get_state();
2676+
const DeviceImageImplPtr &MainImgImpl = getSyclObjImpl(MainImg);
2677+
const bundle_state DevImageState = MainImgImpl->get_state();
26772678
// At this time, there is no circumstance where a device image should ever
26782679
// be in the source state. That not good.
26792680
assert(DevImageState != bundle_state::ext_oneapi_source);
@@ -2689,9 +2690,8 @@ void ProgramManager::bringSYCLDeviceImagesToState(
26892690
break;
26902691
case bundle_state::object:
26912692
if (DevImageState == bundle_state::input) {
2692-
ImgWithDeps =
2693-
compile(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2694-
/*PropList=*/{});
2693+
ImgWithDeps = compile(ImgWithDeps, MainImgImpl->get_devices(),
2694+
/*PropList=*/{});
26952695
break;
26962696
}
26972697
// Device image is expected to be object state then.
@@ -2705,7 +2705,7 @@ void ProgramManager::bringSYCLDeviceImagesToState(
27052705
assert(DevImageState != bundle_state::ext_oneapi_source);
27062706
break;
27072707
case bundle_state::input:
2708-
ImgWithDeps = build(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2708+
ImgWithDeps = build(ImgWithDeps, MainImgImpl->get_devices(),
27092709
/*PropList=*/{});
27102710
break;
27112711
case bundle_state::object: {
@@ -2719,7 +2719,7 @@ void ProgramManager::bringSYCLDeviceImagesToState(
27192719
break;
27202720
}
27212721
case bundle_state::executable:
2722-
ImgWithDeps = build(ImgWithDeps, getSyclObjImpl(MainImg)->get_devices(),
2722+
ImgWithDeps = build(ImgWithDeps, MainImgImpl->get_devices(),
27232723
/*PropList=*/{});
27242724
break;
27252725
}
@@ -2861,7 +2861,8 @@ static void mergeImageData(const std::vector<device_image_plain> &Imgs,
28612861
std::vector<unsigned char> &NewSpecConstBlob,
28622862
device_image_impl::SpecConstMapT &NewSpecConstMap) {
28632863
for (const device_image_plain &Img : Imgs) {
2864-
std::shared_ptr<device_image_impl> DeviceImageImpl = getSyclObjImpl(Img);
2864+
const std::shared_ptr<device_image_impl> &DeviceImageImpl =
2865+
getSyclObjImpl(Img);
28652866
// Duplicates are not expected here, otherwise urProgramLink should fail
28662867
KernelIDs.insert(KernelIDs.end(),
28672868
DeviceImageImpl->get_kernel_ids_ptr()->begin(),
@@ -2922,16 +2923,15 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
29222923
std::string LinkOptionsStr;
29232924
applyLinkOptionsFromEnvironment(LinkOptionsStr);
29242925
const device_image_plain &MainImg = ImgWithDeps.getMain();
2926+
const std::shared_ptr<device_image_impl> &InputImpl = getSyclObjImpl(MainImg);
29252927
if (LinkOptionsStr.empty()) {
2926-
const std::shared_ptr<device_image_impl> &InputImpl =
2927-
getSyclObjImpl(MainImg);
29282928
appendLinkOptionsFromImage(LinkOptionsStr,
29292929
*(InputImpl->get_bin_image_ref()));
29302930
}
29312931
// Should always come last!
29322932
appendLinkEnvironmentVariablesThatAppend(LinkOptionsStr);
2933-
const context &Context = getSyclObjImpl(MainImg)->get_context();
2934-
const ContextImplPtr ContextImpl = getSyclObjImpl(Context);
2933+
const context &Context = InputImpl->get_context();
2934+
const ContextImplPtr &ContextImpl = getSyclObjImpl(Context);
29352935
const AdapterPtr &Adapter = ContextImpl->getAdapter();
29362936

29372937
ur_program_handle_t LinkedProg = nullptr;
@@ -2957,8 +2957,7 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
29572957

29582958
if (Error != UR_RESULT_SUCCESS) {
29592959
if (LinkedProg) {
2960-
const std::string ErrorMsg =
2961-
getProgramBuildLog(LinkedProg, std::move(ContextImpl));
2960+
const std::string ErrorMsg = getProgramBuildLog(LinkedProg, ContextImpl);
29622961
throw sycl::exception(make_error_code(errc::build), ErrorMsg);
29632962
}
29642963
throw set_ur_error(exception(make_error_code(errc::build), "link() failed"),
@@ -2984,7 +2983,7 @@ ProgramManager::link(const DevImgPlainWithDeps &ImgWithDeps,
29842983
}
29852984
}
29862985

2987-
auto BinImg = getSyclObjImpl(MainImg)->get_bin_image_ref();
2986+
auto BinImg = InputImpl->get_bin_image_ref();
29882987
DeviceImageImplPtr ExecutableImpl =
29892988
std::make_shared<detail::device_image_impl>(
29902989
BinImg, Context, Devs, bundle_state::executable, std::move(KernelIDs),
@@ -3013,7 +3012,6 @@ ProgramManager::build(const DevImgPlainWithDeps &DevImgWithDeps,
30133012
getSyclObjImpl(DevImgWithDeps.getMain());
30143013

30153014
const context Context = MainInputImpl->get_context();
3016-
const ContextImplPtr ContextImpl = getSyclObjImpl(Context);
30173015

30183016
std::vector<const RTDeviceBinaryImage *> BinImgs;
30193017
BinImgs.reserve(DevImgWithDeps.size());
@@ -3065,7 +3063,7 @@ ProgramManager::getOrCreateKernel(const context &Context,
30653063
PropList, NoAllowedPropertiesCheck, NoAllowedPropertiesCheck);
30663064
}
30673065

3068-
const ContextImplPtr Ctx = getSyclObjImpl(Context);
3066+
const ContextImplPtr &Ctx = getSyclObjImpl(Context);
30693067

30703068
KernelProgramCache &Cache = Ctx->getKernelProgramCache();
30713069

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(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)