[llvm] [Option] Declare library command line options in TableGen (PR #226087)

Steffen Larsen via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 23:04:48 PDT 2026


================
@@ -1564,3 +1564,40 @@ TODO: complete this section
 :::{todo}
 TODO: fill in this section
 :::
+
+## Declaring a Library's Options in TableGen
+
+A library can declare its options in a `.td` file instead of as `cl::opt` globals.
+`llvm-tblgen -gen-opt-parser-defs` generates a struct with a member per option,
+the table that parses them, and the hooks through which
+`cl::ParseCommandLineOptions` parses them and `-help-hidden` lists them.
+
+```text
+include "llvm/Option/OptParser.td"
+
+def FooOptions : OptionsStruct;
+
+defm : BoolField<"foo-enable", "1", "Enable foo">;
+defm : ValueField<"foo-threshold", "unsigned", "8", "The threshold">;
+let Hidden = 0 in
+defm : ValueField<"foo-path", "StringRef", "\"-\"", "The input path">;
+```
+
+The struct is in namespace `llvm` unless the def names another, as in
+`OptionsStruct<"mlir">`. A member is named after its option, `foo_enable` for
+`-foo-enable`, unless the `defm` names it.
+
+A `BoolField` is set by `-x` or `-x=true|false|1|0`. A `ValueField` of an
+integer type, `double`, or `StringRef` is set by `-x=value` or `-x value`.
----------------
steffenlarsen wrote:

It reads a little like `x` here refers to an actual name and not just some placeholder, while other parts use the names from the example. Maybe either say "where `x` is the option name\*" or just use `foo_enable` instead of `x`.

\* "Option name" is a little overloaded, so maybe there is a better monocer?

https://github.com/llvm/llvm-project/pull/226087


More information about the llvm-commits mailing list