17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/TableGen/Record.h"
30 std::string inputFilename = records.getInputFilename();
33 StringRef nameRef = sys::path::filename(inputFilename);
34 nameRef.consume_back(
".td");
37 std::string uniqueName(tag);
38 for (
char c : nameRef) {
39 if (isAlnum(c) || c ==
'_')
40 uniqueName.push_back(c);
42 uniqueName.append(utohexstr((
unsigned char)c));
47 StaticVerifierFunctionEmitter::StaticVerifierFunctionEmitter(
48 raw_ostream &os,
const RecordKeeper &records, StringRef tag)
54 emitTypeConstraints();
55 emitAttrConstraints();
56 emitSuccessorConstraints();
57 emitRegionConstraints();
60 void StaticVerifierFunctionEmitter::emitPatternConstraints(
62 collectPatternConstraints(constraints);
72 const auto *it = typeConstraints.find(constraint);
73 assert(it != typeConstraints.end() &&
"expected to find a type constraint");
81 const auto *it = attrConstraints.find(constraint);
82 return it == attrConstraints.end() ? std::optional<StringRef>()
83 : StringRef(it->second);
88 const auto *it = successorConstraints.find(constraint);
89 assert(it != successorConstraints.end() &&
90 "expected to find a sucessor constraint");
96 const auto *it = regionConstraints.find(constraint);
97 assert(it != regionConstraints.end() &&
98 "expected to find a region constraint");
116 static ::llvm::LogicalResult {0}(
117 ::mlir::Operation *op, ::mlir::Type type, ::llvm::StringRef valueKind,
118 unsigned valueIndex) {
120 return op->emitOpError(valueKind) << " #" << valueIndex
121 << " must be {2}, but got " << type;
123 return ::mlir::success();
134 static ::llvm::LogicalResult {0}(
135 ::mlir::Attribute attr, ::llvm::StringRef attrName, llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) {{
137 return emitError() << "attribute '" << attrName
138 << "' failed to satisfy constraint: {2}";
139 return ::mlir::success();
141 static ::llvm::LogicalResult {0}(
142 ::mlir::Operation *op, ::mlir::Attribute attr, ::llvm::StringRef attrName) {{
143 return {0}(attr, attrName, [op]() {{
144 return op->emitOpError();
151 static ::llvm::LogicalResult {0}(
152 ::mlir::Operation *op, ::mlir::Block *successor,
153 ::llvm::StringRef successorName, unsigned successorIndex) {
155 return op->emitOpError("successor #") << successorIndex << " ('"
156 << successorName << ")' failed to verify constraint: {2}";
158 return ::mlir::success();
165 static ::llvm::LogicalResult {0}(
166 ::mlir::Operation *op, ::mlir::Region ®ion, ::llvm::StringRef regionName,
167 unsigned regionIndex) {
169 return op->emitOpError("region #") << regionIndex
170 << (regionName.empty() ? " " : " ('" + regionName + "') ")
171 << "failed to verify constraint: {2}";
173 return ::mlir::success();
181 static ::llvm::LogicalResult {0}(
182 ::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::{3},
183 ::llvm::StringRef failureStr) {
185 return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
186 diag << failureStr << ": {2}";
189 return ::mlir::success();
193 void StaticVerifierFunctionEmitter::emitConstraints(
194 const ConstraintMap &constraints, StringRef selfName,
195 const char *
const codeTemplate) {
198 for (
auto &it : constraints) {
199 os << formatv(codeTemplate, it.second,
200 tgfmt(it.first.getConditionTemplate(), &ctx),
205 void StaticVerifierFunctionEmitter::emitTypeConstraints() {
209 void StaticVerifierFunctionEmitter::emitAttrConstraints() {
213 void StaticVerifierFunctionEmitter::emitSuccessorConstraints() {
217 void StaticVerifierFunctionEmitter::emitRegionConstraints() {
221 void StaticVerifierFunctionEmitter::emitPatternConstraints() {
224 for (
auto &it : typeConstraints) {
226 tgfmt(it.first.getConditionTemplate(), &ctx),
230 for (
auto &it : attrConstraints) {
232 tgfmt(it.first.getConditionTemplate(), &ctx),
248 auto test =
tgfmt(attr.getConditionTemplate(),
251 return !StringRef(test).contains(
"<no-subst-found>");
254 std::string StaticVerifierFunctionEmitter::getUniqueName(StringRef
kind,
256 return (
"__mlir_ods_local_" +
kind +
"_constraint_" + uniqueOutputLabel +
261 void StaticVerifierFunctionEmitter::collectConstraint(ConstraintMap &map,
264 auto [it, inserted] = map.try_emplace(constraint);
266 it->second = getUniqueName(
kind, map.size());
273 if (value.hasPredicate())
274 collectConstraint(typeConstraints,
"type", value.constraint);
277 for (
const Record *def : opDefs) {
284 if (!namedAttr.attr.getPredicate().isNull() &&
285 !namedAttr.attr.isDerivedAttr() &&
287 collectConstraint(attrConstraints,
"attr", namedAttr.attr);
291 if (!successor.constraint.getPredicate().isNull()) {
292 collectConstraint(successorConstraints,
"successor",
293 successor.constraint);
298 if (!region.constraint.getPredicate().isNull())
299 collectConstraint(regionConstraints,
"region", region.constraint);
303 void StaticVerifierFunctionEmitter::collectPatternConstraints(
305 for (
auto &leaf : constraints) {
306 assert(leaf.isOperandMatcher() || leaf.isAttrMatcher());
308 leaf.isOperandMatcher() ? typeConstraints : attrConstraints,
309 leaf.isOperandMatcher() ?
"type" :
"attr", leaf.getAsConstraint());
319 raw_string_ostream os(ret);
320 os.write_escaped(value);
static const char *const successorConstraintCode
Code for a successor constraint.
static const char *const regionConstraintCode
Code for a region constraint.
static const char *const typeConstraintCode
Code templates for emitting type, attribute, successor, and region constraints.
static std::string getUniqueOutputLabel(const RecordKeeper &records, StringRef tag)
Generate a unique label based on the current file name to prevent name collisions if multiple generat...
static const char *const patternAttrOrTypeConstraintCode
Code for a pattern type or attribute constraint.
static bool canUniqueAttrConstraint(Attribute attr)
An attribute constraint that references anything other than itself and the current op cannot be gener...
static const char *const attrConstraintCode
Code for an attribute constraint.
union mlir::linalg::@1197::ArityGroupAndKind::Kind kind
Attributes are known-constant values of operations.
Format context containing substitutions for special placeholders.
FmtContext & withBuilder(Twine subst)
FmtContext & withSelf(Twine subst)
FmtContext & addSubst(StringRef placeholder, const Twine &subst)
Wrapper class that contains a MLIR op's information (e.g., operands, attributes) defined in TableGen ...
llvm::iterator_range< const_region_iterator > getRegions() const
const_value_range getResults() const
const_value_range getOperands() const
llvm::iterator_range< const_attribute_iterator > getAttributes() const
llvm::iterator_range< const_successor_iterator > getSuccessors() const
StringRef getRegionConstraintFn(const Constraint &constraint) const
Get the name of the static function used for the given region constraint.
void emitPatternConstraints(const ArrayRef< DagLeaf > constraints)
Unique all compatible type and attribute constraints from a pattern file and emit them at the top of ...
std::optional< StringRef > getAttrConstraintFn(const Constraint &constraint) const
Get the name of the static function used for the given attribute constraint.
void emitOpConstraints(ArrayRef< const llvm::Record * > opDefs)
Collect and unique all compatible type, attribute, successor, and region constraints from the operati...
void collectOpConstraints(ArrayRef< const llvm::Record * > opDefs)
Collect and unique all the constraints used by operations.
StringRef getTypeConstraintFn(const Constraint &constraint) const
Get the name of the static function used for the given type constraint.
StringRef getSuccessorConstraintFn(const Constraint &constraint) const
Get the name of the static function used for the given successor constraint.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals) -> FmtObject< decltype(std::make_tuple(llvm::support::detail::build_format_adapter(std::forward< Ts >(vals))...))>
Formats text by substituting placeholders in format string with replacement parameters.
std::string escapeString(StringRef value)
Escape a string using C++ encoding. E.g. foo"bar -> foo\x22bar.
Include the generated interface declarations.