Skip to content

Commit 34a35a8

Browse files
committed
[mlir] Move FunctionInterfaces to Interfaces directory and inherit from CallableOpInterface
Functions are always callable operations and thus every operation implementing the `FunctionOpInterface` also implements the `CallableOpInterface`. The only exception was the FuncOp in the toy example. To make implementation of the `FunctionOpInterface` easier, this commit lets `FunctionOpInterface` inherit from `CallableOpInterface` and merges some of their methods. More precisely, the `CallableOpInterface` has methods to get the argument and result attributes and a method to get the result types of the callable region. These methods are always implemented the same way as their analogues in `FunctionOpInterface` and thus this commit moves all the argument and result attribute handling methods to the callable interface as well as the methods to get the argument and result types. The `FuntionOpInterface` then does not have to declare them as well, but just inherits them from the `CallableOpInterface`. Adding the inheritance relation also required to move the `FunctionOpInterface` from the IR directory to the Interfaces directory since IR should not depend on Interfaces. Reviewed By: jpienaar, springerm Differential Revision: https://reviews.llvm.org/D157988
1 parent 22044f0 commit 34a35a8

File tree

126 files changed

+394
-552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+394
-552
lines changed

mlir/docs/Interfaces.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,14 @@ interface section goes as follows:
731731
- `void setCalleeFromCallable(CallInterfaceCallable)`
732732
* `CallableOpInterface` - Used to represent the target callee of call.
733733
- `Region * getCallableRegion()`
734-
- `ArrayRef<Type> getCallableResults()`
735-
- `ArrayAttr getCallableArgAttrs()`
736-
- `ArrayAttr getCallableResAttrs()`
734+
- `ArrayRef<Type> getArgumentTypes()`
735+
- `ArrayRef<Type> getResultsTypes()`
736+
- `ArrayAttr getArgAttrsAttr()`
737+
- `ArrayAttr getResAttrsAttr()`
738+
- `void setArgAttrsAttr(ArrayAttr)`
739+
- `void setResAttrsAttr(ArrayAttr)`
740+
- `Attribute removeArgAttrsAttr()`
741+
- `Attribute removeResAttrsAttr()`
737742

738743
##### RegionKindInterfaces
739744

mlir/docs/Tutorials/Toy/Ch-4.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,6 @@ GenericCallOp. This means that we just need to provide a definition:
165165
/// Returns the region on the function operation that is callable.
166166
Region *FuncOp::getCallableRegion() { return &getBody(); }
167167

168-
/// Returns the results types that the callable region produces when
169-
/// executed.
170-
ArrayRef<Type> FuncOp::getCallableResults() { return getType().getResults(); }
171-
172-
/// Returns the argument attributes for all callable region arguments or
173-
/// null if there are none.
174-
ArrayAttr FuncOp::getCallableArgAttrs() {
175-
return getArgAttrs().value_or(nullptr);
176-
}
177-
178-
/// Returns the result attributes for all callable region results or
179-
/// null if there are none.
180-
ArrayAttr FuncOp::getCallableResAttrs() {
181-
return getResAttrs().value_or(nullptr);
182-
}
183-
184168
// ....
185169

186170
/// Return the callee of the generic call operation, this is required by the

mlir/examples/toy/Ch2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
2020
target_link_libraries(toyc-ch2
2121
PRIVATE
2222
MLIRAnalysis
23+
MLIRFunctionInterfaces
2324
MLIRIR
2425
MLIRParser
2526
MLIRSideEffectInterfaces

mlir/examples/toy/Ch2/include/toy/Dialect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/IR/Dialect.h"
19-
#include "mlir/IR/FunctionInterfaces.h"
2019
#include "mlir/IR/SymbolTable.h"
2120
#include "mlir/Interfaces/CallInterfaces.h"
21+
#include "mlir/Interfaces/FunctionInterfaces.h"
2222
#include "mlir/Interfaces/SideEffectInterfaces.h"
2323

2424
/// Include the auto-generated header file containing the declaration of the toy

mlir/examples/toy/Ch2/include/toy/Ops.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define TOY_OPS
1515

1616
include "mlir/IR/OpBase.td"
17-
include "mlir/IR/FunctionInterfaces.td"
17+
include "mlir/Interfaces/FunctionInterfaces.td"
1818
include "mlir/IR/SymbolInterfaces.td"
1919
include "mlir/Interfaces/SideEffectInterfaces.td"
2020

@@ -144,6 +144,7 @@ def FuncOp : Toy_Op<"func", [
144144
"StringRef":$name, "FunctionType":$type,
145145
CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)
146146
>];
147+
147148
let extraClassDeclaration = [{
148149
//===------------------------------------------------------------------===//
149150
// FunctionOpInterface Methods
@@ -154,7 +155,10 @@ def FuncOp : Toy_Op<"func", [
154155

155156
/// Returns the result types of this function.
156157
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
158+
159+
Region *getCallableRegion() { return &getBody(); }
157160
}];
161+
158162
let hasCustomAssemblyFormat = 1;
159163
let skipDefaultBuilders = 1;
160164
}

mlir/examples/toy/Ch2/mlir/Dialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
#include "mlir/IR/Builders.h"
1717
#include "mlir/IR/BuiltinTypes.h"
18-
#include "mlir/IR/FunctionImplementation.h"
1918
#include "mlir/IR/OpImplementation.h"
19+
#include "mlir/Interfaces/FunctionImplementation.h"
2020

2121
using namespace mlir;
2222
using namespace mlir::toy;

mlir/examples/toy/Ch3/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/)
2727
target_link_libraries(toyc-ch3
2828
PRIVATE
2929
MLIRAnalysis
30+
MLIRFunctionInterfaces
3031
MLIRIR
3132
MLIRParser
3233
MLIRPass

mlir/examples/toy/Ch3/include/toy/Dialect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/IR/Dialect.h"
19-
#include "mlir/IR/FunctionInterfaces.h"
2019
#include "mlir/IR/SymbolTable.h"
2120
#include "mlir/Interfaces/CallInterfaces.h"
21+
#include "mlir/Interfaces/FunctionInterfaces.h"
2222
#include "mlir/Interfaces/SideEffectInterfaces.h"
2323

2424
/// Include the auto-generated header file containing the declaration of the toy

mlir/examples/toy/Ch3/include/toy/Ops.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef TOY_OPS
1414
#define TOY_OPS
1515

16-
include "mlir/IR/FunctionInterfaces.td"
16+
include "mlir/Interfaces/FunctionInterfaces.td"
1717
include "mlir/IR/SymbolInterfaces.td"
1818
include "mlir/Interfaces/SideEffectInterfaces.td"
1919

@@ -153,6 +153,9 @@ def FuncOp : Toy_Op<"func", [
153153

154154
/// Returns the result types of this function.
155155
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
156+
157+
/// Returns the region on the current operation that is callable.
158+
::mlir::Region *getCallableRegion() { return &getBody(); }
156159
}];
157160
let hasCustomAssemblyFormat = 1;
158161
let skipDefaultBuilders = 1;

mlir/examples/toy/Ch3/mlir/Dialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
#include "mlir/IR/Builders.h"
1717
#include "mlir/IR/BuiltinTypes.h"
18-
#include "mlir/IR/FunctionImplementation.h"
1918
#include "mlir/IR/OpImplementation.h"
19+
#include "mlir/Interfaces/FunctionImplementation.h"
2020

2121
using namespace mlir;
2222
using namespace mlir::toy;

mlir/examples/toy/Ch4/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ target_link_libraries(toyc-ch4
3131
MLIRAnalysis
3232
MLIRCastInterfaces
3333
MLIRCallInterfaces
34+
MLIRFunctionInterfaces
3435
MLIRIR
3536
MLIRParser
3637
MLIRPass

mlir/examples/toy/Ch4/include/toy/Dialect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/IR/BuiltinTypes.h"
1919
#include "mlir/IR/Dialect.h"
20-
#include "mlir/IR/FunctionInterfaces.h"
2120
#include "mlir/IR/SymbolTable.h"
2221
#include "mlir/Interfaces/CallInterfaces.h"
2322
#include "mlir/Interfaces/CastInterfaces.h"
23+
#include "mlir/Interfaces/FunctionInterfaces.h"
2424
#include "mlir/Interfaces/SideEffectInterfaces.h"
2525
#include "toy/ShapeInferenceInterface.h"
2626

mlir/examples/toy/Ch4/include/toy/Ops.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef TOY_OPS
1414
#define TOY_OPS
1515

16-
include "mlir/IR/FunctionInterfaces.td"
16+
include "mlir/Interfaces/FunctionInterfaces.td"
1717
include "mlir/IR/SymbolInterfaces.td"
1818
include "mlir/Interfaces/CallInterfaces.td"
1919
include "mlir/Interfaces/CastInterfaces.td"
@@ -141,8 +141,7 @@ def CastOp : Toy_Op<"cast", [
141141
//===----------------------------------------------------------------------===//
142142

143143
def FuncOp : Toy_Op<"func", [
144-
DeclareOpInterfaceMethods<CallableOpInterface>, FunctionOpInterface,
145-
IsolatedFromAbove
144+
FunctionOpInterface, IsolatedFromAbove
146145
]> {
147146
let summary = "user defined function operation";
148147
let description = [{
@@ -173,6 +172,7 @@ def FuncOp : Toy_Op<"func", [
173172
"StringRef":$name, "FunctionType":$type,
174173
CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)
175174
>];
175+
176176
let extraClassDeclaration = [{
177177
//===------------------------------------------------------------------===//
178178
// FunctionOpInterface Methods
@@ -183,7 +183,10 @@ def FuncOp : Toy_Op<"func", [
183183

184184
/// Returns the result types of this function.
185185
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
186+
187+
Region *getCallableRegion() { return &getBody(); }
186188
}];
189+
187190
let hasCustomAssemblyFormat = 1;
188191
let skipDefaultBuilders = 1;
189192
}

mlir/examples/toy/Ch4/mlir/Dialect.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
#include "mlir/IR/Builders.h"
1717
#include "mlir/IR/BuiltinTypes.h"
18-
#include "mlir/IR/FunctionImplementation.h"
1918
#include "mlir/IR/OpImplementation.h"
19+
#include "mlir/Interfaces/FunctionImplementation.h"
2020
#include "mlir/Transforms/InliningUtils.h"
2121

2222
using namespace mlir;
@@ -298,27 +298,6 @@ void FuncOp::print(mlir::OpAsmPrinter &p) {
298298
getArgAttrsAttrName(), getResAttrsAttrName());
299299
}
300300

301-
/// Returns the region on the function operation that is callable.
302-
mlir::Region *FuncOp::getCallableRegion() { return &getBody(); }
303-
304-
/// Returns the results types that the callable region produces when
305-
/// executed.
306-
llvm::ArrayRef<mlir::Type> FuncOp::getCallableResults() {
307-
return getFunctionType().getResults();
308-
}
309-
310-
/// Returns the argument attributes for all callable region arguments or
311-
/// null if there are none.
312-
ArrayAttr FuncOp::getCallableArgAttrs() {
313-
return getArgAttrs().value_or(nullptr);
314-
}
315-
316-
/// Returns the result attributes for all callable region results or
317-
/// null if there are none.
318-
ArrayAttr FuncOp::getCallableResAttrs() {
319-
return getResAttrs().value_or(nullptr);
320-
}
321-
322301
//===----------------------------------------------------------------------===//
323302
// GenericCallOp
324303
//===----------------------------------------------------------------------===//

mlir/examples/toy/Ch5/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ target_link_libraries(toyc-ch5
3636
MLIRAnalysis
3737
MLIRCallInterfaces
3838
MLIRCastInterfaces
39+
MLIRFunctionInterfaces
3940
MLIRIR
4041
MLIRParser
4142
MLIRPass

mlir/examples/toy/Ch5/include/toy/Dialect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/IR/BuiltinTypes.h"
1919
#include "mlir/IR/Dialect.h"
20-
#include "mlir/IR/FunctionInterfaces.h"
2120
#include "mlir/IR/SymbolTable.h"
2221
#include "mlir/Interfaces/CallInterfaces.h"
2322
#include "mlir/Interfaces/CastInterfaces.h"
23+
#include "mlir/Interfaces/FunctionInterfaces.h"
2424
#include "mlir/Interfaces/SideEffectInterfaces.h"
2525
#include "toy/ShapeInferenceInterface.h"
2626

mlir/examples/toy/Ch5/include/toy/Ops.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef TOY_OPS
1414
#define TOY_OPS
1515

16-
include "mlir/IR/FunctionInterfaces.td"
16+
include "mlir/Interfaces/FunctionInterfaces.td"
1717
include "mlir/IR/SymbolInterfaces.td"
1818
include "mlir/Interfaces/CallInterfaces.td"
1919
include "mlir/Interfaces/CastInterfaces.td"
@@ -141,8 +141,7 @@ def CastOp : Toy_Op<"cast", [
141141
//===----------------------------------------------------------------------===//
142142

143143
def FuncOp : Toy_Op<"func", [
144-
DeclareOpInterfaceMethods<CallableOpInterface>, FunctionOpInterface,
145-
IsolatedFromAbove
144+
FunctionOpInterface, IsolatedFromAbove
146145
]> {
147146
let summary = "user defined function operation";
148147
let description = [{
@@ -183,6 +182,9 @@ def FuncOp : Toy_Op<"func", [
183182

184183
/// Returns the result types of this function.
185184
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
185+
186+
/// Returns the region on the function operation that is callable.
187+
Region *getCallableRegion() { return &getBody(); }
186188
}];
187189
let hasCustomAssemblyFormat = 1;
188190
let skipDefaultBuilders = 1;

mlir/examples/toy/Ch5/mlir/Dialect.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
#include "mlir/IR/Builders.h"
1717
#include "mlir/IR/BuiltinTypes.h"
18-
#include "mlir/IR/FunctionImplementation.h"
1918
#include "mlir/IR/OpImplementation.h"
19+
#include "mlir/Interfaces/FunctionImplementation.h"
2020
#include "mlir/Transforms/InliningUtils.h"
2121

2222
using namespace mlir;
@@ -298,27 +298,6 @@ void FuncOp::print(mlir::OpAsmPrinter &p) {
298298
getArgAttrsAttrName(), getResAttrsAttrName());
299299
}
300300

301-
/// Returns the region on the function operation that is callable.
302-
mlir::Region *FuncOp::getCallableRegion() { return &getBody(); }
303-
304-
/// Returns the results types that the callable region produces when
305-
/// executed.
306-
llvm::ArrayRef<mlir::Type> FuncOp::getCallableResults() {
307-
return getFunctionType().getResults();
308-
}
309-
310-
/// Returns the argument attributes for all callable region arguments or
311-
/// null if there are none.
312-
ArrayAttr FuncOp::getCallableArgAttrs() {
313-
return getArgAttrs().value_or(nullptr);
314-
}
315-
316-
/// Returns the result attributes for all callable region results or
317-
/// null if there are none.
318-
ArrayAttr FuncOp::getCallableResAttrs() {
319-
return getResAttrs().value_or(nullptr);
320-
}
321-
322301
//===----------------------------------------------------------------------===//
323302
// GenericCallOp
324303
//===----------------------------------------------------------------------===//

mlir/examples/toy/Ch6/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ target_link_libraries(toyc-ch6
5050
MLIRCallInterfaces
5151
MLIRCastInterfaces
5252
MLIRExecutionEngine
53+
MLIRFunctionInterfaces
5354
MLIRIR
5455
MLIRLLVMCommonConversion
5556
MLIRLLVMDialect

mlir/examples/toy/Ch6/include/toy/Dialect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/IR/BuiltinTypes.h"
1919
#include "mlir/IR/Dialect.h"
20-
#include "mlir/IR/FunctionInterfaces.h"
2120
#include "mlir/IR/SymbolTable.h"
2221
#include "mlir/Interfaces/CallInterfaces.h"
2322
#include "mlir/Interfaces/CastInterfaces.h"
23+
#include "mlir/Interfaces/FunctionInterfaces.h"
2424
#include "mlir/Interfaces/SideEffectInterfaces.h"
2525
#include "toy/ShapeInferenceInterface.h"
2626

mlir/examples/toy/Ch6/include/toy/Ops.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef TOY_OPS
1414
#define TOY_OPS
1515

16-
include "mlir/IR/FunctionInterfaces.td"
16+
include "mlir/Interfaces/FunctionInterfaces.td"
1717
include "mlir/IR/SymbolInterfaces.td"
1818
include "mlir/Interfaces/CallInterfaces.td"
1919
include "mlir/Interfaces/CastInterfaces.td"
@@ -141,8 +141,7 @@ def CastOp : Toy_Op<"cast", [
141141
//===----------------------------------------------------------------------===//
142142

143143
def FuncOp : Toy_Op<"func", [
144-
DeclareOpInterfaceMethods<CallableOpInterface>, FunctionOpInterface,
145-
IsolatedFromAbove
144+
FunctionOpInterface, IsolatedFromAbove
146145
]> {
147146
let summary = "user defined function operation";
148147
let description = [{
@@ -183,6 +182,9 @@ def FuncOp : Toy_Op<"func", [
183182

184183
/// Returns the result types of this function.
185184
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
185+
186+
/// Returns the region on the function operation that is callable.
187+
Region *getCallableRegion() { return &getBody(); }
186188
}];
187189
let hasCustomAssemblyFormat = 1;
188190
let skipDefaultBuilders = 1;

0 commit comments

Comments
 (0)