2018-12-27 20:56:03 +08:00
|
|
|
//===- RewriterGen.cpp - MLIR pattern rewriter generator ------------===//
|
2018-12-12 19:09:11 +08:00
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
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-01-17 01:23:14 +08:00
|
|
|
#include "mlir/TableGen/Attribute.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"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2018-12-29 04:02:08 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2019-01-26 02:13:35 +08:00
|
|
|
using mlir::tblgen::Argument;
|
2019-01-10 05:50:20 +08:00
|
|
|
using mlir::tblgen::Attribute;
|
2019-01-29 06:04:40 +08:00
|
|
|
using mlir::tblgen::DagNode;
|
2019-01-26 02:13:35 +08:00
|
|
|
using mlir::tblgen::NamedAttribute;
|
|
|
|
using mlir::tblgen::Operand;
|
2019-01-09 09:19:37 +08:00
|
|
|
using mlir::tblgen::Operator;
|
2019-01-29 06:04:40 +08:00
|
|
|
using mlir::tblgen::Pattern;
|
|
|
|
using mlir::tblgen::RecordOperatorMap;
|
2019-01-09 09:19:37 +08:00
|
|
|
using mlir::tblgen::Type;
|
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2019-01-17 02:23:21 +08:00
|
|
|
// Wrapper around DAG argument.
|
2018-12-29 23:55:08 +08:00
|
|
|
struct DagArg {
|
2019-01-26 02:13:35 +08:00
|
|
|
DagArg(Argument arg, Init *constraintInit)
|
2019-01-17 02:23:21 +08:00
|
|
|
: arg(arg), constraintInit(constraintInit) {}
|
2018-12-29 23:55:08 +08:00
|
|
|
bool isAttr();
|
|
|
|
|
2019-01-26 02:13:35 +08:00
|
|
|
Argument arg;
|
2019-01-17 02:23:21 +08:00
|
|
|
Init *constraintInit;
|
2018-12-29 23:55:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
2019-01-26 02:13:35 +08:00
|
|
|
bool DagArg::isAttr() { return arg.is<NamedAttribute *>(); }
|
2018-12-29 23:55:08 +08:00
|
|
|
|
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-01-29 06:04:40 +08:00
|
|
|
static void emit(StringRef rewriteName, Record *p, RecordOperatorMap *mapper,
|
|
|
|
raw_ostream &os);
|
2018-12-29 23:55:08 +08:00
|
|
|
|
|
|
|
private:
|
2019-01-29 06:04:40 +08:00
|
|
|
PatternEmitter(Record *pat, RecordOperatorMap *mapper, raw_ostream &os)
|
|
|
|
: loc(pat->getLoc()), opMap(mapper), pattern(pat, mapper), os(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-01-29 06:04:40 +08:00
|
|
|
// Emits the match() method.
|
|
|
|
void emitMatchMethod(DagNode tree);
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-26 02:09:15 +08:00
|
|
|
// Emits the rewrite() method.
|
|
|
|
void emitRewriteMethod();
|
|
|
|
|
|
|
|
// Emits the C++ statement to replace the matched DAG with an existing value.
|
2019-01-29 06:04:40 +08:00
|
|
|
void emitReplaceWithExistingValue(DagNode resultTree);
|
2019-01-26 02:09:15 +08:00
|
|
|
// Emits the C++ statement to replace the matched DAG with a new op.
|
2019-01-29 06:04:40 +08:00
|
|
|
void emitReplaceOpWithNewOp(DagNode resultTree);
|
2019-01-26 02:09:15 +08:00
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
// Emits the value of constant attribute to `os`.
|
2018-12-30 06:34:06 +08:00
|
|
|
void emitAttributeValue(Record *constAttr);
|
2018-12-29 23:55:08 +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-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;
|
|
|
|
// Op's TableGen Record to wrapper object
|
|
|
|
RecordOperatorMap *opMap;
|
|
|
|
// Handy wrapper for pattern being emitted
|
|
|
|
Pattern pattern;
|
2018-12-29 23:55:08 +08:00
|
|
|
raw_ostream &os;
|
2018-12-29 04:02:08 +08:00
|
|
|
};
|
|
|
|
} // end namespace
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitAttributeValue(Record *constAttr) {
|
2019-01-10 05:50:20 +08:00
|
|
|
Attribute attr(constAttr->getValueAsDef("attr"));
|
2018-12-30 06:34:06 +08:00
|
|
|
auto value = constAttr->getValue("value");
|
|
|
|
|
2019-01-17 01:23:14 +08:00
|
|
|
if (!attr.isConstBuildable())
|
2019-01-29 06:04:40 +08:00
|
|
|
PrintFatalError(loc, "Attribute " + attr.getTableGenDefName() +
|
|
|
|
" 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
|
2019-01-17 01:23:14 +08:00
|
|
|
os << formatv(attr.getConstBuilderTemplate().str().c_str(), "rewriter",
|
2019-01-09 09:19:22 +08:00
|
|
|
value->getValue()->getAsUnquotedString());
|
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);
|
2018-12-29 23:55:08 +08:00
|
|
|
int indent = 4 + 2 * depth;
|
|
|
|
// Skip the operand matching at depth 0 as the pattern rewriter already does.
|
|
|
|
if (depth != 0) {
|
|
|
|
// Skip if there is no defining instruction (e.g., arguments to function).
|
|
|
|
os.indent(indent) << formatv("if (!op{0}) return matchFailure();\n", depth);
|
|
|
|
os.indent(indent) << formatv(
|
2019-01-03 08:11:42 +08:00
|
|
|
"if (!op{0}->isa<{1}>()) return matchFailure();\n", depth,
|
2019-01-30 01:27:04 +08:00
|
|
|
op.getQualCppClassName());
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
2019-01-29 06:04:40 +08:00
|
|
|
if (tree.getNumArgs() != op.getNumArgs())
|
|
|
|
PrintFatalError(loc, Twine("mismatch in number of arguments to op '") +
|
|
|
|
op.getOperationName() +
|
|
|
|
"' in pattern and op's definition");
|
|
|
|
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
|
2019-01-08 01:52:26 +08:00
|
|
|
auto opArg = op.getArg(i);
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
if (DagNode argTree = tree.getArgAsNestedDag(i)) {
|
2018-12-29 23:55:08 +08:00
|
|
|
os.indent(indent) << "{\n";
|
|
|
|
os.indent(indent + 2) << formatv(
|
|
|
|
"auto op{0} = op{1}->getOperand({2})->getDefiningInst();\n",
|
|
|
|
depth + 1, depth, i);
|
2019-01-29 06:04:40 +08:00
|
|
|
emitOpMatch(argTree, depth + 1);
|
2018-12-29 23:55:08 +08:00
|
|
|
os.indent(indent) << "}\n";
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
|
|
|
|
// Verify arguments.
|
2019-01-29 06:04:40 +08:00
|
|
|
if (auto defInit = tree.getArgAsDefInit(i)) {
|
2019-01-06 00:11:29 +08:00
|
|
|
// Verify operands.
|
2019-01-26 02:13:35 +08:00
|
|
|
if (auto *operand = opArg.dyn_cast<Operand *>()) {
|
2019-01-06 00:11:29 +08:00
|
|
|
// Skip verification where not needed due to definition of op.
|
2019-01-30 22:05:27 +08:00
|
|
|
if (operand->type == Type(defInit))
|
2019-01-08 01:52:26 +08:00
|
|
|
goto StateCapture;
|
2019-01-06 00:11:29 +08:00
|
|
|
|
|
|
|
if (!defInit->getDef()->isSubClassOf("Type"))
|
2019-01-29 06:04:40 +08:00
|
|
|
PrintFatalError(loc, "type argument required for operand");
|
2019-01-06 00:11:29 +08:00
|
|
|
|
2019-01-16 02:42:21 +08:00
|
|
|
auto constraint = tblgen::TypeConstraint(*defInit);
|
2019-01-06 00:11:29 +08:00
|
|
|
os.indent(indent)
|
|
|
|
<< "if (!("
|
2019-01-17 04:36:10 +08:00
|
|
|
<< formatv(constraint.getConditionTemplate().c_str(),
|
2019-01-06 00:11:29 +08:00
|
|
|
formatv("op{0}->getOperand({1})->getType()", depth, i))
|
|
|
|
<< ")) return matchFailure();\n";
|
|
|
|
}
|
2019-01-08 01:52:26 +08:00
|
|
|
|
|
|
|
// TODO(jpienaar): Verify attributes.
|
2019-01-26 02:13:35 +08:00
|
|
|
if (auto *namedAttr = opArg.dyn_cast<NamedAttribute *>()) {
|
2019-01-28 23:13:40 +08:00
|
|
|
auto constraint = tblgen::AttrConstraint(defInit);
|
|
|
|
std::string condition = formatv(
|
|
|
|
constraint.getConditionTemplate().c_str(),
|
|
|
|
formatv("op{0}->getAttrOfType<{1}>(\"{2}\")", depth,
|
|
|
|
namedAttr->attr.getStorageType(), namedAttr->getName()));
|
|
|
|
os.indent(indent) << "if (!(" << condition
|
|
|
|
<< ")) return matchFailure();\n";
|
2019-01-08 01:52:26 +08:00
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 01:52:26 +08:00
|
|
|
StateCapture:
|
2019-01-29 06:04:40 +08:00
|
|
|
auto name = tree.getArgName(i);
|
2018-12-29 23:55:08 +08:00
|
|
|
if (name.empty())
|
|
|
|
continue;
|
2019-01-26 02:13:35 +08:00
|
|
|
if (opArg.is<Operand *>())
|
2019-01-08 01:52:26 +08:00
|
|
|
os.indent(indent) << "state->" << name << " = op" << depth
|
|
|
|
<< "->getOperand(" << i << ");\n";
|
2019-01-26 02:13:35 +08:00
|
|
|
if (auto namedAttr = opArg.dyn_cast<NamedAttribute *>()) {
|
2019-01-08 01:52:26 +08:00
|
|
|
os.indent(indent) << "state->" << name << " = op" << depth
|
2019-01-10 05:50:20 +08:00
|
|
|
<< "->getAttrOfType<"
|
|
|
|
<< namedAttr->attr.getStorageType() << ">(\""
|
|
|
|
<< namedAttr->getName() << "\");\n";
|
2019-01-08 01:52:26 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitMatchMethod(DagNode tree) {
|
2018-12-29 23:55:08 +08:00
|
|
|
// Emit the heading.
|
2018-12-29 04:02:08 +08:00
|
|
|
os << R"(
|
2018-12-29 23:55:08 +08:00
|
|
|
PatternMatchResult match(OperationInst *op0) const override {
|
2018-12-29 04:02:08 +08:00
|
|
|
// TODO: This just handle 1 result
|
2018-12-29 23:55:08 +08:00
|
|
|
if (op0->getNumResults() != 1) return matchFailure();
|
2019-02-01 00:33:47 +08:00
|
|
|
auto ctx = op0->getContext(); (void)ctx;
|
2018-12-29 04:02:08 +08:00
|
|
|
auto state = std::make_unique<MatchedState>();)"
|
|
|
|
<< "\n";
|
2019-01-29 06:04:40 +08:00
|
|
|
emitOpMatch(tree, 0);
|
2018-12-29 04:02:08 +08:00
|
|
|
os.indent(4) << "return matchSuccess(std::move(state));\n }\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emit(StringRef rewriteName) {
|
|
|
|
// Get the DAG tree for the source pattern
|
|
|
|
DagNode tree = pattern.getSourcePattern();
|
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
// TODO(jpienaar): the benefit metric is simply number of ops matched at the
|
|
|
|
// moment, revise.
|
2019-01-29 06:04:40 +08:00
|
|
|
unsigned benefit = tree.getNumOps();
|
|
|
|
|
|
|
|
const Operator &rootOp = pattern.getSourceRootOp();
|
|
|
|
auto rootName = rootOp.getOperationName();
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2018-12-29 23:55:08 +08:00
|
|
|
// Emit RewritePattern for Pattern.
|
|
|
|
os << formatv(R"(struct {0} : public RewritePattern {
|
2019-01-29 06:04:40 +08:00
|
|
|
{0}(MLIRContext *context) : RewritePattern("{1}", {2}, context) {{})",
|
|
|
|
rewriteName, rootName, benefit)
|
2018-12-29 23:55:08 +08:00
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
// Emit matched state.
|
|
|
|
os << " struct MatchedState : public PatternState {\n";
|
2019-01-29 06:04:40 +08:00
|
|
|
for (const auto &arg : pattern.getSourcePatternBoundArgs()) {
|
2019-01-26 02:13:35 +08:00
|
|
|
if (auto namedAttr = arg.second.arg.dyn_cast<NamedAttribute *>()) {
|
2019-01-17 02:23:21 +08:00
|
|
|
os.indent(4) << namedAttr->attr.getStorageType() << " " << arg.first()
|
2019-01-10 05:50:20 +08:00
|
|
|
<< ";\n";
|
2018-12-29 23:55:08 +08:00
|
|
|
} else {
|
|
|
|
os.indent(4) << "Value* " << arg.first() << ";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os << " };\n";
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
emitMatchMethod(tree);
|
2019-01-26 02:09:15 +08:00
|
|
|
emitRewriteMethod();
|
|
|
|
|
|
|
|
os << "};\n";
|
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitRewriteMethod() {
|
|
|
|
if (pattern.getNumResults() != 1)
|
2018-12-29 04:02:08 +08:00
|
|
|
PrintFatalError("only single result rules supported");
|
2019-01-29 06:04:40 +08:00
|
|
|
|
|
|
|
DagNode resultTree = pattern.getResultPattern(0);
|
2018-12-29 04:02:08 +08:00
|
|
|
|
|
|
|
// TODO(jpienaar): Expand to multiple results.
|
2019-01-29 06:04:40 +08:00
|
|
|
for (unsigned i = 0, e = resultTree.getNumArgs(); i != e; ++i)
|
|
|
|
if (resultTree.getArgAsNestedDag(i))
|
|
|
|
PrintFatalError(loc, "only single op result supported");
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-26 02:09:15 +08:00
|
|
|
os << R"(
|
2018-12-29 04:02:08 +08:00
|
|
|
void rewrite(OperationInst *op, std::unique_ptr<PatternState> state,
|
|
|
|
PatternRewriter &rewriter) const override {
|
|
|
|
auto& s = *static_cast<MatchedState *>(state.get());
|
2019-01-26 02:09:15 +08:00
|
|
|
)";
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
if (resultTree.isReplaceWithValue())
|
2019-01-26 02:09:15 +08:00
|
|
|
emitReplaceWithExistingValue(resultTree);
|
|
|
|
else
|
|
|
|
emitReplaceOpWithNewOp(resultTree);
|
|
|
|
|
|
|
|
os << " }\n";
|
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitReplaceWithExistingValue(DagNode resultTree) {
|
|
|
|
if (resultTree.getNumArgs() != 1) {
|
|
|
|
PrintFatalError(loc, "exactly one argument needed in the result pattern");
|
2019-01-26 02:09:15 +08:00
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
auto name = resultTree.getArgName(0);
|
|
|
|
pattern.ensureArgBoundInSourcePattern(name);
|
2019-01-26 02:09:15 +08:00
|
|
|
os.indent(4) << "rewriter.replaceOp(op, {s." << name << "});\n";
|
|
|
|
}
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emitReplaceOpWithNewOp(DagNode resultTree) {
|
|
|
|
Operator &resultOp = resultTree.getDialectOp(opMap);
|
|
|
|
auto numOpArgs =
|
|
|
|
resultOp.getNumOperands() + resultOp.getNumNativeAttributes();
|
2019-01-26 02:09:15 +08:00
|
|
|
|
|
|
|
os << formatv(R"(
|
2018-12-29 04:02:08 +08:00
|
|
|
rewriter.replaceOpWithNewOp<{0}>(op, op->getResult(0)->getType())",
|
2019-01-30 01:27:04 +08:00
|
|
|
resultOp.getCppClassName());
|
2019-01-29 06:04:40 +08:00
|
|
|
if (numOpArgs != resultTree.getNumArgs()) {
|
|
|
|
PrintFatalError(loc, Twine("mismatch between arguments of resultant op (") +
|
|
|
|
Twine(numOpArgs) +
|
|
|
|
") and arguments provided for rewrite (" +
|
|
|
|
Twine(resultTree.getNumArgs()) + Twine(')'));
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the builder call for the result.
|
|
|
|
// Add operands.
|
|
|
|
int i = 0;
|
|
|
|
for (auto operand : resultOp.getOperands()) {
|
|
|
|
// Start each operand on its own line.
|
|
|
|
(os << ",\n").indent(6);
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
auto name = resultTree.getArgName(i);
|
|
|
|
pattern.ensureArgBoundInSourcePattern(name);
|
2019-01-30 22:05:27 +08:00
|
|
|
if (!operand.name.empty())
|
|
|
|
os << "/*" << operand.name << "=*/";
|
2018-12-29 04:02:08 +08:00
|
|
|
os << "s." << name;
|
|
|
|
// TODO(jpienaar): verify types
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add attributes.
|
2019-01-29 06:04:40 +08:00
|
|
|
for (int e = resultTree.getNumArgs(); i != e; ++i) {
|
2018-12-29 04:02:08 +08:00
|
|
|
// Start each attribute on its own line.
|
|
|
|
(os << ",\n").indent(6);
|
|
|
|
|
|
|
|
// The argument in the result DAG pattern.
|
2019-01-29 06:04:40 +08:00
|
|
|
auto argName = resultTree.getArgName(i);
|
2019-01-08 01:52:26 +08:00
|
|
|
auto opName = resultOp.getArgName(i);
|
2019-01-29 06:04:40 +08:00
|
|
|
auto *defInit = resultTree.getArgAsDefInit(i);
|
2018-12-29 04:02:08 +08:00
|
|
|
auto *value = defInit ? defInit->getDef()->getValue("value") : nullptr;
|
2019-01-08 01:52:26 +08:00
|
|
|
if (!value) {
|
2019-01-29 06:04:40 +08:00
|
|
|
pattern.ensureArgBoundInSourcePattern(argName);
|
|
|
|
auto result = "s." + argName;
|
2019-01-17 02:23:21 +08:00
|
|
|
os << "/*" << opName << "=*/";
|
|
|
|
if (defInit) {
|
|
|
|
auto transform = defInit->getDef();
|
|
|
|
if (transform->isSubClassOf("tAttr")) {
|
|
|
|
// TODO(jpienaar): move to helper class.
|
|
|
|
os << formatv(
|
|
|
|
transform->getValueAsString("attrTransform").str().c_str(),
|
|
|
|
result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os << result;
|
2019-01-08 01:52:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
|
|
|
// TODO(jpienaar): Refactor out into map to avoid recomputing these.
|
|
|
|
auto argument = resultOp.getArg(i);
|
2019-01-26 02:13:35 +08:00
|
|
|
if (!argument.is<NamedAttribute *>())
|
2019-01-29 06:04:40 +08:00
|
|
|
PrintFatalError(loc, Twine("expected attribute ") + Twine(i));
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
if (!argName.empty())
|
|
|
|
os << "/*" << argName << "=*/";
|
2018-12-30 06:34:06 +08:00
|
|
|
emitAttributeValue(defInit->getDef());
|
2018-12-29 04:02:08 +08:00
|
|
|
// TODO(jpienaar): verify types
|
|
|
|
}
|
2019-01-26 02:09:15 +08:00
|
|
|
os << "\n );\n";
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2018-12-12 19:09:11 +08:00
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
void PatternEmitter::emit(StringRef rewriteName, Record *p,
|
|
|
|
RecordOperatorMap *mapper, raw_ostream &os) {
|
|
|
|
PatternEmitter(p, mapper, os).emit(rewriteName);
|
2018-12-29 23:55:08 +08:00
|
|
|
}
|
|
|
|
|
2018-12-12 19:09:11 +08:00
|
|
|
static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
|
|
|
|
emitSourceFileHeader("Rewriters", os);
|
|
|
|
const auto &patterns = recordKeeper.getAllDerivedDefinitions("Pattern");
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
// We put the map here because it can be shared among multiple patterns.
|
|
|
|
RecordOperatorMap recordOpMap;
|
|
|
|
|
2018-12-12 19:09:11 +08:00
|
|
|
// Ensure unique patterns simply by appending unique suffix.
|
|
|
|
std::string baseRewriteName = "GeneratedConvert";
|
2018-12-29 04:02:08 +08:00
|
|
|
int rewritePatternCount = 0;
|
|
|
|
for (Record *p : patterns) {
|
2019-01-29 06:04:40 +08:00
|
|
|
PatternEmitter::emit(baseRewriteName + llvm::utostr(rewritePatternCount++),
|
|
|
|
p, &recordOpMap, os);
|
2018-12-12 19:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit function to add the generated matchers to the pattern list.
|
|
|
|
os << "void populateWithGenerated(MLIRContext *context, "
|
|
|
|
<< "OwningRewritePatternList *patterns) {\n";
|
|
|
|
for (unsigned i = 0; i != rewritePatternCount; ++i) {
|
2018-12-29 04:02:08 +08:00
|
|
|
os.indent(2) << "patterns->push_back(std::make_unique<" << baseRewriteName
|
|
|
|
<< i << ">(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;
|
|
|
|
});
|