Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:53

0001 //===- OffloadBundler.h - File Bundling and Unbundling ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file defines an offload bundling API that bundles different files
0011 /// that relate with the same source code but different targets into a single
0012 /// one. Also the implements the opposite functionality, i.e. unbundle files
0013 /// previous created by this API.
0014 ///
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H
0018 #define LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H
0019 
0020 #include "llvm/Support/Compression.h"
0021 #include "llvm/Support/Error.h"
0022 #include "llvm/TargetParser/Triple.h"
0023 #include <llvm/Support/MemoryBuffer.h>
0024 #include <string>
0025 #include <vector>
0026 
0027 namespace clang {
0028 
0029 class OffloadBundlerConfig {
0030 public:
0031   OffloadBundlerConfig();
0032 
0033   bool AllowNoHost = false;
0034   bool AllowMissingBundles = false;
0035   bool CheckInputArchive = false;
0036   bool PrintExternalCommands = false;
0037   bool HipOpenmpCompatible = false;
0038   bool Compress = false;
0039   bool Verbose = false;
0040   llvm::compression::Format CompressionFormat;
0041   int CompressionLevel;
0042   uint16_t CompressedBundleVersion;
0043 
0044   unsigned BundleAlignment = 1;
0045   unsigned HostInputIndex = ~0u;
0046 
0047   std::string FilesType;
0048   std::string ObjcopyPath;
0049 
0050   // TODO: Convert these to llvm::SmallVector
0051   std::vector<std::string> TargetNames;
0052   std::vector<std::string> InputFileNames;
0053   std::vector<std::string> OutputFileNames;
0054 };
0055 
0056 class OffloadBundler {
0057 public:
0058   const OffloadBundlerConfig &BundlerConfig;
0059 
0060   // TODO: Add error checking from ClangOffloadBundler.cpp
0061   OffloadBundler(const OffloadBundlerConfig &BC) : BundlerConfig(BC) {}
0062 
0063   // List bundle IDs. Return true if an error was found.
0064   static llvm::Error
0065   ListBundleIDsInFile(llvm::StringRef InputFileName,
0066                       const OffloadBundlerConfig &BundlerConfig);
0067 
0068   llvm::Error BundleFiles();
0069   llvm::Error UnbundleFiles();
0070   llvm::Error UnbundleArchive();
0071 };
0072 
0073 /// Obtain the offload kind, real machine triple, and an optional TargetID
0074 /// out of the target information specified by the user.
0075 /// Bundle Entry ID (or, Offload Target String) has following components:
0076 ///  * Offload Kind - Host, OpenMP, or HIP
0077 ///  * Triple - Standard LLVM Triple
0078 ///  * TargetID (Optional) - target ID, like gfx906:xnack+ or sm_30
0079 struct OffloadTargetInfo {
0080   llvm::StringRef OffloadKind;
0081   llvm::Triple Triple;
0082   llvm::StringRef TargetID;
0083 
0084   const OffloadBundlerConfig &BundlerConfig;
0085 
0086   OffloadTargetInfo(const llvm::StringRef Target,
0087                     const OffloadBundlerConfig &BC);
0088   bool hasHostKind() const;
0089   bool isOffloadKindValid() const;
0090   bool isOffloadKindCompatible(const llvm::StringRef TargetOffloadKind) const;
0091   bool isTripleValid() const;
0092   bool operator==(const OffloadTargetInfo &Target) const;
0093   std::string str() const;
0094 };
0095 
0096 // CompressedOffloadBundle represents the format for the compressed offload
0097 // bundles.
0098 //
0099 // The format is as follows:
0100 // - Magic Number (4 bytes) - A constant "CCOB".
0101 // - Version (2 bytes)
0102 // - Compression Method (2 bytes) - Uses the values from
0103 // llvm::compression::Format.
0104 // - Total file size (4 bytes in V2, 8 bytes in V3).
0105 // - Uncompressed Size (4 bytes in V1/V2, 8 bytes in V3).
0106 // - Truncated MD5 Hash (8 bytes).
0107 // - Compressed Data (variable length).
0108 class CompressedOffloadBundle {
0109 private:
0110   static inline const size_t MagicSize = 4;
0111   static inline const size_t VersionFieldSize = sizeof(uint16_t);
0112   static inline const size_t MethodFieldSize = sizeof(uint16_t);
0113   // Legacy size fields for V1/V2
0114   static inline const size_t FileSizeFieldSizeV2 = sizeof(uint32_t);
0115   static inline const size_t UncompressedSizeFieldSizeV2 = sizeof(uint32_t);
0116   // New size fields for V3
0117   static inline const size_t FileSizeFieldSizeV3 = sizeof(uint64_t);
0118   static inline const size_t UncompressedSizeFieldSizeV3 = sizeof(uint64_t);
0119   static inline const size_t HashFieldSize = sizeof(uint64_t);
0120 
0121   // Keep V1 header size for backward compatibility
0122   static inline const size_t V1HeaderSize =
0123       MagicSize + VersionFieldSize + MethodFieldSize +
0124       UncompressedSizeFieldSizeV2 + HashFieldSize;
0125 
0126   // Keep V2 header size for backward compatibility
0127   static inline const size_t V2HeaderSize =
0128       MagicSize + VersionFieldSize + FileSizeFieldSizeV2 + MethodFieldSize +
0129       UncompressedSizeFieldSizeV2 + HashFieldSize;
0130 
0131   // Add V3 header size with 64-bit fields
0132   static inline const size_t V3HeaderSize =
0133       MagicSize + VersionFieldSize + FileSizeFieldSizeV3 + MethodFieldSize +
0134       UncompressedSizeFieldSizeV3 + HashFieldSize;
0135 
0136   static inline const llvm::StringRef MagicNumber = "CCOB";
0137 
0138 public:
0139   static inline const uint16_t DefaultVersion = 2;
0140 
0141   // Helper method to get header size based on version
0142   static size_t getHeaderSize(uint16_t Version) {
0143     switch (Version) {
0144     case 1:
0145       return V1HeaderSize;
0146     case 2:
0147       return V2HeaderSize;
0148     case 3:
0149       return V3HeaderSize;
0150     default:
0151       llvm_unreachable("Unsupported version");
0152     }
0153   }
0154 
0155   static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
0156   compress(llvm::compression::Params P, const llvm::MemoryBuffer &Input,
0157            uint16_t Version, bool Verbose = false);
0158   static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
0159   decompress(const llvm::MemoryBuffer &Input, bool Verbose = false);
0160 };
0161 } // namespace clang
0162 
0163 #endif // LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H