Pull TableGen op argument definitions into their own files

PiperOrigin-RevId: 230923050
This commit is contained in:
Lei Zhang 2019-01-25 10:13:35 -08:00 committed by jpienaar
parent 2de5e9fd19
commit ba1715f407
5 changed files with 125 additions and 44 deletions

View file

@ -0,0 +1,73 @@
//===- Argument.h - Argument definitions ------------------------*- C++ -*-===//
//
// 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.
// =============================================================================
//
// This header file contains definitions for TableGen operation's arguments.
// Operation arguments fall into two categories:
//
// 1. Operands: SSA values operated on by the operation
// 2. Attributes: compile-time known properties that have influence over
// the operation's behavior
//
// These two categories are modelled with the unified argument concept in
// TableGen because we need similar pattern matching mechanisms for them.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TABLEGEN_ARGUMENT_H_
#define MLIR_TABLEGEN_ARGUMENT_H_
#include "mlir/TableGen/Attribute.h"
#include "mlir/TableGen/Type.h"
#include "llvm/ADT/PointerUnion.h"
#include <string>
namespace llvm {
class DefInit;
class StringInit;
} // end namespace llvm
namespace mlir {
namespace tblgen {
// A struct wrapping an attribute and its name together
struct NamedAttribute {
// Returns the MLIR attribute name.
std::string getName() const;
llvm::StringInit *name;
Attribute attr;
};
// A struct wrapping an operand and its name together
struct Operand {
// Returns true if this operand has constraint that need to be satisfied.
bool hasMatcher() const;
// Returns the type constraint applicable to this operand.
TypeConstraint getTypeConstraint() const;
llvm::StringInit *name;
llvm::DefInit *defInit;
};
// Operation argument: either attribute or operand
using Argument = llvm::PointerUnion<NamedAttribute *, Operand *>;
} // end namespace tblgen
} // end namespace mlir
#endif // MLIR_TABLEGEN_ARGUMENT_H_

View file

@ -23,6 +23,7 @@
#define MLIR_TABLEGEN_OPERATOR_H_
#include "mlir/Support/LLVM.h"
#include "mlir/TableGen/Argument.h"
#include "mlir/TableGen/Attribute.h"
#include "mlir/TableGen/Type.h"
#include "llvm/ADT/PointerUnion.h"
@ -59,13 +60,6 @@ public:
// Returns the C++ class name of the op with namespace added.
std::string qualifiedCppClassName() const;
struct NamedAttribute {
std::string getName() const;
llvm::StringInit *name;
Attribute attr;
};
// Op attribute interators.
using attribute_iterator = NamedAttribute *;
attribute_iterator attribute_begin();
@ -79,15 +73,6 @@ public:
return attributes[index];
}
struct Operand {
bool hasMatcher() const;
// Return the type constraint applicable to this operand.
tblgen::TypeConstraint getTypeConstraint() const;
llvm::StringInit *name;
llvm::DefInit *defInit;
};
// Op operand iterators.
using operand_iterator = Operand *;
operand_iterator operand_begin();
@ -100,7 +85,6 @@ public:
const Operand &getOperand(int index) const { return operands[index]; }
// Op argument (attribute or operand) accessors.
using Argument = llvm::PointerUnion<NamedAttribute *, Operand *>;
Argument getArg(int index);
StringRef getArgName(int index) const;
int getNumArgs() const { return operands.size() + attributes.size(); }

View file

@ -0,0 +1,39 @@
//===- Argument.cpp - Argument definitions ----------------------*- C++ -*-===//
//
// 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.
// =============================================================================
#include "mlir/TableGen/Argument.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/TableGen/Record.h"
using namespace mlir;
std::string tblgen::NamedAttribute::getName() const {
std::string ret = name->getAsUnquotedString();
// TODO(jpienaar): Revise this post dialect prefixing attribute discussion.
auto split = StringRef(ret).split("__");
if (split.second.empty())
return ret;
return llvm::join_items("$", split.first, split.second);
}
bool tblgen::Operand::hasMatcher() const {
return !tblgen::TypeConstraint(*defInit).getPredicate().isNull();
}
tblgen::TypeConstraint tblgen::Operand::getTypeConstraint() const {
return tblgen::TypeConstraint(*defInit);
}

View file

@ -149,23 +149,6 @@ void tblgen::Operator::populateOperandsAndAttributes() {
}
}
std::string tblgen::Operator::NamedAttribute::getName() const {
std::string ret = name->getAsUnquotedString();
// TODO(jpienaar): Revise this post dialect prefixing attribute discussion.
auto split = StringRef(ret).split("__");
if (split.second.empty())
return ret;
return llvm::join_items("$", split.first, split.second);
}
bool tblgen::Operator::Operand::hasMatcher() const {
return !tblgen::TypeConstraint(*defInit).getPredicate().isNull();
}
tblgen::TypeConstraint tblgen::Operator::Operand::getTypeConstraint() const {
return tblgen::TypeConstraint(*defInit);
}
bool tblgen::Operator::hasDescription() const {
return def.getValue("description") != nullptr;
}

View file

@ -38,7 +38,10 @@
using namespace llvm;
using namespace mlir;
using mlir::tblgen::Argument;
using mlir::tblgen::Attribute;
using mlir::tblgen::NamedAttribute;
using mlir::tblgen::Operand;
using mlir::tblgen::Operator;
using mlir::tblgen::Type;
@ -46,17 +49,17 @@ namespace {
// Wrapper around DAG argument.
struct DagArg {
DagArg(mlir::tblgen::Operator::Argument arg, Init *constraintInit)
DagArg(Argument arg, Init *constraintInit)
: arg(arg), constraintInit(constraintInit) {}
bool isAttr();
mlir::tblgen::Operator::Argument arg;
Argument arg;
Init *constraintInit;
};
} // end namespace
bool DagArg::isAttr() { return arg.is<Operator::NamedAttribute *>(); }
bool DagArg::isAttr() { return arg.is<NamedAttribute *>(); }
namespace {
class Pattern {
@ -186,7 +189,7 @@ void Pattern::matchOp(DagInit *tree, int depth) {
// Verify arguments.
if (auto defInit = dyn_cast<DefInit>(arg)) {
// Verify operands.
if (auto *operand = opArg.dyn_cast<Operator::Operand *>()) {
if (auto *operand = opArg.dyn_cast<Operand *>()) {
// Skip verification where not needed due to definition of op.
if (operand->defInit == defInit)
goto StateCapture;
@ -204,7 +207,7 @@ void Pattern::matchOp(DagInit *tree, int depth) {
}
// TODO(jpienaar): Verify attributes.
if (auto *namedAttr = opArg.dyn_cast<Operator::NamedAttribute *>()) {
if (auto *namedAttr = opArg.dyn_cast<NamedAttribute *>()) {
// TODO(jpienaar): move to helper class.
if (defInit->getDef()->isSubClassOf("mAttr")) {
auto pred =
@ -224,10 +227,10 @@ void Pattern::matchOp(DagInit *tree, int depth) {
auto name = tree->getArgNameStr(i);
if (name.empty())
continue;
if (opArg.is<Operator::Operand *>())
if (opArg.is<Operand *>())
os.indent(indent) << "state->" << name << " = op" << depth
<< "->getOperand(" << i << ");\n";
if (auto namedAttr = opArg.dyn_cast<Operator::NamedAttribute *>()) {
if (auto namedAttr = opArg.dyn_cast<NamedAttribute *>()) {
os.indent(indent) << "state->" << name << " = op" << depth
<< "->getAttrOfType<"
<< namedAttr->attr.getStorageType() << ">(\""
@ -266,8 +269,7 @@ void Pattern::emit(StringRef rewriteName) {
// Emit matched state.
os << " struct MatchedState : public PatternState {\n";
for (auto &arg : boundArguments) {
if (auto namedAttr =
arg.second.arg.dyn_cast<Operator::NamedAttribute *>()) {
if (auto namedAttr = arg.second.arg.dyn_cast<NamedAttribute *>()) {
os.indent(4) << namedAttr->attr.getStorageType() << " " << arg.first()
<< ";\n";
} else {
@ -382,7 +384,7 @@ void Pattern::emitReplaceOpWithNewOp(DagInit *resultTree) {
// TODO(jpienaar): Refactor out into map to avoid recomputing these.
auto argument = resultOp.getArg(i);
if (!argument.is<Operator::NamedAttribute *>())
if (!argument.is<NamedAttribute *>())
PrintFatalError(pattern->getLoc(),
Twine("expected attribute ") + Twine(i));