MLIR  19.0.0git
Functions | Variables
CodeGenHelpers.cpp File Reference
#include "mlir/TableGen/CodeGenHelpers.h"
#include "mlir/TableGen/Operator.h"
#include "mlir/TableGen/Pattern.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "llvm/TableGen/Record.h"

Go to the source code of this file.

Functions

static std::string getUniqueOutputLabel (const llvm::RecordKeeper &records, StringRef tag)
 Generate a unique label based on the current file name to prevent name collisions if multiple generated files are included at once. More...
 
static bool canUniqueAttrConstraint (Attribute attr)
 An attribute constraint that references anything other than itself and the current op cannot be generically extracted into a function. More...
 

Variables

static const char *const typeConstraintCode
 Code templates for emitting type, attribute, successor, and region constraints. More...
 
static const char *const attrConstraintCode
 Code for an attribute constraint. More...
 
static const char *const successorConstraintCode
 Code for a successor constraint. More...
 
static const char *const regionConstraintCode
 Code for a region constraint. More...
 
static const char *const patternAttrOrTypeConstraintCode
 Code for a pattern type or attribute constraint. More...
 

Function Documentation

◆ canUniqueAttrConstraint()

static bool canUniqueAttrConstraint ( Attribute  attr)
static

An attribute constraint that references anything other than itself and the current op cannot be generically extracted into a function.

Most prohibitive are operands and results, which require calls to getODSOperands or getODSResults. Attribute references are tricky too because ops use cached identifiers.

Definition at line 243 of file CodeGenHelpers.cpp.

References mlir::tblgen::FmtContext::addSubst(), mlir::tblgen::tgfmt(), and mlir::tblgen::FmtContext::withSelf().

◆ getUniqueOutputLabel()

static std::string getUniqueOutputLabel ( const llvm::RecordKeeper &  records,
StringRef  tag 
)
static

Generate a unique label based on the current file name to prevent name collisions if multiple generated files are included at once.

Definition at line 27 of file CodeGenHelpers.cpp.

Variable Documentation

◆ attrConstraintCode

const char* const attrConstraintCode
static
Initial value:
= R"(
static ::mlir::LogicalResult {0}(
::mlir::Attribute attr, ::llvm::StringRef attrName, llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) {{
if (attr && !({1}))
return emitError() << "attribute '" << attrName
<< "' failed to satisfy constraint: {2}";
return ::mlir::success();
}
static ::mlir::LogicalResult {0}(
::mlir::Operation *op, ::mlir::Attribute attr, ::llvm::StringRef attrName) {{
return {0}(attr, attrName, [op]() {{
return op->emitOpError();
});
}
)"

Code for an attribute constraint.

These may be called from ops only. Attribute constraints cannot reference anything other than $_self and $_op.

TODO: Unique constraints for adaptors. However, most Adaptor::verify functions are stripped anyways.

Definition at line 131 of file CodeGenHelpers.cpp.

◆ patternAttrOrTypeConstraintCode

const char* const patternAttrOrTypeConstraintCode
static
Initial value:
= R"(
static ::mlir::LogicalResult {0}(
::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::{3},
::llvm::StringRef failureStr) {
if (!({1})) {
return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
diag << failureStr << ": {2}";
});
}
return ::mlir::success();
}
)"

Code for a pattern type or attribute constraint.

{3}: "Type type" or "Attribute attr".

Definition at line 178 of file CodeGenHelpers.cpp.

◆ regionConstraintCode

const char* const regionConstraintCode
static
Initial value:
= R"(
static ::mlir::LogicalResult {0}(
::mlir::Operation *op, ::mlir::Region &region, ::llvm::StringRef regionName,
unsigned regionIndex) {
if (!({1})) {
return op->emitOpError("region #") << regionIndex
<< (regionName.empty() ? " " : " ('" + regionName + "') ")
<< "failed to verify constraint: {2}";
}
return ::mlir::success();
}
)"

Code for a region constraint.

Callers will need to pass in the region's name for emitting an error message.

Definition at line 162 of file CodeGenHelpers.cpp.

◆ successorConstraintCode

const char* const successorConstraintCode
static
Initial value:
= R"(
static ::mlir::LogicalResult {0}(
::mlir::Operation *op, ::mlir::Block *successor,
::llvm::StringRef successorName, unsigned successorIndex) {
if (!({1})) {
return op->emitOpError("successor #") << successorIndex << " ('"
<< successorName << ")' failed to verify constraint: {2}";
}
return ::mlir::success();
}
)"

Code for a successor constraint.

Definition at line 148 of file CodeGenHelpers.cpp.

◆ typeConstraintCode

const char* const typeConstraintCode
static
Initial value:
= R"(
static ::mlir::LogicalResult {0}(
::mlir::Operation *op, ::mlir::Type type, ::llvm::StringRef valueKind,
unsigned valueIndex) {
if (!({1})) {
return op->emitOpError(valueKind) << " #" << valueIndex
<< " must be {2}, but got " << type;
}
return ::mlir::success();
}
)"

Code templates for emitting type, attribute, successor, and region constraints.

Each of these templates require the following arguments:

{0}: The unique constraint name. {1}: The constraint code. {2}: The constraint description. Code for a type constraint. These may be called on the type of either operands or results.

Definition at line 113 of file CodeGenHelpers.cpp.