clang  16.0.0git
Classes | Macros | Functions
OffloadBundler.cpp File Reference
#include "clang/Driver/OffloadBundler.h"
#include "clang/Basic/Cuda.h"
#include "clang/Basic/TargetID.h"
#include "clang/Basic/Version.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ArchiveWriter.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <forward_list>
#include <memory>
#include <set>
#include <string>
#include <system_error>
#include <utility>
Include dependency graph for OffloadBundler.cpp:

Go to the source code of this file.

Classes

class  FileHandler
 Generic file handler interface. More...
 
struct  FileHandler::BundleInfo
 
class  BinaryFileHandler
 
class  ObjectFileHandler
 Handler for object files. More...
 
class  TextFileHandler
 Handler for text files. More...
 

Macros

#define OFFLOAD_BUNDLER_MAGIC_STR   "__CLANG_OFFLOAD_BUNDLE__"
 Magic string that marks the existence of offloading data. More...
 

Functions

static StringRef getDeviceFileExtension (StringRef Device, StringRef BundleFileName)
 
static std::string getDeviceLibraryFileName (StringRef BundleFileName, StringRef Device)
 
bool isCodeObjectCompatible (const OffloadTargetInfo &CodeObjectInfo, const OffloadTargetInfo &TargetInfo)
 Checks if a code object CodeObjectInfo is compatible with a given target TargetInfo. More...
 
static uint64_t Read8byteIntegerFromBuffer (StringRef Buffer, size_t pos)
 Handler for binary files. More...
 
static void Write8byteIntegerToBuffer (raw_fd_ostream &OS, uint64_t Val)
 Write 8-byte integers to a buffer in little-endian format. More...
 
static std::unique_ptr< FileHandlerCreateObjectFileHandler (MemoryBuffer &FirstInput, const OffloadBundlerConfig &BundlerConfig)
 Return an appropriate object file handler. More...
 
static Expected< std::unique_ptr< FileHandler > > CreateFileHandler (MemoryBuffer &FirstInput, const OffloadBundlerConfig &BundlerConfig)
 Return an appropriate handler given the input files and options. More...
 
static Archive::Kind getDefaultArchiveKindForHost ()
 
static bool getCompatibleOffloadTargets (OffloadTargetInfo &CodeObjectInfo, SmallVectorImpl< StringRef > &CompatibleTargets, const OffloadBundlerConfig &BundlerConfig)
 Computes a list of targets among all given targets which are compatible with this code object. More...
 

Detailed Description

This file implements an offload bundling API that bundles different files that relate with the same source code but different targets into a single one. Also the implements the opposite functionality, i.e. unbundle files previous created by this API.

Definition in file OffloadBundler.cpp.

Macro Definition Documentation

◆ OFFLOAD_BUNDLER_MAGIC_STR

#define OFFLOAD_BUNDLER_MAGIC_STR   "__CLANG_OFFLOAD_BUNDLE__"

Magic string that marks the existence of offloading data.

Definition at line 62 of file OffloadBundler.cpp.

Function Documentation

◆ CreateFileHandler()

static Expected<std::unique_ptr<FileHandler> > CreateFileHandler ( MemoryBuffer &  FirstInput,
const OffloadBundlerConfig BundlerConfig 
)
static

◆ CreateObjectFileHandler()

static std::unique_ptr<FileHandler> CreateObjectFileHandler ( MemoryBuffer &  FirstInput,
const OffloadBundlerConfig BundlerConfig 
)
static

Return an appropriate object file handler.

We use the specific object handler if we know how to deal with that format, otherwise we use a default binary file handler.

Definition at line 830 of file OffloadBundler.cpp.

Referenced by CreateFileHandler().

◆ getCompatibleOffloadTargets()

static bool getCompatibleOffloadTargets ( OffloadTargetInfo CodeObjectInfo,
SmallVectorImpl< StringRef > &  CompatibleTargets,
const OffloadBundlerConfig BundlerConfig 
)
static

Computes a list of targets among all given targets which are compatible with this code object.

Parameters
[in]CodeObjectInfoCode Object
[out]CompatibleTargetsList of all compatible targets among all given targets
Returns
false, if no compatible target is found.

Definition at line 1096 of file OffloadBundler.cpp.

References isCodeObjectCompatible(), clang::Target, and clang::OffloadBundlerConfig::TargetNames.

Referenced by clang::OffloadBundler::UnbundleArchive().

◆ getDefaultArchiveKindForHost()

static Archive::Kind getDefaultArchiveKindForHost ( )
static

Definition at line 1084 of file OffloadBundler.cpp.

Referenced by clang::OffloadBundler::UnbundleArchive().

◆ getDeviceFileExtension()

static StringRef getDeviceFileExtension ( StringRef  Device,
StringRef  BundleFileName 
)
static

Definition at line 122 of file OffloadBundler.cpp.

Referenced by getDeviceLibraryFileName().

◆ getDeviceLibraryFileName()

static std::string getDeviceLibraryFileName ( StringRef  BundleFileName,
StringRef  Device 
)
static

Definition at line 131 of file OffloadBundler.cpp.

References getDeviceFileExtension(), and string().

Referenced by clang::OffloadBundler::UnbundleArchive().

◆ isCodeObjectCompatible()

bool isCodeObjectCompatible ( const OffloadTargetInfo CodeObjectInfo,
const OffloadTargetInfo TargetInfo 
)

◆ Read8byteIntegerFromBuffer()

static uint64_t Read8byteIntegerFromBuffer ( StringRef  Buffer,
size_t  pos 
)
static

Handler for binary files.

The bundled file will have the following format (all integers are stored in little-endian format):

"OFFLOAD_BUNDLER_MAGIC_STR" (ASCII encoding of the string)

NumberOfOffloadBundles (8-byte integer)

OffsetOfBundle1 (8-byte integer) SizeOfBundle1 (8-byte integer) NumberOfBytesInTripleOfBundle1 (8-byte integer) TripleOfBundle1 (byte length defined before)

...

OffsetOfBundleN (8-byte integer) SizeOfBundleN (8-byte integer) NumberOfBytesInTripleOfBundleN (8-byte integer) TripleOfBundleN (byte length defined before)

Bundle1 ... BundleN Read 8-byte integers from a buffer in little-endian format.

Definition at line 297 of file OffloadBundler.cpp.

Referenced by BinaryFileHandler::ReadHeader().

◆ Write8byteIntegerToBuffer()

static void Write8byteIntegerToBuffer ( raw_fd_ostream &  OS,
uint64_t  Val 
)
static

Write 8-byte integers to a buffer in little-endian format.

Definition at line 310 of file OffloadBundler.cpp.

Referenced by BinaryFileHandler::WriteHeader().