summaryrefslogtreecommitdiff
path: root/mlir/docs
diff options
context:
space:
mode:
authorMarkus Böck <markus.boeck02@gmail.com>2023-01-22 16:11:27 +0100
committerMarkus Böck <markus.boeck02@gmail.com>2023-01-22 16:18:44 +0100
commit715b0258522ff3c99aa57801d1f4d2b1b7a90ee1 (patch)
tree49be74322ffa64aba932dfeab8518816d2981b66 /mlir/docs
parent6aa43fed4c17fe39ce17cc105aec9f838d1adbbd (diff)
downloadllvm-715b0258522ff3c99aa57801d1f4d2b1b7a90ee1.tar.gz
[mlir][ods] Simplify signature of `custom` printers and parsers of Attributes and Types in presence of default constructible parameters
The vast majority of parameters of C++ types used as parameters for Attributes and Types are likely to be default constructible. Nevertheless, TableGen conservatively generates code for the custom directive, expecting signatures using FailureOr<T> for all parameter types T to accomodate them possibly not being default constructible. This however reduces the ergonomics of the likely case of default constructible parameters. This patch fixes that issue, while barely changing the generated TableGen code, by using a helper function that is used to pass any parameters into custom parser methods. If the type is default constructible, as deemed by the C++ compiler, a default constructible instance is created and passed into the parser method by reference. In all other cases it is a Noop and a FailureOr is passed as before. Documentation was also updated to document the new behaviour. Fixes https://github.com/llvm/llvm-project/issues/60178 Differential Revision: https://reviews.llvm.org/D142301
Diffstat (limited to 'mlir/docs')
-rw-r--r--mlir/docs/DefiningDialects/AttributesAndTypes.md23
1 files changed, 19 insertions, 4 deletions
diff --git a/mlir/docs/DefiningDialects/AttributesAndTypes.md b/mlir/docs/DefiningDialects/AttributesAndTypes.md
index afceda9f3b50..e9f13e7cecb1 100644
--- a/mlir/docs/DefiningDialects/AttributesAndTypes.md
+++ b/mlir/docs/DefiningDialects/AttributesAndTypes.md
@@ -866,17 +866,33 @@ The `custom` directive `custom<Foo>($foo)` will in the parser and printer
respectively generate calls to:
```c++
-LogicalResult parseFoo(AsmParser &parser, FailureOr<int> &foo);
+LogicalResult parseFoo(AsmParser &parser, int &foo);
void printFoo(AsmPrinter &printer, int foo);
```
+As you can see, by default parameters are passed into the parse function by
+reference. This is only possible if the C++ type is default constructible.
+If the C++ type is not default constructible, the parameter is wrapped in a
+`FailureOr`. Therefore, given the following definition:
+
+```tablegen
+let parameters = (ins "NotDefaultConstructible":$foobar);
+let assemblyFormat = "custom<Fizz>($foobar)";
+```
+
+It will generate calls expecting the following signature for `parseFizz`:
+
+```c++
+LogicalResult parseFizz(AsmParser &parser, FailureOr<NotDefaultConstructible> &foobar);
+```
+
A previously bound variable can be passed as a parameter to a `custom` directive
by wrapping it in a `ref` directive. In the previous example, `$foo` is bound by
the first directive. The second directive references it and expects the
following printer and parser signatures:
```c++
-LogicalResult parseBar(AsmParser &parser, FailureOr<int> &bar, int foo);
+LogicalResult parseBar(AsmParser &parser, int &bar, int foo);
void printBar(AsmPrinter &printer, int bar, int foo);
```
@@ -885,8 +901,7 @@ is that the parameter for the parser must use the storage type of the parameter.
For example, `StringRefParameter` expects the parser and printer signatures as:
```c++
-LogicalResult parseStringParam(AsmParser &parser,
- FailureOr<std::string> &value);
+LogicalResult parseStringParam(AsmParser &parser, std::string &value);
void printStringParam(AsmPrinter &printer, StringRef value);
```