1# Table-driven Declarative Rewrite Rule (DRR) 2 3In addition to subclassing the `mlir::RewritePattern` C++ class, MLIR also 4supports defining rewrite rules in a declarative manner. Similar to 5[Op Definition Specification](OpDefinitions.md) (ODS), this is achieved via 6[TableGen][TableGen], which is a language to maintain records of domain-specific 7information. The rewrite rules are specified concisely in a TableGen record, 8which will be expanded into an equivalent `mlir::RewritePattern` subclass at 9compiler build time. 10 11This manual explains in detail all of the available mechanisms for defining 12rewrite rules in such a declarative manner. It aims to be a specification 13instead of a tutorial. Please refer to 14[Quickstart tutorial to adding MLIR graph 15rewrite](Tutorials/QuickstartRewrites.md) for the latter. 16 17Given that declarative rewrite rules depend on op definition specification, this 18manual assumes knowledge of the [ODS](OpDefinitions.md) doc. 19 20## Benefits 21 22Compared to the hand-written C++ classes, this declarative approach has several 23benefits, including but not limited to: 24 25* **Being declarative**: The pattern creator just needs to state the rewrite 26 pattern declaratively, without worrying about the concrete C++ methods to 27 call. 28* **Removing boilerplate and showing the very essence of the rewrite**: 29 `mlir::RewritePattern` is already good at hiding boilerplate for defining a 30 rewrite rule. But we still need to write the class and function structures 31 required by the C++ programming language, inspect ops for matching, and call 32 op `build()` methods for constructing. These statements are typically quite 33 simple and similar, so they can be further condensed with auto-generation. 34 Because we reduce the boilerplate to the bare minimum, the declarative 35 rewrite rule will just contain the very essence of the rewrite. This makes 36 it very easy to understand the pattern. 37 38## Strengths and Limitations 39 40The declarative rewrite rule is **operation-based**: it describes a rule to 41match against a directed acyclic graph (DAG) of operations and generate DAGs of 42operations. This gives DRR both its strengths and limitations: it is good at 43expressing op to op conversions, but not that well suited for, say, converting 44an op into a loop nest. 45 46Per the current implementation, DRR does not have good support for the following 47features: 48 49* Matching and generating ops with regions. 50* Matching and generating ops with block arguments. 51* Matching multi-result ops in nested patterns. 52* Matching and generating variadic operand/result ops in nested patterns. 53* Packing and unpacking variadic operands/results during generation. 54* [`NativeCodeCall`](#native-code-call-transforming-the-generated-op) 55 returning more than one results. 56 57## Rule Definition 58 59The core construct for defining a rewrite rule is defined in 60[`OpBase.td`][OpBase] as 61 62```tablegen 63class Pattern< 64 dag sourcePattern, list<dag> resultPatterns, 65 list<dag> additionalConstraints = [], 66 dag benefitsAdded = (addBenefit 0)>; 67``` 68 69A declarative rewrite rule contains two main components: 70 71* A _source pattern_, which is used for matching a DAG of operations. 72* One or more _result patterns_, which are used for generating DAGs of 73 operations to replace the matched DAG of operations. 74 75We allow multiple result patterns to support 76[multi-result ops](#supporting-multi-result-ops) and 77[auxiliary ops](#supporting-auxiliary-ops), but frequently we just want to 78convert one DAG of operations to another DAG of operations. There is a handy 79wrapper of `Pattern`, `Pat`, which takes a single result pattern: 80 81```tablegen 82class Pat< 83 dag sourcePattern, dag resultPattern, 84 list<dag> additionalConstraints = [], 85 dag benefitsAdded = (addBenefit 0)> : 86 Pattern<sourcePattern, [resultPattern], additionalConstraints, benefitAdded>; 87``` 88 89Each pattern is specified as a TableGen `dag` object with the syntax of 90`(operator arg0, arg1, ...)`. 91 92`operator` is typically an MLIR op, but it can also be other 93[directives](#special-directives). `argN` is for matching (if used in source 94pattern) or generating (if used in result pattern) the `N`-th argument for 95`operator`. If the `operator` is some MLIR operation, it means the `N`-th 96argument as specified in the `arguments` list of the op's definition. 97Therefore, we say op argument specification in pattern is **position-based**: 98the position where they appear matters. 99 100`argN` can be a `dag` object itself, thus we can have nested `dag` tree to model 101the def-use relationship between ops. 102 103### Source pattern 104 105The source pattern is for matching a DAG of operations. Arguments in the `dag` 106object are intended to **capture** the op arguments. They can also be used to 107**further limit** the match criteria. The capturing is done by specifying a 108symbol starting with the `$` sign, while further constraints are introduced by 109specifying a `TypeConstraint` (for an operand) or a `AttrConstraint` (for an 110attribute). 111 112#### Binding op arguments and limiting the match 113 114For example, 115 116```tablegen 117def AOp : Op<"a_op"> { 118 let arguments = (ins 119 AnyType:$a_input, 120 AnyAttr:$a_attr 121 ); 122 123 let results = (outs 124 AnyType:$a_output 125 ); 126} 127 128def : Pat<(AOp $input, F32Attr:$attr), ...>; 129``` 130 131In the above, we are matching an `AOp` whose `$input` can be anything valid as 132defined by the op and whose `$attr` must be a float attribute. If the match 133succeeds, we bind the `$input` symbol to the op's only input (`$a_input`) and 134`$attr` to the only attribute (`$a_attr`); we can reference them using `$input` 135and `$attr` in result patterns and additional constraints. 136 137The pattern is position-based: the symbol names used for capturing here do not 138need to match with the op definition as shown in the above example. As another 139example, the pattern can be written as `def : Pat<(AOp $a, F32Attr:$b), ...>;` 140and use `$a` and `$b` to refer to the captured input and attribute. But using 141the ODS name directly in the pattern is also allowed. Operands in the source 142pattern can have the same name. This bounds one operand to the name while 143verifying the rest are all equal. 144 145Also note that we only need to add `TypeConstraint` or `AttributeConstraint` 146when we need to further limit the match criteria. If all valid cases to the op 147are acceptable, then we can leave the constraint unspecified. 148 149`$_` is a special symbol to mean ignore capturing an argument. For example, 150`def : Pat<(AOp $_, $b), ...>` means only `$b` is interesting to capture and 151will be referenced later in result patterns. It's still possible to place 152additional constraints even if the symbol is not to be captured; for such case, 153you can simply use just the `TypeConstraint` or `AttributeConstraint` without a 154bound symbol, for example, `def : Pat<(AOp $a, F32Attr), ...>`. 155 156#### Matching DAG of operations 157 158To match a DAG of ops, use nested `dag` objects: 159 160```tablegen 161 162def BOp : Op<"b_op"> { 163 let arguments = (ins); 164 165 let results = (outs 166 AnyType:$b_output 167 ); 168} 169 170 171def : Pat<(AOp (BOp), $attr), ...>; 172``` 173 174The above pattern matches an `AOp` whose only operand is generated by a `BOp`, 175that is, the following MLIR code: 176 177```mlir 178%0 = "b_op"() : () -> (...) 179%1 = "a_op"(%0) {attr: ...} : () -> (...) 180``` 181 182#### Binding op results 183 184To bind a symbol to the results of a matched op for later reference, attach the 185symbol to the op itself: 186 187```tablegen 188def : Pat<(AOp (BOp:$b_result), $attr), ...>; 189``` 190 191The above will bind `$b_result` to the matched `BOp`'s result. (There are more 192details regarding multi-result ops, which is covered 193[later](#supporting-multi-result-ops).) 194 195### Result pattern 196 197The result pattern is for generating a DAG of operations. Arguments in the `dag` 198object are intended to **reference** values captured in the source pattern and 199potentially **apply transformations**. 200 201#### Referencing bound symbols 202 203For example, 204 205```tablegen 206def COp : Op<"c_op"> { 207 let arguments = (ins 208 AnyType:$c_input, 209 AnyAttr:$c_attr 210 ); 211 212 let results = (outs 213 AnyType:$c_output 214 ); 215} 216 217def : Pat<(AOp $input, $attr), (COp $input, $attr)>; 218``` 219 220In the above, `AOp`'s only operand and attribute are bound to `$input` and 221`$attr`, respectively. We then reference them in the result pattern for 222generating the `COp` by passing them in as arguments to `COp`'s `build()` 223method. 224 225We can also reference symbols bound to matched op's results: 226 227```tablegen 228def : Pat<(AOp (BOp:$b_result) $attr), (COp $b_result $attr)>; 229``` 230 231In the above, we are using `BOp`'s result for building `COp`. 232 233#### Building operations 234 235Given that `COp` was specified with table-driven op definition, there will be 236several `build()` methods generated for it. One of them has aggregated 237parameters for result types, operands, and attributes in the signature: `void 238COp::build(..., ArrayRef<Type> resultTypes, Array<Value> operands, 239ArrayRef<NamedAttribute> attr)`. The pattern in the above calls this `build()` 240method for constructing the `COp`. 241 242In general, arguments in the result pattern will be passed directly to the 243`build()` method to leverage the auto-generated `build()` method, list them in 244the pattern by following the exact same order as the ODS `arguments` definition. 245Otherwise, a custom `build()` method that matches the argument list is required. 246 247Right now all ODS-generated `build()` methods require specifying the result 248type(s), unless the op has known traits like `SameOperandsAndResultType` that 249we can use to auto-generate a `build()` method with result type deduction. 250When generating an op to replace the result of the matched root op, we can use 251the matched root op's result type when calling the ODS-generated builder. 252Otherwise (e.g., generating an [auxiliary op](#supporting-auxiliary-ops) or 253generating an op with a nested result pattern), DRR will not be able to deduce 254the result type(s). The pattern author will need to define a custom builder 255that has result type deduction ability via `OpBuilder` in ODS. For example, 256in the following pattern 257 258```tablegen 259def : Pat<(AOp $input, $attr), (COp (AOp $input, $attr) $attr)>; 260``` 261 262`AOp` is generated via a nested result pattern; DRR won't be able to deduce the 263result type for it. A custom builder for `AOp` should be defined and it should 264deduce the result type by itself. The builder should have the separate parameter 265for each operand and attribute and deduce the result type internally by itself. 266For example, for the above `AOp`, a possible builder is: 267 268```c++ 269 270void AOp::build(OpBuilder &builder, OperationState &state, 271 Value input, Attribute attr) { 272 state.addOperands({input}); 273 state.addAttribute("a_attr", attr); 274 Type type = ...; // Deduce result type here 275 state.addTypes({type}); 276} 277``` 278 279Failing to define such a builder will result in an error at C++ compilation time 280saying the call to `AOp::build()` cannot be resolved because of the number of 281parameters mismatch. 282 283#### Generating DAG of operations 284 285`dag` objects can be nested to generate a DAG of operations: 286 287```tablegen 288def : Pat<(AOp $input, $attr), (COp (BOp), $attr)>; 289``` 290 291In the above, we generate a `BOp`, and then use its result to generate the `COp` 292to replace the matched `AOp`. 293 294#### Binding op results 295 296In the result pattern, we can bind to the result(s) of a newly built op by 297attaching symbols to the op. (But we **cannot** bind to op arguments given that 298they are referencing previously bound symbols.) This is useful for reusing 299newly created results where suitable. For example, 300 301```tablegen 302def DOp : Op<"d_op"> { 303 let arguments = (ins 304 AnyType:$d_input1, 305 AnyType:$d_input2, 306 ); 307 308 let results = (outs 309 AnyType:$d_output 310 ); 311} 312 313def : Pat<(AOp $input, $ignored_attr), (DOp (BOp:$b_result) $b_result)>; 314``` 315 316In this pattern, an `AOp` is matched and replaced with a `DOp` whose two 317operands are from the result of a single `BOp`. This is only possible by binding 318the result of the `BOp` to a name and reuse it for the second operand of the 319`DOp` 320 321#### `NativeCodeCall`: transforming the generated op 322 323Sometimes the captured arguments are not exactly what we want so they cannot be 324directly fed in as arguments to build the new op. For such cases, we can apply 325transformations on the arguments by calling into C++ helper functions. This is 326achieved by `NativeCodeCall`. 327 328For example, if we want to capture some op's attributes and group them as an 329array attribute to construct a new op: 330 331```tablegen 332 333def TwoAttrOp : Op<"two_attr_op"> { 334 let arguments = (ins 335 AnyAttr:$op_attr1, 336 AnyAttr:$op_attr2 337 ); 338 339 let results = (outs 340 AnyType:$op_output 341 ); 342} 343 344def OneAttrOp : Op<"one_attr_op"> { 345 let arguments = (ins 346 ArrayAttr:$op_attr 347 ); 348 349 let results = (outs 350 AnyType:$op_output 351 ); 352} 353``` 354 355We can write a C++ helper function: 356 357```c++ 358Attribute createArrayAttr(Builder &builder, Attribute a, Attribute b) { 359 return builder.getArrayAttr({a, b}); 360} 361``` 362 363And then write the pattern as: 364 365```tablegen 366def createArrayAttr : NativeCodeCall<"createArrayAttr($_builder, $0, $1)">; 367 368def : Pat<(TwoAttrOp $attr1, $attr2), 369 (OneAttrOp (createArrayAttr $attr1, $attr2))>; 370``` 371 372And make sure the generated C++ code from the above pattern has access to the 373definition of the C++ helper function. 374 375In the above example, we are using a string to specialize the `NativeCodeCall` 376template. The string can be an arbitrary C++ expression that evaluates into 377some C++ object expected at the `NativeCodeCall` site (here it would be 378expecting an array attribute). Typically the string should be a function call. 379 380Note that currently `NativeCodeCall` must return no more than one value or 381attribute. This might change in the future. 382 383##### `NativeCodeCall` placeholders 384 385In `NativeCodeCall`, we can use placeholders like `$_builder`, `$N`. The former 386is called _special placeholder_, while the latter is called _positional 387placeholder_. 388 389`NativeCodeCall` right now only supports three special placeholders: 390`$_builder`, `$_loc`, and `$_self`: 391 392* `$_builder` will be replaced by the current `mlir::PatternRewriter`. 393* `$_loc` will be replaced by the fused location or custom location (as 394 determined by location directive). 395* `$_self` will be replaced with the entity `NativeCodeCall` is attached to. 396 397We have seen how `$_builder` can be used in the above; it allows us to pass a 398`mlir::Builder` (`mlir::PatternRewriter` is a subclass of `mlir::OpBuilder`, 399which is a subclass of `mlir::Builder`) to the C++ helper function to use the 400handy methods on `mlir::Builder`. 401 402`$_self` is useful when we want to write something in the form of 403`NativeCodeCall<"...">:$symbol`. For example, if we want to reverse the previous 404example and decompose the array attribute into two attributes: 405 406```tablegen 407class getNthAttr<int n> : NativeCodeCall<"$_self[" # n # "]">; 408 409def : Pat<(OneAttrOp $attr), 410 (TwoAttrOp (getNthAttr<0>:$attr), (getNthAttr<1>:$attr)>; 411``` 412 413In the above, `$_self` is substituted by the attribute bound by `$attr`, which 414is `OneAttrOp`'s array attribute. 415 416Positional placeholders will be substituted by the `dag` object parameters at 417the `NativeCodeCall` use site. For example, if we define `SomeCall : 418NativeCodeCall<"someFn($1, $2, $0)">` and use it like `(SomeCall $in0, $in1, 419$in2)`, then this will be translated into C++ call `someFn($in1, $in2, $in0)`. 420 421##### Customizing entire op building 422 423`NativeCodeCall` is not only limited to transforming arguments for building an 424op; it can be also used to specify how to build an op entirely. An example: 425 426If we have a C++ function for building an op: 427 428```c++ 429Operation *createMyOp(OpBuilder builder, Value input, Attribute attr); 430``` 431 432We can wrap it up and invoke it like: 433 434```tablegen 435def createMyOp : NativeCodeCall<"createMyOp($_builder, $0, $1)">; 436 437def : Pat<(... $input, $attr), (createMyOp $input, $attr)>; 438``` 439 440### Supporting auxiliary ops 441 442A declarative rewrite rule supports multiple result patterns. One of the 443purposes is to allow generating _auxiliary ops_. Auxiliary ops are operations 444used for building the replacement ops; but they are not directly used for 445replacement themselves. 446 447For the case of uni-result ops, if there are multiple result patterns, only the 448value generated from the last result pattern will be used to replace the matched 449root op's result; all other result patterns will be considered as generating 450auxiliary ops. 451 452Normally we want to specify ops as nested `dag` objects if their def-use 453relationship can be expressed in the way that an op's result can feed as the 454argument to consuming op. But that is not always possible. For example, if we 455want to allocate memory and store some computation (in pseudocode): 456 457```mlir 458%dst = addi %lhs, %rhs 459``` 460 461into 462 463```mlir 464%shape = shape %lhs 465%mem = alloc %shape 466%sum = addi %lhs, %rhs 467store %mem, %sum 468%dst = load %mem 469``` 470 471We cannot fit in with just one result pattern given `store` does not return a 472value. Instead we can use multiple result patterns: 473 474```tablegen 475def : Pattern<(AddIOp $lhs, $rhs), 476 [(StoreOp (AllocOp:$mem (ShapeOp $lhs)), (AddIOp $lhs, $rhs)), 477 (LoadOp $mem)]; 478``` 479 480In the above we use the first result pattern to generate the first four ops, and 481use the last pattern to generate the last op, which is used to replace the 482matched op. 483 484### Supporting multi-result ops 485 486Multi-result ops bring extra complexity to declarative rewrite rules. We use 487TableGen `dag` objects to represent ops in patterns; there is no native way to 488indicate that an op generates multiple results. The approach adopted is based 489on **naming convention**: a `__N` suffix is added to a symbol to indicate the 490`N`-th result. 491 492#### `__N` suffix 493 494The `__N` suffix is specifying the `N`-th result as a whole (which can be 495[variadic](#supporting-variadic-ops)). For example, we can bind a symbol to some 496multi-result op and reference a specific result later: 497 498```tablegen 499def ThreeResultOp : Op<"three_result_op"> { 500 let arguments = (ins ...); 501 502 let results = (outs 503 AnyTensor:$op_output1, 504 AnyTensor:$op_output2, 505 AnyTensor:$op_output3 506 ); 507} 508 509def : Pattern<(ThreeResultOp:$results ...), 510 [(... $results__0), ..., (... $results__2), ...]>; 511``` 512 513In the above pattern we bind `$results` to all the results generated by 514`ThreeResultOp` and references its `$input1` and `$input3` later in the result 515patterns. 516 517We can also bind a symbol and reference one of its specific result at the same 518time, which is typically useful when generating multi-result ops: 519 520```tablegen 521// TwoResultOp has similar definition as ThreeResultOp, but only has two 522// results. 523 524def : Pattern<(TwoResultOp ...), 525 [(ThreeResultOp:$results__2, ...), 526 (replaceWithValue $results__0)]>; 527``` 528 529In the above, we created a `ThreeResultOp` and bind `results` to its results, 530and uses its last result (`$output3`) and first result (`$output1`) to replace 531the `TwoResultOp`'s two results, respectively. 532 533#### Replacing multi-result ops 534 535The above example also shows how to replace a matched multi-result op. 536 537To replace an `N`-result op, the result patterns must generate at least `N` 538declared values (see [Declared vs. actual value](#declared-vs-actual-value) for 539definition). If there are more than `N` declared values generated, only the 540last `N` declared values will be used to replace the matched op. Note that 541because of the existence of multi-result op, one result pattern **may** generate 542multiple declared values. So it means we do not necessarily need `N` result 543patterns to replace an `N`-result op. For example, to replace an op with three 544results, you can have 545 546```tablegen 547// ThreeResultOp/TwoResultOp/OneResultOp generates three/two/one result(s), 548// respectively. 549 550// Replace each result with a result generated from an individual op. 551def : Pattern<(ThreeResultOp ...), 552 [(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>; 553 554// Replace the first two results with two results generated from the same op. 555def : Pattern<(ThreeResultOp ...), 556 [(TwoResultOp ...), (OneResultOp ...)]>; 557 558// Replace all three results with three results generated from the same op. 559def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>; 560 561def : Pattern<(ThreeResultOp ...), 562 [(AuxiliaryOp ...), (ThreeResultOp ...)]>; 563``` 564 565But using a single op to serve as both auxiliary op and replacement op is 566forbidden, i.e., the following is not allowed because that the first 567`TwoResultOp` generates two results but only the second result is used for 568replacing the matched op's result: 569 570```tablegen 571def : Pattern<(ThreeResultOp ...), 572 [(TwoResultOp ...), (TwoResultOp ...)]>; 573``` 574 575### Supporting variadic ops 576 577#### Declared vs. actual value 578 579Before going into details on variadic op support, we need to define a few terms 580regarding an op's values. 581 582* _Value_: either an operand or a result 583* _Declared operand/result/value_: an operand/result/value statically declared 584 in ODS of the op 585* _Actual operand/result/value_: an operand/result/value of an op instance at 586 runtime 587 588The above terms are needed because ops can have multiple results, and some of the 589results can also be variadic. For example, 590 591```tablegen 592def MultiVariadicOp : Op<"multi_variadic_op"> { 593 let arguments = (ins 594 AnyTensor:$input1, 595 Variadic<AnyTensor>:$input2, 596 AnyTensor:$input3 597 ); 598 599 let results = (outs 600 AnyTensor:$output1, 601 Variadic<AnyTensor>:$output2, 602 AnyTensor:$output3 603 ); 604} 605``` 606 607We say the above op has 3 declared operands and 3 declared results. But at 608runtime, an instance can have 3 values corresponding to `$input2` and 2 values 609correspond to `$output2`; we say it has 5 actual operands and 4 actual 610results. A variadic operand/result is a considered as a declared value that can 611correspond to multiple actual values. 612 613[TODO] 614 615### Supplying additional constraints 616 617Constraints can be placed on op arguments when matching. But sometimes we need 618to also place constraints on the matched op's results or sometimes need to limit 619the matching with some constraints that cover both the arguments and the 620results. The third parameter to `Pattern` (and `Pat`) is for this purpose. 621 622For example, we can write 623 624```tablegen 625def HasNoUseOf: Constraint<CPred<"$_self.use_empty()">, "has no use">; 626 627def HasSameElementType : Constraint< 628 CPred<"$0.cast<ShapedType>().getElementType() == " 629 "$1.cast<ShapedType>().getElementType()">, 630 "has same element type">; 631 632def : Pattern<(TwoResultOp:$results $input), 633 [(...), (...)], 634 [(F32Tensor:$results__0), (HasNoUseOf:$results__1), 635 (HasSameElementShape $results__0, $input)]>; 636``` 637 638You can 639 640* Use normal `TypeConstraint`s on previous bound symbols (the first result of 641 `TwoResultOp` must be a float tensor); 642* Define new `Constraint` for previous bound symbols (the second result of 643 `TwoResultOp` must has no use); 644* Apply constraints on multiple bound symbols (`$input` and `TwoResultOp`'s 645 first result must have the same element type). 646 647### Adjusting benefits 648 649The benefit of a `Pattern` is an integer value indicating the benefit of matching 650the pattern. It determines the priorities of patterns inside the pattern rewrite 651driver. A pattern with a higher benefit is applied before one with a lower 652benefit. 653 654In DRR, a rule is set to have a benefit of the number of ops in the source 655pattern. This is based on the heuristics and assumptions that: 656 657* Larger matches are more beneficial than smaller ones. 658* If a smaller one is applied first the larger one may not apply anymore. 659 660 661The fourth parameter to `Pattern` (and `Pat`) allows to manually tweak a 662pattern's benefit. Just supply `(addBenefit N)` to add `N` to the benefit value. 663 664## Rewrite directives 665 666### `location` 667 668By default the C++ pattern expanded from a DRR pattern uses the fused location 669of all source ops as the location for all generated ops. This is not always the 670best location mapping relationship. For such cases, DRR provides the `location` 671directive to provide finer control. 672 673`location` is of the following syntax: 674 675```tablegen 676(location $symbol0, $symbol1, ...) 677``` 678 679where all `$symbol` should be bound previously in the pattern and one optional 680string may be specified as an attribute. The following locations are created: 681 682* If only 1 symbol is specified then that symbol's location is used, 683* If multiple are specified then a fused location is created; 684* If no symbol is specified then string must be specified and a NamedLoc is 685 created instead; 686 687`location` must be used as the last argument to an op creation. For example, 688 689```tablegen 690def : Pat<(LocSrc1Op:$src1 (LocSrc2Op:$src2 ...), 691 (LocDst1Op (LocDst2Op ..., (location $src2)), (location "outer"))>; 692``` 693 694In the above pattern, the generated `LocDst2Op` will use the matched location 695of `LocSrc2Op` while the root `LocDst1Op` node will used the named location 696`outer`. 697 698### `replaceWithValue` 699 700The `replaceWithValue` directive is used to eliminate a matched op by replacing 701all of it uses with a captured value. It is of the following syntax: 702 703```tablegen 704(replaceWithValue $symbol) 705``` 706 707where `$symbol` should be a symbol bound previously in the pattern. 708 709For example, 710 711```tablegen 712def : Pat<(Foo $input), (replaceWithValue $input)>; 713``` 714 715The above pattern removes the `Foo` and replaces all uses of `Foo` with 716`$input`. 717 718## Debugging Tips 719 720### Run `mlir-tblgen` to see the generated content 721 722TableGen syntax sometimes can be obscure; reading the generated content can be 723a very helpful way to understand and debug issues. To build `mlir-tblgen`, run 724`cmake --build . --target mlir-tblgen` in your build directory and find the 725`mlir-tblgen` binary in the `bin/` subdirectory. All the supported generators 726can be found via `mlir-tblgen --help`. 727 728To see the generated code, invoke `mlir-tblgen` with a specific generator by 729providing include paths via `-I`. For example, 730 731```sh 732# To see all the C++ pattern rewrite classes 733mlir-tblgen --gen-rewriters -I /path/to/mlir/include /path/to/input/td/file 734``` 735 736### Compilation error: no matching member function for call to 'build' 737 738This is because DRR is failing to call a `build()` method with result type 739deduction ability. See [building operations](#building-operations) for more 740details. 741 742[TableGen]: https://llvm.org/docs/TableGen/index.html 743[OpBase]: https://github.com/llvm/llvm-project/blob/master/mlir/include/mlir/IR/OpBase.td 744