From 1dd748209afcff815dc65cf3e7b7b10b08379a61 Mon Sep 17 00:00:00 2001 From: Andrei Elovikov Date: Wed, 25 Jun 2025 08:27:51 -0700 Subject: [PATCH] [NFC][SYCL] Use `context_impl &` in `sampler_impl` ctor and near it `SetArgBasedOnType` argument is only used to pass to the `sampler_impl` ctor so update it. `getCGKernelInfo` is only called in a function that also calls `sampler_impl` ctor so updating its signuature allows to update that caller's local `ContextImpl` variable, so makes sense to do as part of this PR as well. Continuation of the refactoring in https://github.com/intel/llvm/pull/18795 https://github.com/intel/llvm/pull/18877 https://github.com/intel/llvm/pull/18966 https://github.com/intel/llvm/pull/18979 https://github.com/intel/llvm/pull/18980 https://github.com/intel/llvm/pull/18981 https://github.com/intel/llvm/pull/19007 https://github.com/intel/llvm/pull/19030 https://github.com/intel/llvm/pull/19123 https://github.com/intel/llvm/pull/19126 --- sycl/source/detail/sampler_impl.cpp | 23 +++++++++++++---------- sycl/source/detail/sampler_impl.hpp | 8 ++++---- sycl/source/detail/scheduler/commands.cpp | 13 ++++++------- sycl/source/detail/scheduler/commands.hpp | 4 +--- sycl/source/sampler.cpp | 2 +- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/sycl/source/detail/sampler_impl.cpp b/sycl/source/detail/sampler_impl.cpp index 108d83b46cefb..4aae3821fbd8c 100644 --- a/sycl/source/detail/sampler_impl.cpp +++ b/sycl/source/detail/sampler_impl.cpp @@ -24,15 +24,14 @@ sampler_impl::sampler_impl(coordinate_normalization_mode normalizationMode, verifyProps(MPropList); } -sampler_impl::sampler_impl(cl_sampler clSampler, - const ContextImplPtr &syclContext) { - const AdapterPtr &Adapter = syclContext->getAdapter(); +sampler_impl::sampler_impl(cl_sampler clSampler, context_impl &syclContext) { + const AdapterPtr &Adapter = syclContext.getAdapter(); ur_sampler_handle_t Sampler{}; Adapter->call( reinterpret_cast(clSampler), - syclContext->getHandleRef(), nullptr, &Sampler); + syclContext.getHandleRef(), nullptr, &Sampler); - MContextToSampler[syclContext] = Sampler; + MContextToSampler[syclContext.shared_from_this()] = Sampler; bool NormalizedCoords; Adapter->call( @@ -95,10 +94,14 @@ sampler_impl::~sampler_impl() { } ur_sampler_handle_t -sampler_impl::getOrCreateSampler(const ContextImplPtr &ContextImpl) { +sampler_impl::getOrCreateSampler(context_impl &ContextImpl) { + // Just for the `MContextToSampler` lookups. Could probably be changed once we + // move to C++20 and would have heterogeneous lookup. + std::shared_ptr ContextImplPtr = ContextImpl.shared_from_this(); + { std::lock_guard Lock(MMutex); - auto It = MContextToSampler.find(ContextImpl); + auto It = MContextToSampler.find(ContextImplPtr); if (It != MContextToSampler.end()) return It->second; } @@ -135,10 +138,10 @@ sampler_impl::getOrCreateSampler(const ContextImplPtr &ContextImpl) { ur_result_t errcode_ret = UR_RESULT_SUCCESS; ur_sampler_handle_t resultSampler = nullptr; - const AdapterPtr &Adapter = ContextImpl->getAdapter(); + const AdapterPtr &Adapter = ContextImpl.getAdapter(); errcode_ret = Adapter->call_nocheck( - ContextImpl->getHandleRef(), &desc, &resultSampler); + ContextImpl.getHandleRef(), &desc, &resultSampler); if (errcode_ret == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) throw sycl::exception(sycl::errc::feature_not_supported, @@ -146,7 +149,7 @@ sampler_impl::getOrCreateSampler(const ContextImplPtr &ContextImpl) { Adapter->checkUrResult(errcode_ret); std::lock_guard Lock(MMutex); - MContextToSampler[ContextImpl] = resultSampler; + MContextToSampler[ContextImplPtr] = resultSampler; return resultSampler; } diff --git a/sycl/source/detail/sampler_impl.hpp b/sycl/source/detail/sampler_impl.hpp index 0abc6a7ad273b..e69d24548d602 100644 --- a/sycl/source/detail/sampler_impl.hpp +++ b/sycl/source/detail/sampler_impl.hpp @@ -30,7 +30,6 @@ enum class coordinate_normalization_mode : unsigned int; namespace detail { class context_impl; -using ContextImplPtr = std::shared_ptr; class sampler_impl { public: @@ -38,7 +37,7 @@ class sampler_impl { addressing_mode addressingMode, filtering_mode filteringMode, const property_list &propList); - sampler_impl(cl_sampler clSampler, const ContextImplPtr &syclContext); + sampler_impl(cl_sampler clSampler, context_impl &syclContext); addressing_mode get_addressing_mode() const; @@ -46,7 +45,7 @@ class sampler_impl { coordinate_normalization_mode get_coordinate_normalization_mode() const; - ur_sampler_handle_t getOrCreateSampler(const ContextImplPtr &ContextImpl); + ur_sampler_handle_t getOrCreateSampler(context_impl &ContextImpl); ~sampler_impl(); @@ -56,7 +55,8 @@ class sampler_impl { /// Protects all the fields that can be changed by class' methods. std::mutex MMutex; - std::unordered_map MContextToSampler; + std::unordered_map, ur_sampler_handle_t> + MContextToSampler; coordinate_normalization_mode MCoordNormMode; addressing_mode MAddrMode; diff --git a/sycl/source/detail/scheduler/commands.cpp b/sycl/source/detail/scheduler/commands.cpp index 835fdc535f6b7..f159bf5e41832 100644 --- a/sycl/source/detail/scheduler/commands.cpp +++ b/sycl/source/detail/scheduler/commands.cpp @@ -2313,8 +2313,7 @@ void SetArgBasedOnType( const AdapterPtr &Adapter, ur_kernel_handle_t Kernel, const std::shared_ptr &DeviceImageImpl, const std::function &getMemAllocationFunc, - const ContextImplPtr &ContextImpl, detail::ArgDesc &Arg, - size_t NextTrueIndex) { + context_impl &ContextImpl, detail::ArgDesc &Arg, size_t NextTrueIndex) { switch (Arg.MType) { case kernel_param_kind_t::kind_dynamic_work_group_memory: break; @@ -2442,7 +2441,7 @@ static ur_result_t SetKernelParamsAndLaunch( auto setFunc = [&Adapter, Kernel, &DeviceImageImpl, &getMemAllocationFunc, &Queue](detail::ArgDesc &Arg, size_t NextTrueIndex) { SetArgBasedOnType(Adapter, Kernel, DeviceImageImpl, getMemAllocationFunc, - Queue.getContextImplPtr(), Arg, NextTrueIndex); + Queue.getContextImpl(), Arg, NextTrueIndex); }; applyFuncOnFilteredArgs(EliminatedArgMask, Args, setFunc); } @@ -2530,7 +2529,7 @@ static ur_result_t SetKernelParamsAndLaunch( static std::tuple, const KernelArgMask *> -getCGKernelInfo(const CGExecKernel &CommandGroup, ContextImplPtr ContextImpl, +getCGKernelInfo(const CGExecKernel &CommandGroup, context_impl &ContextImpl, device_impl &DeviceImpl, std::vector &KernelCacheValsToRelease) { @@ -2552,7 +2551,7 @@ getCGKernelInfo(const CGExecKernel &CommandGroup, ContextImplPtr ContextImpl, } else { FastKernelCacheValPtr FastKernelCacheVal = sycl::detail::ProgramManager::getInstance().getOrCreateKernel( - *ContextImpl, DeviceImpl, CommandGroup.MKernelName, + ContextImpl, DeviceImpl, CommandGroup.MKernelName, CommandGroup.MKernelNameBasedCachePtr); UrKernel = FastKernelCacheVal->MKernelHandle; EliminatedArgMask = FastKernelCacheVal->MKernelArgMask; @@ -2579,7 +2578,7 @@ ur_result_t enqueueImpCommandBufferKernel( std::shared_ptr DeviceImageImpl = nullptr; const KernelArgMask *EliminatedArgMask = nullptr; - auto ContextImpl = sycl::detail::getSyclObjImpl(Ctx); + context_impl &ContextImpl = *sycl::detail::getSyclObjImpl(Ctx); std::tie(UrKernel, DeviceImageImpl, EliminatedArgMask) = getCGKernelInfo( CommandGroup, ContextImpl, DeviceImpl, FastKernelCacheValsToRelease); @@ -2599,7 +2598,7 @@ ur_result_t enqueueImpCommandBufferKernel( AltUrKernels.push_back(AltUrKernel); } - const sycl::detail::AdapterPtr &Adapter = ContextImpl->getAdapter(); + const sycl::detail::AdapterPtr &Adapter = ContextImpl.getAdapter(); auto SetFunc = [&Adapter, &UrKernel, &DeviceImageImpl, &ContextImpl, &getMemAllocationFunc](sycl::detail::ArgDesc &Arg, size_t NextTrueIndex) { diff --git a/sycl/source/detail/scheduler/commands.hpp b/sycl/source/detail/scheduler/commands.hpp index a54fc7e231284..c09d014d6e1c9 100644 --- a/sycl/source/detail/scheduler/commands.hpp +++ b/sycl/source/detail/scheduler/commands.hpp @@ -44,7 +44,6 @@ class context_impl; class DispatchHostTask; using EventImplPtr = std::shared_ptr; -using ContextImplPtr = std::shared_ptr; using StreamImplPtr = std::shared_ptr; class Command; @@ -749,8 +748,7 @@ void SetArgBasedOnType( const detail::AdapterPtr &Adapter, ur_kernel_handle_t Kernel, const std::shared_ptr &DeviceImageImpl, const std::function &getMemAllocationFunc, - const ContextImplPtr &ContextImpl, detail::ArgDesc &Arg, - size_t NextTrueIndex); + context_impl &ContextImpl, detail::ArgDesc &Arg, size_t NextTrueIndex); template void applyFuncOnFilteredArgs(const KernelArgMask *EliminatedArgMask, diff --git a/sycl/source/sampler.cpp b/sycl/source/sampler.cpp index 62ef4fcfed688..a3029931469bd 100644 --- a/sycl/source/sampler.cpp +++ b/sycl/source/sampler.cpp @@ -22,7 +22,7 @@ sampler::sampler(coordinate_normalization_mode normalizationMode, sampler::sampler(cl_sampler clSampler, const context &syclContext) : impl(std::make_shared( - clSampler, detail::getSyclObjImpl(syclContext))) {} + clSampler, *detail::getSyclObjImpl(syclContext))) {} addressing_mode sampler::get_addressing_mode() const { return impl->get_addressing_mode();