17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/TableGen/Record.h"
30 StringRef 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 emitPropConstraints();
57 emitSuccessorConstraints();
58 emitRegionConstraints();
61 void StaticVerifierFunctionEmitter::emitPatternConstraints(
63 collectPatternConstraints(constraints);
73 const auto *it = typeConstraints.find(constraint);
74 assert(it != typeConstraints.end() &&
"expected to find a type constraint");
82 const auto *it = attrConstraints.find(constraint);
83 return it == attrConstraints.end() ? std::optional<StringRef>()
84 : StringRef(it->second);
91 const auto *it = propConstraints.find(constraint);
92 return it == propConstraints.end() ? std::optional<StringRef>()
93 : StringRef(it->second);
98 const auto *it = successorConstraints.find(constraint);
99 assert(it != successorConstraints.end() &&
100 "expected to find a sucessor constraint");
106 const auto *it = regionConstraints.find(constraint);
107 assert(it != regionConstraints.end() &&
108 "expected to find a region constraint");
126 static ::llvm::LogicalResult {0}(
127 ::mlir::Operation *op, ::mlir::Type type, ::llvm::StringRef valueKind,
128 unsigned valueIndex) {
130 return op->emitOpError(valueKind) << " #" << valueIndex
131 << " must be {2}, but got " << type;
133 return ::mlir::success();
144 static ::llvm::LogicalResult {0}(
145 ::mlir::Attribute attr, ::llvm::StringRef attrName, llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) {{
147 return emitError() << "attribute '" << attrName
148 << "' failed to satisfy constraint: {2}";
149 return ::mlir::success();
151 static ::llvm::LogicalResult {0}(
152 ::mlir::Operation *op, ::mlir::Attribute attr, ::llvm::StringRef attrName) {{
153 return {0}(attr, attrName, [op]() {{
154 return op->emitOpError();
163 static ::llvm::LogicalResult {0}(
164 {3} prop, ::llvm::StringRef propName, llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) {{
166 return emitError() << "property '" << propName
167 << "' failed to satisfy constraint: {2}";
168 return ::mlir::success();
170 static ::llvm::LogicalResult {0}(
171 ::mlir::Operation *op, {3} prop, ::llvm::StringRef propName) {{
172 return {0}(prop, propName, [op]() {{
173 return op->emitOpError();
180 static ::llvm::LogicalResult {0}(
181 ::mlir::Operation *op, ::mlir::Block *successor,
182 ::llvm::StringRef successorName, unsigned successorIndex) {
184 return op->emitOpError("successor #") << successorIndex << " ('"
185 << successorName << ")' failed to verify constraint: {2}";
187 return ::mlir::success();
194 static ::llvm::LogicalResult {0}(
195 ::mlir::Operation *op, ::mlir::Region ®ion, ::llvm::StringRef regionName,
196 unsigned regionIndex) {
198 return op->emitOpError("region #") << regionIndex
199 << (regionName.empty() ? " " : " ('" + regionName + "') ")
200 << "failed to verify constraint: {2}";
202 return ::mlir::success();
210 static ::llvm::LogicalResult {0}(
211 ::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::{3},
212 ::llvm::StringRef failureStr) {
214 return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
215 diag << failureStr << ": {2}";
218 return ::mlir::success();
222 void StaticVerifierFunctionEmitter::emitConstraints(
223 const ConstraintMap &constraints, StringRef selfName,
224 const char *
const codeTemplate) {
227 for (
auto &it : constraints) {
228 os << formatv(codeTemplate, it.second,
229 tgfmt(it.first.getConditionTemplate(), &ctx),
234 void StaticVerifierFunctionEmitter::emitTypeConstraints() {
238 void StaticVerifierFunctionEmitter::emitAttrConstraints() {
244 void StaticVerifierFunctionEmitter::emitPropConstraints() {
247 for (
auto &it : propConstraints) {
248 auto propConstraint = cast<PropConstraint>(it.first);
250 tgfmt(propConstraint.getConditionTemplate(), &ctx),
252 propConstraint.getInterfaceType());
256 void StaticVerifierFunctionEmitter::emitSuccessorConstraints() {
260 void StaticVerifierFunctionEmitter::emitRegionConstraints() {
264 void StaticVerifierFunctionEmitter::emitPatternConstraints() {
267 for (
auto &it : typeConstraints) {
269 tgfmt(it.first.getConditionTemplate(), &ctx),
273 for (
auto &it : attrConstraints) {
275 tgfmt(it.first.getConditionTemplate(), &ctx),
291 auto test =
tgfmt(attr.getConditionTemplate(),
294 return !StringRef(test).contains(
"<no-subst-found>");
307 return !StringRef(test).contains(
"<no-subst-found>") && test !=
"true" &&
311 std::string StaticVerifierFunctionEmitter::getUniqueName(StringRef
kind,
313 return (
"__mlir_ods_local_" +
kind +
"_constraint_" + uniqueOutputLabel +
318 void StaticVerifierFunctionEmitter::collectConstraint(ConstraintMap &map,
321 auto [it, inserted] = map.try_emplace(constraint);
323 it->second = getUniqueName(
kind, map.size());
330 if (value.hasPredicate())
331 collectConstraint(typeConstraints,
"type", value.constraint);
334 for (
const Record *def : opDefs) {
341 if (!namedAttr.attr.getPredicate().isNull() &&
342 !namedAttr.attr.isDerivedAttr() &&
344 collectConstraint(attrConstraints,
"attr", namedAttr.attr);
348 if (!namedProp.prop.getPredicate().isNull() &&
350 collectConstraint(propConstraints,
"prop", namedProp.prop);
355 if (!successor.constraint.getPredicate().isNull()) {
356 collectConstraint(successorConstraints,
"successor",
357 successor.constraint);
362 if (!region.constraint.getPredicate().isNull())
363 collectConstraint(regionConstraints,
"region", region.constraint);
367 void StaticVerifierFunctionEmitter::collectPatternConstraints(
369 for (
auto &leaf : constraints) {
370 assert(leaf.isOperandMatcher() || leaf.isAttrMatcher());
372 leaf.isOperandMatcher() ? typeConstraints : attrConstraints,
373 leaf.isOperandMatcher() ?
"type" :
"attr", leaf.getAsConstraint());
383 raw_string_ostream os(ret);
384 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 const char *const propConstraintCode
Code for a property constraint.
static bool canUniqueAttrConstraint(Attribute attr)
An attribute constraint that references anything other than itself and the current op cannot be gener...
static bool canUniquePropConstraint(Property prop)
A property constraint that references anything other than itself and the current op cannot be generic...
static const char *const attrConstraintCode
Code for an attribute constraint.
union mlir::linalg::@1199::ArityGroupAndKind::Kind kind
Attributes are known-constant values of operations.
std::string getConditionTemplate() const
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
llvm::iterator_range< const_property_iterator > getProperties() const
StringRef getInterfaceType() 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.
std::optional< StringRef > getPropConstraintFn(const Constraint &constraint) const
Get the name of the static function used for the given property 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.