2019-05-15 06:03:48 +08:00
|
|
|
//===- Pattern.cpp - Pattern wrapper class --------------------------------===//
|
2019-01-29 06:04:40 +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
|
2019-01-29 06:04:40 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-01-29 06:04:40 +08:00
|
|
|
//
|
|
|
|
// Pattern wrapper class to simplify using TableGen Record defining a MLIR
|
|
|
|
// Pattern.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-01-02 09:26:44 +08:00
|
|
|
#include <utility>
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
#include "mlir/TableGen/Pattern.h"
|
2020-03-01 09:11:12 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2019-01-29 06:04:40 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2019-10-17 22:25:50 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2019-04-09 06:14:59 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
[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
|
|
|
#include "llvm/TableGen/Error.h"
|
2019-01-29 06:04:40 +08:00
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
2019-10-17 22:25:50 +08:00
|
|
|
#define DEBUG_TYPE "mlir-tblgen-pattern"
|
|
|
|
|
2019-01-29 06:04:40 +08:00
|
|
|
using namespace mlir;
|
2020-08-12 08:47:07 +08:00
|
|
|
using namespace tblgen;
|
2019-01-29 06:04:40 +08:00
|
|
|
|
2019-04-30 00:24:09 +08:00
|
|
|
using llvm::formatv;
|
2019-01-29 06:04:40 +08:00
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DagLeaf
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isUnspecified() const {
|
2019-04-01 23:58:53 +08:00
|
|
|
return dyn_cast_or_null<llvm::UnsetInit>(def);
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isOperandMatcher() const {
|
2019-02-02 07:40:22 +08:00
|
|
|
// Operand matchers specify a type constraint.
|
2019-04-01 23:58:53 +08:00
|
|
|
return isSubClassOf("TypeConstraint");
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isAttrMatcher() const {
|
2019-03-13 04:55:50 +08:00
|
|
|
// Attribute matchers specify an attribute constraint.
|
2019-04-01 23:58:53 +08:00
|
|
|
return isSubClassOf("AttrConstraint");
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isNativeCodeCall() const {
|
2019-04-23 05:13:45 +08:00
|
|
|
return isSubClassOf("NativeCodeCall");
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isConstantAttr() const { return isSubClassOf("ConstantAttr"); }
|
2019-04-01 23:58:53 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isEnumAttrCase() const {
|
2019-07-01 20:26:14 +08:00
|
|
|
return isSubClassOf("EnumAttrCaseInfo");
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 05:25:29 +08:00
|
|
|
bool DagLeaf::isStringAttr() const { return isa<llvm::StringInit>(def); }
|
2020-04-10 08:18:34 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Constraint DagLeaf::getAsConstraint() const {
|
[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
|
|
|
assert((isOperandMatcher() || isAttrMatcher()) &&
|
|
|
|
"the DAG leaf must be operand or attribute");
|
|
|
|
return Constraint(cast<llvm::DefInit>(def)->getDef());
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
ConstantAttr DagLeaf::getAsConstantAttr() const {
|
2019-02-02 07:40:22 +08:00
|
|
|
assert(isConstantAttr() && "the DAG leaf must be constant attribute");
|
|
|
|
return ConstantAttr(cast<llvm::DefInit>(def));
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
EnumAttrCase DagLeaf::getAsEnumAttrCase() const {
|
2019-04-01 23:58:53 +08:00
|
|
|
assert(isEnumAttrCase() && "the DAG leaf must be an enum attribute case");
|
|
|
|
return EnumAttrCase(cast<llvm::DefInit>(def));
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string DagLeaf::getConditionTemplate() const {
|
[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
|
|
|
return getAsConstraint().getConditionTemplate();
|
2019-02-02 07:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
llvm::StringRef DagLeaf::getNativeCodeTemplate() const {
|
2019-04-23 05:13:45 +08:00
|
|
|
assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
|
|
|
|
return cast<llvm::DefInit>(def)->getDef()->getValueAsString("expression");
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:23:06 +08:00
|
|
|
int DagLeaf::getNumReturnsOfNativeCode() const {
|
|
|
|
assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
|
|
|
|
return cast<llvm::DefInit>(def)->getDef()->getValueAsInt("numReturns");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string DagLeaf::getStringAttr() const {
|
2020-04-10 08:18:34 +08:00
|
|
|
assert(isStringAttr() && "the DAG leaf must be string attribute");
|
|
|
|
return def->getAsUnquotedString();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagLeaf::isSubClassOf(StringRef superclass) const {
|
2019-04-01 23:58:53 +08:00
|
|
|
if (auto *defInit = dyn_cast_or_null<llvm::DefInit>(def))
|
|
|
|
return defInit->getDef()->isSubClassOf(superclass);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void DagLeaf::print(raw_ostream &os) const {
|
2019-10-17 22:25:50 +08:00
|
|
|
if (def)
|
|
|
|
def->print(os);
|
|
|
|
}
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DagNode
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagNode::isNativeCodeCall() const {
|
2019-04-23 05:13:45 +08:00
|
|
|
if (auto *defInit = dyn_cast_or_null<llvm::DefInit>(node->getOperator()))
|
|
|
|
return defInit->getDef()->isSubClassOf("NativeCodeCall");
|
|
|
|
return false;
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagNode::isOperation() const {
|
2021-09-16 05:25:29 +08:00
|
|
|
return !isNativeCodeCall() && !isReplaceWithValue() &&
|
2021-11-09 06:56:40 +08:00
|
|
|
!isLocationDirective() && !isReturnTypeDirective() && !isEither();
|
2019-05-25 10:35:23 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
llvm::StringRef DagNode::getNativeCodeTemplate() const {
|
2019-04-23 05:13:45 +08:00
|
|
|
assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
|
2019-03-13 04:55:50 +08:00
|
|
|
return cast<llvm::DefInit>(node->getOperator())
|
|
|
|
->getDef()
|
2019-04-23 05:13:45 +08:00
|
|
|
->getValueAsString("expression");
|
2019-03-13 04:55:50 +08:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:23:06 +08:00
|
|
|
int DagNode::getNumReturnsOfNativeCode() const {
|
|
|
|
assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
|
|
|
|
return cast<llvm::DefInit>(node->getOperator())
|
|
|
|
->getDef()
|
|
|
|
->getValueAsInt("numReturns");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
llvm::StringRef DagNode::getSymbol() const { return node->getNameStr(); }
|
2019-02-14 06:30:40 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator &DagNode::getDialectOp(RecordOperatorMap *mapper) const {
|
2019-01-29 06:04:40 +08:00
|
|
|
llvm::Record *opDef = cast<llvm::DefInit>(node->getOperator())->getDef();
|
2019-04-13 00:52:11 +08:00
|
|
|
auto it = mapper->find(opDef);
|
|
|
|
if (it != mapper->end())
|
|
|
|
return *it->second;
|
2019-08-18 02:05:35 +08:00
|
|
|
return *mapper->try_emplace(opDef, std::make_unique<Operator>(opDef))
|
2019-04-13 00:52:11 +08:00
|
|
|
.first->second;
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int DagNode::getNumOps() const {
|
2021-11-09 06:56:40 +08:00
|
|
|
// We want to get number of operations recursively involved in the DAG tree.
|
|
|
|
// All other directives should be excluded.
|
|
|
|
int count = isOperation() ? 1 : 0;
|
2019-05-04 10:48:57 +08:00
|
|
|
for (int i = 0, e = getNumArgs(); i != e; ++i) {
|
2019-01-29 06:04:40 +08:00
|
|
|
if (auto child = getArgAsNestedDag(i))
|
|
|
|
count += child.getNumOps();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int DagNode::getNumArgs() const { return node->getNumArgs(); }
|
2019-01-29 06:04:40 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagNode::isNestedDagArg(unsigned index) const {
|
2019-01-29 06:04:40 +08:00
|
|
|
return isa<llvm::DagInit>(node->getArg(index));
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
DagNode DagNode::getArgAsNestedDag(unsigned index) const {
|
2019-01-29 06:04:40 +08:00
|
|
|
return DagNode(dyn_cast_or_null<llvm::DagInit>(node->getArg(index)));
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
DagLeaf DagNode::getArgAsLeaf(unsigned index) const {
|
2019-02-02 07:40:22 +08:00
|
|
|
assert(!isNestedDagArg(index));
|
|
|
|
return DagLeaf(node->getArg(index));
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef DagNode::getArgName(unsigned index) const {
|
2019-01-29 06:04:40 +08:00
|
|
|
return node->getArgNameStr(index);
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagNode::isReplaceWithValue() const {
|
2019-01-29 06:04:40 +08:00
|
|
|
auto *dagOpDef = cast<llvm::DefInit>(node->getOperator())->getDef();
|
|
|
|
return dagOpDef->getName() == "replaceWithValue";
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool DagNode::isLocationDirective() const {
|
2020-04-07 22:44:19 +08:00
|
|
|
auto *dagOpDef = cast<llvm::DefInit>(node->getOperator())->getDef();
|
|
|
|
return dagOpDef->getName() == "location";
|
|
|
|
}
|
|
|
|
|
2021-09-16 05:25:29 +08:00
|
|
|
bool DagNode::isReturnTypeDirective() const {
|
|
|
|
auto *dagOpDef = cast<llvm::DefInit>(node->getOperator())->getDef();
|
|
|
|
return dagOpDef->getName() == "returnType";
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:56:40 +08:00
|
|
|
bool DagNode::isEither() const {
|
|
|
|
auto *dagOpDef = cast<llvm::DefInit>(node->getOperator())->getDef();
|
|
|
|
return dagOpDef->getName() == "either";
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void DagNode::print(raw_ostream &os) const {
|
2019-10-17 22:25:50 +08:00
|
|
|
if (node)
|
|
|
|
node->print(os);
|
|
|
|
}
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolInfoMap
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef SymbolInfoMap::getValuePackName(StringRef symbol, int *index) {
|
2019-08-10 10:03:58 +08:00
|
|
|
StringRef name, indexStr;
|
|
|
|
int idx = -1;
|
|
|
|
std::tie(name, indexStr) = symbol.rsplit("__");
|
|
|
|
|
|
|
|
if (indexStr.consumeInteger(10, idx)) {
|
|
|
|
// The second part is not an index; we return the whole symbol as-is.
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
if (index) {
|
|
|
|
*index = idx;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
SymbolInfoMap::SymbolInfo::SymbolInfo(const Operator *op, SymbolInfo::Kind kind,
|
2021-07-21 11:23:06 +08:00
|
|
|
Optional<DagAndConstant> dagAndConstant)
|
2022-01-02 09:26:44 +08:00
|
|
|
: op(op), kind(kind), dagAndConstant(std::move(dagAndConstant)) {}
|
2019-08-10 10:03:58 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int SymbolInfoMap::SymbolInfo::getStaticValueCount() const {
|
2019-08-10 10:03:58 +08:00
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attr:
|
|
|
|
case Kind::Operand:
|
|
|
|
case Kind::Value:
|
|
|
|
return 1;
|
|
|
|
case Kind::Result:
|
|
|
|
return op->getNumResults();
|
2021-07-21 11:23:06 +08:00
|
|
|
case Kind::MultipleValues:
|
|
|
|
return getSize();
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2019-08-14 05:22:58 +08:00
|
|
|
llvm_unreachable("unknown kind");
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
std::string SymbolInfoMap::SymbolInfo::getVarName(StringRef name) const {
|
|
|
|
return alternativeName.hasValue() ? alternativeName.getValue() : name.str();
|
|
|
|
}
|
|
|
|
|
2021-09-21 06:59:56 +08:00
|
|
|
std::string SymbolInfoMap::SymbolInfo::getVarTypeStr(StringRef name) const {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "getVarTypeStr for '" << name << "': ");
|
2019-08-10 10:03:58 +08:00
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attr: {
|
2021-09-21 06:59:56 +08:00
|
|
|
if (op)
|
|
|
|
return op->getArg(getArgIndex())
|
|
|
|
.get<NamedAttribute *>()
|
|
|
|
->attr.getStorageType()
|
|
|
|
.str();
|
2020-10-10 04:32:01 +08:00
|
|
|
// TODO(suderman): Use a more exact type when available.
|
2022-02-03 18:50:26 +08:00
|
|
|
return "::mlir::Attribute";
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2019-08-21 20:35:07 +08:00
|
|
|
case Kind::Operand: {
|
|
|
|
// Use operand range for captured operands (to support potential variadic
|
|
|
|
// operands).
|
2021-09-21 06:59:56 +08:00
|
|
|
return "::mlir::Operation::operand_range";
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-08-10 10:03:58 +08:00
|
|
|
case Kind::Value: {
|
2021-09-21 06:59:56 +08:00
|
|
|
return "::mlir::Value";
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2021-07-21 11:23:06 +08:00
|
|
|
case Kind::MultipleValues: {
|
2021-09-21 06:59:56 +08:00
|
|
|
return "::mlir::ValueRange";
|
2021-07-21 11:23:06 +08:00
|
|
|
}
|
2019-08-10 10:03:58 +08:00
|
|
|
case Kind::Result: {
|
2019-08-21 20:35:07 +08:00
|
|
|
// Use the op itself for captured results.
|
2021-09-21 06:59:56 +08:00
|
|
|
return op->getQualCppClassName();
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 05:22:58 +08:00
|
|
|
llvm_unreachable("unknown kind");
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 06:59:56 +08:00
|
|
|
std::string SymbolInfoMap::SymbolInfo::getVarDecl(StringRef name) const {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "getVarDecl for '" << name << "': ");
|
|
|
|
std::string varInit = kind == Kind::Operand ? "(op0->getOperands())" : "";
|
|
|
|
return std::string(
|
|
|
|
formatv("{0} {1}{2};\n", getVarTypeStr(name), getVarName(name), varInit));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SymbolInfoMap::SymbolInfo::getArgDecl(StringRef name) const {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "getArgDecl for '" << name << "': ");
|
|
|
|
return std::string(
|
|
|
|
formatv("{0} &{1}", getVarTypeStr(name), getVarName(name)));
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string SymbolInfoMap::SymbolInfo::getValueAndRangeUse(
|
2019-08-21 20:35:07 +08:00
|
|
|
StringRef name, int index, const char *fmt, const char *separator) const {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "getValueAndRangeUse for '" << name << "': ");
|
2019-08-21 20:35:07 +08:00
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attr: {
|
|
|
|
assert(index < 0);
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, name);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (Attr)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
case Kind::Operand: {
|
|
|
|
assert(index < 0);
|
2021-07-21 11:23:06 +08:00
|
|
|
auto *operand = op->getArg(getArgIndex()).get<NamedTypeConstraint *>();
|
2019-08-21 20:35:07 +08:00
|
|
|
// If this operand is variadic, then return a range. Otherwise, return the
|
|
|
|
// value itself.
|
2020-04-11 05:11:45 +08:00
|
|
|
if (operand->isVariableLength()) {
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, name);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicOperand)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, formatv("(*{0}.begin())", name));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (SingleOperand)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
case Kind::Result: {
|
|
|
|
// If `index` is greater than zero, then we are referencing a specific
|
|
|
|
// result of a multi-result op. The result can still be variadic.
|
|
|
|
if (index >= 0) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string v =
|
|
|
|
std::string(formatv("{0}.getODSResults({1})", name, index));
|
2019-08-21 20:35:07 +08:00
|
|
|
if (!op->getResult(index).isVariadic())
|
2020-01-29 03:23:46 +08:00
|
|
|
v = std::string(formatv("(*{0}.begin())", v));
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, v);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (SingleResult)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:01:56 +08:00
|
|
|
// If this op has no result at all but still we bind a symbol to it, it
|
|
|
|
// means we want to capture the op itself.
|
|
|
|
if (op->getNumResults() == 0) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << name << " (Op)\n");
|
2022-02-03 22:07:46 +08:00
|
|
|
return formatv(fmt, name);
|
2019-10-18 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// We are referencing all results of the multi-result op. A specific result
|
|
|
|
// can either be a value or a range. Then join them with `separator`.
|
|
|
|
SmallVector<std::string, 4> values;
|
|
|
|
values.reserve(op->getNumResults());
|
|
|
|
|
|
|
|
for (int i = 0, e = op->getNumResults(); i < e; ++i) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string v = std::string(formatv("{0}.getODSResults({1})", name, i));
|
2019-08-21 20:35:07 +08:00
|
|
|
if (!op->getResult(i).isVariadic()) {
|
2020-01-29 03:23:46 +08:00
|
|
|
v = std::string(formatv("(*{0}.begin())", v));
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2020-01-29 03:23:46 +08:00
|
|
|
values.push_back(std::string(formatv(fmt, v)));
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = llvm::join(values, separator);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicResult)\n");
|
|
|
|
return repl;
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
case Kind::Value: {
|
|
|
|
assert(index < 0);
|
|
|
|
assert(op == nullptr);
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, name);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (Value)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2021-07-21 11:23:06 +08:00
|
|
|
case Kind::MultipleValues: {
|
|
|
|
assert(op == nullptr);
|
|
|
|
assert(index < getSize());
|
|
|
|
if (index >= 0) {
|
|
|
|
std::string repl =
|
|
|
|
formatv(fmt, std::string(formatv("{0}[{1}]", name, index)));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (MultipleValues)\n");
|
|
|
|
return repl;
|
|
|
|
}
|
|
|
|
// If it doesn't specify certain element, unpack them all.
|
|
|
|
auto repl =
|
|
|
|
formatv(fmt, std::string(formatv("{0}.begin(), {0}.end()", name)));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (MultipleValues)\n");
|
|
|
|
return std::string(repl);
|
|
|
|
}
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
2019-09-26 10:04:59 +08:00
|
|
|
llvm_unreachable("unknown kind");
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string SymbolInfoMap::SymbolInfo::getAllRangeUse(
|
2019-08-21 20:35:07 +08:00
|
|
|
StringRef name, int index, const char *fmt, const char *separator) const {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "getAllRangeUse for '" << name << "': ");
|
2019-08-10 10:03:58 +08:00
|
|
|
switch (kind) {
|
|
|
|
case Kind::Attr:
|
|
|
|
case Kind::Operand: {
|
|
|
|
assert(index < 0 && "only allowed for symbol bound to result");
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, name);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (Operand/Attr)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
case Kind::Result: {
|
|
|
|
if (index >= 0) {
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = formatv(fmt, formatv("{0}.getODSResults({1})", name, index));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (SingleResult)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:35:07 +08:00
|
|
|
// We are referencing all results of the multi-result op. Each result should
|
|
|
|
// have a value range, and then join them with `separator`.
|
2019-08-10 10:03:58 +08:00
|
|
|
SmallVector<std::string, 4> values;
|
2019-08-21 20:35:07 +08:00
|
|
|
values.reserve(op->getNumResults());
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
for (int i = 0, e = op->getNumResults(); i < e; ++i) {
|
2020-01-29 03:23:46 +08:00
|
|
|
values.push_back(std::string(
|
|
|
|
formatv(fmt, formatv("{0}.getODSResults({1})", name, i))));
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
auto repl = llvm::join(values, separator);
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicResult)\n");
|
|
|
|
return repl;
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
case Kind::Value: {
|
|
|
|
assert(index < 0 && "only allowed for symbol bound to result");
|
|
|
|
assert(op == nullptr);
|
2020-04-25 03:33:03 +08:00
|
|
|
auto repl = formatv(fmt, formatv("{{{0}}", name));
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (Value)\n");
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(repl);
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2021-07-21 11:23:06 +08:00
|
|
|
case Kind::MultipleValues: {
|
|
|
|
assert(op == nullptr);
|
|
|
|
assert(index < getSize());
|
|
|
|
if (index >= 0) {
|
|
|
|
std::string repl =
|
|
|
|
formatv(fmt, std::string(formatv("{0}[{1}]", name, index)));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (MultipleValues)\n");
|
|
|
|
return repl;
|
|
|
|
}
|
|
|
|
auto repl =
|
|
|
|
formatv(fmt, std::string(formatv("{0}.begin(), {0}.end()", name)));
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << repl << " (MultipleValues)\n");
|
|
|
|
return std::string(repl);
|
|
|
|
}
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2019-08-14 05:22:58 +08:00
|
|
|
llvm_unreachable("unknown kind");
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2021-07-20 15:26:23 +08:00
|
|
|
bool SymbolInfoMap::bindOpArgument(DagNode node, StringRef symbol,
|
|
|
|
const Operator &op, int argIndex) {
|
2019-08-10 10:03:58 +08:00
|
|
|
StringRef name = getValuePackName(symbol);
|
|
|
|
if (name != symbol) {
|
|
|
|
auto error = formatv(
|
|
|
|
"symbol '{0}' with trailing index cannot bind to op argument", symbol);
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto symInfo = op.getArg(argIndex).is<NamedAttribute *>()
|
|
|
|
? SymbolInfo::getAttr(&op, argIndex)
|
2021-07-20 15:26:23 +08:00
|
|
|
: SymbolInfo::getOperand(node, &op, argIndex);
|
2019-08-10 10:03:58 +08:00
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
std::string key = symbol.str();
|
|
|
|
if (symbolInfoMap.count(key)) {
|
|
|
|
// Only non unique name for the operand is supported.
|
|
|
|
if (symInfo.kind != SymbolInfo::Kind::Operand) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot add new operand if there is already non operand with the same
|
|
|
|
// name.
|
|
|
|
if (symbolInfoMap.find(key)->second.kind != SymbolInfo::Kind::Operand) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
symbolInfoMap.emplace(key, symInfo);
|
|
|
|
return true;
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool SymbolInfoMap::bindOpResult(StringRef symbol, const Operator &op) {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
std::string name = getValuePackName(symbol).str();
|
|
|
|
auto inserted = symbolInfoMap.emplace(name, SymbolInfo::getResult(&op));
|
|
|
|
|
|
|
|
return symbolInfoMap.count(inserted->first) == 1;
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:23:06 +08:00
|
|
|
bool SymbolInfoMap::bindValues(StringRef symbol, int numValues) {
|
|
|
|
std::string name = getValuePackName(symbol).str();
|
|
|
|
if (numValues > 1)
|
|
|
|
return bindMultipleValues(name, numValues);
|
|
|
|
return bindValue(name);
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool SymbolInfoMap::bindValue(StringRef symbol) {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
auto inserted = symbolInfoMap.emplace(symbol.str(), SymbolInfo::getValue());
|
|
|
|
return symbolInfoMap.count(inserted->first) == 1;
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:23:06 +08:00
|
|
|
bool SymbolInfoMap::bindMultipleValues(StringRef symbol, int numValues) {
|
|
|
|
std::string name = getValuePackName(symbol).str();
|
|
|
|
auto inserted =
|
|
|
|
symbolInfoMap.emplace(name, SymbolInfo::getMultipleValues(numValues));
|
|
|
|
return symbolInfoMap.count(inserted->first) == 1;
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
bool SymbolInfoMap::bindAttr(StringRef symbol) {
|
2020-10-16 08:01:06 +08:00
|
|
|
auto inserted = symbolInfoMap.emplace(symbol.str(), SymbolInfo::getAttr());
|
2020-10-10 04:32:01 +08:00
|
|
|
return symbolInfoMap.count(inserted->first) == 1;
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool SymbolInfoMap::contains(StringRef symbol) const {
|
2019-08-10 10:03:58 +08:00
|
|
|
return find(symbol) != symbolInfoMap.end();
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
SymbolInfoMap::const_iterator SymbolInfoMap::find(StringRef key) const {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
std::string name = getValuePackName(key).str();
|
|
|
|
|
2019-08-10 10:03:58 +08:00
|
|
|
return symbolInfoMap.find(name);
|
|
|
|
}
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
SymbolInfoMap::const_iterator
|
2021-07-20 15:26:23 +08:00
|
|
|
SymbolInfoMap::findBoundSymbol(StringRef key, DagNode node, const Operator &op,
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
int argIndex) const {
|
2021-09-21 06:59:56 +08:00
|
|
|
return findBoundSymbol(key, SymbolInfo::getOperand(node, &op, argIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolInfoMap::const_iterator
|
2022-01-02 09:26:44 +08:00
|
|
|
SymbolInfoMap::findBoundSymbol(StringRef key,
|
|
|
|
const SymbolInfo &symbolInfo) const {
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
std::string name = getValuePackName(key).str();
|
|
|
|
auto range = symbolInfoMap.equal_range(name);
|
|
|
|
|
2021-07-21 11:23:06 +08:00
|
|
|
for (auto it = range.first; it != range.second; ++it)
|
|
|
|
if (it->second.dagAndConstant == symbolInfo.dagAndConstant)
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
return it;
|
|
|
|
|
|
|
|
return symbolInfoMap.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<SymbolInfoMap::iterator, SymbolInfoMap::iterator>
|
|
|
|
SymbolInfoMap::getRangeOfEqualElements(StringRef key) {
|
|
|
|
std::string name = getValuePackName(key).str();
|
|
|
|
|
|
|
|
return symbolInfoMap.equal_range(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SymbolInfoMap::count(StringRef key) const {
|
|
|
|
std::string name = getValuePackName(key).str();
|
|
|
|
return symbolInfoMap.count(name);
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int SymbolInfoMap::getStaticValueCount(StringRef symbol) const {
|
2019-08-10 10:03:58 +08:00
|
|
|
StringRef name = getValuePackName(symbol);
|
|
|
|
if (name != symbol) {
|
|
|
|
// If there is a trailing index inside symbol, it references just one
|
|
|
|
// static value.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Otherwise, find how many it represents by querying the symbol's info.
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
return find(name)->second.getStaticValueCount();
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string SymbolInfoMap::getValueAndRangeUse(StringRef symbol,
|
|
|
|
const char *fmt,
|
|
|
|
const char *separator) const {
|
2019-08-21 20:35:07 +08:00
|
|
|
int index = -1;
|
|
|
|
StringRef name = getValuePackName(symbol, &index);
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
auto it = symbolInfoMap.find(name.str());
|
2019-08-21 20:35:07 +08:00
|
|
|
if (it == symbolInfoMap.end()) {
|
|
|
|
auto error = formatv("referencing unbound symbol '{0}'", symbol);
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
return it->second.getValueAndRangeUse(name, index, fmt, separator);
|
2019-08-21 20:35:07 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string SymbolInfoMap::getAllRangeUse(StringRef symbol, const char *fmt,
|
|
|
|
const char *separator) const {
|
2019-08-10 10:03:58 +08:00
|
|
|
int index = -1;
|
|
|
|
StringRef name = getValuePackName(symbol, &index);
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
auto it = symbolInfoMap.find(name.str());
|
2019-08-10 10:03:58 +08:00
|
|
|
if (it == symbolInfoMap.end()) {
|
|
|
|
auto error = formatv("referencing unbound symbol '{0}'", symbol);
|
|
|
|
PrintFatalError(loc, error);
|
|
|
|
}
|
|
|
|
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
return it->second.getAllRangeUse(name, index, fmt, separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolInfoMap::assignUniqueAlternativeNames() {
|
|
|
|
llvm::StringSet<> usedNames;
|
|
|
|
|
|
|
|
for (auto symbolInfoIt = symbolInfoMap.begin();
|
|
|
|
symbolInfoIt != symbolInfoMap.end();) {
|
|
|
|
auto range = symbolInfoMap.equal_range(symbolInfoIt->first);
|
|
|
|
auto startRange = range.first;
|
|
|
|
auto endRange = range.second;
|
|
|
|
|
|
|
|
auto operandName = symbolInfoIt->first;
|
|
|
|
int startSearchIndex = 0;
|
|
|
|
for (++startRange; startRange != endRange; ++startRange) {
|
|
|
|
// Current operand name is not unique, find a unique one
|
|
|
|
// and set the alternative name.
|
|
|
|
for (int i = startSearchIndex;; ++i) {
|
|
|
|
std::string alternativeName = operandName + std::to_string(i);
|
|
|
|
if (!usedNames.contains(alternativeName) &&
|
|
|
|
symbolInfoMap.count(alternativeName) == 0) {
|
|
|
|
usedNames.insert(alternativeName);
|
|
|
|
startRange->second.alternativeName = alternativeName;
|
|
|
|
startSearchIndex = i + 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
symbolInfoIt = endRange;
|
|
|
|
}
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pattern
|
|
|
|
//==----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Pattern::Pattern(const llvm::Record *def, RecordOperatorMap *mapper)
|
2019-08-10 10:03:58 +08:00
|
|
|
: def(*def), recordOpMap(mapper) {}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
DagNode Pattern::getSourcePattern() const {
|
|
|
|
return DagNode(def.getValueAsDag("sourcePattern"));
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int Pattern::getNumResultPatterns() const {
|
[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
|
|
|
auto *results = def.getValueAsListInit("resultPatterns");
|
2019-01-29 06:04:40 +08:00
|
|
|
return results->size();
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
DagNode Pattern::getResultPattern(unsigned index) const {
|
[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
|
|
|
auto *results = def.getValueAsListInit("resultPatterns");
|
2020-08-12 08:47:07 +08:00
|
|
|
return DagNode(cast<llvm::DagInit>(results->getElement(index)));
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Pattern::collectSourcePatternBoundSymbols(SymbolInfoMap &infoMap) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "start collecting source pattern bound symbols\n");
|
2019-08-10 10:03:58 +08:00
|
|
|
collectBoundSymbols(getSourcePattern(), infoMap, /*isSrcPattern=*/true);
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done collecting source pattern bound symbols\n");
|
[DDR] Introduce implicit equality check for the source pattern operands with the same name.
This CL allows user to specify the same name for the operands in the source pattern which implicitly enforces equality on operands with the same name.
E.g., Pat<(OpA $a, $b, $a) ... > would create a matching rule for checking equality for the first and the last operands. Equality of the operands is enforced at any depth, e.g., OpA ($a, $b, OpB($a, $c, OpC ($a))).
Example usage: Pat<(Reshape $arg0, (Shape $arg0)), (replaceWithValue $arg0)>
Note, this feature only covers operands but not attributes.
Current use cases are based on the operand equality and explicitly add the constraint into the pattern. Attribute equality will be worked out on the different CL.
Reviewed By: jpienaar
Differential Revision: https://reviews.llvm.org/D89254
2020-10-15 01:51:16 +08:00
|
|
|
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "start assigning alternative names for symbols\n");
|
|
|
|
infoMap.assignUniqueAlternativeNames();
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done assigning alternative names for symbols\n");
|
2019-01-29 06:04:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Pattern::collectResultPatternBoundSymbols(SymbolInfoMap &infoMap) {
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "start collecting result pattern bound symbols\n");
|
2019-08-10 10:03:58 +08:00
|
|
|
for (int i = 0, e = getNumResultPatterns(); i < e; ++i) {
|
|
|
|
auto pattern = getResultPattern(i);
|
|
|
|
collectBoundSymbols(pattern, infoMap, /*isSrcPattern=*/false);
|
|
|
|
}
|
2019-10-17 22:25:50 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "done collecting result pattern bound symbols\n");
|
2019-02-14 06:30:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
const Operator &Pattern::getSourceRootOp() {
|
2019-01-29 06:04:40 +08:00
|
|
|
return getSourcePattern().getDialectOp(recordOpMap);
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator &Pattern::getDialectOp(DagNode node) {
|
2019-01-29 06:04:40 +08:00
|
|
|
return node.getDialectOp(recordOpMap);
|
|
|
|
}
|
2019-02-14 06:30:40 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::vector<AppliedConstraint> Pattern::getConstraints() const {
|
2019-02-14 06:30:40 +08:00
|
|
|
auto *listInit = def.getValueAsListInit("constraints");
|
2020-08-12 08:47:07 +08:00
|
|
|
std::vector<AppliedConstraint> ret;
|
2019-02-14 06:30:40 +08:00
|
|
|
ret.reserve(listInit->size());
|
[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
|
|
|
|
2021-12-21 03:45:05 +08:00
|
|
|
for (auto *it : *listInit) {
|
[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
|
|
|
auto *dagInit = dyn_cast<llvm::DagInit>(it);
|
|
|
|
if (!dagInit)
|
2020-10-10 04:32:01 +08:00
|
|
|
PrintFatalError(&def, "all elements in Pattern multi-entity "
|
|
|
|
"constraints should be DAG nodes");
|
[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
|
|
|
|
|
|
|
std::vector<std::string> entities;
|
|
|
|
entities.reserve(dagInit->arg_size());
|
2019-10-31 02:12:21 +08:00
|
|
|
for (auto *argName : dagInit->getArgNames()) {
|
|
|
|
if (!argName) {
|
|
|
|
PrintFatalError(
|
2020-10-10 04:32:01 +08:00
|
|
|
&def,
|
2019-10-31 02:12:21 +08:00
|
|
|
"operands to additional constraints can only be symbol references");
|
|
|
|
}
|
2021-12-22 08:19:53 +08:00
|
|
|
entities.emplace_back(argName->getValue());
|
2019-10-31 02:12:21 +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
|
|
|
|
|
|
|
ret.emplace_back(cast<llvm::DefInit>(dagInit->getOperator())->getDef(),
|
2019-08-02 02:50:47 +08:00
|
|
|
dagInit->getNameStr(), std::move(entities));
|
2019-02-14 06:30:40 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2019-03-30 00:36:09 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int Pattern::getBenefit() const {
|
2019-04-02 00:39:59 +08:00
|
|
|
// The initial benefit value is a heuristic with number of ops in the source
|
2019-03-30 00:36:09 +08:00
|
|
|
// pattern.
|
2019-04-02 00:39:59 +08:00
|
|
|
int initBenefit = getSourcePattern().getNumOps();
|
2019-03-30 00:36:09 +08:00
|
|
|
llvm::DagInit *delta = def.getValueAsDag("benefitDelta");
|
2019-04-02 00:39:59 +08:00
|
|
|
if (delta->getNumArgs() != 1 || !isa<llvm::IntInit>(delta->getArg(0))) {
|
2020-10-10 04:32:01 +08:00
|
|
|
PrintFatalError(&def,
|
2019-04-02 00:39:59 +08:00
|
|
|
"The 'addBenefit' takes and only takes one integer value");
|
|
|
|
}
|
|
|
|
return initBenefit + dyn_cast<llvm::IntInit>(delta->getArg(0))->getValue();
|
2019-03-30 00:36:09 +08:00
|
|
|
}
|
2019-04-09 06:14:59 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::vector<Pattern::IdentifierLine> Pattern::getLocation() const {
|
2019-05-24 07:08:52 +08:00
|
|
|
std::vector<std::pair<StringRef, unsigned>> result;
|
|
|
|
result.reserve(def.getLoc().size());
|
|
|
|
for (auto loc : def.getLoc()) {
|
|
|
|
unsigned buf = llvm::SrcMgr.FindBufferContainingLoc(loc);
|
|
|
|
assert(buf && "invalid source location");
|
|
|
|
result.emplace_back(
|
|
|
|
llvm::SrcMgr.getBufferInfo(buf).Buffer->getBufferIdentifier(),
|
|
|
|
llvm::SrcMgr.getLineAndColumn(loc, buf).first);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
void Pattern::verifyBind(bool result, StringRef symbolName) {
|
|
|
|
if (!result) {
|
|
|
|
auto err = formatv("symbol '{0}' bound more than once", symbolName);
|
|
|
|
PrintFatalError(&def, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Pattern::collectBoundSymbols(DagNode tree, SymbolInfoMap &infoMap,
|
|
|
|
bool isSrcPattern) {
|
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 treeName = tree.getSymbol();
|
2020-10-10 04:32:01 +08:00
|
|
|
auto numTreeArgs = tree.getNumArgs();
|
|
|
|
|
|
|
|
if (tree.isNativeCodeCall()) {
|
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 (!treeName.empty()) {
|
2021-04-16 13:34:10 +08:00
|
|
|
if (!isSrcPattern) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "found symbol bound to NativeCodeCall: "
|
|
|
|
<< treeName << '\n');
|
2021-07-21 11:23:06 +08:00
|
|
|
verifyBind(
|
|
|
|
infoMap.bindValues(treeName, tree.getNumReturnsOfNativeCode()),
|
|
|
|
treeName);
|
2021-04-16 13:34:10 +08:00
|
|
|
} else {
|
|
|
|
PrintFatalError(&def,
|
|
|
|
formatv("binding symbol '{0}' to NativecodeCall in "
|
|
|
|
"MatchPattern is not supported",
|
|
|
|
treeName));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
for (int i = 0; i != numTreeArgs; ++i) {
|
|
|
|
if (auto treeArg = tree.getArgAsNestedDag(i)) {
|
|
|
|
// This DAG node argument is a DAG node itself. Go inside recursively.
|
|
|
|
collectBoundSymbols(treeArg, infoMap, isSrcPattern);
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-07 22:44:19 +08:00
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
if (!isSrcPattern)
|
|
|
|
continue;
|
2019-04-09 06:14:59 +08:00
|
|
|
|
2020-10-10 04:32:01 +08:00
|
|
|
// We can only bind symbols to arguments in source pattern. Those
|
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
|
|
|
// symbols are referenced in result patterns.
|
2019-04-09 06:14:59 +08:00
|
|
|
auto treeArgName = tree.getArgName(i);
|
2020-10-10 04:32:01 +08:00
|
|
|
|
2019-12-02 23:54:23 +08:00
|
|
|
// `$_` is a special symbol meaning ignore the current argument.
|
|
|
|
if (!treeArgName.empty() && treeArgName != "_") {
|
2020-10-10 04:32:01 +08:00
|
|
|
DagLeaf leaf = tree.getArgAsLeaf(i);
|
|
|
|
|
2021-04-16 13:34:10 +08:00
|
|
|
// In (NativeCodeCall<"Foo($_self, $0, $1, $2)"> I8Attr:$a, I8:$b, $c),
|
|
|
|
if (leaf.isUnspecified()) {
|
|
|
|
// This is case of $c, a Value without any constraints.
|
|
|
|
verifyBind(infoMap.bindValue(treeArgName), treeArgName);
|
|
|
|
} else {
|
|
|
|
auto constraint = leaf.getAsConstraint();
|
|
|
|
bool isAttr = leaf.isAttrMatcher() || leaf.isEnumAttrCase() ||
|
|
|
|
leaf.isConstantAttr() ||
|
|
|
|
constraint.getKind() == Constraint::Kind::CK_Attr;
|
|
|
|
|
|
|
|
if (isAttr) {
|
|
|
|
// This is case of $a, a binding to a certain attribute.
|
|
|
|
verifyBind(infoMap.bindAttr(treeArgName), treeArgName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is case of $b, a binding to a certain type.
|
|
|
|
verifyBind(infoMap.bindValue(treeArgName), treeArgName);
|
|
|
|
}
|
2019-08-10 10:03:58 +08:00
|
|
|
}
|
2019-04-09 06:14:59 +08:00
|
|
|
}
|
2020-10-10 04:32:01 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree.isOperation()) {
|
|
|
|
auto &op = getDialectOp(tree);
|
|
|
|
auto numOpArgs = op.getNumArgs();
|
2021-11-09 06:56:40 +08:00
|
|
|
int numEither = 0;
|
2020-10-10 04:32:01 +08:00
|
|
|
|
2021-11-09 06:56:40 +08:00
|
|
|
// We need to exclude the trailing directives and `either` directive groups
|
|
|
|
// two operands of the operation.
|
2021-09-16 05:25:29 +08:00
|
|
|
int numDirectives = 0;
|
|
|
|
for (int i = numTreeArgs - 1; i >= 0; --i) {
|
|
|
|
if (auto dagArg = tree.getArgAsNestedDag(i)) {
|
|
|
|
if (dagArg.isLocationDirective() || dagArg.isReturnTypeDirective())
|
|
|
|
++numDirectives;
|
2021-11-09 06:56:40 +08:00
|
|
|
else if (dagArg.isEither())
|
|
|
|
++numEither;
|
2021-09-16 05:25:29 +08:00
|
|
|
}
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 06:56:40 +08:00
|
|
|
if (numOpArgs != numTreeArgs - numDirectives + numEither) {
|
|
|
|
auto err =
|
|
|
|
formatv("op '{0}' argument number mismatch: "
|
|
|
|
"{1} in pattern vs. {2} in definition",
|
|
|
|
op.getOperationName(), numTreeArgs + numEither, numOpArgs);
|
2020-10-10 04:32:01 +08:00
|
|
|
PrintFatalError(&def, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name attached to the DAG node's operator is for representing the
|
|
|
|
// results generated from this op. It should be remembered as bound results.
|
|
|
|
if (!treeName.empty()) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
|
|
<< "found symbol bound to op result: " << treeName << '\n');
|
|
|
|
verifyBind(infoMap.bindOpResult(treeName, op), treeName);
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:56:40 +08:00
|
|
|
// The operand in `either` DAG should be bound to the operation in the
|
|
|
|
// parent DagNode.
|
|
|
|
auto collectSymbolInEither = [&](DagNode parent, DagNode tree,
|
|
|
|
int &opArgIdx) {
|
|
|
|
for (int i = 0; i < tree.getNumArgs(); ++i, ++opArgIdx) {
|
|
|
|
if (DagNode subTree = tree.getArgAsNestedDag(i)) {
|
|
|
|
collectBoundSymbols(subTree, infoMap, isSrcPattern);
|
|
|
|
} else {
|
|
|
|
auto argName = tree.getArgName(i);
|
|
|
|
if (!argName.empty() && argName != "_")
|
|
|
|
verifyBind(infoMap.bindOpArgument(parent, argName, op, opArgIdx),
|
|
|
|
argName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0, opArgIdx = 0; i != numTreeArgs; ++i, ++opArgIdx) {
|
2020-10-10 04:32:01 +08:00
|
|
|
if (auto treeArg = tree.getArgAsNestedDag(i)) {
|
2021-11-09 06:56:40 +08:00
|
|
|
if (treeArg.isEither()) {
|
|
|
|
collectSymbolInEither(tree, treeArg, opArgIdx);
|
|
|
|
} else {
|
|
|
|
// This DAG node argument is a DAG node itself. Go inside recursively.
|
|
|
|
collectBoundSymbols(treeArg, infoMap, isSrcPattern);
|
|
|
|
}
|
2020-10-10 04:32:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSrcPattern) {
|
|
|
|
// We can only bind symbols to op arguments in source pattern. Those
|
|
|
|
// symbols are referenced in result patterns.
|
|
|
|
auto treeArgName = tree.getArgName(i);
|
|
|
|
// `$_` is a special symbol meaning ignore the current argument.
|
|
|
|
if (!treeArgName.empty() && treeArgName != "_") {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "found symbol bound to op argument: "
|
|
|
|
<< treeArgName << '\n');
|
2021-11-09 06:56:40 +08:00
|
|
|
verifyBind(infoMap.bindOpArgument(tree, treeArgName, op, opArgIdx),
|
2021-07-20 15:26:23 +08:00
|
|
|
treeArgName);
|
2020-10-10 04:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!treeName.empty()) {
|
|
|
|
PrintFatalError(
|
|
|
|
&def, formatv("binding symbol '{0}' to non-operation/native code call "
|
|
|
|
"unsupported right now",
|
|
|
|
treeName));
|
2019-04-09 06:14:59 +08:00
|
|
|
}
|
|
|
|
}
|