1- // ===- OffloadBundle.cpp - Utilities for handling offloading code -*- C++
2- // -*-===//
1+ // ===- OffloadBundle.cpp - Utilities for offload bundles---*- C++ -*-===//
32//
43// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54// See https://llvm.org/LICENSE.txt for license information.
65// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76//
8- // ===---------------------------------------------------------------------- ===//
7+ // ===----------------------------------------------------------------===//
98
109#include " llvm/Object/OffloadBundle.h"
1110#include " llvm/ADT/StringSwitch.h"
@@ -79,9 +78,8 @@ Error OffloadBundleFatBin::readEntries(StringRef Buffer,
7978
8079 // Read the Magic String first.
8180 StringRef Magic;
82- if (auto EC = Reader.readFixedString (Magic, 24 )) {
81+ if (auto EC = Reader.readFixedString (Magic, 24 ))
8382 return errorCodeToError (object_error::parse_failed);
84- }
8583
8684 // Read the number of Code Objects (Entries) in the current Bundle.
8785 if (auto EC = Reader.readInteger (NumOfEntries))
@@ -108,9 +106,8 @@ Error OffloadBundleFatBin::readEntries(StringRef Buffer,
108106 if (auto EC = Reader.readFixedString (EntryID, EntryIDSize))
109107 return errorCodeToError (object_error::parse_failed);
110108
111- // create a Bundle Entry object:
112- auto Entry = new OffloadBundleEntry (EntryOffset + SectionOffset, EntrySize,
113- EntryIDSize, EntryID);
109+ auto Entry = std::make_unique<OffloadBundleEntry>(
110+ EntryOffset + SectionOffset, EntrySize, EntryIDSize, EntryID);
114111
115112 Entries.push_back (*Entry);
116113 }
@@ -142,15 +139,16 @@ Error OffloadBundleFatBin::extractBundle(const ObjectFile &Source) {
142139 // This will extract all entries in the Bundle
143140 for (OffloadBundleEntry &Entry : Entries) {
144141
145- if (Entry.Size > 0 ) {
146- // create output file name. Which should be
147- // <fileName>-offset<Offset>-size<Size>.co"
148- std::string Str = getFileName ().str () + " -offset" + itostr (Entry.Offset ) +
149- " -size" + itostr (Entry.Size ) + " .co" ;
150- if (Error Err = object::extractCodeObject (Source, Entry.Offset ,
151- Entry.Size , StringRef (Str)))
152- return Err;
153- }
142+ if (Entry.Size == 0 )
143+ continue ;
144+
145+ // create output file name. Which should be
146+ // <fileName>-offset<Offset>-size<Size>.co"
147+ std::string Str = getFileName ().str () + " -offset" + itostr (Entry.Offset ) +
148+ " -size" + itostr (Entry.Size ) + " .co" ;
149+ if (Error Err = object::extractCodeObject (Source, Entry.Offset , Entry.Size ,
150+ StringRef (Str)))
151+ return Err;
154152 }
155153
156154 return Error::success ();
@@ -160,7 +158,7 @@ Error object::extractOffloadBundleFatBinary(
160158 const ObjectFile &Obj, SmallVectorImpl<OffloadBundleFatBin> &Bundles) {
161159 assert ((Obj.isELF () || Obj.isCOFF ()) && " Invalid file type" );
162160
163- // iterate through Sections until we find an offload_bundle section.
161+ // Iterate through Sections until we find an offload_bundle section.
164162 for (SectionRef Sec : Obj.sections ()) {
165163 Expected<StringRef> Buffer = Sec.getContents ();
166164 if (!Buffer)
@@ -174,11 +172,9 @@ Error object::extractOffloadBundleFatBinary(
174172 uint64_t SectionOffset = 0 ;
175173 if (Obj.isELF ()) {
176174 SectionOffset = ELFSectionRef (Sec).getOffset ();
177- } else if (Obj.isCOFF ()) {
178- if (const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
179- const coff_section *CoffSection = COFFObj->getCOFFSection (Sec);
180- }
181- }
175+ } else if (Obj.isCOFF ()) // TODO: add COFF Support
176+ return createStringError (object_error::parse_failed,
177+ " COFF object files not supported.\n " );
182178
183179 MemoryBufferRef Contents (*Buffer, Obj.getFileName ());
184180
@@ -341,7 +337,7 @@ CompressedOffloadBundle::decompress(llvm::MemoryBufferRef &Input,
341337 double DecompressionTimeSeconds =
342338 DecompressTimer.getTotalTime ().getWallTime ();
343339
344- // Recalculate MD5 hash for integrity check
340+ // Recalculate MD5 hash for integrity check.
345341 llvm::Timer HashRecalcTimer (" Hash Recalculation Timer" ,
346342 " Hash recalculation time" ,
347343 OffloadBundlerTimerGroup);
0 commit comments