summaryrefslogtreecommitdiff
path: root/mlir/examples
diff options
context:
space:
mode:
authorMarkus Böck <markus.boeck02@gmail.com>2022-12-25 19:29:31 +0100
committerMarkus Böck <markus.boeck02@gmail.com>2023-01-11 14:32:21 +0100
commitbbfa7ef16dd9900b36abfa1a5f2faddb81afeb51 (patch)
tree3639e00eaaabccfb705bfa6bccdd9aaf7dc47c5d /mlir/examples
parentcf6f21751622fcae326d1fe13bc5afd74c4e720f (diff)
downloadllvm-bbfa7ef16dd9900b36abfa1a5f2faddb81afeb51.tar.gz
[mlir] Add a new fold API using Generic Adaptors
This is part of the RFC for a better fold API: https://discourse.llvm.org/t/rfc-a-better-fold-api-using-more-generic-adaptors/67374 This patch implements the required foldHook changes and the TableGen machinery for generating `fold` method signatures using `FoldAdaptor` for ops, based on the value of `useFoldAPI` of the dialect. It may be one of 2 values, with convenient named constants to create a quasi enum. The new `fold` method will then be generated if `kEmitFoldAdaptorFolder` is used. Since the new `FoldAdaptor` approach is strictly better than the old signature, part of this patch updates the documentation and all example to encourage use of the new `fold` signature. Included are also tests exercising the new API, ensuring proper construction of the `FoldAdaptor` and proper generation by TableGen. Differential Revision: https://reviews.llvm.org/D140886
Diffstat (limited to 'mlir/examples')
-rw-r--r--mlir/examples/toy/Ch7/include/toy/Ops.td2
-rw-r--r--mlir/examples/toy/Ch7/mlir/ToyCombine.cpp12
2 files changed, 6 insertions, 8 deletions
diff --git a/mlir/examples/toy/Ch7/include/toy/Ops.td b/mlir/examples/toy/Ch7/include/toy/Ops.td
index 08671a7347c1..504d316f2f93 100644
--- a/mlir/examples/toy/Ch7/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch7/include/toy/Ops.td
@@ -33,6 +33,8 @@ def Toy_Dialect : Dialect {
// We set this bit to generate the declarations for the dialect's type parsing
// and printing hooks.
let useDefaultTypePrinterParser = 1;
+
+ let useFoldAPI = kEmitFoldAdaptorFolder;
}
// Base class for toy dialect operations. This operation inherits from the base
diff --git a/mlir/examples/toy/Ch7/mlir/ToyCombine.cpp b/mlir/examples/toy/Ch7/mlir/ToyCombine.cpp
index 36ba04bdc5c4..62b00d99476a 100644
--- a/mlir/examples/toy/Ch7/mlir/ToyCombine.cpp
+++ b/mlir/examples/toy/Ch7/mlir/ToyCombine.cpp
@@ -24,18 +24,14 @@ namespace {
} // namespace
/// Fold constants.
-OpFoldResult ConstantOp::fold(ArrayRef<Attribute> operands) {
- return getValue();
-}
+OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
/// Fold struct constants.
-OpFoldResult StructConstantOp::fold(ArrayRef<Attribute> operands) {
- return getValue();
-}
+OpFoldResult StructConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
/// Fold simple struct access operations that access into a constant.
-OpFoldResult StructAccessOp::fold(ArrayRef<Attribute> operands) {
- auto structAttr = operands.front().dyn_cast_or_null<mlir::ArrayAttr>();
+OpFoldResult StructAccessOp::fold(FoldAdaptor adaptor) {
+ auto structAttr = adaptor.getInput().dyn_cast_or_null<mlir::ArrayAttr>();
if (!structAttr)
return nullptr;