@@ -57,45 +57,16 @@ 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
96- // Add required flag to enable dynamic linking.
97- if (IsLibModule)
98- BuildOptions += " -library-compilation " ;
68+ bool IsLibModule =
69+ BuildOptions.find (" -library-compilation" ) != std::string::npos;
9970
10071 ze_module_desc_t ModuleDesc{};
10172 ModuleDesc.stype = ZE_STRUCTURE_TYPE_MODULE_DESC;
@@ -128,7 +99,7 @@ Error L0ProgramTy::addModule(size_t Size, const uint8_t *Image,
12899 return Plugin::success ();
129100}
130101
131- Error L0ProgramTy ::linkModules () {
102+ Error L0ProgramBuilderTy ::linkModules () {
132103 auto &l0Device = getL0Device ();
133104 if (!RequiresModuleLink) {
134105 DP (" Module link is not required\n " );
@@ -147,24 +118,8 @@ Error L0ProgramTy::linkModules() {
147118 return Plugin::success ();
148119}
149120
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 {
121+ static void replaceDriverOptsWithBackendOpts (const L0DeviceTy &Device,
122+ std::string &Options) {
168123 // Options that need to be replaced with backend-specific options
169124 static const struct {
170125 std::string Option;
@@ -256,7 +211,7 @@ bool isValidOneOmpImage(StringRef Image, uint64_t &MajorVer,
256211 return Res;
257212}
258213
259- Error L0ProgramTy ::buildModules (const std::string_view BuildOptions) {
214+ Error L0ProgramBuilderTy ::buildModules (const std::string_view BuildOptions) {
260215 auto &l0Device = getL0Device ();
261216 auto Image = getMemoryBuffer ();
262217 if (identify_magic (Image.getBuffer ()) == file_magic::spirv_object) {
@@ -272,8 +227,6 @@ Error L0ProgramTy::buildModules(const std::string_view BuildOptions) {
272227 return Plugin::error (ErrorCode::UNKNOWN, " Invalid oneAPI OpenMP image" );
273228 }
274229
275- setLibModule ();
276-
277230 // Iterate over the images and pick the first one that fits.
278231 uint64_t ImageCount = 0 ;
279232 struct V1ImageInfo {
@@ -473,12 +426,32 @@ Error L0ProgramTy::buildModules(const std::string_view BuildOptions) {
473426 }
474427 DP (" Created module from image #%" PRIu64 " .\n " , Idx);
475428
429+ if (RequiresModuleLink) {
430+ DP (" Linking modules after adding image #%" PRIu64 " .\n " , Idx);
431+ if (auto Err = linkModules ())
432+ return Err;
433+ }
434+
476435 return Plugin::success ();
477436 }
478437
479438 return Plugin::error(ErrorCode::UNKNOWN, " Failed to create program modules." );
480439}
481440
441+ Expected<std::unique_ptr<MemoryBuffer>> L0ProgramBuilderTy::getELF () {
442+ assert (GlobalModule != nullptr && " GlobalModule is null" );
443+
444+ size_t Size = 0 ;
445+
446+ CALL_ZE_RET_ERROR (zeModuleGetNativeBinary, GlobalModule, &Size, nullptr );
447+ std::vector<uint8_t > ELFData (Size);
448+ CALL_ZE_RET_ERROR (zeModuleGetNativeBinary, GlobalModule, &Size,
449+ ELFData.data ());
450+ return MemoryBuffer::getMemBufferCopy (
451+ StringRef (reinterpret_cast <const char *>(ELFData.data ()), Size),
452+ /* BufferName=*/ " L0Program ELF" );
453+ }
454+
482455Expected<void *> L0ProgramTy::getOffloadVarDeviceAddr (const char *CName) const {
483456 DP (" Looking up OpenMP global variable '%s'.\n " , CName);
484457
0 commit comments