Files
clang-p2996/llvm/lib/Object/OffloadBinary.cpp
Joseph Huber e471ba3d01 [Object] Add binary format for bundling offloading metadata
We need to embed certain metadata along with a binary image when we wish
to perform a device-linking job on it. Currently this metadata was
embedded in the section name of the data itself. This worked, but made
adding new metadata very difficult and didn't work if the user did any
sort of section linking.

This patch introduces a custom binary format for bundling offloading
metadata with a device object file. This binary format is fundamentally
a simple string map table with some additional data and an embedded
image. I decided to use a custom format rather than using an existing
format (ELF, JSON, etc) because of the specialty use-case of this. We
need a simple binary format that can be concatenated without requiring
other external dependencies.

This extension will make it easier to extend the linker wrapper's
capabilties with whatever data is necessary. Eventually this will allow
us to remove all the external arguments passed to the linker wrapper and
embed it directly in the host's linker so device linking behaves exactly
like host linking.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D122069
2022-04-14 10:50:52 -04:00

145 lines
4.7 KiB
C++

//===- Offloading.cpp - Utilities for handling offloading code -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/OffloadBinary.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
using namespace llvm;
namespace llvm {
Expected<std::unique_ptr<OffloadBinary>>
OffloadBinary::create(MemoryBufferRef Buf) {
if (Buf.getBufferSize() < sizeof(Header) + sizeof(Entry))
return errorCodeToError(llvm::object::object_error::parse_failed);
// Check for 0x10FF1OAD magic bytes.
if (!Buf.getBuffer().startswith("\x10\xFF\x10\xAD"))
return errorCodeToError(llvm::object::object_error::parse_failed);
const char *Start = Buf.getBufferStart();
const Header *TheHeader = reinterpret_cast<const Header *>(Start);
const Entry *TheEntry =
reinterpret_cast<const Entry *>(&Start[TheHeader->EntryOffset]);
return std::unique_ptr<OffloadBinary>(
new OffloadBinary(Buf.getBufferStart(), TheHeader, TheEntry));
}
std::unique_ptr<MemoryBuffer>
OffloadBinary::write(const OffloadingImage &OffloadingData) {
// Create a null-terminated string table with all the used strings.
StringTableBuilder StrTab(StringTableBuilder::ELF);
for (auto &KeyAndValue : OffloadingData.StringData) {
StrTab.add(KeyAndValue.getKey());
StrTab.add(KeyAndValue.getValue());
}
StrTab.finalize();
uint64_t StringEntrySize =
sizeof(StringEntry) * OffloadingData.StringData.size();
// Create the header and fill in the offsets. The entry will be directly
// placed after the header in memory. Align the size to the alignment of the
// header so this can be placed contiguously in a single section.
Header TheHeader;
TheHeader.Size =
alignTo(sizeof(Header) + sizeof(Entry) + StringEntrySize +
OffloadingData.Image.getBufferSize() + StrTab.getSize(),
getAlignment());
TheHeader.EntryOffset = sizeof(Header);
TheHeader.EntrySize = sizeof(Entry);
// Create the entry using the string table offsets. The string table will be
// placed directly after the entry in memory, and the image after that.
Entry TheEntry;
TheEntry.TheImageKind = OffloadingData.TheImageKind;
TheEntry.TheOffloadKind = OffloadingData.TheOffloadKind;
TheEntry.Flags = OffloadingData.Flags;
TheEntry.StringOffset = sizeof(Header) + sizeof(Entry);
TheEntry.NumStrings = OffloadingData.StringData.size();
TheEntry.ImageOffset =
sizeof(Header) + sizeof(Entry) + StringEntrySize + StrTab.getSize();
TheEntry.ImageSize = OffloadingData.Image.getBufferSize();
SmallVector<char, 1024> Data;
raw_svector_ostream OS(Data);
OS << StringRef(reinterpret_cast<char *>(&TheHeader), sizeof(Header));
OS << StringRef(reinterpret_cast<char *>(&TheEntry), sizeof(Entry));
for (auto &KeyAndValue : OffloadingData.StringData) {
uint64_t Offset = sizeof(Header) + sizeof(Entry) + StringEntrySize;
StringEntry Map{Offset + StrTab.getOffset(KeyAndValue.getKey()),
Offset + StrTab.getOffset(KeyAndValue.getValue())};
OS << StringRef(reinterpret_cast<char *>(&Map), sizeof(StringEntry));
}
StrTab.write(OS);
OS << OffloadingData.Image.getBuffer();
// Add final padding to required alignment.
assert(TheHeader.Size >= OS.tell() && "Too much data written?");
OS.write_zeros(TheHeader.Size - OS.tell());
assert(TheHeader.Size == OS.tell() && "Size mismatch");
return MemoryBuffer::getMemBufferCopy(OS.str());
}
OffloadKind getOffloadKind(StringRef Name) {
return llvm::StringSwitch<OffloadKind>(Name)
.Case("openmp", OFK_OpenMP)
.Case("cuda", OFK_Cuda)
.Case("hip", OFK_HIP)
.Default(OFK_None);
}
StringRef getOffloadKindName(OffloadKind Kind) {
switch (Kind) {
case OFK_OpenMP:
return "openmp";
case OFK_Cuda:
return "cuda";
case OFK_HIP:
return "hip";
default:
return "none";
}
}
ImageKind getImageKind(StringRef Name) {
return llvm::StringSwitch<ImageKind>(Name)
.Case("o", IMG_Object)
.Case("bc", IMG_Bitcode)
.Case("cubin", IMG_Cubin)
.Case("fatbin", IMG_Fatbinary)
.Case("s", IMG_PTX)
.Default(IMG_None);
}
StringRef getImageKindName(ImageKind Kind) {
switch (Kind) {
case IMG_Object:
return "o";
case IMG_Bitcode:
return "bc";
case IMG_Cubin:
return "cubin";
case IMG_Fatbinary:
return "fatbin";
case IMG_PTX:
return "s";
default:
return "";
}
}
} // namespace llvm