30 "Timer group for offload bundler");
39 size_t NextbundleStart = 0;
43 std::unique_ptr<MemoryBuffer> Buffer =
52 return FatBundleOrErr.takeError();
59 NextbundleStart = Str.find(
StringRef(
"__CLANG_OFFLOAD_BUNDLE__"), 24);
83 NumberOfEntries = NumOfEntries;
104 auto Entry = std::make_unique<OffloadBundleEntry>(
105 EntryOffset + SectionOffset, EntrySize, EntryIDSize, EntryID);
107 Entries.push_back(*Entry);
130 return std::unique_ptr<OffloadBundleFatBin>(TheBundle);
142 std::string Str =
getFileName().
str() +
"-offset" + itostr(Entry.Offset) +
143 "-size" + itostr(Entry.Size) +
".co";
172 "COFF object files not supported.\n");
182 if (!DecompressedBufferOrErr)
185 "Failed to decompress input: " +
188 MemoryBuffer &DecompressedInput = **DecompressedBufferOrErr;
214 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
215 std::copy(InputBuffOrErr->getBufferStart() +
Offset,
216 InputBuffOrErr->getBufferStart() +
Offset +
Size,
217 Buf->getBufferStart());
218 if (
Error E = Buf->commit())
236 "-offset" + itostr(Uri.
Offset) +
"-size" + itostr(Uri.
Size) +
".co";
241 return ObjOrErr.takeError();
243 auto Obj = ObjOrErr->getBinary();
253 std::string Num = std::to_string(
Value);
267 if (Blob.
size() < V1HeaderSize)
277 size_t CurrentOffset = MagicSize;
280 memcpy(&ThisVersion, Blob.
data() + CurrentOffset,
sizeof(
uint16_t));
281 CurrentOffset += VersionFieldSize;
284 memcpy(&CompressionMethod, Blob.
data() + CurrentOffset,
sizeof(
uint16_t));
285 CurrentOffset += MethodFieldSize;
288 if (ThisVersion >= 2) {
289 if (Blob.
size() < V2HeaderSize)
291 "Compressed bundle header size too small");
292 memcpy(&TotalFileSize, Blob.
data() + CurrentOffset,
sizeof(
uint32_t));
293 CurrentOffset += FileSizeFieldSize;
297 memcpy(&UncompressedSize, Blob.
data() + CurrentOffset,
sizeof(
uint32_t));
298 CurrentOffset += UncompressedSizeFieldSize;
301 memcpy(&StoredHash, Blob.
data() + CurrentOffset,
sizeof(
uint64_t));
302 CurrentOffset += HashFieldSize;
305 if (CompressionMethod ==
308 else if (CompressionMethod ==
313 "Unknown compressing method");
315 llvm::Timer DecompressTimer(
"Decompression Timer",
"Decompression time",
323 CompressionFormat, llvm::arrayRefFromStringRef(CompressedData),
324 DecompressedData, UncompressedSize))
326 "Could not decompress embedded file contents: " +
332 double DecompressionTimeSeconds =
336 llvm::Timer HashRecalcTimer(
"Hash Recalculation Timer",
337 "Hash recalculation time",
344 uint64_t RecalculatedHash = Result.low();
346 bool HashMatch = (StoredHash == RecalculatedHash);
348 double CompressionRate =
349 static_cast<double>(UncompressedSize) / CompressedData.
size();
350 double DecompressionSpeedMBs =
351 (UncompressedSize / (1024.0 * 1024.0)) / DecompressionTimeSeconds;
353 llvm::errs() <<
"Compressed bundle format version: " << ThisVersion <<
"\n";
354 if (ThisVersion >= 2)
355 llvm::errs() <<
"Total file size (from header): "
362 <<
"Size before decompression: "
364 <<
"Size after decompression: "
366 <<
"Compression rate: "
368 <<
"Compression ratio: "
369 <<
llvm::format(
"%.2lf%%", 100.0 / CompressionRate) <<
"\n"
370 <<
"Decompression speed: "
371 <<
llvm::format(
"%.2lf MB/s", DecompressionSpeedMBs) <<
"\n"
373 <<
"Recalculated hash: "
375 <<
"Hashes match: " << (HashMatch ?
"Yes" :
"No") <<
"\n";
379 llvm::toStringRef(DecompressedData));
389 "Compression not supported");
391 llvm::Timer HashTimer(
"Hash Calculation Timer",
"Hash calculation time",
399 uint64_t TruncatedHash = Result.low();
408 llvm::Timer CompressTimer(
"Compression Timer",
"Compression time",
419 sizeof(Version) +
sizeof(CompressionMethod) +
420 sizeof(UncompressedSize) +
sizeof(TruncatedHash) +
421 CompressedBuffer.
size();
426 OS.
write(
reinterpret_cast<const char *
>(&Version),
sizeof(Version));
427 OS.
write(
reinterpret_cast<const char *
>(&CompressionMethod),
428 sizeof(CompressionMethod));
429 OS.
write(
reinterpret_cast<const char *
>(&TotalFileSize),
430 sizeof(TotalFileSize));
431 OS.
write(
reinterpret_cast<const char *
>(&UncompressedSize),
432 sizeof(UncompressedSize));
433 OS.
write(
reinterpret_cast<const char *
>(&TruncatedHash),
434 sizeof(TruncatedHash));
435 OS.
write(
reinterpret_cast<const char *
>(CompressedBuffer.
data()),
436 CompressedBuffer.
size());
441 double CompressionRate =
442 static_cast<double>(UncompressedSize) / CompressedBuffer.
size();
444 double CompressionSpeedMBs =
445 (UncompressedSize / (1024.0 * 1024.0)) / CompressionTimeSeconds;
447 llvm::errs() <<
"Compressed bundle format version: " << Version <<
"\n"
448 <<
"Total file size (including headers): "
450 <<
"Compression method used: " << MethodUsed <<
"\n"
451 <<
"Compression level: " <<
P.level <<
"\n"
452 <<
"Binary size before compression: "
454 <<
"Binary size after compression: "
456 <<
"Compression rate: "
458 <<
"Compression ratio: "
459 <<
llvm::format(
"%.2lf%%", 100.0 / CompressionRate) <<
"\n"
460 <<
"Compression speed: "
461 <<
llvm::format(
"%.2lf MB/s", CompressionSpeedMBs) <<
"\n"
462 <<
"Truncated MD5 hash: "
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Module.h This file contains the declarations for the Module class.
Error extractOffloadBundle(MemoryBufferRef Contents, uint64_t SectionOffset, StringRef FileName, SmallVectorImpl< OffloadBundleFatBin > &Bundles)
static llvm::TimerGroup OffloadBundlerTimerGroup("Offload Bundler Timer Group", "Timer group for offload bundler")
static std::string formatWithCommas(unsigned long long Value)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Provides read only access to a subclass of BinaryStream.
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
LLVM_ABI Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
static LLVM_ABI Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
LLVM_ABI void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
size_t getBufferSize() const
StringRef getBuffer() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
StringRef getBuffer() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
static constexpr size_t npos
double getWallTime() const
The TimerGroup class is used to group together related timers into a single report that is printed wh...
This class is used to track the amount of time spent between invocations of its startTimer()/stopTime...
LLVM_ABI void stopTimer()
Stop the timer.
LLVM_ABI void startTimer()
Start the timer running.
TimeRecord getTotalTime() const
Return the duration for which this timer has been running.
LLVM Value Representation.
StringRef getFileName() const
static LLVM_ABI llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > compress(llvm::compression::Params P, const llvm::MemoryBuffer &Input, bool Verbose=false)
static LLVM_ABI llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > decompress(llvm::MemoryBufferRef &Input, bool Verbose=false)
uint64_t getOffset() const
This class is the base class for all object file types.
section_iterator_range sections() const
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Fat binary embedded in object files in clang-offload-bundler format.
static LLVM_ABI Expected< std::unique_ptr< OffloadBundleFatBin > > create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName)
LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset)
LLVM_ABI Error extractBundle(const ObjectFile &Source)
StringRef getFileName() const
This is a value type class that represents a single section in the list of sections in the object fil...
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
LLVM_ABI bool isAvailable()
LLVM_ABI bool isAvailable()
LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
LLVM_ABI void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
LLVM_ABI Error extractCodeObject(const ObjectFile &Source, int64_t Offset, int64_t Size, StringRef OutputFileName)
Extract code object memory from the given Source object file at Offset and of Size,...
LLVM_ABI Error extractOffloadBundleByURI(StringRef URIstr)
Extracts an Offload Bundle Entry given by URI.
LLVM_ABI Error extractOffloadBundleFatBinary(const ObjectFile &Obj, SmallVectorImpl< OffloadBundleFatBin > &Bundles)
Extracts fat binary in binary clang-offload-bundler format from object Obj and return it in Bundles.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
const char * toString(DWARFSectionKind Kind)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
@ offload_bundle
Clang offload bundle file.
@ offload_bundle_compressed
Compressed clang offload bundle file.
Bundle entry in binary clang-offload-bundler format.
static Expected< std::unique_ptr< OffloadBundleURI > > createOffloadBundleURI(StringRef Str, UriTypeT Type)