1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
//===- Context.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Tools/PDLL/ODS/Context.h"
#include "mlir/Tools/PDLL/ODS/Constraint.h"
#include "mlir/Tools/PDLL/ODS/Dialect.h"
#include "mlir/Tools/PDLL/ODS/Operation.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
using namespace mlir;
using namespace mlir::pdll::ods;
//===----------------------------------------------------------------------===//
// Context
//===----------------------------------------------------------------------===//
Context::Context() = default;
Context::~Context() = default;
const AttributeConstraint &
Context::insertAttributeConstraint(StringRef name, StringRef summary,
StringRef cppClass) {
std::unique_ptr<AttributeConstraint> &constraint = attributeConstraints[name];
if (!constraint) {
constraint.reset(new AttributeConstraint(name, summary, cppClass));
} else {
assert(constraint->getCppClass() == cppClass &&
constraint->getSummary() == summary &&
"constraint with the same name was already registered with a "
"different class");
}
return *constraint;
}
const TypeConstraint &Context::insertTypeConstraint(StringRef name,
StringRef summary,
StringRef cppClass) {
std::unique_ptr<TypeConstraint> &constraint = typeConstraints[name];
if (!constraint)
constraint.reset(new TypeConstraint(name, summary, cppClass));
return *constraint;
}
Dialect &Context::insertDialect(StringRef name) {
std::unique_ptr<Dialect> &dialect = dialects[name];
if (!dialect)
dialect.reset(new Dialect(name));
return *dialect;
}
const Dialect *Context::lookupDialect(StringRef name) const {
auto it = dialects.find(name);
return it == dialects.end() ? nullptr : &*it->second;
}
std::pair<Operation *, bool>
Context::insertOperation(StringRef name, StringRef summary, StringRef desc,
StringRef nativeClassName,
bool supportsResultTypeInferrence, SMLoc loc) {
std::pair<StringRef, StringRef> dialectAndName = name.split('.');
return insertDialect(dialectAndName.first)
.insertOperation(name, summary, desc, nativeClassName,
supportsResultTypeInferrence, loc);
}
const Operation *Context::lookupOperation(StringRef name) const {
std::pair<StringRef, StringRef> dialectAndName = name.split('.');
if (const Dialect *dialect = lookupDialect(dialectAndName.first))
return dialect->lookupOperation(name);
return nullptr;
}
template <typename T>
SmallVector<T *> sortMapByName(const llvm::StringMap<std::unique_ptr<T>> &map) {
SmallVector<T *> storage;
for (auto &entry : map)
storage.push_back(entry.second.get());
llvm::sort(storage, [](const auto &lhs, const auto &rhs) {
return lhs->getName() < rhs->getName();
});
return storage;
}
void Context::print(raw_ostream &os) const {
auto printVariableLengthCst = [&](StringRef cst, VariableLengthKind kind) {
switch (kind) {
case VariableLengthKind::Optional:
os << "Optional<" << cst << ">";
break;
case VariableLengthKind::Single:
os << cst;
break;
case VariableLengthKind::Variadic:
os << "Variadic<" << cst << ">";
break;
}
};
llvm::ScopedPrinter printer(os);
llvm::DictScope odsScope(printer, "ODSContext");
for (const Dialect *dialect : sortMapByName(dialects)) {
printer.startLine() << "Dialect `" << dialect->getName() << "` {\n";
printer.indent();
for (const Operation *op : sortMapByName(dialect->getOperations())) {
printer.startLine() << "Operation `" << op->getName() << "` {\n";
printer.indent();
// Attributes.
ArrayRef<Attribute> attributes = op->getAttributes();
if (!attributes.empty()) {
printer.startLine() << "Attributes { ";
llvm::interleaveComma(attributes, os, [&](const Attribute &attr) {
os << attr.getName() << " : ";
auto kind = attr.isOptional() ? VariableLengthKind::Optional
: VariableLengthKind::Single;
printVariableLengthCst(attr.getConstraint().getDemangledName(), kind);
});
os << " }\n";
}
// Operands.
ArrayRef<OperandOrResult> operands = op->getOperands();
if (!operands.empty()) {
printer.startLine() << "Operands { ";
llvm::interleaveComma(
operands, os, [&](const OperandOrResult &operand) {
os << operand.getName() << " : ";
printVariableLengthCst(operand.getConstraint().getDemangledName(),
operand.getVariableLengthKind());
});
os << " }\n";
}
// Results.
ArrayRef<OperandOrResult> results = op->getResults();
if (!results.empty()) {
printer.startLine() << "Results { ";
llvm::interleaveComma(results, os, [&](const OperandOrResult &result) {
os << result.getName() << " : ";
printVariableLengthCst(result.getConstraint().getDemangledName(),
result.getVariableLengthKind());
});
os << " }\n";
}
printer.objectEnd();
}
printer.objectEnd();
}
for (const AttributeConstraint *cst : sortMapByName(attributeConstraints)) {
printer.startLine() << "AttributeConstraint `" << cst->getDemangledName()
<< "` {\n";
printer.indent();
printer.startLine() << "Summary: " << cst->getSummary() << "\n";
printer.startLine() << "CppClass: " << cst->getCppClass() << "\n";
printer.objectEnd();
}
for (const TypeConstraint *cst : sortMapByName(typeConstraints)) {
printer.startLine() << "TypeConstraint `" << cst->getDemangledName()
<< "` {\n";
printer.indent();
printer.startLine() << "Summary: " << cst->getSummary() << "\n";
printer.startLine() << "CppClass: " << cst->getCppClass() << "\n";
printer.objectEnd();
}
printer.objectEnd();
}
|