2019-02-18 23:21:12 +08:00
|
|
|
//===- RewriterGen.cpp - MLIR pattern rewriter generator ------------------===//
|
2018-12-12 19:09:11 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-12-12 19:09:11 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-12-12 19:09:11 +08:00
|
|
|
//
|
2018-12-27 20:56:03 +08:00
|
|
|
// RewriterGen uses pattern rewrite definitions to generate rewriter matchers.
|
2018-12-12 19:09:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
#include "mlir/Support/IndentedOstream.h"
|
2019-01-17 01:23:14 +08:00
|
|
|
#include "mlir/TableGen/Attribute.h"
|
2019-04-12 21:05:49 +08:00
|
|
|
#include "mlir/TableGen/Format.h"
|
2018-12-27 20:56:03 +08:00
|
|
|
#include "mlir/TableGen/GenInfo.h"
|
2018-12-29 04:02:08 +08:00
|
|
|
#include "mlir/TableGen/Operator.h"
|
2019-01-29 06:04:40 +08:00
|
|
|
#include "mlir/TableGen/Pattern.h"
|
2019-01-06 00:11:29 +08:00
|
|
|
#include "mlir/TableGen/Predicate.h"
|
2019-01-09 09:19:22 +08:00
|
|
|
#include "mlir/TableGen/Type.h"
|
2018-12-12 19:09:11 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2018-12-29 04:02:08 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2018-12-12 19:09:11 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-10-17 22:25:50 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2019-05-24 07:08:52 +08:00
|
|
|
#include "llvm/Support/FormatAdapters.h"
|
2018-12-12 19:09:11 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Main.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
using namespace mlir;
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
using namespace mlir::tblgen;
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
using llvm::formatv;
|
|
|
|
using llvm::Record;
|
|
|
|
using llvm::RecordKeeper;
|
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
#define DEBUG_TYPE "mlir-tblgen-rewritergen"
|
|
|
|
|
2019-05-24 07:08:52 +08:00
|
|
|
namespace llvm {
|
2020-06-26 19:20:44 +08:00
|
|
|
template <>
|
|
|
|
struct format_provider<mlir::tblgen::Pattern::IdentifierLine> {
|
2019-05-24 07:08:52 +08:00
|
|
|
static void format(const mlir::tblgen::Pattern::IdentifierLine &v,
|
|
|
|
raw_ostream &os, StringRef style) {
|
|
|
|
os << v.first << ":" << v.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2019-04-04 20:44:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PatternEmitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
namespace {
|
2019-01-29 06:04:40 +08:00
|
|
|
class PatternEmitter {
|
2018-12-29 04:02:08 +08:00
|
|
|
public:
|
2019-02-09 22:36:23 +08:00
|
|
|
PatternEmitter(Record *pat, RecordOperatorMap *mapper, raw_ostream &os);
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
// Emits the mlir::RewritePattern struct named `rewriteName`.
|
2018-12-29 23:55:08 +08:00
|
|
|
void emit(StringRef rewriteName);
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
private:
|
|
|
|
// Emits the code for matching ops.
|
2020-10-10 04:32:01 +08:00
|
|
|
void emitMatchLogic(DagNode tree, StringRef opName);
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
// Emits the code for rewriting ops.
|
|
|
|
void emitRewriteLogic();
|
2019-05-25 10:35:23 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Match utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
// Emits C++ statements for matching the DAG structure.
|
|
|
|
void emitMatch(DagNode tree, StringRef name, int depth);
|
|
|
|
|
|
|
|
// Emits C++ statements for matching using a native code call.
|
|
|
|
void emitNativeCodeMatch(DagNode tree, StringRef name, int depth);
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
// Emits C++ statements for matching the op constrained by the given DAG
|
2020-10-10 04:32:01 +08:00
|
|
|
// `tree` returning the op's variable name.
|
|
|
|
void emitOpMatch(DagNode tree, StringRef opName, int depth);
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-10-22 11:47:49 +08:00
|
|
|
// Emits C++ statements for matching the `argIndex`-th argument of the given
|
2021-01-08 10:44:33 +08:00
|
|
|
// DAG `tree` as an operand. operandIndex is the index in the DAG excluding
|
|
|
|
// the preceding attributes.
|
2020-10-10 04:32:01 +08:00
|
|
|
void emitOperandMatch(DagNode tree, StringRef opName, int argIndex,
|
2021-01-08 10:44:33 +08:00
|
|
|
int operandIndex, int depth);
|
2019-05-26 02:03:51 +08:00
|
|
|
|
2019-10-22 11:47:49 +08:00
|
|
|
// Emits C++ statements for matching the `argIndex`-th argument of the given
|
|
|
|
// DAG `tree` as an attribute.
|
2020-10-10 04:32:01 +08:00
|
|
|
void emitAttributeMatch(DagNode tree, StringRef opName, int argIndex,
|
|
|
|
int depth);
|
2019-02-02 07:40:22 +08:00
|
|
|
|
2020-03-18 11:10:27 +08:00
|
|
|
// Emits C++ for checking a match with a corresponding match failure
|
|
|
|
// diagnostic.
|
2020-10-10 04:32:01 +08:00
|
|
|
void emitMatchCheck(StringRef opName, const FmtObjectBase &matchFmt,
|
2020-03-18 11:10:27 +08:00
|
|
|
const llvm::formatv_object_base &failureFmt);
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
// Emits C++ for checking a match with a corresponding match failure
|
|
|
|
// diagnostics.
|
2020-10-10 04:32:01 +08:00
|
|
|
void emitMatchCheck(StringRef opName, const std::string &matchStr,
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
const std::string &failureStr);
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Rewrite utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// The entry point for handling a result pattern rooted at `resultTree`. This
|
|
|
|
// method dispatches to concrete handlers according to `resultTree`'s kind and
|
|
|
|
// returns a symbol representing the whole value pack. Callers are expected to
|
|
|
|
// further resolve the symbol according to the specific use case.
|
|
|
|
//
|
|
|
|
// `depth` is the nesting level of `resultTree`; 0 means top-level result
|
|
|
|
// pattern. For top-level result pattern, `resultIndex` indicates which result
|
|
|
|
// of the matched root op this pattern is intended to replace, which can be
|
|
|
|
// used to deduce the result type of the op generated from this result
|
|
|
|
// pattern.
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string handleResultPattern(DagNode resultTree, int resultIndex,
|
|
|
|
int depth);
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-04-23 05:13:45 +08:00
|
|
|
// Emits the C++ statement to replace the matched DAG with a value built via
|
|
|
|
// calling native C++ code.
|
2020-10-10 04:32:01 +08:00
|
|
|
std::string handleReplaceWithNativeCodeCall(DagNode resultTree, int depth);
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
// Returns the symbol of the old value serving as the replacement.
|
|
|
|
StringRef handleReplaceWithValue(DagNode tree);
|
|
|
|
|
2020-08-12 05:06:16 +08:00
|
|
|
// Returns the location value to use.
|
|
|
|
std::pair<bool, std::string> getLocation(DagNode tree);
|
|
|
|
|
2020-04-10 08:18:34 +08:00
|
|
|
// Returns the location value to use.
|
|
|
|
std::string handleLocationDirective(DagNode tree);
|
2019-02-09 22:36:23 +08:00
|
|
|
|
|
|
|
// Emits the C++ statement to build a new op out of the given DAG `tree` and
|
2019-03-09 05:57:09 +08:00
|
|
|
// returns the variable name that this op is assigned to. If the root op in
|
|
|
|
// DAG `tree` has a specified name, the created op will be assigned to a
|
|
|
|
// variable of the given name. Otherwise, a unique name will be used as the
|
|
|
|
// result value name.
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string handleOpCreation(DagNode tree, int resultIndex, int depth);
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
using ChildNodeIndexNameMap = DenseMap<unsigned, std::string>;
|
|
|
|
|
|
|
|
// Emits a local variable for each value and attribute to be used for creating
|
|
|
|
// an op.
|
|
|
|
void createSeparateLocalVarsForOpArgs(DagNode node,
|
|
|
|
ChildNodeIndexNameMap &childNodeNames);
|
|
|
|
|
2020-04-05 10:30:01 +08:00
|
|
|
// Emits the concrete arguments used to call an op's builder.
|
2019-11-15 23:33:21 +08:00
|
|
|
void supplyValuesForOpArgs(DagNode node,
|
2020-10-10 04:32:01 +08:00
|
|
|
const ChildNodeIndexNameMap &childNodeNames,
|
|
|
|
int depth);
|
2019-11-15 23:33:21 +08:00
|
|
|
|
|
|
|
// Emits the local variables for holding all values as a whole and all named
|
|
|
|
// attributes as a whole to be used for creating an op.
|
|
|
|
void createAggregateLocalVarsForOpArgs(
|
2020-10-10 04:32:01 +08:00
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames, int depth);
|
2019-11-15 23:33:21 +08:00
|
|
|
|
2019-04-01 23:58:53 +08:00
|
|
|
// Returns the C++ expression to construct a constant attribute of the given
|
|
|
|
// `value` for the given attribute kind `attr`.
|
|
|
|
std::string handleConstantAttr(Attribute attr, StringRef value);
|
2019-03-13 04:55:50 +08:00
|
|
|
|
|
|
|
// Returns the C++ expression to build an argument from the given DAG `leaf`.
|
|
|
|
// `patArgName` is used to bound the argument to the source pattern.
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
std::string handleOpArgument(DagLeaf leaf, StringRef patArgName);
|
2019-03-13 04:55:50 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// General utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Collects all of the operations within the given dag tree.
|
|
|
|
void collectOps(DagNode tree, llvm::SmallPtrSetImpl<const Operator *> &ops);
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
// Returns a unique symbol for a local variable of the given `op`.
|
|
|
|
std::string getUniqueSymbol(const Operator *op);
|
2019-08-06 22:09:55 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Symbol utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
// Returns how many static values the given DAG `node` correspond to.
|
|
|
|
int getNodeValueCount(DagNode node);
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
private:
|
|
|
|
// Pattern instantiation location followed by the location of multiclass
|
|
|
|
// prototypes used. This is intended to be used as a whole to
|
|
|
|
// PrintFatalError() on errors.
|
|
|
|
ArrayRef<llvm::SMLoc> loc;
|
2019-08-10 10:03:58 +08:00
|
|
|
|
|
|
|
// Op's TableGen Record to wrapper object.
|
2019-01-29 06:04:40 +08:00
|
|
|
RecordOperatorMap *opMap;
|
2019-08-10 10:03:58 +08:00
|
|
|
|
|
|
|
// Handy wrapper for pattern being emitted.
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
Pattern pattern;
|
2019-08-10 10:03:58 +08:00
|
|
|
|
|
|
|
// Map for all bound symbols' info.
|
|
|
|
SymbolInfoMap symbolInfoMap;
|
|
|
|
|
|
|
|
// The next unused ID for newly created values.
|
2019-02-09 22:36:23 +08:00
|
|
|
unsigned nextValueId;
|
2019-08-10 10:03:58 +08:00
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
raw_indented_ostream os;
|
2019-04-12 21:05:49 +08:00
|
|
|
|
2019-10-04 19:37:14 +08:00
|
|
|
// Format contexts containing placeholder substitutions.
|
2019-08-06 22:09:55 +08:00
|
|
|
FmtContext fmtCtx;
|
2019-05-26 02:03:51 +08:00
|
|
|
|
|
|
|
// Number of op processed.
|
|
|
|
int opCounter = 0;
|
2018-12-29 04:02:08 +08:00
|
|
|
};
|
2019-04-04 20:44:58 +08:00
|
|
|
} // end anonymous namespace
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-02-09 22:36:23 +08:00
|
|
|
PatternEmitter::PatternEmitter(Record *pat, RecordOperatorMap *mapper,
|
|
|
|
raw_ostream &os)
|
2019-04-04 20:44:58 +08:00
|
|
|
: loc(pat->getLoc()), opMap(mapper), pattern(pat, mapper),
|
2019-08-10 10:03:58 +08:00
|
|
|
symbolInfoMap(pat->getLoc()), nextValueId(0), os(os) {
|
2019-08-06 22:09:55 +08:00
|
|
|
fmtCtx.withBuilder("rewriter");
|
2019-04-12 21:05:49 +08:00
|
|
|
}
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-04-01 23:58:53 +08:00
|
|
|
std::string PatternEmitter::handleConstantAttr(Attribute attr,
|
|
|
|
StringRef value) {
|
2019-01-17 01:23:14 +08:00
|
|
|
if (!attr.isConstBuildable())
|
2019-05-11 07:11:02 +08:00
|
|
|
PrintFatalError(loc, "Attribute " + attr.getAttrDefName() +
|
2019-01-29 06:04:40 +08:00
|
|
|
" does not have the 'constBuilderCall' field");
|
2019-01-03 04:43:52 +08:00
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: Verify the constants here
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(tgfmt(attr.getConstBuilderTemplate(), &fmtCtx, value));
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
// Helper function to match patterns.
|
2020-10-10 04:32:01 +08:00
|
|
|
void PatternEmitter::emitMatch(DagNode tree, StringRef name, int depth) {
|
|
|
|
if (tree.isNativeCodeCall()) {
|
|
|
|
emitNativeCodeMatch(tree, name, depth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree.isOperation()) {
|
|
|
|
emitOpMatch(tree, name, depth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintFatalError(loc, "encountered non-op, non-NativeCodeCall match.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to match patterns.
|
|
|
|
void PatternEmitter::emitNativeCodeMatch(DagNode tree, StringRef opName,
|
|
|
|
int depth) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "handle NativeCodeCall matcher pattern: ");
|
|
|
|
LLVM_DEBUG(tree.print(llvm::dbgs()));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << '\n');
|
|
|
|
|
|
|
|
// TODO(suderman): iterate through arguments, determine their types, output
|
|
|
|
// names.
|
2021-03-03 17:04:08 +08:00
|
|
|
SmallVector<std::string, 8> capture;
|
2020-10-10 04:32:01 +08:00
|
|
|
|
|
|
|
raw_indented_ostream::DelimitedScope scope(os);
|
|
|
|
|
2021-02-03 02:24:06 +08:00
|
|
|
os << "if(!" << opName << ") return ::mlir::failure();\n";
|
2020-10-10 04:32:01 +08:00
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
|
|
|
std::string argName = formatv("arg{0}_{1}", depth, i);
|
|
|
|
if (DagNode argTree = tree.getArgAsNestedDag(i)) {
|
|
|
|
os << "Value " << argName << ";\n";
|
|
|
|
} else {
|
|
|
|
auto leaf = tree.getArgAsLeaf(i);
|
|
|
|
if (leaf.isAttrMatcher() || leaf.isConstantAttr()) {
|
|
|
|
os << "Attribute " << argName << ";\n";
|
2021-04-16 13:34:10 +08:00
|
|
|
} else {
|
|
|
|
os << "Value " << argName << ";\n";
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:04:08 +08:00
|
|
|
capture.push_back(std::move(argName));
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasLocationDirective;
|
|
|
|
std::string locToUse;
|
|
|
|
std::tie(hasLocationDirective, locToUse) = getLocation(tree);
|
|
|
|
|
|
|
|
auto fmt = tree.getNativeCodeTemplate();
|
2021-04-16 13:34:10 +08:00
|
|
|
if (fmt.count("$_self") != 1) {
|
|
|
|
PrintFatalError(loc, "NativeCodeCall must have $_self as argument for "
|
|
|
|
"passing the defining Operation");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nativeCodeCall = std::string(tgfmt(
|
|
|
|
fmt, &fmtCtx.addSubst("_loc", locToUse).withSelf(opName.str()), capture));
|
2020-10-10 04:32:01 +08:00
|
|
|
|
2021-02-03 02:24:06 +08:00
|
|
|
os << "if (failed(" << nativeCodeCall << ")) return ::mlir::failure();\n";
|
2020-10-10 04:32:01 +08:00
|
|
|
|
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
|
|
|
auto name = tree.getArgName(i);
|
|
|
|
if (!name.empty() && name != "_") {
|
2021-04-16 13:34:10 +08:00
|
|
|
os << formatv("{0} = {1};\n", name, capture[i]);
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
2021-04-16 13:34:10 +08:00
|
|
|
std::string argName = capture[i];
|
2020-10-10 04:32:01 +08:00
|
|
|
|
|
|
|
// Handle nested DAG construct first
|
|
|
|
if (DagNode argTree = tree.getArgAsNestedDag(i)) {
|
|
|
|
PrintFatalError(
|
|
|
|
loc, formatv("Matching nested tree in NativeCodecall not support for "
|
|
|
|
"{0} as arg {1}",
|
|
|
|
argName, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
DagLeaf leaf = tree.getArgAsLeaf(i);
|
2021-04-16 13:34:10 +08:00
|
|
|
|
|
|
|
// The parameter for native function doesn't bind any constraints.
|
|
|
|
if (leaf.isUnspecified())
|
|
|
|
continue;
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
auto constraint = leaf.getAsConstraint();
|
|
|
|
|
2021-04-16 13:34:10 +08:00
|
|
|
std::string self;
|
|
|
|
if (leaf.isAttrMatcher() || leaf.isConstantAttr())
|
|
|
|
self = argName;
|
|
|
|
else
|
|
|
|
self = formatv("{0}.getType()", argName);
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatchCheck(
|
|
|
|
opName,
|
|
|
|
tgfmt(constraint.getConditionTemplate(), &fmtCtx.withSelf(self)),
|
|
|
|
formatv("\"operand {0} of native code call '{1}' failed to satisfy "
|
|
|
|
"constraint: "
|
|
|
|
"'{2}'\"",
|
2021-01-07 06:08:03 +08:00
|
|
|
i, tree.getNativeCodeTemplate(), constraint.getSummary()));
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done emitting match for native code call\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to match patterns.
|
|
|
|
void PatternEmitter::emitOpMatch(DagNode tree, StringRef opName, int depth) {
|
2019-01-29 06:04:40 +08:00
|
|
|
Operator &op = tree.getDialectOp(opMap);
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "start emitting match for op '"
|
|
|
|
<< op.getOperationName() << "' at depth " << depth
|
|
|
|
<< '\n');
|
2019-06-20 05:32:07 +08:00
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
std::string castedName = formatv("castedOp{0}", depth);
|
|
|
|
os << formatv("auto {0} = ::llvm::dyn_cast_or_null<{2}>({1}); "
|
|
|
|
"(void){0};\n",
|
|
|
|
castedName, opName, op.getQualCppClassName());
|
2018-12-29 23:55:08 +08:00
|
|
|
// Skip the operand matching at depth 0 as the pattern rewriter already does.
|
|
|
|
if (depth != 0) {
|
2019-03-28 23:24:38 +08:00
|
|
|
// Skip if there is no defining operation (e.g., arguments to function).
|
2021-02-03 02:24:06 +08:00
|
|
|
os << formatv("if (!{0}) return ::mlir::failure();\n", castedName);
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
if (tree.getNumArgs() != op.getNumArgs()) {
|
|
|
|
PrintFatalError(loc, formatv("op '{0}' argument number mismatch: {1} in "
|
|
|
|
"pattern vs. {2} in definition",
|
|
|
|
op.getOperationName(), tree.getNumArgs(),
|
|
|
|
op.getNumArgs()));
|
|
|
|
}
|
|
|
|
|
2019-02-14 06:30:40 +08:00
|
|
|
// If the operand's name is set, set to that variable.
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
auto name = tree.getSymbol();
|
2019-02-14 06:30:40 +08:00
|
|
|
if (!name.empty())
|
2020-10-10 04:32:01 +08:00
|
|
|
os << formatv("{0} = {1};\n", name, castedName);
|
2019-02-14 06:30:40 +08:00
|
|
|
|
2020-12-01 08:22:36 +08:00
|
|
|
for (int i = 0, e = tree.getNumArgs(), nextOperand = 0; i != e; ++i) {
|
2019-01-08 01:52:26 +08:00
|
|
|
auto opArg = op.getArg(i);
|
2020-10-10 04:32:01 +08:00
|
|
|
std::string argName = formatv("op{0}", depth + 1);
|
2019-01-08 01:52:26 +08:00
|
|
|
|
2019-02-02 07:40:22 +08:00
|
|
|
// Handle nested DAG construct first
|
2019-01-29 06:04:40 +08:00
|
|
|
if (DagNode argTree = tree.getArgAsNestedDag(i)) {
|
2019-08-21 20:35:07 +08:00
|
|
|
if (auto *operand = opArg.dyn_cast<NamedTypeConstraint *>()) {
|
2020-04-11 05:11:45 +08:00
|
|
|
if (operand->isVariableLength()) {
|
2019-08-21 20:35:07 +08:00
|
|
|
auto error = formatv("use nested DAG construct to match op {0}'s "
|
|
|
|
"variadic operand #{1} unsupported now",
|
|
|
|
op.getOperationName(), i);
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "{\n";
|
2019-08-21 20:35:07 +08:00
|
|
|
|
2020-12-01 08:22:36 +08:00
|
|
|
// Attributes don't count for getODSOperands.
|
2021-04-16 13:34:10 +08:00
|
|
|
// TODO: Operand is a Value, check if we should remove `getDefiningOp()`.
|
2020-10-04 06:17:38 +08:00
|
|
|
os.indent() << formatv(
|
2020-10-10 04:32:01 +08:00
|
|
|
"auto *{0} = "
|
|
|
|
"(*{1}.getODSOperands({2}).begin()).getDefiningOp();\n",
|
2020-12-01 08:22:36 +08:00
|
|
|
argName, castedName, nextOperand++);
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatch(argTree, argName, depth + 1);
|
|
|
|
os << formatv("tblgen_ops[{0}] = {1};\n", ++opCounter, argName);
|
2020-10-04 06:17:38 +08:00
|
|
|
os.unindent() << "}\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
|
2019-02-02 07:40:22 +08:00
|
|
|
// Next handle DAG leaf: operand or attribute
|
2019-05-11 04:49:22 +08:00
|
|
|
if (opArg.is<NamedTypeConstraint *>()) {
|
2020-12-01 08:22:36 +08:00
|
|
|
// emitOperandMatch's argument indexing counts attributes.
|
2021-01-08 10:44:33 +08:00
|
|
|
emitOperandMatch(tree, castedName, i, nextOperand, depth);
|
2020-12-01 08:22:36 +08:00
|
|
|
++nextOperand;
|
2019-05-11 04:49:22 +08:00
|
|
|
} else if (opArg.is<NamedAttribute *>()) {
|
2020-10-10 04:32:01 +08:00
|
|
|
emitAttributeMatch(tree, opName, i, depth);
|
2019-02-02 07:40:22 +08:00
|
|
|
} else {
|
|
|
|
PrintFatalError(loc, "unhandled case when matching op");
|
2019-01-06 00:11:29 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done emitting match for op '"
|
|
|
|
<< op.getOperationName() << "' at depth " << depth
|
|
|
|
<< '\n');
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
void PatternEmitter::emitOperandMatch(DagNode tree, StringRef opName,
|
2021-01-08 10:44:33 +08:00
|
|
|
int argIndex, int operandIndex,
|
|
|
|
int depth) {
|
2019-02-02 07:40:22 +08:00
|
|
|
Operator &op = tree.getDialectOp(opMap);
|
2019-10-22 11:47:49 +08:00
|
|
|
auto *operand = op.getArg(argIndex).get<NamedTypeConstraint *>();
|
|
|
|
auto matcher = tree.getArgAsLeaf(argIndex);
|
2019-02-02 07:40:22 +08:00
|
|
|
|
|
|
|
// If a constraint is specified, we need to generate C++ statements to
|
|
|
|
// check the constraint.
|
|
|
|
if (!matcher.isUnspecified()) {
|
|
|
|
if (!matcher.isOperandMatcher()) {
|
|
|
|
PrintFatalError(
|
|
|
|
loc, formatv("the {1}-th argument of op '{0}' should be an operand",
|
2019-10-22 11:47:49 +08:00
|
|
|
op.getOperationName(), argIndex + 1));
|
2019-01-08 01:52:26 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
|
|
|
|
// Only need to verify if the matcher's type is different from the one
|
|
|
|
// of op definition.
|
2020-03-18 11:10:27 +08:00
|
|
|
Constraint constraint = matcher.getAsConstraint();
|
|
|
|
if (operand->constraint != constraint) {
|
2020-04-11 05:11:45 +08:00
|
|
|
if (operand->isVariableLength()) {
|
2019-08-21 20:35:07 +08:00
|
|
|
auto error = formatv(
|
|
|
|
"further constrain op {0}'s variadic operand #{1} unsupported now",
|
2019-10-22 11:47:49 +08:00
|
|
|
op.getOperationName(), argIndex);
|
2019-08-21 20:35:07 +08:00
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
2020-10-10 04:32:01 +08:00
|
|
|
auto self = formatv("(*{0}.getODSOperands({1}).begin()).getType()",
|
2021-01-08 10:44:33 +08:00
|
|
|
opName, operandIndex);
|
2020-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(
|
2020-10-10 04:32:01 +08:00
|
|
|
opName,
|
2020-03-18 11:10:27 +08:00
|
|
|
tgfmt(constraint.getConditionTemplate(), &fmtCtx.withSelf(self)),
|
|
|
|
formatv("\"operand {0} of op '{1}' failed to satisfy constraint: "
|
|
|
|
"'{2}'\"",
|
|
|
|
operand - op.operand_begin(), op.getOperationName(),
|
2021-01-07 06:08:03 +08:00
|
|
|
constraint.getSummary()));
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capture the value
|
2019-10-22 11:47:49 +08:00
|
|
|
auto name = tree.getArgName(argIndex);
|
2019-12-02 23:54:23 +08:00
|
|
|
// `$_` is a special symbol to ignore op argument matching.
|
|
|
|
if (!name.empty() && name != "_") {
|
2019-10-22 11:47:49 +08:00
|
|
|
// We need to subtract the number of attributes before this operand to get
|
|
|
|
// the index in the operand list.
|
|
|
|
auto numPrevAttrs = std::count_if(
|
|
|
|
op.arg_begin(), op.arg_begin() + argIndex,
|
|
|
|
[](const Argument &arg) { return arg.is<NamedAttribute *>(); });
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
auto res = symbolInfoMap.findBoundSymbol(name, op, argIndex);
|
2020-10-10 04:32:01 +08:00
|
|
|
os << formatv("{0} = {1}.getODSOperands({2});\n",
|
|
|
|
res->second.getVarName(name), opName,
|
|
|
|
argIndex - numPrevAttrs);
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
|
|
|
|
int argIndex, int depth) {
|
2019-02-02 07:40:22 +08:00
|
|
|
Operator &op = tree.getDialectOp(opMap);
|
2019-10-22 11:47:49 +08:00
|
|
|
auto *namedAttr = op.getArg(argIndex).get<NamedAttribute *>();
|
2019-04-05 00:25:38 +08:00
|
|
|
const auto &attr = namedAttr->attr;
|
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "{\n";
|
2020-10-10 04:32:01 +08:00
|
|
|
os.indent() << formatv("auto tblgen_attr = {0}->getAttrOfType<{1}>(\"{2}\");"
|
|
|
|
"(void)tblgen_attr;\n",
|
|
|
|
opName, attr.getStorageType(), namedAttr->name);
|
2019-04-05 00:25:38 +08:00
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: This should use getter method to avoid duplication.
|
2019-12-03 01:33:24 +08:00
|
|
|
if (attr.hasDefaultValue()) {
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "if (!tblgen_attr) tblgen_attr = "
|
|
|
|
<< std::string(tgfmt(attr.getConstBuilderTemplate(), &fmtCtx,
|
|
|
|
attr.getDefaultValue()))
|
|
|
|
<< ";\n";
|
2019-04-05 00:25:38 +08:00
|
|
|
} else if (attr.isOptional()) {
|
2019-08-06 22:09:55 +08:00
|
|
|
// For a missing attribute that is optional according to definition, we
|
2019-10-04 19:37:14 +08:00
|
|
|
// should just capture a mlir::Attribute() to signal the missing state.
|
2019-04-05 00:25:38 +08:00
|
|
|
// That is precisely what getAttr() returns on missing attributes.
|
|
|
|
} else {
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatchCheck(opName, tgfmt("tblgen_attr", &fmtCtx),
|
2020-03-18 11:10:27 +08:00
|
|
|
formatv("\"expected op '{0}' to have attribute '{1}' "
|
|
|
|
"of type '{2}'\"",
|
|
|
|
op.getOperationName(), namedAttr->name,
|
|
|
|
attr.getStorageType()));
|
2019-04-05 00:25:38 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
|
2019-10-22 11:47:49 +08:00
|
|
|
auto matcher = tree.getArgAsLeaf(argIndex);
|
2019-02-10 09:38:24 +08:00
|
|
|
if (!matcher.isUnspecified()) {
|
|
|
|
if (!matcher.isAttrMatcher()) {
|
|
|
|
PrintFatalError(
|
|
|
|
loc, formatv("the {1}-th argument of op '{0}' should be an attribute",
|
2019-10-22 11:47:49 +08:00
|
|
|
op.getOperationName(), argIndex + 1));
|
2019-02-10 09:38:24 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
|
2019-02-10 09:38:24 +08:00
|
|
|
// If a constraint is specified, we need to generate C++ statements to
|
|
|
|
// check the constraint.
|
2020-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(
|
2020-10-10 04:32:01 +08:00
|
|
|
opName,
|
2020-03-18 11:10:27 +08:00
|
|
|
tgfmt(matcher.getConditionTemplate(), &fmtCtx.withSelf("tblgen_attr")),
|
|
|
|
formatv("\"op '{0}' attribute '{1}' failed to satisfy constraint: "
|
|
|
|
"{2}\"",
|
|
|
|
op.getOperationName(), namedAttr->name,
|
2021-01-07 06:08:03 +08:00
|
|
|
matcher.getAsConstraint().getSummary()));
|
2019-02-10 09:38:24 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
|
|
|
|
// Capture the value
|
2019-10-22 11:47:49 +08:00
|
|
|
auto name = tree.getArgName(argIndex);
|
2019-12-02 23:54:23 +08:00
|
|
|
// `$_` is a special symbol to ignore op argument matching.
|
|
|
|
if (!name.empty() && name != "_") {
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("{0} = tblgen_attr;\n", name);
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2019-04-05 00:25:38 +08:00
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
os.unindent() << "}\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2020-03-18 11:10:27 +08:00
|
|
|
void PatternEmitter::emitMatchCheck(
|
2020-10-10 04:32:01 +08:00
|
|
|
StringRef opName, const FmtObjectBase &matchFmt,
|
2020-03-18 11:10:27 +08:00
|
|
|
const llvm::formatv_object_base &failureFmt) {
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatchCheck(opName, matchFmt.str(), failureFmt.str());
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
void PatternEmitter::emitMatchCheck(StringRef opName,
|
|
|
|
const std::string &matchStr,
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
const std::string &failureStr) {
|
2020-10-10 04:32:01 +08:00
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
os << "if (!(" << matchStr << "))";
|
2020-10-10 04:32:01 +08:00
|
|
|
os.scope("{\n", "\n}\n").os << "return rewriter.notifyMatchFailure(" << opName
|
|
|
|
<< ", [&](::mlir::Diagnostic &diag) {\n diag << "
|
|
|
|
<< failureStr << ";\n});";
|
2020-03-18 11:10:27 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
void PatternEmitter::emitMatchLogic(DagNode tree, StringRef opName) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "--- start emitting match logic ---\n");
|
2020-03-18 11:10:27 +08:00
|
|
|
int depth = 0;
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatch(tree, opName, depth);
|
2019-02-14 06:30:40 +08:00
|
|
|
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
for (auto &appliedConstraint : pattern.getConstraints()) {
|
|
|
|
auto &constraint = appliedConstraint.constraint;
|
|
|
|
auto &entities = appliedConstraint.entities;
|
|
|
|
|
|
|
|
auto condition = constraint.getConditionTemplate();
|
|
|
|
if (isa<TypeConstraint>(constraint)) {
|
2020-01-12 00:54:04 +08:00
|
|
|
auto self = formatv("({0}.getType())",
|
2019-08-21 20:35:07 +08:00
|
|
|
symbolInfoMap.getValueAndRangeUse(entities.front()));
|
2020-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(
|
2020-10-10 04:32:01 +08:00
|
|
|
opName, tgfmt(condition, &fmtCtx.withSelf(self.str())),
|
2020-03-18 11:10:27 +08:00
|
|
|
formatv("\"value entity '{0}' failed to satisfy constraint: {1}\"",
|
2021-01-07 06:08:03 +08:00
|
|
|
entities.front(), constraint.getSummary()));
|
2020-03-18 11:10:27 +08:00
|
|
|
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
} else if (isa<AttrConstraint>(constraint)) {
|
|
|
|
PrintFatalError(
|
|
|
|
loc, "cannot use AttrConstraint in Pattern multi-entity constraints");
|
2019-02-14 06:30:40 +08:00
|
|
|
} else {
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: replace formatv arguments with the exact specified
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
// args.
|
|
|
|
if (entities.size() > 4) {
|
|
|
|
PrintFatalError(loc, "only support up to 4-entity constraints now");
|
|
|
|
}
|
|
|
|
SmallVector<std::string, 4> names;
|
2019-05-04 10:48:57 +08:00
|
|
|
int i = 0;
|
|
|
|
for (int e = entities.size(); i < e; ++i)
|
2019-08-21 20:35:07 +08:00
|
|
|
names.push_back(symbolInfoMap.getValueAndRangeUse(entities[i]));
|
2019-08-02 02:50:47 +08:00
|
|
|
std::string self = appliedConstraint.self;
|
|
|
|
if (!self.empty())
|
2019-08-21 20:35:07 +08:00
|
|
|
self = symbolInfoMap.getValueAndRangeUse(self);
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
for (; i < 4; ++i)
|
|
|
|
names.push_back("<unused>");
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatchCheck(opName,
|
2020-03-18 11:10:27 +08:00
|
|
|
tgfmt(condition, &fmtCtx.withSelf(self), names[0],
|
|
|
|
names[1], names[2], names[3]),
|
|
|
|
formatv("\"entities '{0}' failed to satisfy constraint: "
|
|
|
|
"{1}\"",
|
|
|
|
llvm::join(entities, ", "),
|
2021-01-07 06:08:03 +08:00
|
|
|
constraint.getSummary()));
|
2019-02-14 06:30:40 +08:00
|
|
|
}
|
|
|
|
}
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
|
|
|
|
// Some of the operands could be bound to the same symbol name, we need
|
|
|
|
// to enforce equality constraint on those.
|
|
|
|
// TODO: we should be able to emit equality checks early
|
|
|
|
// and short circuit unnecessary work if vars are not equal.
|
|
|
|
for (auto symbolInfoIt = symbolInfoMap.begin();
|
|
|
|
symbolInfoIt != symbolInfoMap.end();) {
|
|
|
|
auto range = symbolInfoMap.getRangeOfEqualElements(symbolInfoIt->first);
|
|
|
|
auto startRange = range.first;
|
|
|
|
auto endRange = range.second;
|
|
|
|
|
|
|
|
auto firstOperand = symbolInfoIt->second.getVarName(symbolInfoIt->first);
|
|
|
|
for (++startRange; startRange != endRange; ++startRange) {
|
|
|
|
auto secondOperand = startRange->second.getVarName(symbolInfoIt->first);
|
|
|
|
emitMatchCheck(
|
2020-10-10 04:32:01 +08:00
|
|
|
opName,
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
formatv("*{0}.begin() == *{1}.begin()", firstOperand, secondOperand),
|
|
|
|
formatv("\"Operands '{0}' and '{1}' must be equal\"", firstOperand,
|
|
|
|
secondOperand));
|
|
|
|
}
|
|
|
|
|
|
|
|
symbolInfoIt = endRange;
|
|
|
|
}
|
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "--- done emitting match logic ---\n");
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:35:23 +08:00
|
|
|
void PatternEmitter::collectOps(DagNode tree,
|
|
|
|
llvm::SmallPtrSetImpl<const Operator *> &ops) {
|
|
|
|
// Check if this tree is an operation.
|
2019-10-17 22:25:50 +08:00
|
|
|
if (tree.isOperation()) {
|
|
|
|
const Operator &op = tree.getDialectOp(opMap);
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "found operation " << op.getOperationName() << '\n');
|
|
|
|
ops.insert(&op);
|
|
|
|
}
|
2019-05-25 10:35:23 +08:00
|
|
|
|
|
|
|
// Recurse the arguments of the tree.
|
|
|
|
for (unsigned i = 0, e = tree.getNumArgs(); i != e; ++i)
|
|
|
|
if (auto child = tree.getArgAsNestedDag(i))
|
|
|
|
collectOps(child, ops);
|
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emit(StringRef rewriteName) {
|
2019-08-06 22:09:55 +08:00
|
|
|
// Get the DAG tree for the source pattern.
|
|
|
|
DagNode sourceTree = pattern.getSourcePattern();
|
2019-01-29 06:04:40 +08:00
|
|
|
|
|
|
|
const Operator &rootOp = pattern.getSourceRootOp();
|
|
|
|
auto rootName = rootOp.getOperationName();
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-05-25 10:35:23 +08:00
|
|
|
// Collect the set of result operations.
|
2019-08-06 22:09:55 +08:00
|
|
|
llvm::SmallPtrSet<const Operator *, 4> resultOps;
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "start collecting ops used in result patterns\n");
|
|
|
|
for (unsigned i = 0, e = pattern.getNumResultPatterns(); i != e; ++i) {
|
2019-08-06 22:09:55 +08:00
|
|
|
collectOps(pattern.getResultPattern(i), resultOps);
|
2019-10-17 22:25:50 +08:00
|
|
|
}
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done collecting ops used in result patterns\n");
|
2019-05-25 10:35:23 +08:00
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
// Emit RewritePattern for Pattern.
|
2019-05-24 07:08:52 +08:00
|
|
|
auto locs = pattern.getLocation();
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("/* Generated from:\n {0:$[ instantiating\n ]}\n*/\n",
|
2019-05-24 07:08:52 +08:00
|
|
|
make_range(locs.rbegin(), locs.rend()));
|
2020-06-26 19:20:44 +08:00
|
|
|
os << formatv(R"(struct {0} : public ::mlir::RewritePattern {
|
|
|
|
{0}(::mlir::MLIRContext *context)
|
2021-03-24 04:44:14 +08:00
|
|
|
: ::mlir::RewritePattern("{1}", {2}, context, {{)",
|
|
|
|
rewriteName, rootName, pattern.getBenefit());
|
2019-09-27 05:00:22 +08:00
|
|
|
// Sort result operators by name.
|
|
|
|
llvm::SmallVector<const Operator *, 4> sortedResultOps(resultOps.begin(),
|
|
|
|
resultOps.end());
|
|
|
|
llvm::sort(sortedResultOps, [&](const Operator *lhs, const Operator *rhs) {
|
|
|
|
return lhs->getOperationName() < rhs->getOperationName();
|
|
|
|
});
|
2020-04-15 05:53:28 +08:00
|
|
|
llvm::interleaveComma(sortedResultOps, os, [&](const Operator *op) {
|
2019-05-25 10:35:23 +08:00
|
|
|
os << '"' << op->getOperationName() << '"';
|
|
|
|
});
|
2021-03-24 04:44:14 +08:00
|
|
|
os << "}) {}\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
// Emit matchAndRewrite() function.
|
2020-10-04 06:17:38 +08:00
|
|
|
{
|
|
|
|
auto classScope = os.scope();
|
|
|
|
os.reindent(R"(
|
|
|
|
::mlir::LogicalResult matchAndRewrite(::mlir::Operation *op0,
|
|
|
|
::mlir::PatternRewriter &rewriter) const override {)")
|
|
|
|
<< '\n';
|
|
|
|
{
|
|
|
|
auto functionScope = os.scope();
|
|
|
|
|
|
|
|
// Register all symbols bound in the source pattern.
|
|
|
|
pattern.collectSourcePatternBoundSymbols(symbolInfoMap);
|
|
|
|
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "start creating local variables for capturing matches\n");
|
|
|
|
os << "// Variables for capturing values and attributes used while "
|
|
|
|
"creating ops\n";
|
|
|
|
// Create local variables for storing the arguments and results bound
|
|
|
|
// to symbols.
|
|
|
|
for (const auto &symbolInfoPair : symbolInfoMap) {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
const auto &symbol = symbolInfoPair.first;
|
|
|
|
const auto &info = symbolInfoPair.second;
|
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
os << info.getVarDecl(symbol);
|
|
|
|
}
|
|
|
|
// TODO: capture ops with consistent numbering so that it can be
|
|
|
|
// reused for fused loc.
|
|
|
|
os << formatv("::mlir::Operation *tblgen_ops[{0}];\n\n",
|
|
|
|
pattern.getSourcePattern().getNumOps());
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "done creating local variables for capturing matches\n");
|
|
|
|
|
|
|
|
os << "// Match\n";
|
|
|
|
os << "tblgen_ops[0] = op0;\n";
|
2020-10-10 04:32:01 +08:00
|
|
|
emitMatchLogic(sourceTree, "op0");
|
2020-10-04 06:17:38 +08:00
|
|
|
|
|
|
|
os << "\n// Rewrite\n";
|
|
|
|
emitRewriteLogic();
|
|
|
|
|
2020-10-12 16:23:54 +08:00
|
|
|
os << "return ::mlir::success();\n";
|
2020-10-04 06:17:38 +08:00
|
|
|
}
|
|
|
|
os << "};\n";
|
2019-04-23 04:40:30 +08:00
|
|
|
}
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "};\n\n";
|
2019-01-26 02:09:15 +08:00
|
|
|
}
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
void PatternEmitter::emitRewriteLogic() {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "--- start emitting rewrite logic ---\n");
|
2019-04-04 03:29:14 +08:00
|
|
|
const Operator &rootOp = pattern.getSourceRootOp();
|
|
|
|
int numExpectedResults = rootOp.getNumResults();
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
int numResultPatterns = pattern.getNumResultPatterns();
|
|
|
|
|
|
|
|
// First register all symbols bound to ops generated in result patterns.
|
2019-08-10 10:03:58 +08:00
|
|
|
pattern.collectResultPatternBoundSymbols(symbolInfoMap);
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
|
|
|
|
// Only the last N static values generated are used to replace the matched
|
|
|
|
// root N-result op. We need to calculate the starting index (of the results
|
|
|
|
// of the matched op) each result pattern is to replace.
|
|
|
|
SmallVector<int, 4> offsets(numResultPatterns + 1, numExpectedResults);
|
2019-08-10 10:03:58 +08:00
|
|
|
// If we don't need to replace any value at all, set the replacement starting
|
|
|
|
// index as the number of result patterns so we skip all of them when trying
|
|
|
|
// to replace the matched op's results.
|
|
|
|
int replStartIndex = numExpectedResults == 0 ? numResultPatterns : -1;
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
for (int i = numResultPatterns - 1; i >= 0; --i) {
|
|
|
|
auto numValues = getNodeValueCount(pattern.getResultPattern(i));
|
|
|
|
offsets[i] = offsets[i + 1] - numValues;
|
|
|
|
if (offsets[i] == 0) {
|
2019-08-10 10:03:58 +08:00
|
|
|
if (replStartIndex == -1)
|
|
|
|
replStartIndex = i;
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
} else if (offsets[i] < 0 && offsets[i + 1] > 0) {
|
|
|
|
auto error = formatv(
|
|
|
|
"cannot use the same multi-result op '{0}' to generate both "
|
|
|
|
"auxiliary values and values to be used for replacing the matched op",
|
|
|
|
pattern.getResultPattern(i).getSymbol());
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offsets.front() > 0) {
|
|
|
|
const char error[] = "no enough values generated to replace the matched op";
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
2019-04-04 03:29:14 +08:00
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "auto odsLoc = rewriter.getFusedLoc({";
|
2019-05-26 02:03:51 +08:00
|
|
|
for (int i = 0, e = pattern.getSourcePattern().getNumOps(); i != e; ++i) {
|
2019-08-06 22:09:55 +08:00
|
|
|
os << (i ? ", " : "") << "tblgen_ops[" << i << "]->getLoc()";
|
2019-05-26 02:03:51 +08:00
|
|
|
}
|
2020-04-07 22:44:19 +08:00
|
|
|
os << "}); (void)odsLoc;\n";
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2019-10-17 23:39:13 +08:00
|
|
|
// Process auxiliary result patterns.
|
|
|
|
for (int i = 0; i < replStartIndex; ++i) {
|
2019-03-09 05:56:53 +08:00
|
|
|
DagNode resultTree = pattern.getResultPattern(i);
|
2019-10-17 23:39:13 +08:00
|
|
|
auto val = handleResultPattern(resultTree, offsets[i], 0);
|
|
|
|
// Normal op creation will be streamed to `os` by the above call; but
|
|
|
|
// NativeCodeCall will only be materialized to `os` if it is used. Here
|
|
|
|
// we are handling auxiliary patterns so we want the side effect even if
|
|
|
|
// NativeCodeCall is not replacing matched root op's results.
|
|
|
|
if (resultTree.isNativeCodeCall())
|
2020-10-04 06:17:38 +08:00
|
|
|
os << val << ";\n";
|
2019-03-09 05:56:53 +08:00
|
|
|
}
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-10-18 23:19:54 +08:00
|
|
|
if (numExpectedResults == 0) {
|
|
|
|
assert(replStartIndex >= numResultPatterns &&
|
|
|
|
"invalid auxiliary vs. replacement pattern division!");
|
|
|
|
// No result to replace. Just erase the op.
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "rewriter.eraseOp(op0);\n";
|
2019-10-18 23:19:54 +08:00
|
|
|
} else {
|
|
|
|
// Process replacement result patterns.
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values;\n";
|
2019-10-18 23:19:54 +08:00
|
|
|
for (int i = replStartIndex; i < numResultPatterns; ++i) {
|
|
|
|
DagNode resultTree = pattern.getResultPattern(i);
|
|
|
|
auto val = handleResultPattern(resultTree, offsets[i], 0);
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "\n";
|
2019-10-18 23:19:54 +08:00
|
|
|
// Resolve each symbol for all range use so that we can loop over them.
|
2020-04-25 03:33:03 +08:00
|
|
|
// We need an explicit cast to `SmallVector` to capture the cases where
|
|
|
|
// `{0}` resolves to an `Operation::result_range` as well as cases that
|
|
|
|
// are not iterable (e.g. vector that gets wrapped in additional braces by
|
|
|
|
// RewriterGen).
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: Revisit the need for materializing a vector.
|
2019-10-18 23:19:54 +08:00
|
|
|
os << symbolInfoMap.getAllRangeUse(
|
2020-04-25 03:33:03 +08:00
|
|
|
val,
|
2020-10-04 06:17:38 +08:00
|
|
|
"for (auto v: ::llvm::SmallVector<::mlir::Value, 4>{ {0} }) {{\n"
|
|
|
|
" tblgen_repl_values.push_back(v);\n}\n",
|
2019-10-21 03:23:38 +08:00
|
|
|
"\n");
|
2019-10-18 23:19:54 +08:00
|
|
|
}
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "\nrewriter.replaceOp(op0, tblgen_repl_values);\n";
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-10-18 23:19:54 +08:00
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "--- done emitting rewrite logic ---\n");
|
2019-02-09 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
std::string PatternEmitter::getUniqueSymbol(const Operator *op) {
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(
|
|
|
|
formatv("tblgen_{0}_{1}", op->getCppClassName(), nextValueId++));
|
2019-02-09 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string PatternEmitter::handleResultPattern(DagNode resultTree,
|
|
|
|
int resultIndex, int depth) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "handle result pattern: ");
|
|
|
|
LLVM_DEBUG(resultTree.print(llvm::dbgs()));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << '\n');
|
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
if (resultTree.isLocationDirective()) {
|
|
|
|
PrintFatalError(loc,
|
|
|
|
"location directive can only be used with op creation");
|
|
|
|
}
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
if (resultTree.isNativeCodeCall()) {
|
2020-10-10 04:32:01 +08:00
|
|
|
auto symbol = handleReplaceWithNativeCodeCall(resultTree, depth);
|
2019-08-10 10:03:58 +08:00
|
|
|
symbolInfoMap.bindValue(symbol);
|
|
|
|
return symbol;
|
|
|
|
}
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
if (resultTree.isReplaceWithValue())
|
|
|
|
return handleReplaceWithValue(resultTree).str();
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
// Normal op creation.
|
|
|
|
auto symbol = handleOpCreation(resultTree, resultIndex, depth);
|
|
|
|
if (resultTree.getSymbol().empty()) {
|
|
|
|
// This is an op not explicitly bound to a symbol in the rewrite rule.
|
|
|
|
// Register the auto-generated symbol for it.
|
|
|
|
symbolInfoMap.bindOpResult(symbol, pattern.getDialectOp(resultTree));
|
|
|
|
}
|
|
|
|
return symbol;
|
2019-01-26 02:09:15 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
StringRef PatternEmitter::handleReplaceWithValue(DagNode tree) {
|
2019-02-09 22:36:23 +08:00
|
|
|
assert(tree.isReplaceWithValue());
|
|
|
|
|
|
|
|
if (tree.getNumArgs() != 1) {
|
|
|
|
PrintFatalError(
|
|
|
|
loc, "replaceWithValue directive must take exactly one argument");
|
|
|
|
}
|
|
|
|
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
if (!tree.getSymbol().empty()) {
|
2019-08-02 02:50:47 +08:00
|
|
|
PrintFatalError(loc, "cannot bind symbol to replaceWithValue");
|
2019-04-04 20:44:58 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
return tree.getArgName(0);
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:18:34 +08:00
|
|
|
std::string PatternEmitter::handleLocationDirective(DagNode tree) {
|
2020-04-07 22:44:19 +08:00
|
|
|
assert(tree.isLocationDirective());
|
|
|
|
auto lookUpArgLoc = [this, &tree](int idx) {
|
|
|
|
const auto *const lookupFmt = "(*{0}.begin()).getLoc()";
|
|
|
|
return symbolInfoMap.getAllRangeUse(tree.getArgName(idx), lookupFmt);
|
|
|
|
};
|
|
|
|
|
2020-04-10 08:18:34 +08:00
|
|
|
if (tree.getNumArgs() == 0)
|
|
|
|
llvm::PrintFatalError(
|
|
|
|
"At least one argument to location directive required");
|
2020-04-07 22:44:19 +08:00
|
|
|
|
|
|
|
if (!tree.getSymbol().empty())
|
|
|
|
PrintFatalError(loc, "cannot bind symbol to location");
|
|
|
|
|
2020-04-10 08:18:34 +08:00
|
|
|
if (tree.getNumArgs() == 1) {
|
|
|
|
DagLeaf leaf = tree.getArgAsLeaf(0);
|
|
|
|
if (leaf.isStringAttr())
|
2021-02-27 09:57:03 +08:00
|
|
|
return formatv("::mlir::NameLoc::get(rewriter.getIdentifier(\"{0}\"))",
|
2020-04-10 08:18:34 +08:00
|
|
|
leaf.getStringAttr())
|
|
|
|
.str();
|
|
|
|
return lookUpArgLoc(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
llvm::raw_string_ostream os(ret);
|
|
|
|
std::string strAttr;
|
|
|
|
os << "rewriter.getFusedLoc({";
|
|
|
|
bool first = true;
|
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
|
|
|
DagLeaf leaf = tree.getArgAsLeaf(i);
|
|
|
|
// Handle the optional string value.
|
|
|
|
if (leaf.isStringAttr()) {
|
|
|
|
if (!strAttr.empty())
|
|
|
|
llvm::PrintFatalError("Only one string attribute may be specified");
|
|
|
|
strAttr = leaf.getStringAttr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
os << (first ? "" : ", ") << lookUpArgLoc(i);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
os << "}";
|
|
|
|
if (!strAttr.empty()) {
|
|
|
|
os << ", rewriter.getStringAttr(\"" << strAttr << "\")";
|
|
|
|
}
|
|
|
|
os << ")";
|
|
|
|
return os.str();
|
2019-02-09 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
std::string PatternEmitter::handleOpArgument(DagLeaf leaf,
|
|
|
|
StringRef patArgName) {
|
2020-08-10 09:02:08 +08:00
|
|
|
if (leaf.isStringAttr())
|
|
|
|
PrintFatalError(loc, "raw string not supported as argument");
|
2019-03-13 04:55:50 +08:00
|
|
|
if (leaf.isConstantAttr()) {
|
2019-04-01 23:58:53 +08:00
|
|
|
auto constAttr = leaf.getAsConstantAttr();
|
|
|
|
return handleConstantAttr(constAttr.getAttribute(),
|
|
|
|
constAttr.getConstantValue());
|
|
|
|
}
|
|
|
|
if (leaf.isEnumAttrCase()) {
|
|
|
|
auto enumCase = leaf.getAsEnumAttrCase();
|
2019-07-01 20:26:14 +08:00
|
|
|
if (enumCase.isStrCase())
|
|
|
|
return handleConstantAttr(enumCase, enumCase.getSymbol());
|
|
|
|
// This is an enum case backed by an IntegerAttr. We need to get its value
|
|
|
|
// to build the constant.
|
|
|
|
std::string val = std::to_string(enumCase.getValue());
|
|
|
|
return handleConstantAttr(enumCase, val);
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
2019-08-21 20:35:07 +08:00
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "handle argument '" << patArgName << "'\n");
|
2019-08-21 20:35:07 +08:00
|
|
|
auto argName = symbolInfoMap.getValueAndRangeUse(patArgName);
|
2019-03-13 04:55:50 +08:00
|
|
|
if (leaf.isUnspecified() || leaf.isOperandMatcher()) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "replace " << patArgName << " with '" << argName
|
|
|
|
<< "' (via symbol ref)\n");
|
2019-08-06 22:09:55 +08:00
|
|
|
return argName;
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
2019-04-23 05:13:45 +08:00
|
|
|
if (leaf.isNativeCodeCall()) {
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = tgfmt(leaf.getNativeCodeTemplate(), &fmtCtx.withSelf(argName));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "replace " << patArgName << " with '" << repl
|
|
|
|
<< "' (via NativeCodeCall)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
|
|
|
PrintFatalError(loc, "unhandled case when rewriting op");
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
std::string PatternEmitter::handleReplaceWithNativeCodeCall(DagNode tree,
|
|
|
|
int depth) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "handle NativeCodeCall pattern: ");
|
|
|
|
LLVM_DEBUG(tree.print(llvm::dbgs()));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << '\n');
|
|
|
|
|
2019-04-23 05:13:45 +08:00
|
|
|
auto fmt = tree.getNativeCodeTemplate();
|
2021-03-03 17:04:08 +08:00
|
|
|
|
|
|
|
SmallVector<std::string, 16> attrs;
|
|
|
|
|
2020-08-12 05:06:16 +08:00
|
|
|
bool hasLocationDirective;
|
|
|
|
std::string locToUse;
|
|
|
|
std::tie(hasLocationDirective, locToUse) = getLocation(tree);
|
|
|
|
|
|
|
|
for (int i = 0, e = tree.getNumArgs() - hasLocationDirective; i != e; ++i) {
|
2020-10-10 04:32:01 +08:00
|
|
|
if (tree.isNestedDagArg(i)) {
|
2021-03-03 17:04:08 +08:00
|
|
|
attrs.push_back(
|
|
|
|
handleResultPattern(tree.getArgAsNestedDag(i), i, depth + 1));
|
2020-10-10 04:32:01 +08:00
|
|
|
} else {
|
2021-03-03 17:04:08 +08:00
|
|
|
attrs.push_back(
|
|
|
|
handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i)));
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
2019-12-06 21:58:59 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "NativeCodeCall argument #" << i
|
2019-10-17 22:25:50 +08:00
|
|
|
<< " replacement: " << attrs[i] << "\n");
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
2021-03-03 17:04:08 +08:00
|
|
|
|
2021-04-16 13:34:10 +08:00
|
|
|
std::string symbol = tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse), attrs);
|
|
|
|
if (!tree.getSymbol().empty()) {
|
|
|
|
os << formatv("auto {0} = {1};\n", tree.getSymbol(), symbol);
|
|
|
|
symbol = tree.getSymbol().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbol;
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
|
|
|
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
int PatternEmitter::getNodeValueCount(DagNode node) {
|
|
|
|
if (node.isOperation()) {
|
2019-08-10 10:03:58 +08:00
|
|
|
// If the op is bound to a symbol in the rewrite rule, query its result
|
|
|
|
// count from the symbol info map.
|
|
|
|
auto symbol = node.getSymbol();
|
|
|
|
if (!symbol.empty()) {
|
|
|
|
return symbolInfoMap.getStaticValueCount(symbol);
|
|
|
|
}
|
|
|
|
// Otherwise this is an unbound op; we will use all its results.
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
return pattern.getDialectOp(node).getNumResults();
|
|
|
|
}
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: This considers all NativeCodeCall as returning one
|
Fix support for auxiliary ops in declarative rewrite rules
We allow to generate more ops than what are needed for replacing
the matched root op. Only the last N static values generated are
used as replacement; the others serve as auxiliary ops/values for
building the replacement.
With the introduction of multi-result op support, an op, if used
as a whole, may be used to replace multiple static values of
the matched root op. We need to consider this when calculating
the result range an generated op is to replace.
For example, we can have the following pattern:
```tblgen
def : Pattern<(ThreeResultOp ...),
[(OneResultOp ...), (OneResultOp ...), (OneResultOp ...)]>;
// Two op to replace all three results
def : Pattern<(ThreeResultOp ...),
[(TwoResultOp ...), (OneResultOp ...)]>;
// One op to replace all three results
def : Pat<(ThreeResultOp ...), (ThreeResultOp ...)>;
def : Pattern<(ThreeResultOp ...),
[(AuxiliaryOp ...), (ThreeResultOp ...)]>;
```
PiperOrigin-RevId: 261017235
2019-08-01 07:03:13 +08:00
|
|
|
// value. Enhance if multi-value ones are needed.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-12 05:06:16 +08:00
|
|
|
std::pair<bool, std::string> PatternEmitter::getLocation(DagNode tree) {
|
|
|
|
auto numPatArgs = tree.getNumArgs();
|
|
|
|
|
|
|
|
if (numPatArgs != 0) {
|
|
|
|
if (auto lastArg = tree.getArgAsNestedDag(numPatArgs - 1))
|
|
|
|
if (lastArg.isLocationDirective()) {
|
|
|
|
return std::make_pair(true, handleLocationDirective(lastArg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no explicit location is given, use the default, all fused, location.
|
|
|
|
return std::make_pair(false, "odsLoc");
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string PatternEmitter::handleOpCreation(DagNode tree, int resultIndex,
|
|
|
|
int depth) {
|
2019-10-22 11:47:49 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "create op for pattern: ");
|
|
|
|
LLVM_DEBUG(tree.print(llvm::dbgs()));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << '\n');
|
|
|
|
|
2019-02-09 22:36:23 +08:00
|
|
|
Operator &resultOp = tree.getDialectOp(opMap);
|
2019-03-09 05:56:53 +08:00
|
|
|
auto numOpArgs = resultOp.getNumArgs();
|
2020-04-07 22:44:19 +08:00
|
|
|
auto numPatArgs = tree.getNumArgs();
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2020-08-12 05:06:16 +08:00
|
|
|
bool hasLocationDirective;
|
2020-04-07 22:44:19 +08:00
|
|
|
std::string locToUse;
|
2020-08-12 05:06:16 +08:00
|
|
|
std::tie(hasLocationDirective, locToUse) = getLocation(tree);
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2020-08-12 05:06:16 +08:00
|
|
|
auto inPattern = numPatArgs - hasLocationDirective;
|
2020-04-07 22:44:19 +08:00
|
|
|
if (numOpArgs != inPattern) {
|
|
|
|
PrintFatalError(loc,
|
|
|
|
formatv("resultant op '{0}' argument number mismatch: "
|
|
|
|
"{1} in pattern vs. {2} in definition",
|
|
|
|
resultOp.getOperationName(), inPattern, numOpArgs));
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:45:55 +08:00
|
|
|
// A map to collect all nested DAG child nodes' names, with operand index as
|
2019-08-21 20:35:07 +08:00
|
|
|
// the key. This includes both bound and unbound child nodes.
|
2019-11-15 23:33:21 +08:00
|
|
|
ChildNodeIndexNameMap childNodeNames;
|
2019-02-09 22:45:55 +08:00
|
|
|
|
|
|
|
// First go through all the child nodes who are nested DAG constructs to
|
2019-08-21 20:35:07 +08:00
|
|
|
// create ops for them and remember the symbol names for them, so that we can
|
|
|
|
// use the results in the current node. This happens in a recursive manner.
|
2020-12-30 08:19:31 +08:00
|
|
|
for (int i = 0, e = tree.getNumArgs() - hasLocationDirective; i != e; ++i) {
|
2020-04-07 22:44:19 +08:00
|
|
|
if (auto child = tree.getArgAsNestedDag(i))
|
2019-08-06 22:09:55 +08:00
|
|
|
childNodeNames[i] = handleResultPattern(child, i, depth + 1);
|
2019-02-09 22:45:55 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// The name of the local variable holding this op.
|
|
|
|
std::string valuePackName;
|
|
|
|
// The symbol for holding the result of this pattern. Note that the result of
|
|
|
|
// this pattern is not necessarily the same as the variable created by this
|
|
|
|
// pattern because we can use `__N` suffix to refer only a specific result if
|
|
|
|
// the generated op is a multi-result op.
|
|
|
|
std::string resultValue;
|
|
|
|
if (tree.getSymbol().empty()) {
|
|
|
|
// No symbol is explicitly bound to this op in the pattern. Generate a
|
|
|
|
// unique name.
|
|
|
|
valuePackName = resultValue = getUniqueSymbol(&resultOp);
|
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
resultValue = std::string(tree.getSymbol());
|
2019-08-21 20:35:07 +08:00
|
|
|
// Strip the index to get the name for the value pack and use it to name the
|
|
|
|
// local variable for the op.
|
2020-01-29 03:23:46 +08:00
|
|
|
valuePackName = std::string(SymbolInfoMap::getValuePackName(resultValue));
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-02-09 22:36:23 +08:00
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// Create the local variable for this op.
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("{0} {1};\n{{\n", resultOp.getQualCppClassName(),
|
|
|
|
valuePackName);
|
2019-08-21 20:35:07 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
// Right now ODS don't have general type inference support. Except a few
|
|
|
|
// special cases listed below, DRR needs to supply types for all results
|
|
|
|
// when building an op.
|
|
|
|
bool isSameOperandsAndResultType =
|
2020-09-15 04:01:07 +08:00
|
|
|
resultOp.getTrait("::mlir::OpTrait::SameOperandsAndResultType");
|
|
|
|
bool useFirstAttr =
|
|
|
|
resultOp.getTrait("::mlir::OpTrait::FirstAttrDerivedResultType");
|
2019-11-15 23:33:21 +08:00
|
|
|
|
|
|
|
if (isSameOperandsAndResultType || useFirstAttr) {
|
|
|
|
// We know how to deduce the result type for ops with these traits and we've
|
2019-12-06 21:58:59 +08:00
|
|
|
// generated builders taking aggregate parameters. Use those builders to
|
2019-11-15 23:33:21 +08:00
|
|
|
// create the ops.
|
|
|
|
|
|
|
|
// First prepare local variables for op arguments used in builder call.
|
2020-10-10 04:32:01 +08:00
|
|
|
createAggregateLocalVarsForOpArgs(tree, childNodeNames, depth);
|
2020-04-07 22:44:19 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
// Then create the op.
|
2020-10-04 06:17:38 +08:00
|
|
|
os.scope("", "\n}\n").os << formatv(
|
|
|
|
"{0} = rewriter.create<{1}>({2}, tblgen_values, tblgen_attrs);",
|
2020-04-07 22:44:19 +08:00
|
|
|
valuePackName, resultOp.getQualCppClassName(), locToUse);
|
2019-11-15 23:33:21 +08:00
|
|
|
return resultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usePartialResults = valuePackName != resultValue;
|
|
|
|
|
2020-04-07 06:55:25 +08:00
|
|
|
if (usePartialResults || depth > 0 || resultIndex < 0) {
|
2019-11-15 23:33:21 +08:00
|
|
|
// For these cases (broadcastable ops, op results used both as auxiliary
|
|
|
|
// values and replacement values, ops in nested patterns, auxiliary ops), we
|
|
|
|
// still need to supply the result types when building the op. But because
|
|
|
|
// we don't generate a builder automatically with ODS for them, it's the
|
2020-01-20 11:14:37 +08:00
|
|
|
// developer's responsibility to make sure such a builder (with result type
|
2019-11-15 23:33:21 +08:00
|
|
|
// deduction ability) exists. We go through the separate-parameter builder
|
|
|
|
// here given that it's easier for developers to write compared to
|
|
|
|
// aggregate-parameter builders.
|
|
|
|
createSeparateLocalVarsForOpArgs(tree, childNodeNames);
|
2020-04-07 22:44:19 +08:00
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
os.scope().os << formatv("{0} = rewriter.create<{1}>({2}", valuePackName,
|
|
|
|
resultOp.getQualCppClassName(), locToUse);
|
2020-10-10 04:32:01 +08:00
|
|
|
supplyValuesForOpArgs(tree, childNodeNames, depth);
|
2020-10-04 06:17:38 +08:00
|
|
|
os << "\n );\n}\n";
|
2019-11-15 23:33:21 +08:00
|
|
|
return resultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If depth == 0 and resultIndex >= 0, it means we are replacing the values
|
|
|
|
// generated from the source pattern root op. Then we can use the source
|
|
|
|
// pattern's value types to determine the value type of the generated op
|
|
|
|
// here.
|
|
|
|
|
|
|
|
// First prepare local variables for op arguments used in builder call.
|
2020-10-10 04:32:01 +08:00
|
|
|
createAggregateLocalVarsForOpArgs(tree, childNodeNames, depth);
|
2019-11-15 23:33:21 +08:00
|
|
|
|
|
|
|
// Then prepare the result types. We need to specify the types for all
|
|
|
|
// results.
|
2020-10-04 06:17:38 +08:00
|
|
|
os.indent() << formatv("::mlir::SmallVector<::mlir::Type, 4> tblgen_types; "
|
|
|
|
"(void)tblgen_types;\n");
|
2019-11-15 23:33:21 +08:00
|
|
|
int numResults = resultOp.getNumResults();
|
|
|
|
if (numResults != 0) {
|
|
|
|
for (int i = 0; i < numResults; ++i)
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("for (auto v: castedOp0.getODSResults({0})) {{\n"
|
|
|
|
" tblgen_types.push_back(v.getType());\n}\n",
|
|
|
|
resultIndex + i);
|
2019-11-15 23:33:21 +08:00
|
|
|
}
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("{0} = rewriter.create<{1}>({2}, tblgen_types, "
|
|
|
|
"tblgen_values, tblgen_attrs);\n",
|
|
|
|
valuePackName, resultOp.getQualCppClassName(), locToUse);
|
|
|
|
os.unindent() << "}\n";
|
2019-11-15 23:33:21 +08:00
|
|
|
return resultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatternEmitter::createSeparateLocalVarsForOpArgs(
|
|
|
|
DagNode node, ChildNodeIndexNameMap &childNodeNames) {
|
|
|
|
Operator &resultOp = node.getDialectOp(opMap);
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// Now prepare operands used for building this op:
|
2019-12-23 13:59:55 +08:00
|
|
|
// * If the operand is non-variadic, we create a `Value` local variable.
|
|
|
|
// * If the operand is variadic, we create a `SmallVector<Value>` local
|
2019-08-21 20:35:07 +08:00
|
|
|
// variable.
|
|
|
|
|
|
|
|
int valueIndex = 0; // An index for uniquing local variable names.
|
2019-10-22 11:47:49 +08:00
|
|
|
for (int argIndex = 0, e = resultOp.getNumArgs(); argIndex < e; ++argIndex) {
|
|
|
|
const auto *operand =
|
|
|
|
resultOp.getArg(argIndex).dyn_cast<NamedTypeConstraint *>();
|
2020-10-04 06:17:38 +08:00
|
|
|
// We do not need special handling for attributes.
|
|
|
|
if (!operand)
|
2019-10-22 11:47:49 +08:00
|
|
|
continue;
|
2019-11-15 23:33:21 +08:00
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
raw_indented_ostream::DelimitedScope scope(os);
|
2019-08-21 20:35:07 +08:00
|
|
|
std::string varName;
|
2019-10-22 11:47:49 +08:00
|
|
|
if (operand->isVariadic()) {
|
2020-01-29 03:23:46 +08:00
|
|
|
varName = std::string(formatv("tblgen_values_{0}", valueIndex++));
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("::mlir::SmallVector<::mlir::Value, 4> {0};\n", varName);
|
2019-08-21 20:35:07 +08:00
|
|
|
std::string range;
|
2019-11-15 23:33:21 +08:00
|
|
|
if (node.isNestedDagArg(argIndex)) {
|
2019-08-21 20:35:07 +08:00
|
|
|
range = childNodeNames[argIndex];
|
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
range = std::string(node.getArgName(argIndex));
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
// Resolve the symbol for all range use so that we have a uniform way of
|
|
|
|
// capturing the values.
|
|
|
|
range = symbolInfoMap.getValueAndRangeUse(range);
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("for (auto v: {0}) {{\n {1}.push_back(v);\n}\n", range,
|
|
|
|
varName);
|
2019-08-21 20:35:07 +08:00
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
varName = std::string(formatv("tblgen_value_{0}", valueIndex++));
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("::mlir::Value {0} = ", varName);
|
2019-11-15 23:33:21 +08:00
|
|
|
if (node.isNestedDagArg(argIndex)) {
|
2019-08-21 20:35:07 +08:00
|
|
|
os << symbolInfoMap.getValueAndRangeUse(childNodeNames[argIndex]);
|
|
|
|
} else {
|
2019-11-15 23:33:21 +08:00
|
|
|
DagLeaf leaf = node.getArgAsLeaf(argIndex);
|
2019-08-21 20:35:07 +08:00
|
|
|
auto symbol =
|
2019-11-15 23:33:21 +08:00
|
|
|
symbolInfoMap.getValueAndRangeUse(node.getArgName(argIndex));
|
2019-08-21 20:35:07 +08:00
|
|
|
if (leaf.isNativeCodeCall()) {
|
2020-01-29 03:23:46 +08:00
|
|
|
os << std::string(
|
|
|
|
tgfmt(leaf.getNativeCodeTemplate(), &fmtCtx.withSelf(symbol)));
|
2019-08-21 20:35:07 +08:00
|
|
|
} else {
|
|
|
|
os << symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os << ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update to use the newly created local variable for building the op later.
|
|
|
|
childNodeNames[argIndex] = varName;
|
|
|
|
}
|
2019-11-15 23:33:21 +08:00
|
|
|
}
|
2019-08-21 20:35:07 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
void PatternEmitter::supplyValuesForOpArgs(
|
2020-10-10 04:32:01 +08:00
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames, int depth) {
|
2019-11-15 23:33:21 +08:00
|
|
|
Operator &resultOp = node.getDialectOp(opMap);
|
|
|
|
for (int argIndex = 0, numOpArgs = resultOp.getNumArgs();
|
|
|
|
argIndex != numOpArgs; ++argIndex) {
|
2019-12-06 21:58:59 +08:00
|
|
|
// Start each argument on its own line.
|
2020-10-04 06:17:38 +08:00
|
|
|
os << ",\n ";
|
2019-10-22 11:47:49 +08:00
|
|
|
|
|
|
|
Argument opArg = resultOp.getArg(argIndex);
|
|
|
|
// Handle the case of operand first.
|
|
|
|
if (auto *operand = opArg.dyn_cast<NamedTypeConstraint *>()) {
|
2019-11-15 23:33:21 +08:00
|
|
|
if (!operand->name.empty())
|
2019-10-22 11:47:49 +08:00
|
|
|
os << "/*" << operand->name << "=*/";
|
2019-11-15 23:33:21 +08:00
|
|
|
os << childNodeNames.lookup(argIndex);
|
2019-10-22 11:47:49 +08:00
|
|
|
continue;
|
2019-04-04 20:44:58 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-02-02 07:40:22 +08:00
|
|
|
// The argument in the op definition.
|
2019-08-10 10:03:58 +08:00
|
|
|
auto opArgName = resultOp.getArgName(argIndex);
|
2019-11-15 23:33:21 +08:00
|
|
|
if (auto subTree = node.getArgAsNestedDag(argIndex)) {
|
2019-04-23 05:13:45 +08:00
|
|
|
if (!subTree.isNativeCodeCall())
|
|
|
|
PrintFatalError(loc, "only NativeCodeCall allowed in nested dag node "
|
|
|
|
"for creating attribute");
|
|
|
|
os << formatv("/*{0}=*/{1}", opArgName,
|
2020-10-10 04:32:01 +08:00
|
|
|
handleReplaceWithNativeCodeCall(subTree, depth));
|
2019-02-02 07:40:22 +08:00
|
|
|
} else {
|
2019-11-15 23:33:21 +08:00
|
|
|
auto leaf = node.getArgAsLeaf(argIndex);
|
2019-03-13 04:55:50 +08:00
|
|
|
// The argument in the result DAG pattern.
|
2019-11-15 23:33:21 +08:00
|
|
|
auto patArgName = node.getArgName(argIndex);
|
2019-04-01 23:58:53 +08:00
|
|
|
if (leaf.isConstantAttr() || leaf.isEnumAttrCase()) {
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: Refactor out into map to avoid recomputing these.
|
2019-10-22 11:47:49 +08:00
|
|
|
if (!opArg.is<NamedAttribute *>())
|
2019-08-10 10:03:58 +08:00
|
|
|
PrintFatalError(loc, Twine("expected attribute ") + Twine(argIndex));
|
2019-03-13 04:55:50 +08:00
|
|
|
if (!patArgName.empty())
|
|
|
|
os << "/*" << patArgName << "=*/";
|
|
|
|
} else {
|
|
|
|
os << "/*" << opArgName << "=*/";
|
|
|
|
}
|
|
|
|
os << handleOpArgument(leaf, patArgName);
|
2019-01-08 01:52:26 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2019-11-15 23:33:21 +08:00
|
|
|
}
|
2019-02-01 09:57:06 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
void PatternEmitter::createAggregateLocalVarsForOpArgs(
|
2020-10-10 04:32:01 +08:00
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames, int depth) {
|
2019-11-15 23:33:21 +08:00
|
|
|
Operator &resultOp = node.getDialectOp(opMap);
|
|
|
|
|
2020-10-04 06:17:38 +08:00
|
|
|
auto scope = os.scope();
|
|
|
|
os << formatv("::mlir::SmallVector<::mlir::Value, 4> "
|
|
|
|
"tblgen_values; (void)tblgen_values;\n");
|
|
|
|
os << formatv("::mlir::SmallVector<::mlir::NamedAttribute, 4> "
|
|
|
|
"tblgen_attrs; (void)tblgen_attrs;\n");
|
2019-11-15 23:33:21 +08:00
|
|
|
|
2020-06-22 23:10:23 +08:00
|
|
|
const char *addAttrCmd =
|
2020-10-04 06:17:38 +08:00
|
|
|
"if (auto tmpAttr = {1}) {\n"
|
|
|
|
" tblgen_attrs.emplace_back(rewriter.getIdentifier(\"{0}\"), "
|
|
|
|
"tmpAttr);\n}\n";
|
2019-11-15 23:33:21 +08:00
|
|
|
for (int argIndex = 0, e = resultOp.getNumArgs(); argIndex < e; ++argIndex) {
|
2019-12-17 02:27:55 +08:00
|
|
|
if (resultOp.getArg(argIndex).is<NamedAttribute *>()) {
|
2019-11-15 23:33:21 +08:00
|
|
|
// The argument in the op definition.
|
|
|
|
auto opArgName = resultOp.getArgName(argIndex);
|
|
|
|
if (auto subTree = node.getArgAsNestedDag(argIndex)) {
|
|
|
|
if (!subTree.isNativeCodeCall())
|
|
|
|
PrintFatalError(loc, "only NativeCodeCall allowed in nested dag node "
|
|
|
|
"for creating attribute");
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv(addAttrCmd, opArgName,
|
2020-10-10 04:32:01 +08:00
|
|
|
handleReplaceWithNativeCodeCall(subTree, depth + 1));
|
2019-11-15 23:33:21 +08:00
|
|
|
} else {
|
|
|
|
auto leaf = node.getArgAsLeaf(argIndex);
|
|
|
|
// The argument in the result DAG pattern.
|
|
|
|
auto patArgName = node.getArgName(argIndex);
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv(addAttrCmd, opArgName,
|
|
|
|
handleOpArgument(leaf, patArgName));
|
2019-11-15 23:33:21 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *operand =
|
|
|
|
resultOp.getArg(argIndex).get<NamedTypeConstraint *>();
|
|
|
|
std::string varName;
|
|
|
|
if (operand->isVariadic()) {
|
|
|
|
std::string range;
|
|
|
|
if (node.isNestedDagArg(argIndex)) {
|
|
|
|
range = childNodeNames.lookup(argIndex);
|
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
range = std::string(node.getArgName(argIndex));
|
2019-11-15 23:33:21 +08:00
|
|
|
}
|
|
|
|
// Resolve the symbol for all range use so that we have a uniform way of
|
|
|
|
// capturing the values.
|
|
|
|
range = symbolInfoMap.getValueAndRangeUse(range);
|
2020-10-04 06:17:38 +08:00
|
|
|
os << formatv("for (auto v: {0}) {{\n tblgen_values.push_back(v);\n}\n",
|
|
|
|
range);
|
2019-11-15 23:33:21 +08:00
|
|
|
} else {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
os << formatv("tblgen_values.push_back(");
|
2019-11-15 23:33:21 +08:00
|
|
|
if (node.isNestedDagArg(argIndex)) {
|
|
|
|
os << symbolInfoMap.getValueAndRangeUse(
|
|
|
|
childNodeNames.lookup(argIndex));
|
|
|
|
} else {
|
|
|
|
DagLeaf leaf = node.getArgAsLeaf(argIndex);
|
|
|
|
auto symbol =
|
|
|
|
symbolInfoMap.getValueAndRangeUse(node.getArgName(argIndex));
|
|
|
|
if (leaf.isNativeCodeCall()) {
|
2020-01-29 03:23:46 +08:00
|
|
|
os << std::string(
|
|
|
|
tgfmt(leaf.getNativeCodeTemplate(), &fmtCtx.withSelf(symbol)));
|
2019-11-15 23:33:21 +08:00
|
|
|
} else {
|
|
|
|
os << symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os << ");\n";
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 09:57:06 +08:00
|
|
|
}
|
|
|
|
|
2018-12-12 19:09:11 +08:00
|
|
|
static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
|
|
|
|
emitSourceFileHeader("Rewriters", os);
|
2019-04-11 02:37:53 +08:00
|
|
|
|
2018-12-12 19:09:11 +08:00
|
|
|
const auto &patterns = recordKeeper.getAllDerivedDefinitions("Pattern");
|
2019-04-11 02:37:53 +08:00
|
|
|
auto numPatterns = patterns.size();
|
2018-12-12 19:09:11 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
// We put the map here because it can be shared among multiple patterns.
|
|
|
|
RecordOperatorMap recordOpMap;
|
|
|
|
|
2019-04-11 02:37:53 +08:00
|
|
|
std::vector<std::string> rewriterNames;
|
|
|
|
rewriterNames.reserve(numPatterns);
|
|
|
|
|
|
|
|
std::string baseRewriterName = "GeneratedConvert";
|
|
|
|
int rewriterIndex = 0;
|
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
for (Record *p : patterns) {
|
2019-04-11 02:37:53 +08:00
|
|
|
std::string name;
|
|
|
|
if (p->isAnonymous()) {
|
|
|
|
// If no name is provided, ensure unique rewriter names simply by
|
|
|
|
// appending unique suffix.
|
|
|
|
name = baseRewriterName + llvm::utostr(rewriterIndex++);
|
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
name = std::string(p->getName());
|
2019-04-11 02:37:53 +08:00
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "=== start generating pattern '" << name << "' ===\n");
|
2019-08-06 22:09:55 +08:00
|
|
|
PatternEmitter(p, &recordOpMap, os).emit(name);
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "=== done generating pattern '" << name << "' ===\n");
|
2019-04-11 02:37:53 +08:00
|
|
|
rewriterNames.push_back(std::move(name));
|
2018-12-12 19:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit function to add the generated matchers to the pattern list.
|
2021-03-22 01:38:35 +08:00
|
|
|
os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated("
|
2021-03-23 07:58:34 +08:00
|
|
|
"::mlir::RewritePatternSet &patterns) {\n";
|
2019-04-11 02:37:53 +08:00
|
|
|
for (const auto &name : rewriterNames) {
|
2021-03-23 07:58:34 +08:00
|
|
|
os << " patterns.add<" << name << ">(patterns.getContext());\n";
|
2018-12-12 19:09:11 +08:00
|
|
|
}
|
|
|
|
os << "}\n";
|
|
|
|
}
|
|
|
|
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-23 01:31:04 +08:00
|
|
|
static mlir::GenRegistration
|
2018-12-27 20:56:03 +08:00
|
|
|
genRewriters("gen-rewriters", "Generate pattern rewriters",
|
|
|
|
[](const RecordKeeper &records, raw_ostream &os) {
|
|
|
|
emitRewriters(records, os);
|
|
|
|
return false;
|
|
|
|
});
|