[clang] [cir-offload-merge] Switch to OptTable (PR #246762)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 13:55:17 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
Parse the command line with OptTable instead of cl::opt, as part of
removing cl::opt from clang/. Help output uses the OptTable format, and
LLVM's global options (e.g. -debug-only, --version) are no longer
accepted.
LLM-aided
---
Full diff: https://github.com/llvm/llvm-project/pull/246762.diff
3 Files Affected:
- (modified) clang/tools/cir-offload-merge/CMakeLists.txt (+12)
- (added) clang/tools/cir-offload-merge/Opts.td (+7)
- (modified) clang/tools/cir-offload-merge/cir-offload-merge.cpp (+43-21)
``````````diff
diff --git a/clang/tools/cir-offload-merge/CMakeLists.txt b/clang/tools/cir-offload-merge/CMakeLists.txt
index 3c78b25b6b9c59..ec6e08f76bad11 100644
--- a/clang/tools/cir-offload-merge/CMakeLists.txt
+++ b/clang/tools/cir-offload-merge/CMakeLists.txt
@@ -1,8 +1,20 @@
include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+set(LLVM_LINK_COMPONENTS
+ Option
+ Support
+ )
+
+set(LLVM_TARGET_DEFINITIONS Opts.td)
+llvm_tablegen(Opts.inc -gen-opt-parser-defs)
+add_public_tablegen_target(CIROffloadMergeOptsTableGen)
+
add_clang_tool(cir-offload-merge
cir-offload-merge.cpp
+
+ DEPENDS
+ CIROffloadMergeOptsTableGen
)
target_link_libraries(cir-offload-merge
diff --git a/clang/tools/cir-offload-merge/Opts.td b/clang/tools/cir-offload-merge/Opts.td
new file mode 100644
index 00000000000000..cfe8bd3a79c486
--- /dev/null
+++ b/clang/tools/cir-offload-merge/Opts.td
@@ -0,0 +1,7 @@
+include "llvm/Option/OptParser.td"
+
+def combine : Flag<["-", "--"], "combine">, HelpText<"Combine CIR inputs">;
+def help : Flag<["-", "--"], "help">, HelpText<"Display available options">;
+def o : Separate<["-", "--"], "o">, MetaVarName<"<filename>">,
+ HelpText<"Output CIR file">;
+def : Joined<["-", "--"], "o=">, Alias<o>;
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index cde2cd28345fe9..94c05147a9ed76 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -23,28 +23,34 @@
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/InitAllDialects.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/CommandLine.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/WithColor.h"
#include <optional>
#include <string>
-static llvm::cl::OptionCategory
- CIROffloadMergeCategory("cir-offload-merge options");
-
-static llvm::cl::opt<bool> Combine("combine",
- llvm::cl::desc("Combine CIR inputs"),
- llvm::cl::cat(CIROffloadMergeCategory));
-
-static llvm::cl::list<std::string>
- InputFileNames(llvm::cl::Positional, llvm::cl::desc("<input files>"),
- llvm::cl::cat(CIROffloadMergeCategory));
-
-static llvm::cl::opt<std::string>
- OutputFileName("o", llvm::cl::desc("Output CIR file"),
- llvm::cl::value_desc("filename"), llvm::cl::init("-"),
- llvm::cl::cat(CIROffloadMergeCategory));
+namespace {
+using namespace llvm::opt;
+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 CIROffloadMergeOptTable : public OptTable {
+public:
+ CIROffloadMergeOptTable() : OptTable(optionTables()) {
+ setDashDashParsing(true);
+ }
+};
+} // namespace
// The host/device role and language of a module come from the LangOptions
// that CIRGen records in cir.lowering_options.
@@ -79,7 +85,8 @@ static void setOffloadAttrs(mlir::ModuleOp moduleOp, llvm::StringRef name,
}
static mlir::OwningOpRef<mlir::ModuleOp>
-combineInputs(mlir::MLIRContext &context) {
+combineInputs(mlir::MLIRContext &context,
+ llvm::ArrayRef<std::string> InputFileNames) {
mlir::Location unknownLoc = mlir::UnknownLoc::get(&context);
mlir::OwningOpRef<mlir::ModuleOp> hostModule;
llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules;
@@ -166,9 +173,23 @@ static int writeModuleToOutput(mlir::ModuleOp moduleOp,
int main(int argc, char **argv) {
llvm::InitLLVM y(argc, argv);
- llvm::cl::HideUnrelatedOptions(CIROffloadMergeCategory);
- llvm::cl::ParseCommandLineOptions(argc, argv,
- "CIR host-device offload merge\n");
+ llvm::BumpPtrAllocator Alloc;
+ llvm::StringSaver Saver(Alloc);
+ CIROffloadMergeOptTable Tbl;
+ llvm::opt::InputArgList Args =
+ Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [](llvm::StringRef Msg) {
+ llvm::WithColor::error(llvm::errs(), "cir-offload-merge")
+ << Msg << '\n';
+ exit(1);
+ });
+ if (Args.hasArg(OPT_help)) {
+ Tbl.printHelp(llvm::outs(), "cir-offload-merge [options] <input files>",
+ "CIR host-device offload merge");
+ return 0;
+ }
+ bool Combine = Args.hasArg(OPT_combine);
+ std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT);
+ std::string OutputFileName = Args.getLastArgValue(OPT_o, "-").str();
mlir::DialectRegistry registry;
cir::registerAllDialects(registry);
@@ -185,7 +206,8 @@ int main(int argc, char **argv) {
return 1;
}
- mlir::OwningOpRef<mlir::ModuleOp> container = combineInputs(context);
+ mlir::OwningOpRef<mlir::ModuleOp> container =
+ combineInputs(context, InputFileNames);
if (!container)
return 1;
if (mlir::failed(mlir::verify(*container))) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/246762
More information about the cfe-commits
mailing list