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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-02-14 06:30:40 +08:00
|
|
|
#include "mlir/Support/STLExtras.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 {
|
|
|
|
template <> struct format_provider<mlir::tblgen::Pattern::IdentifierLine> {
|
|
|
|
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.
|
|
|
|
void emitMatchLogic(DagNode tree);
|
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
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
// Emits C++ statements for matching the op constrained by the given DAG
|
|
|
|
// `tree`.
|
|
|
|
void emitOpMatch(DagNode tree, 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
|
|
|
|
// DAG `tree` as an operand.
|
|
|
|
void emitOperandMatch(DagNode tree, int argIndex, int depth, int indent);
|
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.
|
|
|
|
void emitAttributeMatch(DagNode tree, int argIndex, int depth, int indent);
|
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.
|
|
|
|
void emitMatchCheck(int depth, const FmtObjectBase &matchFmt,
|
|
|
|
const llvm::formatv_object_base &failureFmt);
|
|
|
|
|
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.
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string handleReplaceWithNativeCodeCall(DagNode resultTree);
|
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);
|
|
|
|
|
|
|
|
// Returns the symbol of the value whose location to use.
|
|
|
|
std::string handleUseLocationOf(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,
|
|
|
|
const ChildNodeIndexNameMap &childNodeNames);
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames);
|
|
|
|
|
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
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
raw_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
|
|
|
|
2019-01-09 09:19:22 +08:00
|
|
|
// TODO(jpienaar): 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.
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitOpMatch(DagNode tree, int depth) {
|
|
|
|
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
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
int indent = 4 + 2 * depth;
|
2019-08-10 10:03:58 +08:00
|
|
|
os.indent(indent) << formatv(
|
|
|
|
"auto castedOp{0} = dyn_cast_or_null<{1}>(op{0}); (void)castedOp{0};\n",
|
|
|
|
depth, 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).
|
2020-03-18 11:07:55 +08:00
|
|
|
os.indent(indent) << formatv("if (!castedOp{0}) return failure();\n",
|
2019-08-10 10:03:58 +08:00
|
|
|
depth);
|
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())
|
2019-08-10 10:03:58 +08:00
|
|
|
os.indent(indent) << formatv("{0} = castedOp{1};\n", name, depth);
|
2019-02-14 06:30:40 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
2019-01-08 01:52:26 +08:00
|
|
|
auto opArg = op.getArg(i);
|
|
|
|
|
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 *>()) {
|
|
|
|
if (operand->isVariadic()) {
|
|
|
|
auto error = formatv("use nested DAG construct to match op {0}'s "
|
|
|
|
"variadic operand #{1} unsupported now",
|
|
|
|
op.getOperationName(), i);
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 23:55:08 +08:00
|
|
|
os.indent(indent) << "{\n";
|
2019-08-21 20:35:07 +08:00
|
|
|
|
|
|
|
os.indent(indent + 2) << formatv(
|
|
|
|
"auto *op{0} = "
|
2020-01-12 00:54:04 +08:00
|
|
|
"(*castedOp{1}.getODSOperands({2}).begin()).getDefiningOp();\n",
|
2019-08-21 20:35:07 +08:00
|
|
|
depth + 1, depth, i);
|
2019-01-29 06:04:40 +08:00
|
|
|
emitOpMatch(argTree, depth + 1);
|
2019-05-26 02:03:51 +08:00
|
|
|
os.indent(indent + 2)
|
2019-08-06 22:09:55 +08:00
|
|
|
<< formatv("tblgen_ops[{0}] = op{1};\n", ++opCounter, depth + 1);
|
2018-12-29 23:55:08 +08:00
|
|
|
os.indent(indent) << "}\n";
|
|
|
|
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 *>()) {
|
2019-02-02 07:40:22 +08:00
|
|
|
emitOperandMatch(tree, i, depth, indent);
|
2019-05-11 04:49:22 +08:00
|
|
|
} else if (opArg.is<NamedAttribute *>()) {
|
2019-02-02 07:40:22 +08:00
|
|
|
emitAttributeMatch(tree, i, depth, indent);
|
|
|
|
} 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
|
|
|
|
2019-10-22 11:47:49 +08:00
|
|
|
void PatternEmitter::emitOperandMatch(DagNode tree, int argIndex, int depth,
|
2019-02-02 07:40:22 +08:00
|
|
|
int indent) {
|
|
|
|
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) {
|
2019-08-21 20:35:07 +08:00
|
|
|
if (operand->isVariadic()) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
auto self =
|
2020-01-12 00:54:04 +08:00
|
|
|
formatv("(*castedOp{0}.getODSOperands({1}).begin()).getType()", depth,
|
|
|
|
argIndex);
|
2020-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(
|
|
|
|
depth,
|
|
|
|
tgfmt(constraint.getConditionTemplate(), &fmtCtx.withSelf(self)),
|
|
|
|
formatv("\"operand {0} of op '{1}' failed to satisfy constraint: "
|
|
|
|
"'{2}'\"",
|
|
|
|
operand - op.operand_begin(), op.getOperationName(),
|
|
|
|
constraint.getDescription()));
|
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 *>(); });
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
os.indent(indent) << formatv("{0} = castedOp{1}.getODSOperands({2});\n",
|
2019-10-22 11:47:49 +08:00
|
|
|
name, depth, argIndex - numPrevAttrs);
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 11:47:49 +08:00
|
|
|
void PatternEmitter::emitAttributeMatch(DagNode tree, int argIndex, int depth,
|
2019-02-02 07:40:22 +08:00
|
|
|
int indent) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
os.indent(indent) << "{\n";
|
|
|
|
indent += 2;
|
|
|
|
os.indent(indent) << formatv(
|
2019-12-31 22:28:18 +08:00
|
|
|
"auto tblgen_attr = op{0}->getAttrOfType<{1}>(\"{2}\");"
|
|
|
|
"(void)tblgen_attr;\n",
|
|
|
|
depth, attr.getStorageType(), namedAttr->name);
|
2019-04-05 00:25:38 +08:00
|
|
|
|
|
|
|
// TODO(antiagainst): This should use getter method to avoid duplication.
|
2019-12-03 01:33:24 +08:00
|
|
|
if (attr.hasDefaultValue()) {
|
2019-08-06 22:09:55 +08:00
|
|
|
os.indent(indent) << "if (!tblgen_attr) tblgen_attr = "
|
2020-01-29 03:23:46 +08:00
|
|
|
<< std::string(tgfmt(attr.getConstBuilderTemplate(),
|
|
|
|
&fmtCtx, attr.getDefaultValue()))
|
2019-04-05 00:25:38 +08:00
|
|
|
<< ";\n";
|
|
|
|
} 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-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(depth, tgfmt("tblgen_attr", &fmtCtx),
|
|
|
|
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(
|
|
|
|
depth,
|
|
|
|
tgfmt(matcher.getConditionTemplate(), &fmtCtx.withSelf("tblgen_attr")),
|
|
|
|
formatv("\"op '{0}' attribute '{1}' failed to satisfy constraint: "
|
|
|
|
"{2}\"",
|
|
|
|
op.getOperationName(), namedAttr->name,
|
|
|
|
matcher.getAsConstraint().getDescription()));
|
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 != "_") {
|
2019-08-06 22:09:55 +08:00
|
|
|
os.indent(indent) << formatv("{0} = tblgen_attr;\n", name);
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2019-04-05 00:25:38 +08:00
|
|
|
|
|
|
|
indent -= 2;
|
|
|
|
os.indent(indent) << "}\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(
|
|
|
|
int depth, const FmtObjectBase &matchFmt,
|
|
|
|
const llvm::formatv_object_base &failureFmt) {
|
|
|
|
// {0} The match depth (used to get the operation that failed to match).
|
|
|
|
// {1} The format for the match string.
|
|
|
|
// {2} The format for the failure string.
|
|
|
|
const char *matchStr = R"(
|
|
|
|
if (!({1})) {
|
|
|
|
return rewriter.notifyMatchFailure(op{0}, [&](::mlir::Diagnostic &diag) {
|
|
|
|
diag << {2};
|
|
|
|
});
|
|
|
|
})";
|
|
|
|
os << llvm::formatv(matchStr, depth, matchFmt.str(), failureFmt.str())
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
void PatternEmitter::emitMatchLogic(DagNode tree) {
|
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;
|
|
|
|
emitOpMatch(tree, 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(
|
|
|
|
depth, tgfmt(condition, &fmtCtx.withSelf(self.str())),
|
|
|
|
formatv("\"value entity '{0}' failed to satisfy constraint: {1}\"",
|
|
|
|
entities.front(), constraint.getDescription()));
|
|
|
|
|
[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 {
|
2019-08-02 02:50:47 +08:00
|
|
|
// TODO(b/138794486): 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-03-18 11:10:27 +08:00
|
|
|
emitMatchCheck(depth,
|
|
|
|
tgfmt(condition, &fmtCtx.withSelf(self), names[0],
|
|
|
|
names[1], names[2], names[3]),
|
|
|
|
formatv("\"entities '{0}' failed to satisfy constraint: "
|
|
|
|
"{1}\"",
|
|
|
|
llvm::join(entities, ", "),
|
|
|
|
constraint.getDescription()));
|
2019-02-14 06:30:40 +08:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
os << formatv("/* Generated from:\n\t{0:$[ instantiating\n\t]}\n*/\n",
|
|
|
|
make_range(locs.rbegin(), locs.rend()));
|
2018-12-29 23:55:08 +08:00
|
|
|
os << formatv(R"(struct {0} : public RewritePattern {
|
2019-08-06 22:09:55 +08:00
|
|
|
{0}(MLIRContext *context)
|
|
|
|
: RewritePattern("{1}", {{)",
|
2019-05-25 10:35:23 +08:00
|
|
|
rewriteName, rootName);
|
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();
|
|
|
|
});
|
|
|
|
interleaveComma(sortedResultOps, os, [&](const Operator *op) {
|
2019-05-25 10:35:23 +08:00
|
|
|
os << '"' << op->getOperationName() << '"';
|
|
|
|
});
|
|
|
|
os << formatv(R"(}, {0}, context) {{})", pattern.getBenefit()) << "\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
// Emit matchAndRewrite() function.
|
|
|
|
os << R"(
|
2020-03-18 11:07:55 +08:00
|
|
|
LogicalResult matchAndRewrite(Operation *op0,
|
2019-08-06 22:09:55 +08:00
|
|
|
PatternRewriter &rewriter) const override {
|
|
|
|
)";
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
// Register all symbols bound in the source pattern.
|
|
|
|
pattern.collectSourcePatternBoundSymbols(symbolInfoMap);
|
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
llvm::dbgs() << "start creating local variables for capturing matches\n");
|
2019-08-06 22:09:55 +08:00
|
|
|
os.indent(4) << "// Variables for capturing values and attributes used for "
|
|
|
|
"creating ops\n";
|
2019-08-10 10:03:58 +08:00
|
|
|
// Create local variables for storing the arguments and results bound
|
|
|
|
// to symbols.
|
|
|
|
for (const auto &symbolInfoPair : symbolInfoMap) {
|
|
|
|
StringRef symbol = symbolInfoPair.getKey();
|
|
|
|
auto &info = symbolInfoPair.getValue();
|
|
|
|
os.indent(4) << info.getVarDecl(symbol);
|
2019-04-23 04:40:30 +08:00
|
|
|
}
|
2019-08-06 22:09:55 +08:00
|
|
|
// TODO(jpienaar): capture ops with consistent numbering so that it can be
|
|
|
|
// reused for fused loc.
|
|
|
|
os.indent(4) << formatv("Operation *tblgen_ops[{0}];\n\n",
|
|
|
|
pattern.getSourcePattern().getNumOps());
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
llvm::dbgs() << "done creating local variables for capturing matches\n");
|
2018-12-29 23:55:08 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
os.indent(4) << "// Match\n";
|
|
|
|
os.indent(4) << "tblgen_ops[0] = op0;\n";
|
|
|
|
emitMatchLogic(sourceTree);
|
|
|
|
os << "\n";
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
os.indent(4) << "// Rewrite\n";
|
|
|
|
emitRewriteLogic();
|
|
|
|
|
2020-03-18 11:07:55 +08:00
|
|
|
os.indent(4) << "return success();\n";
|
2019-08-06 22:09:55 +08:00
|
|
|
os << " };\n";
|
2019-01-26 02:09:15 +08:00
|
|
|
os << "};\n";
|
|
|
|
}
|
|
|
|
|
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-04-07 22:44:19 +08:00
|
|
|
os.indent(4) << "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())
|
|
|
|
os.indent(4) << 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.
|
|
|
|
os.indent(4) << "rewriter.eraseOp(op0);\n";
|
|
|
|
} else {
|
|
|
|
// Process replacement result patterns.
|
2019-12-24 06:45:01 +08:00
|
|
|
os.indent(4) << "SmallVector<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);
|
|
|
|
os.indent(4) << "\n";
|
|
|
|
// Resolve each symbol for all range use so that we can loop over them.
|
2019-12-31 22:28:18 +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).
|
|
|
|
// TODO(b/147096809): Revisit the need for materializing a vector.
|
2019-10-18 23:19:54 +08:00
|
|
|
os << symbolInfoMap.getAllRangeUse(
|
2019-12-31 22:28:18 +08:00
|
|
|
val,
|
|
|
|
" for (auto v : SmallVector<Value, 4>{ {0} }) {{ "
|
|
|
|
"tblgen_repl_values.push_back(v); }",
|
2019-10-21 03:23:38 +08:00
|
|
|
"\n");
|
2019-10-18 23:19:54 +08:00
|
|
|
}
|
2019-08-21 20:35:07 +08:00
|
|
|
os.indent(4) << "\n";
|
2019-11-15 23:33:21 +08:00
|
|
|
os.indent(4) << "rewriter.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()) {
|
|
|
|
auto symbol = handleReplaceWithNativeCodeCall(resultTree);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PatternEmitter::handleUseLocationOf(DagNode tree) {
|
|
|
|
assert(tree.isLocationDirective());
|
|
|
|
auto lookUpArgLoc = [this, &tree](int idx) {
|
|
|
|
const auto *const lookupFmt = "(*{0}.begin()).getLoc()";
|
|
|
|
return symbolInfoMap.getAllRangeUse(tree.getArgName(idx), lookupFmt);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tree.getNumArgs() != 1) {
|
|
|
|
std::string ret;
|
|
|
|
llvm::raw_string_ostream os(ret);
|
|
|
|
os << "rewriter.getFusedLoc({";
|
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i)
|
|
|
|
os << (i ? ", " : "") << lookUpArgLoc(i);
|
|
|
|
os << "})";
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tree.getSymbol().empty())
|
|
|
|
PrintFatalError(loc, "cannot bind symbol to location");
|
|
|
|
|
|
|
|
return lookUpArgLoc(0);
|
2019-02-09 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
std::string PatternEmitter::handleOpArgument(DagLeaf leaf,
|
|
|
|
StringRef patArgName) {
|
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");
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:09:55 +08:00
|
|
|
std::string PatternEmitter::handleReplaceWithNativeCodeCall(DagNode tree) {
|
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();
|
2019-08-02 02:50:47 +08:00
|
|
|
// TODO(b/138794486): replace formatv arguments with the exact specified args.
|
2019-03-13 04:55:50 +08:00
|
|
|
SmallVector<std::string, 8> attrs(8);
|
|
|
|
if (tree.getNumArgs() > 8) {
|
2019-04-23 05:13:45 +08:00
|
|
|
PrintFatalError(loc, "unsupported NativeCodeCall argument numbers: " +
|
2019-03-13 04:55:50 +08:00
|
|
|
Twine(tree.getNumArgs()));
|
|
|
|
}
|
2019-05-04 10:48:57 +08:00
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
2019-03-13 04:55:50 +08:00
|
|
|
attrs[i] = handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i));
|
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
|
|
|
}
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(tgfmt(fmt, &fmtCtx, attrs[0], attrs[1], attrs[2], attrs[3],
|
|
|
|
attrs[4], attrs[5], attrs[6], attrs[7]));
|
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();
|
|
|
|
}
|
|
|
|
// TODO(antiagainst): This considers all NativeCodeCall as returning one
|
|
|
|
// value. Enhance if multi-value ones are needed.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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-04-07 22:44:19 +08:00
|
|
|
// Get the location for this operation if explicitly provided.
|
|
|
|
std::string locToUse;
|
|
|
|
if (numPatArgs != 0) {
|
|
|
|
if (auto lastArg = tree.getArgAsNestedDag(numPatArgs - 1))
|
|
|
|
if (lastArg.isLocationDirective())
|
|
|
|
locToUse = handleUseLocationOf(lastArg);
|
2019-02-09 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:44:19 +08:00
|
|
|
auto inPattern = numPatArgs - !locToUse.empty();
|
|
|
|
if (numOpArgs != inPattern) {
|
|
|
|
PrintFatalError(loc,
|
|
|
|
formatv("resultant op '{0}' argument number mismatch: "
|
|
|
|
"{1} in pattern vs. {2} in definition",
|
|
|
|
resultOp.getOperationName(), inPattern, numOpArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no explicit location is given, use the default, all fused, location.
|
|
|
|
if (locToUse.empty())
|
|
|
|
locToUse = "odsLoc";
|
|
|
|
|
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.
|
2019-05-04 10:48:57 +08:00
|
|
|
for (int i = 0, e = resultOp.getNumOperands(); 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.
|
|
|
|
os.indent(4) << formatv("{0} {1};\n", resultOp.getQualCppClassName(),
|
|
|
|
valuePackName);
|
|
|
|
os.indent(4) << "{\n";
|
|
|
|
|
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 =
|
2019-11-26 09:26:16 +08:00
|
|
|
resultOp.getTrait("OpTrait::SameOperandsAndResultType");
|
|
|
|
bool useFirstAttr = resultOp.getTrait("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.
|
|
|
|
createAggregateLocalVarsForOpArgs(tree, childNodeNames);
|
2020-04-07 22:44:19 +08:00
|
|
|
|
2019-11-15 23:33:21 +08:00
|
|
|
// Then create the op.
|
|
|
|
os.indent(6) << formatv(
|
2020-04-07 22:44:19 +08:00
|
|
|
"{0} = rewriter.create<{1}>({2}, tblgen_values, tblgen_attrs);\n",
|
|
|
|
valuePackName, resultOp.getQualCppClassName(), locToUse);
|
2019-11-15 23:33:21 +08:00
|
|
|
os.indent(4) << "}\n";
|
|
|
|
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
|
|
|
|
|
|
|
os.indent(6) << formatv("{0} = rewriter.create<{1}>({2}", valuePackName,
|
|
|
|
resultOp.getQualCppClassName(), locToUse);
|
2019-11-15 23:33:21 +08:00
|
|
|
supplyValuesForOpArgs(tree, childNodeNames);
|
|
|
|
os << "\n );\n";
|
|
|
|
os.indent(4) << "}\n";
|
|
|
|
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.
|
|
|
|
createAggregateLocalVarsForOpArgs(tree, childNodeNames);
|
|
|
|
|
|
|
|
// Then prepare the result types. We need to specify the types for all
|
|
|
|
// results.
|
|
|
|
os.indent(6) << formatv(
|
|
|
|
"SmallVector<Type, 4> tblgen_types; (void)tblgen_types;\n");
|
|
|
|
int numResults = resultOp.getNumResults();
|
|
|
|
if (numResults != 0) {
|
|
|
|
for (int i = 0; i < numResults; ++i)
|
2019-12-23 13:59:55 +08:00
|
|
|
os.indent(6) << formatv("for (auto v : castedOp0.getODSResults({0})) {{"
|
2020-01-12 00:54:04 +08:00
|
|
|
"tblgen_types.push_back(v.getType()); }\n",
|
2019-11-15 23:33:21 +08:00
|
|
|
resultIndex + i);
|
|
|
|
}
|
2020-04-07 22:44:19 +08:00
|
|
|
os.indent(6) << formatv("{0} = rewriter.create<{1}>({2}, tblgen_types, "
|
2019-11-15 23:33:21 +08:00
|
|
|
"tblgen_values, tblgen_attrs);\n",
|
2020-04-07 22:44:19 +08:00
|
|
|
valuePackName, resultOp.getQualCppClassName(),
|
|
|
|
locToUse);
|
2019-11-15 23:33:21 +08:00
|
|
|
os.indent(4) << "}\n";
|
|
|
|
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 *>();
|
|
|
|
if (!operand) {
|
|
|
|
// We do not need special handling for attributes.
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-15 23:33:21 +08:00
|
|
|
|
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++));
|
2019-12-24 06:45:01 +08:00
|
|
|
os.indent(6) << formatv("SmallVector<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);
|
2019-12-23 13:59:55 +08:00
|
|
|
os.indent(6) << formatv("for (auto v : {0}) {1}.push_back(v);\n", range,
|
2019-08-21 20:35:07 +08:00
|
|
|
varName);
|
|
|
|
} else {
|
2020-01-29 03:23:46 +08:00
|
|
|
varName = std::string(formatv("tblgen_value_{0}", valueIndex++));
|
2019-12-24 06:45:01 +08:00
|
|
|
os.indent(6) << formatv("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(
|
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames) {
|
|
|
|
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.
|
2019-08-21 20:35:07 +08:00
|
|
|
(os << ",\n").indent(8);
|
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,
|
2019-08-06 22:09:55 +08:00
|
|
|
handleReplaceWithNativeCodeCall(subTree));
|
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()) {
|
2019-03-13 04:55:50 +08:00
|
|
|
// TODO(jpienaar): 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(
|
|
|
|
DagNode node, const ChildNodeIndexNameMap &childNodeNames) {
|
|
|
|
Operator &resultOp = node.getDialectOp(opMap);
|
|
|
|
|
|
|
|
os.indent(6) << formatv(
|
2019-12-24 06:45:01 +08:00
|
|
|
"SmallVector<Value, 4> tblgen_values; (void)tblgen_values;\n");
|
2019-11-15 23:33:21 +08:00
|
|
|
os.indent(6) << formatv(
|
|
|
|
"SmallVector<NamedAttribute, 4> tblgen_attrs; (void)tblgen_attrs;\n");
|
|
|
|
|
|
|
|
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
|
|
|
const char *addAttrCmd = "if ({1}) {{"
|
|
|
|
" tblgen_attrs.emplace_back(rewriter."
|
|
|
|
"getIdentifier(\"{0}\"), {1}); }\n";
|
|
|
|
// 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");
|
|
|
|
os.indent(6) << formatv(addAttrCmd, opArgName,
|
|
|
|
handleReplaceWithNativeCodeCall(subTree));
|
|
|
|
} else {
|
|
|
|
auto leaf = node.getArgAsLeaf(argIndex);
|
|
|
|
// The argument in the result DAG pattern.
|
|
|
|
auto patArgName = node.getArgName(argIndex);
|
|
|
|
os.indent(6) << formatv(addAttrCmd, opArgName,
|
|
|
|
handleOpArgument(leaf, patArgName));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
os.indent(6) << formatv(
|
2019-12-23 13:59:55 +08:00
|
|
|
"for (auto v : {0}) tblgen_values.push_back(v);\n", range);
|
2019-11-15 23:33:21 +08:00
|
|
|
} else {
|
|
|
|
os.indent(6) << formatv("tblgen_values.push_back(", varName);
|
|
|
|
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.
|
2020-01-04 05:31:24 +08:00
|
|
|
os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated(MLIRContext "
|
|
|
|
"*context, OwningRewritePatternList *patterns) {\n";
|
2019-04-11 02:37:53 +08:00
|
|
|
for (const auto &name : rewriterNames) {
|
2019-08-06 09:37:56 +08:00
|
|
|
os << " patterns->insert<" << name << ">(context);\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;
|
|
|
|
});
|