-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR] feat(mlir-tblgen): Add support for dialect interfaces #170046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
504bb5c
4ca9d25
e3d4a43
3f0fc8b
aa94279
0531140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,63 @@ if (DialectInlinerInterface *interface = dyn_cast<DialectInlinerInterface>(diale | |
| } | ||
| ``` | ||
|
|
||
| #### Utilizing the ODS framework | ||
|
|
||
| Note: Before reading this section, the reader should have some familiarity with | ||
| the concepts described in the | ||
| [`Operation Definition Specification`](DefiningDialects/Operations.md) documentation. | ||
|
|
||
| MLIR also supports defining dialect interfaces directly in **TableGen**. | ||
| This reduces boilerplate and allows authors to specify high-level interface | ||
| structure declaratively. | ||
|
|
||
| For example, the above interface can be defined using ODS as follows: | ||
| ```tablegen | ||
| def DialectInlinerInterface : DialectInterface<"DialectInlinerInterface"> { | ||
| let description = [{ | ||
| Define a base inlining interface class to allow for dialects to opt-in to | ||
| the inliner. | ||
| }]; | ||
|
|
||
| let methods = [ | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
|
||
| Returns true if the given region 'src' can be inlined into the region | ||
| 'dest' that is attached to an operation registered to the current dialect. | ||
| 'valueMapping' contains any remapped values from within the 'src' region. | ||
| This can be used to examine what values will replace entry arguments into | ||
| the 'src' region, for example. | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "isLegalToInline", | ||
| /*args=*/ (ins "Region *":$dest, "Region *":$src, | ||
| "IRMapping &":$valueMapping), | ||
|
||
| /*methodBody=*/ [{ | ||
| return false; | ||
| }] | ||
| > | ||
| ]; | ||
| } | ||
| ``` | ||
|
|
||
| `DialectInterfaces` class make use of the following components: | ||
|
|
||
| * C++ Class Name (Provided via template parameter) | ||
| - The name of the C++ interface class. | ||
| * Description (`description`) | ||
| - A string description of the interface, its invariants, example usages, | ||
| etc. | ||
| * C++ Namespace (`cppNamespace`) | ||
| - The C++ namespace that the interface class should be generated in. | ||
| * Methods (`methods`) | ||
| - The list of interface hook methods that are defined by the IR object. | ||
| - The structure of these methods is defined [here](#interface-methods). | ||
|
|
||
| The header file can be generated via the following command: | ||
| ```bash | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline above. You could perhaps even point to example in code / cmake usage. |
||
| mlir-tblgen gen-dialect-interface-decls DialectInterface.td | ||
|
||
| ``` | ||
|
|
||
| #### DialectInterfaceCollection | ||
|
|
||
| An additional utility is provided via `DialectInterfaceCollection`. This class | ||
|
|
@@ -364,10 +421,6 @@ void *TestDialect::getRegisteredInterfaceForOp(TypeID typeID, | |
|
|
||
| #### Utilizing the ODS Framework | ||
|
|
||
| Note: Before reading this section, the reader should have some familiarity with | ||
| the concepts described in the | ||
| [`Operation Definition Specification`](DefiningDialects/Operations.md) documentation. | ||
|
|
||
| As detailed above, [Interfaces](#attributeoperationtype-interfaces) allow for | ||
| attributes, operations, and types to expose method calls without requiring that | ||
| the caller know the specific derived type. The downside to this infrastructure, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #ifndef DIALECTINLINERINTERFACE | ||
| #define DIALECTINLINERINTERFACE | ||
|
|
||
| include "mlir/IR/Interfaces.td" | ||
|
|
||
| def DialectInlinerInterface : DialectInterface<"DialectInlinerInterface"> { | ||
| let description = [{ | ||
| This is the interface that must be implemented by the dialects of operations | ||
| to be inlined. This interface should only handle the operations of the | ||
| given dialect. | ||
| }]; | ||
|
|
||
|
||
|
|
||
| let cppNamespace = "::mlir"; | ||
|
|
||
| let methods = [ | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Returns true if the given operation 'callable', that implements the | ||
| 'CallableOpInterface', can be inlined into the position given call | ||
| operation 'call', that is registered to the current dialect and implements | ||
| the `CallOpInterface`. 'wouldBeCloned' is set to true if the region of the | ||
| given 'callable' is set to be cloned during the inlining process, or false | ||
| if the region is set to be moved in-place(i.e. no duplicates would be | ||
| created). | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "isLegalToInline", | ||
| /*args=*/ (ins "::mlir::Operation *":$call, "::mlir::Operation *":$callable, "bool":$wouldBeCloned), | ||
| /*methodBody=*/ [{ | ||
| return false; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Returns true if the given region 'src' can be inlined into the region | ||
| 'dest' that is attached to an operation registered to the current dialect. | ||
| 'wouldBeCloned' is set to true if the given 'src' region is set to be | ||
| cloned during the inlining process, or false if the region is set to be | ||
| moved in-place(i.e. no duplicates would be created). 'valueMapping' | ||
|
||
| contains any remapped values from within the 'src' region. This can be | ||
| used to examine what values will replace entry arguments into the 'src' | ||
| region for example. | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "isLegalToInline", | ||
| /*args=*/ (ins "::mlir::Region *":$dest, "::mlir::Region *":$src, "bool":$wouldBeCloned, | ||
| "::mlir::IRMapping &":$valueMapping), | ||
| /*methodBody=*/ [{ | ||
| return false; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Returns true if the given region 'src' can be inlined into the region | ||
| 'dest' that is attached to an operation registered to the current dialect. | ||
| 'wouldBeCloned' is set to true if the given 'src' region is set to be | ||
| cloned during the inlining process, or false if the region is set to be | ||
| moved in-place(i.e. no duplicates would be created). 'valueMapping' | ||
| contains any remapped values from within the 'src' region. This can be | ||
| used to examine what values will replace entry arguments into the 'src' | ||
| region for example. | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "isLegalToInline", | ||
| /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::Region *":$dest, "bool":$wouldBeCloned, | ||
| "::mlir::IRMapping &":$valueMapping), | ||
| /*methodBody=*/ [{ | ||
| return false; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| This hook is invoked on an operation that contains regions. It should | ||
| return true if the analyzer should recurse within the regions of this | ||
| operation when computing legality and cost, false otherwise. The default | ||
| implementation returns true. | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "shouldAnalyzeRecursively", | ||
| /*args=*/ (ins "::mlir::Operation *":$op), | ||
| /*methodBody=*/ [{ | ||
| return true; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Handle the given inlined terminator by replacing it with a new operation | ||
| as necessary. This overload is called when the inlined region has more | ||
| than one block. The 'newDest' block represents the new final branching | ||
| destination of blocks within this region, i.e. operations that release | ||
| control to the parent operation will likely now branch to this block. | ||
| Its block arguments correspond to any values that need to be replaced by | ||
| terminators within the inlined region. | ||
| }], | ||
| /*returnType=*/ "void", | ||
| /*methodName=*/ "handleTerminator", | ||
| /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::Block *":$newDest), | ||
| /*methodBody=*/ [{ | ||
| llvm_unreachable("must implement handleTerminator in the case of multiple " | ||
| "inlined blocks"); | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Handle the given inlined terminator by replacing it with a new operation | ||
| as necessary. This overload is called when the inlined region only | ||
| contains one block. 'valuesToReplace' contains the previously returned | ||
| values of the call site before inlining. These values must be replaced by | ||
| this callback if they had any users (for example for traditional function | ||
| calls, these are directly replaced with the operands of the `return` | ||
| operation). The given 'op' will be removed by the caller, after this | ||
| function has been called. | ||
| }], | ||
| /*returnType=*/ "void", | ||
| /*methodName=*/ "handleTerminator", | ||
| /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::ValueRange":$valuesToReplace), | ||
| /*methodBody=*/ [{ | ||
| llvm_unreachable( | ||
| "must implement handleTerminator in the case of one inlined block"); | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Attempt to materialize a conversion for a type mismatch between a call | ||
| from this dialect, and a callable region. This method should generate an | ||
| operation that takes 'input' as the only operand, and produces a single | ||
| result of 'resultType'. If a conversion can not be generated, nullptr | ||
| should be returned. For example, this hook may be invoked in the following | ||
| scenarios: | ||
| func @foo(i32) -> i32 { ... } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| // Mismatched input operand | ||
| ... = foo.call @foo(%input : i16) -> i32 | ||
|
|
||
| // Mismatched result type. | ||
| ... = foo.call @foo(%input : i32) -> i16 | ||
|
|
||
| NOTE: This hook may be invoked before the 'isLegal' checks above. | ||
| }], | ||
| /*returnType=*/ "::mlir::Operation *", | ||
| /*methodName=*/ "materializeCallConversion", | ||
| /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Value":$input, | ||
| "::mlir::Type":$resultType, "::mlir::Location":$conversionLoc), | ||
| /*methodBody=*/ [{ | ||
| return nullptr; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Hook to transform the call arguments before using them to replace the | ||
| callee arguments. Returns a value of the same type or the `argument` | ||
| itself if nothing changed. The `argumentAttrs` dictionary is non-null even | ||
| if no attribute is present. The hook is called after converting the | ||
| callsite argument types using the materializeCallConversion callback, and | ||
| right before inlining the callee region. Any operations created using the | ||
| provided `builder` are inserted right before the inlined callee region. An | ||
| example use case is the insertion of copies for by value arguments. | ||
| }], | ||
| /*returnType=*/ "::mlir::Value", | ||
| /*methodName=*/ "handleArgument", | ||
| /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Operation *":$call, | ||
| "::mlir::Operation *":$callable, "::mlir::Value":$argument, | ||
| "::mlir::DictionaryAttr":$argumentAttrs), | ||
| /*methodBody=*/ [{ | ||
| return argument; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Hook to transform the callee results before using them to replace the call | ||
| results. Returns a value of the same type or the `result` itself if | ||
| nothing changed. The `resultAttrs` dictionary is non-null even if no | ||
| attribute is present. The hook is called right before handling | ||
| terminators, and obtains the callee result before converting its type | ||
| using the `materializeCallConversion` callback. Any operations created | ||
| using the provided `builder` are inserted right after the inlined callee | ||
| region. An example use case is the insertion of copies for by value | ||
| results. NOTE: This hook is invoked after inlining the `callable` region. | ||
| }], | ||
| /*returnType=*/ "::mlir::Value", | ||
| /*methodName=*/ "handleResult", | ||
| /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Operation *":$call, | ||
| "::mlir::Operation *":$callable, "::mlir::Value":$result, | ||
| "::mlir::DictionaryAttr":$resultAttrs), | ||
| /*methodBody=*/ [{ | ||
| return result; | ||
| }] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Process a set of blocks that have been inlined for a call. This callback | ||
| is invoked before inlined terminator operations have been processed. | ||
| }], | ||
| /*returnType=*/ "void", | ||
| /*methodName=*/ "processInlinedCallBlocks", | ||
| /*args=*/ (ins "::mlir::Operation *":$call, | ||
| "::mlir::iterator_range<::mlir::Region::iterator>":$inlinedBlocks), | ||
| /*methodBody=*/ [{}] | ||
| >, | ||
| InterfaceMethod< | ||
| /*desc=*/ [{ | ||
| Returns true if the inliner can assume a fast path of not creating a new | ||
| block, if there is only one block. | ||
| }], | ||
| /*returnType=*/ "bool", | ||
| /*methodName=*/ "allowSingleBlockOptimization", | ||
| /*args=*/ (ins "::mlir::iterator_range<::mlir::Region::iterator>":$inlinedBlocks), | ||
| /*methodBody=*/ [{ | ||
| return true; | ||
| }] | ||
| > | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: newline above (certain markdown renders require separation between text and codeblock)