@@ -85,16 +85,18 @@ struct ol_program_impl_t {
8585 plugin::DeviceImageTy *Image;
8686 std::unique_ptr<llvm::MemoryBuffer> ImageData;
8787 std::vector<std::unique_ptr<ol_symbol_impl_t >> Symbols;
88+ std::mutex SymbolListMutex;
8889 __tgt_device_image DeviceImage;
8990};
9091
9192struct ol_symbol_impl_t {
92- ol_symbol_impl_t (GenericKernelTy *Kernel)
93- : PluginImpl(Kernel), Kind(OL_SYMBOL_KIND_KERNEL) {}
94- ol_symbol_impl_t (GlobalTy &&Global)
95- : PluginImpl(Global), Kind(OL_SYMBOL_KIND_GLOBAL_VARIABLE) {}
93+ ol_symbol_impl_t (const char *Name, GenericKernelTy *Kernel)
94+ : PluginImpl(Kernel), Kind(OL_SYMBOL_KIND_KERNEL), Name(Name) {}
95+ ol_symbol_impl_t (const char *Name, GlobalTy &&Global)
96+ : PluginImpl(Global), Kind(OL_SYMBOL_KIND_GLOBAL_VARIABLE), Name(Name) {}
9697 std::variant<GenericKernelTy *, GlobalTy> PluginImpl;
9798 ol_symbol_kind_t Kind;
99+ const char *Name;
98100};
99101
100102namespace llvm {
@@ -714,6 +716,18 @@ Error olGetSymbol_impl(ol_program_handle_t Program, const char *Name,
714716 ol_symbol_kind_t Kind, ol_symbol_handle_t *Symbol) {
715717 auto &Device = Program->Image ->getDevice ();
716718
719+ std::lock_guard<std::mutex> Lock{Program->SymbolListMutex };
720+
721+ // If it already exists, return an existing handle
722+ auto Check = std::find_if (
723+ Program->Symbols .begin (), Program->Symbols .end (), [&](auto &Sym) {
724+ return Sym->Kind == Kind && !std::strcmp (Sym->Name , Name);
725+ });
726+ if (Check != Program->Symbols .end ()) {
727+ *Symbol = Check->get ();
728+ return Error::success ();
729+ }
730+
717731 switch (Kind) {
718732 case OL_SYMBOL_KIND_KERNEL: {
719733 auto KernelImpl = Device.constructKernel (Name);
@@ -723,10 +737,10 @@ Error olGetSymbol_impl(ol_program_handle_t Program, const char *Name,
723737 if (auto Err = KernelImpl->init (Device, *Program->Image ))
724738 return Err;
725739
726- *Symbol =
727- Program-> Symbols
728- . emplace_back (std::make_unique< ol_symbol_impl_t >( &*KernelImpl))
729- .get ();
740+ *Symbol = Program-> Symbols
741+ . emplace_back (std::make_unique< ol_symbol_impl_t >(
742+ KernelImpl-> getName (), &*KernelImpl))
743+ .get ();
730744 return Error::success ();
731745 }
732746 case OL_SYMBOL_KIND_GLOBAL_VARIABLE: {
@@ -736,8 +750,8 @@ Error olGetSymbol_impl(ol_program_handle_t Program, const char *Name,
736750 return Res;
737751
738752 *Symbol = Program->Symbols
739- .emplace_back (
740- std::make_unique< ol_symbol_impl_t >( std::move (GlobalObj)))
753+ .emplace_back (std::make_unique< ol_symbol_impl_t >(
754+ GlobalObj. getName (). c_str (), std::move (GlobalObj)))
741755 .get ();
742756
743757 return Error::success ();
0 commit comments