Skip to content

Commit 0f3f357

Browse files
committed
[clang-offload-bundler] Library-ize ClangOffloadBundler
Lifting the core functionalities of the clang-offload-bundler into a user-facing library/API. This will allow online and JIT compilers to bundle and unbundle files without spawning a new process. This patch lifts the classes and functions used to implement the clang-offload-bundler into a separate OffloadBundler.cpp, and defines three top-level API functions in OfflaodBundler.h. BundleFiles() UnbundleFiles() UnbundleArchives() This patch also introduces a Config class that locally stores the previously global cl::opt options and arrays to allow users to call the APIs in a multi-threaded context, and introduces an OffloadBundler class to encapsulate the top-level API functions. We also lift the BundlerExecutable variable, which is specific to the clang-offload-bundler tool, from the API, and replace its use with an ObjcopyPath variable. This variable must be set in order to internally call llvm-objcopy. Finally, we move the API files from clang/tools/clang-offload-bundler into clang/lib/Driver and clang/include/clang/Driver. Differential Revision: https://reviews.llvm.org/D129873
1 parent 68901fd commit 0f3f357

File tree

5 files changed

+1469
-1272
lines changed

5 files changed

+1469
-1272
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===- OffloadBundler.h - File Bundling and Unbundling ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file defines an offload bundling API that bundles different files
11+
/// that relate with the same source code but different targets into a single
12+
/// one. Also the implements the opposite functionality, i.e. unbundle files
13+
/// previous created by this API.
14+
///
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H
18+
#define LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H
19+
20+
#include "llvm/ADT/Triple.h"
21+
#include "llvm/Support/Error.h"
22+
#include <string>
23+
#include <vector>
24+
25+
namespace clang {
26+
27+
class OffloadBundlerConfig {
28+
public:
29+
bool AllowNoHost = false;
30+
bool AllowMissingBundles = false;
31+
bool CheckInputArchive = false;
32+
bool PrintExternalCommands = false;
33+
bool HipOpenmpCompatible = false;
34+
35+
unsigned BundleAlignment = 1;
36+
unsigned HostInputIndex = ~0u;
37+
38+
std::string FilesType;
39+
std::string ObjcopyPath;
40+
41+
// TODO: Convert these to llvm::SmallVector
42+
std::vector<std::string> TargetNames;
43+
std::vector<std::string> InputFileNames;
44+
std::vector<std::string> OutputFileNames;
45+
};
46+
47+
class OffloadBundler {
48+
public:
49+
const OffloadBundlerConfig &BundlerConfig;
50+
51+
// TODO: Add error checking from ClangOffloadBundler.cpp
52+
OffloadBundler(const OffloadBundlerConfig &BC) : BundlerConfig(BC) {}
53+
54+
// List bundle IDs. Return true if an error was found.
55+
static llvm::Error
56+
ListBundleIDsInFile(llvm::StringRef InputFileName,
57+
const OffloadBundlerConfig &BundlerConfig);
58+
59+
llvm::Error BundleFiles();
60+
llvm::Error UnbundleFiles();
61+
llvm::Error UnbundleArchive();
62+
};
63+
64+
/// Obtain the offload kind, real machine triple, and an optional GPUArch
65+
/// out of the target information specified by the user.
66+
/// Bundle Entry ID (or, Offload Target String) has following components:
67+
/// * Offload Kind - Host, OpenMP, or HIP
68+
/// * Triple - Standard LLVM Triple
69+
/// * GPUArch (Optional) - Processor name, like gfx906 or sm_30
70+
struct OffloadTargetInfo {
71+
llvm::StringRef OffloadKind;
72+
llvm::Triple Triple;
73+
llvm::StringRef GPUArch;
74+
75+
const OffloadBundlerConfig &BundlerConfig;
76+
77+
OffloadTargetInfo(const llvm::StringRef Target,
78+
const OffloadBundlerConfig &BC);
79+
bool hasHostKind() const;
80+
bool isOffloadKindValid() const;
81+
bool isOffloadKindCompatible(const llvm::StringRef TargetOffloadKind) const;
82+
bool isTripleValid() const;
83+
bool operator==(const OffloadTargetInfo &Target) const;
84+
std::string str();
85+
};
86+
87+
} // namespace clang
88+
89+
#endif // LLVM_CLANG_DRIVER_OFFLOADBUNDLER_H

clang/lib/Driver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(LLVM_LINK_COMPONENTS
22
BinaryFormat
33
MC
4+
Object
45
Option
56
ProfileData
67
Support
@@ -20,6 +21,7 @@ add_clang_library(clangDriver
2021
DriverOptions.cpp
2122
Job.cpp
2223
Multilib.cpp
24+
OffloadBundler.cpp
2325
OptionUtils.cpp
2426
Phases.cpp
2527
SanitizerArgs.cpp

0 commit comments

Comments
 (0)