2018-12-29 04:02:08 +08:00
|
|
|
//===- Operator.cpp - Operator class --------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
2019-01-08 02:09:34 +08:00
|
|
|
// Operator wrapper to simplify using TableGen Record defining a MLIR Op.
|
2018-12-29 04:02:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/TableGen/Operator.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-29 04:02:08 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using llvm::DagInit;
|
|
|
|
using llvm::DefInit;
|
|
|
|
using llvm::Record;
|
|
|
|
|
|
|
|
Operator::Operator(const llvm::Record &def) : def(def) {
|
|
|
|
SplitString(def.getName(), splittedDefName, "_");
|
|
|
|
populateOperandsAndAttributes();
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
const SmallVectorImpl<StringRef> &Operator::getSplitDefName() const {
|
2018-12-29 04:02:08 +08:00
|
|
|
return splittedDefName;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Operator::getOperationName() const {
|
|
|
|
return def.getValueAsString("opName");
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
StringRef Operator::cppClassName() const { return getSplitDefName().back(); }
|
|
|
|
std::string Operator::qualifiedCppClassName() const {
|
2019-01-03 08:11:42 +08:00
|
|
|
return llvm::join(getSplitDefName(), "::");
|
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
|
|
|
StringRef Operator::getArgName(int index) const {
|
|
|
|
DagInit *argumentValues = def.getValueAsDag("arguments");
|
|
|
|
return argumentValues->getArgName(index)->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Operator::attribute_begin() -> attribute_iterator {
|
|
|
|
return attributes.begin();
|
|
|
|
}
|
|
|
|
auto Operator::attribute_end() -> attribute_iterator {
|
|
|
|
return attributes.end();
|
|
|
|
}
|
|
|
|
auto Operator::getAttributes() -> llvm::iterator_range<attribute_iterator> {
|
|
|
|
return {attribute_begin(), attribute_end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Operator::operand_begin() -> operand_iterator { return operands.begin(); }
|
|
|
|
auto Operator::operand_end() -> operand_iterator { return operands.end(); }
|
|
|
|
auto Operator::getOperands() -> llvm::iterator_range<operand_iterator> {
|
|
|
|
return {operand_begin(), operand_end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Operator::getArg(int index) -> Argument {
|
2019-01-08 02:09:34 +08:00
|
|
|
if (index < nativeAttrStart)
|
2018-12-29 04:02:08 +08:00
|
|
|
return {&operands[index]};
|
2019-01-08 02:09:34 +08:00
|
|
|
return {&attributes[index - nativeAttrStart]};
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operator::populateOperandsAndAttributes() {
|
|
|
|
auto &recordKeeper = def.getRecords();
|
|
|
|
auto attrClass = recordKeeper.getClass("Attr");
|
|
|
|
auto derivedAttrClass = recordKeeper.getClass("DerivedAttr");
|
|
|
|
derivedAttrStart = -1;
|
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
// The argument ordering is operands, native attributes, derived
|
2018-12-29 04:02:08 +08:00
|
|
|
// attributes.
|
|
|
|
DagInit *argumentValues = def.getValueAsDag("arguments");
|
|
|
|
unsigned i = 0;
|
|
|
|
// Handle operands.
|
|
|
|
for (unsigned e = argumentValues->getNumArgs(); i != e; ++i) {
|
|
|
|
auto arg = argumentValues->getArg(i);
|
|
|
|
auto givenName = argumentValues->getArgName(i);
|
|
|
|
auto argDefInit = dyn_cast<DefInit>(arg);
|
|
|
|
if (!argDefInit)
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
Twine("undefined type for argument ") + Twine(i));
|
|
|
|
Record *argDef = argDefInit->getDef();
|
|
|
|
if (argDef->isSubClassOf(attrClass))
|
|
|
|
break;
|
|
|
|
operands.push_back(Operand{givenName, argDefInit});
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
// Handle native attributes.
|
|
|
|
nativeAttrStart = i;
|
2018-12-29 04:02:08 +08:00
|
|
|
for (unsigned e = argumentValues->getNumArgs(); i != e; ++i) {
|
|
|
|
auto arg = argumentValues->getArg(i);
|
|
|
|
auto givenName = argumentValues->getArgName(i);
|
|
|
|
Record *argDef = cast<DefInit>(arg)->getDef();
|
|
|
|
if (!argDef->isSubClassOf(attrClass))
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
Twine("expected attribute as argument ") + Twine(i));
|
|
|
|
|
|
|
|
if (!givenName)
|
|
|
|
PrintFatalError(argDef->getLoc(), "attributes must be named");
|
|
|
|
bool isDerived = argDef->isSubClassOf(derivedAttrClass);
|
2019-01-04 07:53:54 +08:00
|
|
|
if (isDerived)
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
"derived attributes not allowed in argument list");
|
|
|
|
attributes.push_back({givenName, argDef, isDerived});
|
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
// Handle derived attributes.
|
2019-01-04 07:53:54 +08:00
|
|
|
derivedAttrStart = i;
|
|
|
|
for (const auto &val : def.getValues()) {
|
|
|
|
if (auto *record = dyn_cast<llvm::RecordRecTy>(val.getType())) {
|
|
|
|
if (!record->isSubClassOf(attrClass))
|
|
|
|
continue;
|
|
|
|
if (!record->isSubClassOf(derivedAttrClass))
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
"unexpected Attr where only DerivedAttr is allowed");
|
|
|
|
|
|
|
|
if (record->getClasses().size() != 1) {
|
2018-12-29 04:02:08 +08:00
|
|
|
PrintFatalError(
|
|
|
|
def.getLoc(),
|
2019-01-04 07:53:54 +08:00
|
|
|
"unsupported attribute modelling, only single class expected");
|
|
|
|
}
|
|
|
|
attributes.push_back({cast<llvm::StringInit>(val.getNameInit()),
|
|
|
|
cast<DefInit>(val.getValue())->getDef(),
|
|
|
|
/*isDerived=*/true});
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
|
2019-01-08 01:52:26 +08:00
|
|
|
std::string mlir::Operator::Attribute::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);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef mlir::Operator::Attribute::getReturnType() const {
|
|
|
|
return record->getValueAsString("returnType").trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef mlir::Operator::Attribute::getStorageType() const {
|
|
|
|
return record->getValueAsString("storageType").trim();
|
|
|
|
}
|
|
|
|
|
2019-01-06 00:11:29 +08:00
|
|
|
bool mlir::Operator::Operand::hasMatcher() const {
|
2019-01-09 09:19:22 +08:00
|
|
|
return !tblgen::Type(defInit).getPredicate().isEmpty();
|
2019-01-06 00:11:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string mlir::Operator::Operand::createTypeMatcherTemplate() const {
|
2019-01-09 09:19:22 +08:00
|
|
|
return tblgen::Type(defInit).getPredicate().createTypeMatcherTemplate();
|
2019-01-06 00:11:29 +08:00
|
|
|
}
|