WritingAPass doc: demonstrate registration of a non-default-constructible pass
authorAlex Zinenko <zinenko@google.com>
Fri, 2 Aug 2019 14:54:30 +0000 (07:54 -0700)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Fri, 2 Aug 2019 14:54:53 +0000 (07:54 -0700)
This functionality was added recently and is intended to ensure that parametric
passes can be configured programmatically and not only from command-line flags,
which are mostly useless outside of standalone mlir-opt biary.

PiperOrigin-RevId: 261320932

mlir/g3doc/WritingAPass.md

index 3ce12ab..dc06ace 100644 (file)
@@ -302,6 +302,25 @@ static PassRegistration<MyPass> pass("command-line-arg", "description");
     pass from `mlir-opt`.
 *   "description" is a description of the pass.
 
+For passes that cannot be default-constructed, use the third optional argument
+of `PassRegistration` that takes a callback creating the pass:
+
+```c++
+static PassRegistration<MyParametricPass> pass(
+    "command-line-arg", "description",
+    []() -> Pass * {
+      Pass *p = new MyParametricPass(/*options*/);
+      /*... non-trivial-logic to configure the pass ...*/;
+      return p;
+    });
+```
+
+This variant of registration can be used, for example, to accept the
+configuration of a pass from command-line arguments and pass it over to the pass
+constructor. Pass registration mechanism takes ownership of the pass. Make sure
+that the pass is copy-constructible in a way that does not share data since
+[pass manager](#pass-manager) may create copies of the pass to run in parallel.
+
 ### Pass Pipeline Registration
 
 Described above is the mechanism used for registering a specific derived pass