2019-05-15 06:03:48 +08:00
|
|
|
//===- Attribute.cpp - Attribute wrapper class ----------------------------===//
|
2019-01-10 05:50:20 +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.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// Attribute wrapper to simplify using TableGen Record defining a MLIR
|
|
|
|
// Attribute.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
#include "mlir/TableGen/Format.h"
|
2019-01-10 05:50:20 +08:00
|
|
|
#include "mlir/TableGen/Operator.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
using llvm::CodeInit;
|
|
|
|
using llvm::DefInit;
|
|
|
|
using llvm::Init;
|
|
|
|
using llvm::Record;
|
|
|
|
using llvm::StringInit;
|
|
|
|
|
2019-01-10 05:50:20 +08:00
|
|
|
// Returns the initializer's value as string if the given TableGen initializer
|
|
|
|
// is a code or string initializer. Returns the empty StringRef otherwise.
|
2019-04-12 21:05:49 +08:00
|
|
|
static StringRef getValueAsString(const Init *init) {
|
|
|
|
if (const auto *code = dyn_cast<CodeInit>(init))
|
2019-01-10 05:50:20 +08:00
|
|
|
return code->getValue().trim();
|
2019-04-12 21:05:49 +08:00
|
|
|
else if (const auto *str = dyn_cast<StringInit>(init))
|
2019-01-10 05:50:20 +08:00
|
|
|
return str->getValue().trim();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
tblgen::AttrConstraint::AttrConstraint(const Record *record)
|
[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
|
|
|
: Constraint(Constraint::CK_Attr, record) {
|
2019-01-28 23:13:40 +08:00
|
|
|
assert(def->isSubClassOf("AttrConstraint") &&
|
|
|
|
"must be subclass of TableGen 'AttrConstraint' class");
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
tblgen::Attribute::Attribute(const Record *record) : AttrConstraint(record) {
|
2019-01-28 23:13:40 +08:00
|
|
|
assert(record->isSubClassOf("Attr") &&
|
|
|
|
"must be subclass of TableGen 'Attr' class");
|
|
|
|
}
|
2019-01-17 02:23:21 +08:00
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
tblgen::Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {}
|
2019-01-10 05:50:20 +08:00
|
|
|
|
|
|
|
bool tblgen::Attribute::isDerivedAttr() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
return def->isSubClassOf("DerivedAttr");
|
2019-01-10 05:50:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 06:31:15 +08:00
|
|
|
bool tblgen::Attribute::isTypeAttr() const {
|
|
|
|
return def->isSubClassOf("TypeAttrBase");
|
|
|
|
}
|
|
|
|
|
2019-07-18 09:41:28 +08:00
|
|
|
bool tblgen::Attribute::isEnumAttr() const {
|
|
|
|
return def->isSubClassOf("EnumAttrInfo");
|
|
|
|
}
|
|
|
|
|
2019-01-10 05:50:20 +08:00
|
|
|
StringRef tblgen::Attribute::getStorageType() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
const auto *init = def->getValueInit("storageType");
|
2019-01-10 05:50:20 +08:00
|
|
|
auto type = getValueAsString(init);
|
|
|
|
if (type.empty())
|
|
|
|
return "Attribute";
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::Attribute::getReturnType() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
const auto *init = def->getValueInit("returnType");
|
2019-01-10 05:50:20 +08:00
|
|
|
return getValueAsString(init);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::Attribute::getConvertFromStorageCall() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
const auto *init = def->getValueInit("convertFromStorage");
|
2019-01-10 05:50:20 +08:00
|
|
|
return getValueAsString(init);
|
|
|
|
}
|
|
|
|
|
2019-01-17 01:23:14 +08:00
|
|
|
bool tblgen::Attribute::isConstBuildable() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
const auto *init = def->getValueInit("constBuilderCall");
|
2019-01-17 01:23:14 +08:00
|
|
|
return !getValueAsString(init).empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::Attribute::getConstBuilderTemplate() const {
|
2019-01-17 02:23:21 +08:00
|
|
|
const auto *init = def->getValueInit("constBuilderCall");
|
2019-01-17 01:23:14 +08:00
|
|
|
return getValueAsString(init);
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:33:54 +08:00
|
|
|
tblgen::Attribute tblgen::Attribute::getBaseAttr() const {
|
|
|
|
if (const auto *defInit =
|
|
|
|
llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseAttr"))) {
|
|
|
|
return Attribute(defInit).getBaseAttr();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
bool tblgen::Attribute::hasDefaultValueInitializer() const {
|
2019-01-23 02:26:09 +08:00
|
|
|
const auto *init = def->getValueInit("defaultValue");
|
|
|
|
return !getValueAsString(init).empty();
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
StringRef tblgen::Attribute::getDefaultValueInitializer() const {
|
|
|
|
const auto *init = def->getValueInit("defaultValue");
|
|
|
|
return getValueAsString(init);
|
Spike to define real math ops and lowering of one variant of add to corresponding integer ops.
The only reason in starting with a fixedpoint add is that it is the absolute simplest variant and illustrates the level of abstraction I'm aiming for.
The overall flow would be:
1. Determine quantization parameters (out of scope of this cl).
2. Source dialect rules to lower supported math ops to the quantization dialect (out of scope of this cl).
3. Quantization passes: [-quant-convert-const, -quant-lower-uniform-real-math, -quant-lower-unsupported-to-float] (the last one not implemented yet)
4. Target specific lowering of the integral arithmetic ops (roughly at the level of gemmlowp) to more fundamental operations (i.e. calls to gemmlowp, simd instructions, DSP instructions, etc).
How I'm doing this should facilitate implementation of just about any kind of backend except TFLite, which has a very course, adhoc surface area for its quantized kernels. Options there include (I'm not taking an opinion on this - just trying to provide options):
a) Not using any of this: just match q/dbarrier + tf math ops to the supported TFLite quantized op set.
b) Implement the more fundamental integer math ops on TFLite and convert to those instead of the current op set.
Note that I've hand-waved over the process of choosing appropriate quantization parameters. Getting to that next. As you can see, different implementations will likely have different magic combinations of specific math support, and we will need the target system that has been discussed for some of the esoteric cases (i.e. many DSPs only support POT fixedpoint).
Two unrelated changes to the overall goal of this CL and can be broken out of desired:
- Adding optional attribute support to TabelGen
- Allowing TableGen native rewrite hooks to return nullptr, signalling that no rewrite has been done.
PiperOrigin-RevId: 235267229
2019-02-23 07:11:00 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
bool tblgen::Attribute::isOptional() const {
|
|
|
|
return def->getValueAsBit("isOptional");
|
2019-01-23 02:26:09 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 07:11:02 +08:00
|
|
|
StringRef tblgen::Attribute::getAttrDefName() const {
|
2019-07-16 19:33:54 +08:00
|
|
|
if (def->isAnonymous()) {
|
|
|
|
return getBaseAttr().def->getName();
|
|
|
|
}
|
2019-01-17 02:23:21 +08:00
|
|
|
return def->getName();
|
2019-01-17 01:23:14 +08:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:50:20 +08:00
|
|
|
StringRef tblgen::Attribute::getDerivedCodeBody() const {
|
|
|
|
assert(isDerivedAttr() && "only derived attribute has 'body' field");
|
2019-01-17 02:23:21 +08:00
|
|
|
return def->getValueAsString("body");
|
2019-01-10 05:50:20 +08:00
|
|
|
}
|
2019-02-02 07:40:22 +08:00
|
|
|
|
2019-04-12 21:05:49 +08:00
|
|
|
tblgen::ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
|
2019-02-02 07:40:22 +08:00
|
|
|
assert(def->isSubClassOf("ConstantAttr") &&
|
|
|
|
"must be subclass of TableGen 'ConstantAttr' class");
|
|
|
|
}
|
|
|
|
|
|
|
|
tblgen::Attribute tblgen::ConstantAttr::getAttribute() const {
|
|
|
|
return Attribute(def->getValueAsDef("attr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::ConstantAttr::getConstantValue() const {
|
|
|
|
return def->getValueAsString("value");
|
|
|
|
}
|
2019-04-01 23:58:53 +08:00
|
|
|
|
|
|
|
tblgen::EnumAttrCase::EnumAttrCase(const llvm::DefInit *init)
|
|
|
|
: Attribute(init) {
|
2019-07-01 20:26:14 +08:00
|
|
|
assert(def->isSubClassOf("EnumAttrCaseInfo") &&
|
|
|
|
"must be subclass of TableGen 'EnumAttrInfo' class");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tblgen::EnumAttrCase::isStrCase() const {
|
2019-07-02 23:27:32 +08:00
|
|
|
return def->isSubClassOf("StrEnumAttrCase");
|
2019-04-01 23:58:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttrCase::getSymbol() const {
|
|
|
|
return def->getValueAsString("symbol");
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
int64_t tblgen::EnumAttrCase::getValue() const {
|
|
|
|
return def->getValueAsInt("value");
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:58:53 +08:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) {
|
2019-07-01 20:26:14 +08:00
|
|
|
assert(def->isSubClassOf("EnumAttrInfo") &&
|
2019-04-01 23:58:53 +08:00
|
|
|
"must be subclass of TableGen 'EnumAttr' class");
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {}
|
|
|
|
|
2019-04-01 23:58:53 +08:00
|
|
|
tblgen::EnumAttr::EnumAttr(const llvm::DefInit *init)
|
|
|
|
: EnumAttr(init->getDef()) {}
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getEnumClassName() const {
|
|
|
|
return def->getValueAsString("className");
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
StringRef tblgen::EnumAttr::getCppNamespace() const {
|
|
|
|
return def->getValueAsString("cppNamespace");
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getUnderlyingType() const {
|
|
|
|
return def->getValueAsString("underlyingType");
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:51:58 +08:00
|
|
|
StringRef tblgen::EnumAttr::getUnderlyingToSymbolFnName() const {
|
|
|
|
return def->getValueAsString("underlyingToSymbolFnName");
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
StringRef tblgen::EnumAttr::getStringToSymbolFnName() const {
|
|
|
|
return def->getValueAsString("stringToSymbolFnName");
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::EnumAttr::getSymbolToStringFnName() const {
|
|
|
|
return def->getValueAsString("symbolToStringFnName");
|
|
|
|
}
|
|
|
|
|
2019-06-20 07:09:57 +08:00
|
|
|
StringRef tblgen::EnumAttr::getMaxEnumValFnName() const {
|
|
|
|
return def->getValueAsString("maxEnumValFnName");
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:58:53 +08:00
|
|
|
std::vector<tblgen::EnumAttrCase> tblgen::EnumAttr::getAllCases() const {
|
|
|
|
const auto *inits = def->getValueAsListInit("enumerants");
|
|
|
|
|
|
|
|
std::vector<tblgen::EnumAttrCase> cases;
|
|
|
|
cases.reserve(inits->size());
|
|
|
|
|
|
|
|
for (const llvm::Init *init : *inits) {
|
|
|
|
cases.push_back(tblgen::EnumAttrCase(cast<llvm::DefInit>(init)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cases;
|
|
|
|
}
|
2019-08-31 03:51:31 +08:00
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::Record *record)
|
|
|
|
: def(record) {
|
|
|
|
assert(def->isSubClassOf("StructFieldAttr") &&
|
|
|
|
"must be subclass of TableGen 'StructFieldAttr' class");
|
|
|
|
}
|
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::Record &record)
|
|
|
|
: StructFieldAttr(&record) {}
|
|
|
|
|
|
|
|
tblgen::StructFieldAttr::StructFieldAttr(const llvm::DefInit *init)
|
|
|
|
: StructFieldAttr(init->getDef()) {}
|
|
|
|
|
|
|
|
StringRef tblgen::StructFieldAttr::getName() const {
|
|
|
|
return def->getValueAsString("name");
|
|
|
|
}
|
|
|
|
|
|
|
|
tblgen::Attribute tblgen::StructFieldAttr::getType() const {
|
|
|
|
auto init = def->getValueInit("type");
|
|
|
|
return tblgen::Attribute(cast<llvm::DefInit>(init));
|
|
|
|
}
|
|
|
|
|
|
|
|
tblgen::StructAttr::StructAttr(const llvm::Record *record) : Attribute(record) {
|
|
|
|
assert(def->isSubClassOf("StructAttr") &&
|
|
|
|
"must be subclass of TableGen 'StructAttr' class");
|
|
|
|
}
|
|
|
|
|
|
|
|
tblgen::StructAttr::StructAttr(const llvm::DefInit *init)
|
|
|
|
: StructAttr(init->getDef()) {}
|
|
|
|
|
|
|
|
StringRef tblgen::StructAttr::getStructClassName() const {
|
|
|
|
return def->getValueAsString("className");
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef tblgen::StructAttr::getCppNamespace() const {
|
|
|
|
Dialect dialect(def->getValueAsDef("structDialect"));
|
|
|
|
return dialect.getCppNamespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<mlir::tblgen::StructFieldAttr>
|
|
|
|
tblgen::StructAttr::getAllFields() const {
|
|
|
|
std::vector<mlir::tblgen::StructFieldAttr> attributes;
|
|
|
|
|
|
|
|
const auto *inits = def->getValueAsListInit("fields");
|
|
|
|
attributes.reserve(inits->size());
|
|
|
|
|
|
|
|
for (const llvm::Init *init : *inits) {
|
|
|
|
attributes.emplace_back(cast<llvm::DefInit>(init));
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes;
|
|
|
|
}
|