@@ -84,9 +84,10 @@ struct ol_program_impl_t {
8484 DeviceImage (DeviceImage) {}
8585 plugin::DeviceImageTy *Image;
8686 std::unique_ptr<llvm::MemoryBuffer> ImageData;
87- llvm::SmallVector<std::unique_ptr<ol_symbol_impl_t >> Symbols;
8887 std::mutex SymbolListMutex;
8988 __tgt_device_image DeviceImage;
89+ llvm::StringMap<std::unique_ptr<ol_symbol_impl_t >> KernelSymbols;
90+ llvm::StringMap<std::unique_ptr<ol_symbol_impl_t >> GlobalSymbols;
9091};
9192
9293struct ol_symbol_impl_t {
@@ -719,38 +720,50 @@ Error olGetSymbol_impl(ol_program_handle_t Program, const char *Name,
719720 std::lock_guard<std::mutex> Lock{Program->SymbolListMutex };
720721
721722 // If it already exists, return an existing handle
722- auto Check = llvm::find_if (Program->Symbols , [&](auto &Sym) {
723- return Sym->Kind == Kind && Sym->Name == Name;
724- });
725- if (Check != Program->Symbols .end ()) {
726- *Symbol = Check->get ();
727- return Error::success ();
728- }
723+ auto CheckCache = [&](StringMap<std::unique_ptr<ol_symbol_impl_t >> &Map)
724+ -> std::optional<ol_symbol_handle_t > {
725+ if (Map.contains (Name))
726+ return Map[Name].get ();
727+ return std::nullopt ;
728+ };
729729
730730 switch (Kind) {
731731 case OL_SYMBOL_KIND_KERNEL: {
732+ if (auto Cache = CheckCache (Program->KernelSymbols )) {
733+ *Symbol = *Cache;
734+ return Plugin::success ();
735+ }
736+
732737 auto KernelImpl = Device.constructKernel (Name);
733738 if (!KernelImpl)
734739 return KernelImpl.takeError ();
735740
736741 if (auto Err = KernelImpl->init (Device, *Program->Image ))
737742 return Err;
738743
739- *Symbol = Program->Symbols
740- .emplace_back (std::make_unique<ol_symbol_impl_t >(
741- KernelImpl->getName (), &*KernelImpl))
744+ *Symbol = Program->KernelSymbols
745+ .insert ({Name, std::make_unique<ol_symbol_impl_t >(
746+ KernelImpl->getName (), &*KernelImpl)})
747+ .first ->getValue ()
742748 .get ();
743749 return Error::success ();
744750 }
745751 case OL_SYMBOL_KIND_GLOBAL_VARIABLE: {
752+ if (auto Cache = CheckCache (Program->GlobalSymbols )) {
753+ *Symbol = *Cache;
754+ return Plugin::success ();
755+ }
756+
746757 GlobalTy GlobalObj{Name};
747758 if (auto Res = Device.Plugin .getGlobalHandler ().getGlobalMetadataFromDevice (
748759 Device, *Program->Image , GlobalObj))
749760 return Res;
750761
751- *Symbol = Program->Symbols
752- .emplace_back (std::make_unique<ol_symbol_impl_t >(
753- GlobalObj.getName ().c_str (), std::move (GlobalObj)))
762+ *Symbol = Program->GlobalSymbols
763+ .insert ({Name, std::make_unique<ol_symbol_impl_t >(
764+ GlobalObj.getName ().c_str (),
765+ std::move (GlobalObj))})
766+ .first ->getValue ()
754767 .get ();
755768
756769 return Error::success ();
0 commit comments