llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: karan sharma (karan2908ks)

<details>
<summary>Changes</summary>

Parse options with OptTable instead of llvm::cl, as part of removing cl::opt 
from clang/. Verbose and HipApi become plain settings declared in 
lib/OffloadArchOptions.h.

Behavior changes: help output uses the OptTable format, LLVM's global cl 
options and --verbose=&lt;bool&gt; are no longer accepted, and a repeated 
option takes the last value.

Testing on arm64 macOS (no supported GPU): OffloadArchTests passed (13/13), and 
--help, --version, --only and the amdgpu-arch symlink were checked manually. 
check-clang was not run.

Part of #<!-- -->246767.




---
Full diff: https://github.com/llvm/llvm-project/pull/246825.diff


8 Files Affected:

- (modified) clang/tools/offload-arch/CMakeLists.txt (+11-2) 
- (modified) clang/tools/offload-arch/OffloadArch.cpp (+72-26) 
- (added) clang/tools/offload-arch/Opts.td (+18) 
- (modified) clang/tools/offload-arch/lib/AMDGPUArchByHIP.cpp (+1-22) 
- (modified) clang/tools/offload-arch/lib/LevelZeroArch.cpp (+1-2) 
- (modified) clang/tools/offload-arch/lib/NVPTXArch.cpp (+1-3) 
- (modified) clang/tools/offload-arch/lib/OffloadArchOptions.cpp (+4-14) 
- (added) clang/tools/offload-arch/lib/OffloadArchOptions.h (+23) 


``````````diff
diff --git a/clang/tools/offload-arch/CMakeLists.txt 
b/clang/tools/offload-arch/CMakeLists.txt
index 83dcdd62f1720d..fd04055b3d1010 100644
--- a/clang/tools/offload-arch/CMakeLists.txt
+++ b/clang/tools/offload-arch/CMakeLists.txt
@@ -1,6 +1,15 @@
-set(LLVM_LINK_COMPONENTS Support)
+set(LLVM_LINK_COMPONENTS Option Support)
 
-add_clang_tool(offload-arch OffloadArch.cpp)
+set(LLVM_TARGET_DEFINITIONS Opts.td)
+llvm_tablegen(Opts.inc -gen-opt-parser-defs)
+add_public_tablegen_target(OffloadArchOptsTableGen)
+
+add_clang_tool(offload-arch
+  OffloadArch.cpp
+
+  DEPENDS
+  OffloadArchOptsTableGen
+  )
 
 # Legacy binary names.
 add_clang_symlink(amdgpu-arch offload-arch)
diff --git a/clang/tools/offload-arch/OffloadArch.cpp 
b/clang/tools/offload-arch/OffloadArch.cpp
index a8c3ce1ee0011b..f487864c0eed22 100644
--- a/clang/tools/offload-arch/OffloadArch.cpp
+++ b/clang/tools/offload-arch/OffloadArch.cpp
@@ -6,17 +6,37 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include "lib/OffloadArchOptions.h"
 #include "clang/Basic/Version.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/LibraryOptionsContext.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/WithColor.h"
+#include <optional>
 
 using namespace llvm;
 
-// Defined in lib/OffloadArchOptions.cpp.
-extern cl::OptionCategory OffloadArchCategory;
+namespace {
+using namespace llvm::opt;
 
-extern cl::OptionCategory AMDGPUArchByHIPCategory;
+enum ID {
+  OPT_INVALID = 0,
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "Opts.inc"
+#undef OPTION
+};
+
+#define OPTTABLE_CODE
+#include "Opts.inc"
+
+class OffloadArchOptTable : public OptTable {
+public:
+  OffloadArchOptTable() : OptTable(optionTables()) {}
+};
+} // namespace
 
 enum VendorName {
   all,
@@ -25,16 +45,9 @@ enum VendorName {
   intel,
 };
 
-static cl::opt<VendorName>
-    Only("only", cl::desc("Restrict to vendor:"), cl::cat(OffloadArchCategory),
-         cl::init(all),
-         cl::values(clEnumVal(all, "Print all GPUs (default)"),
-                    clEnumVal(amdgpu, "Only print AMD GPUs"),
-                    clEnumVal(nvptx, "Only print NVIDIA GPUs"),
-                    clEnumVal(intel, "Only print Intel GPUs")));
-
-static void PrintVersion(raw_ostream &OS) {
-  OS << clang::getClangToolFullVersion("offload-arch") << '\n';
+[[noreturn]] static void reportError(const Twine &Msg) {
+  WithColor::error(errs(), "offload-arch") << Msg << '\n';
+  exit(1);
 }
 
 int printGPUsByKFD();
@@ -60,16 +73,49 @@ const std::array<std::pair<VendorName, 
function_ref<int()>>, 3> VendorTable{
      {VendorName::intel, printIntel}}};
 
 int main(int argc, char *argv[]) {
-  cl::HideUnrelatedOptions({&OffloadArchCategory, &AMDGPUArchByHIPCategory});
-
-  cl::SetVersionPrinter(PrintVersion);
-  cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(
-      LibraryOptions, ArrayRef(argv, argc),
-      "A tool to detect the presence of offloading devices on the system. \n\n"
-      "The tool will output each detected GPU architecture separated by a\n"
-      "newline character. If multiple GPUs of the same architecture are 
found\n"
-      "a string will be printed for each\n");
+  BumpPtrAllocator Alloc;
+  StringSaver Saver(Alloc);
+  OffloadArchOptTable Tbl;
+  opt::InputArgList Args = Tbl.parseArgs(
+      argc, argv, OPT_UNKNOWN, Saver, [](StringRef Msg) { reportError(Msg); });
+
+  if (Args.hasArg(OPT_help)) {
+    Tbl.printHelp(
+        outs(), "offload-arch [options]",
+        "A tool to detect the presence of offloading devices on the system");
+    return 0;
+  }
+  if (Args.hasArg(OPT_version)) {
+    outs() << clang::getClangToolFullVersion("offload-arch") << '\n';
+    return 0;
+  }
+  if (const opt::Arg *A = Args.getLastArg(OPT_INPUT))
+    reportError("unexpected argument '" + Twine(A->getValue()) + "'");
+
+  Verbose = Args.hasArg(OPT_verbose);
+
+  StringRef HipArg = Args.getLastArgValue(OPT_hip_api_version_EQ, "auto");
+  std::optional<HipApiVersion> Hip =
+      StringSwitch<std::optional<HipApiVersion>>(HipArg)
+          .Case("auto", HipApiVersion::Auto)
+          .Case("r0600", HipApiVersion::R0600)
+          .Case("r0000", HipApiVersion::R0000)
+          .Case("unversioned", HipApiVersion::Unversioned)
+          .Default(std::nullopt);
+  if (!Hip)
+    reportError("invalid value for --hip-api-version: '" + HipArg + "'");
+  HipApi = *Hip;
+
+  StringRef OnlyArg = Args.getLastArgValue(OPT_only_EQ, "all");
+  std::optional<VendorName> Only =
+      StringSwitch<std::optional<VendorName>>(OnlyArg)
+          .Case("all", all)
+          .Case("amdgpu", amdgpu)
+          .Case("nvptx", nvptx)
+          .Case("intel", intel)
+          .Default(std::nullopt);
+  if (!Only)
+    reportError("invalid value for --only: '" + OnlyArg + "'");
 
   // Support legacy binaries.
   if (sys::path::stem(argv[0]).starts_with("amdgpu-arch"))
@@ -79,7 +125,7 @@ int main(int argc, char *argv[]) {
 
   int Result = 1;
   for (auto [Name, Func] : VendorTable) {
-    if (Only == VendorName::all || Only == Name)
+    if (*Only == VendorName::all || *Only == Name)
       Result &= Func();
   }
 
diff --git a/clang/tools/offload-arch/Opts.td b/clang/tools/offload-arch/Opts.td
new file mode 100644
index 00000000000000..ee3666ca19b0d1
--- /dev/null
+++ b/clang/tools/offload-arch/Opts.td
@@ -0,0 +1,18 @@
+include "llvm/Option/OptParser.td"
+
+class F<string name, string help> : Flag<["-", "--"], name>, HelpText<help>;
+multiclass Eq<string name, string meta, string help> {
+  def _EQ : Joined<["-", "--"], name # "=">, MetaVarName<meta>, HelpText<help>;
+  def : Separate<["-", "--"], name>, Alias<!cast<Option>(NAME # "_EQ")>;
+}
+
+def help : F<"help", "Display available options">;
+def : F<"h", "Alias for --help">, Alias<help>;
+def version : F<"version", "Display the version">;
+def verbose : F<"verbose", "Enable verbose output">;
+
+defm only : Eq<"only", "<vendor>",
+  "Restrict to vendor: all (default), amdgpu, nvptx, intel">;
+defm hip_api_version : Eq<"hip-api-version", "<version>",
+  "HIP API version for device properties: auto (default), r0600, r0000, "
+  "unversioned">;
diff --git a/clang/tools/offload-arch/lib/AMDGPUArchByHIP.cpp 
b/clang/tools/offload-arch/lib/AMDGPUArchByHIP.cpp
index 36c3c98002a311..abefb17d36170e 100644
--- a/clang/tools/offload-arch/lib/AMDGPUArchByHIP.cpp
+++ b/clang/tools/offload-arch/lib/AMDGPUArchByHIP.cpp
@@ -11,9 +11,9 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include "OffloadArchOptions.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Sequence.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/Error.h"
@@ -61,27 +61,6 @@ typedef hipError_t 
(*hipGetDeviceProperties_t)(hipDeviceProp_tR0000 *, int);
 typedef hipError_t (*hipRuntimeGetVersion_t)(int *);
 typedef const char *(*hipGetErrorString_t)(hipError_t);
 
-extern cl::opt<bool> Verbose;
-
-cl::OptionCategory AMDGPUArchByHIPCategory("amdgpu-arch (HIP) options");
-
-enum class HipApiVersion {
-  Auto,       // Automatic fallback (R0600 -> R0000 -> unversioned)
-  R0600,      // Force R0600 API (HIP 6.x+)
-  R0000,      // Force R0000 API (legacy HIP)
-  Unversioned // Force unversioned API (very old HIP)
-};
-
-static cl::opt<HipApiVersion> HipApi(
-    "hip-api-version", cl::desc("Select HIP API version for device 
properties"),
-    cl::values(clEnumValN(HipApiVersion::Auto, "auto",
-                          "Auto-detect (R0600 -> R0000 -> unversioned)"),
-               clEnumValN(HipApiVersion::R0600, "r0600", "Force R0600 API"),
-               clEnumValN(HipApiVersion::R0000, "r0000", "Force R0000 API"),
-               clEnumValN(HipApiVersion::Unversioned, "unversioned",
-                          "Force unversioned API")),
-    cl::init(HipApiVersion::Auto), cl::cat(AMDGPUArchByHIPCategory));
-
 #ifdef _WIN32
 // Return candidate bin/ directories by walking parent dirs of ExeDir.
 SmallVector<std::string, 8> getCandidateBinPaths(StringRef ExeDir) {
diff --git a/clang/tools/offload-arch/lib/LevelZeroArch.cpp 
b/clang/tools/offload-arch/lib/LevelZeroArch.cpp
index 6720618d2a7d86..0ae0e4eed89e3f 100644
--- a/clang/tools/offload-arch/lib/LevelZeroArch.cpp
+++ b/clang/tools/offload-arch/lib/LevelZeroArch.cpp
@@ -11,7 +11,7 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include "llvm/Support/CommandLine.h"
+#include "OffloadArchOptions.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/Error.h"
 #include "llvm/TargetParser/IntelGPUTargetParser.h"
@@ -89,7 +89,6 @@ ze_result_t zeDeviceGet(ze_driver_handle_t hDriver, uint32_t 
*pCount,
 ze_result_t zeDeviceGetProperties(void *hDevice, void *pProperties);
 
 using namespace llvm;
-extern cl::opt<bool> Verbose;
 
 #define DEFINE_WRAPPER(NAME)                                                   
\
   using NAME##_ty = decltype(NAME);                                            
\
diff --git a/clang/tools/offload-arch/lib/NVPTXArch.cpp 
b/clang/tools/offload-arch/lib/NVPTXArch.cpp
index c6e728ac1d043f..387fc6810ddaf5 100644
--- a/clang/tools/offload-arch/lib/NVPTXArch.cpp
+++ b/clang/tools/offload-arch/lib/NVPTXArch.cpp
@@ -11,7 +11,7 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include "llvm/Support/CommandLine.h"
+#include "OffloadArchOptions.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/Error.h"
 #include <cstdint>
@@ -20,8 +20,6 @@
 
 using namespace llvm;
 
-extern cl::opt<bool> Verbose;
-
 typedef enum cudaError_enum {
   CUDA_SUCCESS = 0,
   CUDA_ERROR_NO_DEVICE = 100,
diff --git a/clang/tools/offload-arch/lib/OffloadArchOptions.cpp 
b/clang/tools/offload-arch/lib/OffloadArchOptions.cpp
index 5f6d334d7ddfe0..c6fe204779436c 100644
--- a/clang/tools/offload-arch/lib/OffloadArchOptions.cpp
+++ b/clang/tools/offload-arch/lib/OffloadArchOptions.cpp
@@ -1,22 +1,12 @@
-//===- OffloadArchOptions.cpp - options shared by offload-arch -*- C++ -*-===//
+//===- OffloadArchOptions.cpp - offload-arch settings 
---------------------===//
 //
 // 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
 //
 
//===----------------------------------------------------------------------===//
-//
-// This file defines the command line options used by both the offload-arch
-// tool and the vendor-specific GPU detection code.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Support/CommandLine.h"
-
-using namespace llvm;
 
-// Mark all our options with this category.
-cl::OptionCategory OffloadArchCategory("offload-arch options");
+#include "OffloadArchOptions.h"
 
-cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
-                      cl::init(false), cl::cat(OffloadArchCategory));
+bool Verbose = false;
+HipApiVersion HipApi = HipApiVersion::Auto;
diff --git a/clang/tools/offload-arch/lib/OffloadArchOptions.h 
b/clang/tools/offload-arch/lib/OffloadArchOptions.h
new file mode 100644
index 00000000000000..b83874169465ef
--- /dev/null
+++ b/clang/tools/offload-arch/lib/OffloadArchOptions.h
@@ -0,0 +1,23 @@
+//===- OffloadArchOptions.h - offload-arch settings ------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_TOOLS_OFFLOAD_ARCH_LIB_OFFLOADARCHOPTIONS_H
+#define CLANG_TOOLS_OFFLOAD_ARCH_LIB_OFFLOADARCHOPTIONS_H
+
+enum class HipApiVersion {
+  Auto,       // Automatic fallback (R0600 -> R0000 -> unversioned)
+  R0600,      // Force R0600 API (HIP 6.x+)
+  R0000,      // Force R0000 API (legacy HIP)
+  Unversioned // Force unversioned API (very old HIP)
+};
+
+// Settings shared by the offload-arch tool and the GPU detection code.
+extern bool Verbose;
+extern HipApiVersion HipApi;
+
+#endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/246825
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to