[PATCH] D129303: [clang-offload-bundler] Library-ize ClangOffloadBundler (2/4)

2022-07-07 Thread Jacob Lambert via Phabricator via cfe-commits
lamb-j created this revision.
lamb-j added reviewers: kzhuravl, scott.linder, yaxunl.
Herald added a reviewer: alexander-shaposhnikov.
Herald added a project: All.
lamb-j requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Lifting the core functionalities of the clang-offload-bundler into a
user-facing library/API.

This patch (2/4) introduces a Config class that locally stores the
previously global cl::opt options and arrays. This localization
will allow users to call the APIs in a multi-threaded context. This
patch also introduces an OffloadBundler class to encapsulate the
top-level API functions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129303

Files:
  clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
  clang/tools/clang-offload-bundler/OffloadBundler.cpp
  clang/tools/clang-offload-bundler/OffloadBundler.h

Index: clang/tools/clang-offload-bundler/OffloadBundler.h
===
--- clang/tools/clang-offload-bundler/OffloadBundler.h
+++ clang/tools/clang-offload-bundler/OffloadBundler.h
@@ -17,9 +17,37 @@
 using namespace llvm;
 using namespace llvm::object;
 
-Error BundleFiles();
-Error UnbundleFiles();
-Error UnbundleArchive();
+class Config {
+public:
+  bool AllowNoHost = false;
+  bool AllowMissingBundles = false;
+  bool CheckInputArchive = false;
+  bool PrintExternalCommands = false;
+  bool HipOpenmpCompatible = false;
+
+  unsigned BundleAlignment = 1;
+  unsigned HostInputIndex = ~0u;
+
+  std::string FilesType;
+  std::string BundlerExecutable;
+
+  // TODO: Convert these to llvm::SmallVector
+  std::vector TargetNames;
+  std::vector InputFileNames;
+  std::vector OutputFileNames;
+};
+
+class OffloadBundler {
+public:
+  Config *BundlerConfig;
+
+  // TODO: Add error checking from ClangOffloadBundler.cpp
+  OffloadBundler(Config *BC) : BundlerConfig(BC) {}
+
+  Error BundleFiles();
+  Error UnbundleFiles();
+  Error UnbundleArchive();
+};
 
 /// Obtain the offload kind, real machine triple, and an optional GPUArch
 /// out of the target information specified by the user.
@@ -31,8 +59,9 @@
   StringRef OffloadKind;
   llvm::Triple Triple;
   StringRef GPUArch;
+  Config *BundlerConfig;
 
-  OffloadTargetInfo(const StringRef Target);
+  OffloadTargetInfo(const StringRef Target, Config *BC);
   bool hasHostKind() const;
   bool isOffloadKindValid() const;
   bool isOffloadKindCompatible(const StringRef TargetOffloadKind) const;
@@ -42,4 +71,4 @@
 };
 
 // List bundle IDs. Return true if an error was found.
-Error ListBundleIDsInFile(StringRef InputFileName);
+Error ListBundleIDsInFile(StringRef InputFileName, Config *BundlerConfig);
Index: clang/tools/clang-offload-bundler/OffloadBundler.cpp
===
--- clang/tools/clang-offload-bundler/OffloadBundler.cpp
+++ clang/tools/clang-offload-bundler/OffloadBundler.cpp
@@ -58,28 +58,12 @@
 using namespace llvm;
 using namespace llvm::object;
 
-extern cl::list InputFileNames;
-extern cl::list OutputFileNames;
-extern cl::list TargetNames;
-extern cl::opt FilesType;
-extern cl::opt PrintExternalCommands;
-extern cl::opt AllowMissingBundles;
-extern cl::opt BundleAlignment;
-extern cl::opt HipOpenmpCompatible;
-
 /// Magic string that marks the existence of offloading data.
 #define OFFLOAD_BUNDLER_MAGIC_STR "__CLANG_OFFLOAD_BUNDLE__"
 
-/// The index of the host input in the list of inputs.
-extern unsigned HostInputIndex;
-
-/// Whether not having host target is allowed.
-extern bool AllowNoHost;
-
-/// Path to the current binary.
-extern std::string BundlerExecutable;
-
-OffloadTargetInfo::OffloadTargetInfo(const StringRef Target) {
+OffloadTargetInfo::OffloadTargetInfo(const StringRef Target, Config *BC) {
+  // TODO: Add error checking from ClangOffloadBundler.cpp
+  BundlerConfig = BC;
   auto TargetFeatures = Target.split(':');
   auto TripleOrGPU = TargetFeatures.first.rsplit('-');
 
@@ -107,7 +91,7 @@
 bool OffloadTargetInfo::isOffloadKindCompatible(const StringRef TargetOffloadKind) const {
   if (OffloadKind == TargetOffloadKind)
 return true;
-  if (HipOpenmpCompatible) {
+  if (BundlerConfig->HipOpenmpCompatible) {
 bool HIPCompatibleWithOpenMP =
   OffloadKind.startswith_insensitive("hip") &&
   TargetOffloadKind == "openmp";
@@ -306,8 +290,12 @@
   /// Current bundle target to be written.
   std::string CurWriteBundleTarget;
 
+  /// Configuration options and arrays for this bundler job
+  Config *BundlerConfig;
+
 public:
-  BinaryFileHandler() {}
+  // TODO: Add error checking from ClangOffloadBundler.cpp
+  BinaryFileHandler(Config *BC) : BundlerConfig(BC) {}
 
   ~BinaryFileHandler() final {}
 
@@ -407,7 +395,7 @@
 HeaderSize += sizeof(OFFLOAD_BUNDLER_MAGIC_STR) - 1;
 HeaderSize += 8; // Number of Bundles
 
-for (auto &T : TargetNames) {
+for (auto &T : BundlerConfig->TargetNames) {
   HeaderSize 

[PATCH] D129303: [clang-offload-bundler] Library-ize ClangOffloadBundler (2/4)

2022-07-15 Thread Jacob Lambert via Phabricator via cfe-commits
lamb-j abandoned this revision.
lamb-j added a comment.

Abandoned in favor of combined patch: https://reviews.llvm.org/D129873


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129303/new/

https://reviews.llvm.org/D129303

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits