@@ -84,17 +84,20 @@ struct ol_program_impl_t {
84
84
DeviceImage (DeviceImage) {}
85
85
plugin::DeviceImageTy *Image;
86
86
std::unique_ptr<llvm::MemoryBuffer> ImageData;
87
- std::vector<std::unique_ptr< ol_symbol_impl_t >> Symbols ;
87
+ std::mutex SymbolListMutex ;
88
88
__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;
89
91
};
90
92
91
93
struct 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) {}
94
+ ol_symbol_impl_t (const char *Name, GenericKernelTy *Kernel)
95
+ : PluginImpl(Kernel), Kind(OL_SYMBOL_KIND_KERNEL), Name(Name) {}
96
+ ol_symbol_impl_t (const char *Name, GlobalTy &&Global)
97
+ : PluginImpl(Global), Kind(OL_SYMBOL_KIND_GLOBAL_VARIABLE), Name(Name) {}
96
98
std::variant<GenericKernelTy *, GlobalTy> PluginImpl;
97
99
ol_symbol_kind_t Kind;
100
+ llvm::StringRef Name;
98
101
};
99
102
100
103
namespace llvm {
@@ -714,32 +717,40 @@ Error olGetSymbol_impl(ol_program_handle_t Program, const char *Name,
714
717
ol_symbol_kind_t Kind, ol_symbol_handle_t *Symbol) {
715
718
auto &Device = Program->Image ->getDevice ();
716
719
720
+ std::lock_guard<std::mutex> Lock{Program->SymbolListMutex };
721
+
717
722
switch (Kind) {
718
723
case OL_SYMBOL_KIND_KERNEL: {
719
- auto KernelImpl = Device.constructKernel (Name);
720
- if (!KernelImpl)
721
- return KernelImpl.takeError ();
724
+ auto &Kernel = Program->KernelSymbols [Name];
725
+ if (!Kernel) {
726
+ auto KernelImpl = Device.constructKernel (Name);
727
+ if (!KernelImpl)
728
+ return KernelImpl.takeError ();
722
729
723
- if (auto Err = KernelImpl->init (Device, *Program->Image ))
724
- return Err;
730
+ if (auto Err = KernelImpl->init (Device, *Program->Image ))
731
+ return Err;
732
+
733
+ Kernel = std::make_unique<ol_symbol_impl_t >(KernelImpl->getName (),
734
+ &*KernelImpl);
735
+ }
725
736
726
- *Symbol =
727
- Program->Symbols
728
- .emplace_back (std::make_unique<ol_symbol_impl_t >(&*KernelImpl))
729
- .get ();
737
+ *Symbol = Kernel.get ();
730
738
return Error::success ();
731
739
}
732
740
case OL_SYMBOL_KIND_GLOBAL_VARIABLE: {
733
- GlobalTy GlobalObj{Name};
734
- if (auto Res = Device.Plugin .getGlobalHandler ().getGlobalMetadataFromDevice (
735
- Device, *Program->Image , GlobalObj))
736
- return Res;
737
-
738
- *Symbol = Program->Symbols
739
- .emplace_back (
740
- std::make_unique<ol_symbol_impl_t >(std::move (GlobalObj)))
741
- .get ();
741
+ auto &Global = Program->KernelSymbols [Name];
742
+ if (!Global) {
743
+ GlobalTy GlobalObj{Name};
744
+ if (auto Res =
745
+ Device.Plugin .getGlobalHandler ().getGlobalMetadataFromDevice (
746
+ Device, *Program->Image , GlobalObj))
747
+ return Res;
748
+
749
+ Global = std::make_unique<ol_symbol_impl_t >(GlobalObj.getName ().c_str (),
750
+ std::move (GlobalObj));
751
+ }
742
752
753
+ *Symbol = Global.get ();
743
754
return Error::success ();
744
755
}
745
756
default :
0 commit comments