[llvm] [Option] Declare library command line options in TableGen (PR #226087)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 00:30:51 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.
----------------
MaskRay wrote:
updated to `defm threshold : ValueField<"foo-threshold", "unsigned", "8", "The threshold">;`
https://github.com/llvm/llvm-project/pull/226087
More information about the llvm-commits
mailing list