@@ -57,42 +57,17 @@ Error L0ProgramTy::deinit() {
5757 return Plugin::success ();
5858}
5959
60- void L0ProgramTy::setLibModule () {
61- #if _WIN32
62- return ;
63- #else
64- // Check if the image belongs to a dynamic library
65- Dl_info DLI{nullptr , nullptr , nullptr , nullptr };
66- if (dladdr (getStart (), &DLI) && DLI.dli_fname ) {
67- std::vector<uint8_t > FileBin;
68- auto Size = readFile (DLI.dli_fname , FileBin);
69- if (Size) {
70- auto MB = MemoryBuffer::getMemBuffer (
71- StringRef (reinterpret_cast <const char *>(FileBin.data ()), Size),
72- /* BufferName=*/ " " , /* RequiresNullTerminator=*/ false );
73- auto ELF = ELFObjectFileBase::createELFObjectFile (MB->getMemBufferRef ());
74- if (ELF) {
75- if (auto *Obj = dyn_cast<ELF64LEObjectFile>((*ELF).get ())) {
76- const auto Header = Obj->getELFFile ().getHeader ();
77- if (Header.e_type == ELF::ET_DYN) {
78- DP (" Processing current image as library\n " );
79- IsLibModule = true ;
80- }
81- }
82- }
83- }
84- }
85- #endif // _WIN32
86- }
87-
88- Error L0ProgramTy::addModule (size_t Size, const uint8_t *Image,
89- const std::string_view CommonBuildOptions,
90- ze_module_format_t Format) {
60+ Error L0ProgramBuilderTy::addModule (size_t Size, const uint8_t *Image,
61+ const std::string_view CommonBuildOptions,
62+ ze_module_format_t Format) {
9163 const ze_module_constants_t SpecConstants =
9264 LevelZeroPluginTy::getOptions ().CommonSpecConstants .getModuleConstants ();
9365 auto &l0Device = getL0Device ();
9466 std::string BuildOptions (CommonBuildOptions);
9567
68+ bool IsLibModule =
69+ BuildOptions.find (" -library-compilation" ) != std::string::npos;
70+
9671 // Add required flag to enable dynamic linking.
9772 if (IsLibModule)
9873 BuildOptions += " -library-compilation " ;
@@ -128,7 +103,7 @@ Error L0ProgramTy::addModule(size_t Size, const uint8_t *Image,
128103 return Plugin::success ();
129104}
130105
131- Error L0ProgramTy ::linkModules () {
106+ Error L0ProgramBuilderTy ::linkModules () {
132107 auto &l0Device = getL0Device ();
133108 if (!RequiresModuleLink) {
134109 DP (" Module link is not required\n " );
@@ -147,24 +122,8 @@ Error L0ProgramTy::linkModules() {
147122 return Plugin::success ();
148123}
149124
150- size_t L0ProgramTy::readFile (const char *FileName,
151- std::vector<uint8_t > &OutFile) const {
152- std::ifstream IFS (FileName, std::ios::binary);
153- if (!IFS.good ())
154- return 0 ;
155- IFS.seekg (0 , IFS.end );
156- auto FileSize = static_cast <size_t >(IFS.tellg ());
157- OutFile.resize (FileSize);
158- IFS.seekg (0 );
159- if (!IFS.read (reinterpret_cast <char *>(OutFile.data ()), FileSize)) {
160- OutFile.clear ();
161- return 0 ;
162- }
163- return FileSize;
164- }
165-
166- void L0ProgramTy::replaceDriverOptsWithBackendOpts (const L0DeviceTy &Device,
167- std::string &Options) const {
125+ static void replaceDriverOptsWithBackendOpts (const L0DeviceTy &Device,
126+ std::string &Options) {
168127 // Options that need to be replaced with backend-specific options
169128 static const struct {
170129 std::string Option;
@@ -256,7 +215,7 @@ bool isValidOneOmpImage(StringRef Image, uint64_t &MajorVer,
256215 return Res;
257216}
258217
259- Error L0ProgramTy ::buildModules (const std::string_view BuildOptions) {
218+ Error L0ProgramBuilderTy ::buildModules (const std::string_view BuildOptions) {
260219 auto &l0Device = getL0Device ();
261220 auto Image = getMemoryBuffer ();
262221 if (identify_magic (Image.getBuffer ()) == file_magic::spirv_object) {
@@ -272,8 +231,6 @@ Error L0ProgramTy::buildModules(const std::string_view BuildOptions) {
272231 return Plugin::error (ErrorCode::UNKNOWN, " Invalid oneAPI OpenMP image" );
273232 }
274233
275- setLibModule ();
276-
277234 // Iterate over the images and pick the first one that fits.
278235 uint64_t ImageCount = 0 ;
279236 struct V1ImageInfo {
@@ -473,12 +430,32 @@ Error L0ProgramTy::buildModules(const std::string_view BuildOptions) {
473430 }
474431 DP (" Created module from image #%" PRIu64 " .\n " , Idx);
475432
433+ if (RequiresModuleLink) {
434+ DP (" Linking modules after adding image #%" PRIu64 " .\n " , Idx);
435+ if (auto Err = linkModules ())
436+ return Err;
437+ }
438+
476439 return Plugin::success ();
477440 }
478441
479442 return Plugin::error(ErrorCode::UNKNOWN, " Failed to create program modules." );
480443}
481444
445+ Expected<std::unique_ptr<MemoryBuffer>> L0ProgramBuilderTy::getELF () {
446+ assert (GlobalModule != nullptr && " GlobalModule is null" );
447+
448+ size_t Size = 0 ;
449+
450+ CALL_ZE_RET_ERROR (zeModuleGetNativeBinary, GlobalModule, &Size, nullptr );
451+ std::vector<uint8_t > ELFData (Size);
452+ CALL_ZE_RET_ERROR (zeModuleGetNativeBinary, GlobalModule, &Size,
453+ ELFData.data ());
454+ return MemoryBuffer::getMemBufferCopy (
455+ StringRef (reinterpret_cast <const char *>(ELFData.data ()), Size),
456+ /* BufferName=*/ " L0Program ELF" );
457+ }
458+
482459Expected<void *> L0ProgramTy::getOffloadVarDeviceAddr (const char *CName) const {
483460 DP (" Looking up OpenMP global variable '%s'.\n " , CName);
484461
0 commit comments