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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
//===- PDL.cpp - Pattern Descriptor Language Dialect ----------------------===//
//
// 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/Dialect/PDL/IR/PDL.h"
#include "mlir/Dialect/PDL/IR/PDLOps.h"
#include "mlir/Dialect/PDL/IR/PDLTypes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include <optional>
using namespace mlir;
using namespace mlir::pdl;
#include "mlir/Dialect/PDL/IR/PDLOpsDialect.cpp.inc"
//===----------------------------------------------------------------------===//
// PDLDialect
//===----------------------------------------------------------------------===//
void PDLDialect::initialize() {
addOperations<
#define GET_OP_LIST
#include "mlir/Dialect/PDL/IR/PDLOps.cpp.inc"
>();
registerTypes();
}
//===----------------------------------------------------------------------===//
// PDL Operations
//===----------------------------------------------------------------------===//
/// Returns true if the given operation is used by a "binding" pdl operation.
static bool hasBindingUse(Operation *op) {
for (Operation *user : op->getUsers())
// A result by itself is not binding, it must also be bound.
if (!isa<ResultOp, ResultsOp>(user) || hasBindingUse(user))
return true;
return false;
}
/// Returns success if the given operation is not in the main matcher body or
/// is used by a "binding" operation. On failure, emits an error.
static LogicalResult verifyHasBindingUse(Operation *op) {
// If the parent is not a pattern, there is nothing to do.
if (!llvm::isa_and_nonnull<PatternOp>(op->getParentOp()))
return success();
if (hasBindingUse(op))
return success();
return op->emitOpError(
"expected a bindable user when defined in the matcher body of a "
"`pdl.pattern`");
}
/// Visits all the pdl.operand(s), pdl.result(s), and pdl.operation(s)
/// connected to the given operation.
static void visit(Operation *op, DenseSet<Operation *> &visited) {
// If the parent is not a pattern, there is nothing to do.
if (!isa<PatternOp>(op->getParentOp()) || isa<RewriteOp>(op))
return;
// Ignore if already visited.
if (visited.contains(op))
return;
// Mark as visited.
visited.insert(op);
// Traverse the operands / parent.
TypeSwitch<Operation *>(op)
.Case<OperationOp>([&visited](auto operation) {
for (Value operand : operation.getOperandValues())
visit(operand.getDefiningOp(), visited);
})
.Case<ResultOp, ResultsOp>([&visited](auto result) {
visit(result.getParent().getDefiningOp(), visited);
});
// Traverse the users.
for (Operation *user : op->getUsers())
visit(user, visited);
}
//===----------------------------------------------------------------------===//
// pdl::ApplyNativeConstraintOp
//===----------------------------------------------------------------------===//
LogicalResult ApplyNativeConstraintOp::verify() {
if (getNumOperands() == 0)
return emitOpError("expected at least one argument");
return success();
}
//===----------------------------------------------------------------------===//
// pdl::ApplyNativeRewriteOp
//===----------------------------------------------------------------------===//
LogicalResult ApplyNativeRewriteOp::verify() {
if (getNumOperands() == 0 && getNumResults() == 0)
return emitOpError("expected at least one argument or result");
return success();
}
//===----------------------------------------------------------------------===//
// pdl::AttributeOp
//===----------------------------------------------------------------------===//
LogicalResult AttributeOp::verify() {
Value attrType = getValueType();
std::optional<Attribute> attrValue = getValue();
if (!attrValue) {
if (isa<RewriteOp>((*this)->getParentOp()))
return emitOpError(
"expected constant value when specified within a `pdl.rewrite`");
return verifyHasBindingUse(*this);
}
if (attrType)
return emitOpError("expected only one of [`type`, `value`] to be set");
return success();
}
//===----------------------------------------------------------------------===//
// pdl::OperandOp
//===----------------------------------------------------------------------===//
LogicalResult OperandOp::verify() { return verifyHasBindingUse(*this); }
//===----------------------------------------------------------------------===//
// pdl::OperandsOp
//===----------------------------------------------------------------------===//
LogicalResult OperandsOp::verify() { return verifyHasBindingUse(*this); }
//===----------------------------------------------------------------------===//
// pdl::OperationOp
//===----------------------------------------------------------------------===//
static ParseResult parseOperationOpAttributes(
OpAsmParser &p,
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &attrOperands,
ArrayAttr &attrNamesAttr) {
Builder &builder = p.getBuilder();
SmallVector<Attribute, 4> attrNames;
if (succeeded(p.parseOptionalLBrace())) {
auto parseOperands = [&]() {
StringAttr nameAttr;
OpAsmParser::UnresolvedOperand operand;
if (p.parseAttribute(nameAttr) || p.parseEqual() ||
p.parseOperand(operand))
return failure();
attrNames.push_back(nameAttr);
attrOperands.push_back(operand);
return success();
};
if (p.parseCommaSeparatedList(parseOperands) || p.parseRBrace())
return failure();
}
attrNamesAttr = builder.getArrayAttr(attrNames);
return success();
}
static void printOperationOpAttributes(OpAsmPrinter &p, OperationOp op,
OperandRange attrArgs,
ArrayAttr attrNames) {
if (attrNames.empty())
return;
p << " {";
interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
[&](int i) { p << attrNames[i] << " = " << attrArgs[i]; });
p << '}';
}
/// Verifies that the result types of this operation, defined within a
/// `pdl.rewrite`, can be inferred.
static LogicalResult verifyResultTypesAreInferrable(OperationOp op,
OperandRange resultTypes) {
// Functor that returns if the given use can be used to infer a type.
Block *rewriterBlock = op->getBlock();
auto canInferTypeFromUse = [&](OpOperand &use) {
// If the use is within a ReplaceOp and isn't the operation being replaced
// (i.e. is not the first operand of the replacement), we can infer a type.
ReplaceOp replOpUser = dyn_cast<ReplaceOp>(use.getOwner());
if (!replOpUser || use.getOperandNumber() == 0)
return false;
// Make sure the replaced operation was defined before this one.
Operation *replacedOp = replOpUser.getOpValue().getDefiningOp();
return replacedOp->getBlock() != rewriterBlock ||
replacedOp->isBeforeInBlock(op);
};
// Check to see if the uses of the operation itself can be used to infer
// types.
if (llvm::any_of(op.getOp().getUses(), canInferTypeFromUse))
return success();
// Handle the case where the operation has no explicit result types.
if (resultTypes.empty()) {
// If we don't know the concrete operation, don't attempt any verification.
// We can't make assumptions if we don't know the concrete operation.
std::optional<StringRef> rawOpName = op.getOpName();
if (!rawOpName)
return success();
std::optional<RegisteredOperationName> opName =
RegisteredOperationName::lookup(*rawOpName, op.getContext());
if (!opName)
return success();
// If no explicit result types were provided, check to see if the operation
// expected at least one result. This doesn't cover all cases, but this
// should cover many cases in which the user intended to infer the results
// of an operation, but it isn't actually possible.
bool expectedAtLeastOneResult =
!opName->hasTrait<OpTrait::ZeroResults>() &&
!opName->hasTrait<OpTrait::VariadicResults>();
if (expectedAtLeastOneResult) {
return op
.emitOpError("must have inferable or constrained result types when "
"nested within `pdl.rewrite`")
.attachNote()
.append("operation is created in a non-inferrable context, but '",
*opName, "' does not implement InferTypeOpInterface");
}
return success();
}
// Otherwise, make sure each of the types can be inferred.
for (const auto &it : llvm::enumerate(resultTypes)) {
Operation *resultTypeOp = it.value().getDefiningOp();
assert(resultTypeOp && "expected valid result type operation");
// If the op was defined by a `apply_native_rewrite`, it is guaranteed to be
// usable.
if (isa<ApplyNativeRewriteOp>(resultTypeOp))
continue;
// If the type operation was defined in the matcher and constrains an
// operand or the result of an input operation, it can be used.
auto constrainsInput = [rewriterBlock](Operation *user) {
return user->getBlock() != rewriterBlock &&
isa<OperandOp, OperandsOp, OperationOp>(user);
};
if (TypeOp typeOp = dyn_cast<TypeOp>(resultTypeOp)) {
if (typeOp.getConstantType() ||
llvm::any_of(typeOp->getUsers(), constrainsInput))
continue;
} else if (TypesOp typeOp = dyn_cast<TypesOp>(resultTypeOp)) {
if (typeOp.getConstantTypes() ||
llvm::any_of(typeOp->getUsers(), constrainsInput))
continue;
}
return op
.emitOpError("must have inferable or constrained result types when "
"nested within `pdl.rewrite`")
.attachNote()
.append("result type #", it.index(), " was not constrained");
}
return success();
}
LogicalResult OperationOp::verify() {
bool isWithinRewrite = isa_and_nonnull<RewriteOp>((*this)->getParentOp());
if (isWithinRewrite && !getOpName())
return emitOpError("must have an operation name when nested within "
"a `pdl.rewrite`");
ArrayAttr attributeNames = getAttributeValueNamesAttr();
auto attributeValues = getAttributeValues();
if (attributeNames.size() != attributeValues.size()) {
return emitOpError()
<< "expected the same number of attribute values and attribute "
"names, got "
<< attributeNames.size() << " names and " << attributeValues.size()
<< " values";
}
// If the operation is within a rewrite body and doesn't have type inference,
// ensure that the result types can be resolved.
if (isWithinRewrite && !mightHaveTypeInference()) {
if (failed(verifyResultTypesAreInferrable(*this, getTypeValues())))
return failure();
}
return verifyHasBindingUse(*this);
}
bool OperationOp::hasTypeInference() {
if (std::optional<StringRef> rawOpName = getOpName()) {
OperationName opName(*rawOpName, getContext());
return opName.hasInterface<InferTypeOpInterface>();
}
return false;
}
bool OperationOp::mightHaveTypeInference() {
if (std::optional<StringRef> rawOpName = getOpName()) {
OperationName opName(*rawOpName, getContext());
return opName.mightHaveInterface<InferTypeOpInterface>();
}
return false;
}
//===----------------------------------------------------------------------===//
// pdl::PatternOp
//===----------------------------------------------------------------------===//
LogicalResult PatternOp::verifyRegions() {
Region &body = getBodyRegion();
Operation *term = body.front().getTerminator();
auto rewriteOp = dyn_cast<RewriteOp>(term);
if (!rewriteOp) {
return emitOpError("expected body to terminate with `pdl.rewrite`")
.attachNote(term->getLoc())
.append("see terminator defined here");
}
// Check that all values defined in the top-level pattern belong to the PDL
// dialect.
WalkResult result = body.walk([&](Operation *op) -> WalkResult {
if (!isa_and_nonnull<PDLDialect>(op->getDialect())) {
emitOpError("expected only `pdl` operations within the pattern body")
.attachNote(op->getLoc())
.append("see non-`pdl` operation defined here");
return WalkResult::interrupt();
}
return WalkResult::advance();
});
if (result.wasInterrupted())
return failure();
// Check that there is at least one operation.
if (body.front().getOps<OperationOp>().empty())
return emitOpError("the pattern must contain at least one `pdl.operation`");
// Determine if the operations within the pdl.pattern form a connected
// component. This is determined by starting the search from the first
// operand/result/operation and visiting their users / parents / operands.
// We limit our attention to operations that have a user in pdl.rewrite,
// those that do not will be detected via other means (expected bindable
// user).
bool first = true;
DenseSet<Operation *> visited;
for (Operation &op : body.front()) {
// The following are the operations forming the connected component.
if (!isa<OperandOp, OperandsOp, ResultOp, ResultsOp, OperationOp>(op))
continue;
// Determine if the operation has a user in `pdl.rewrite`.
bool hasUserInRewrite = false;
for (Operation *user : op.getUsers()) {
Region *region = user->getParentRegion();
if (isa<RewriteOp>(user) ||
(region && isa<RewriteOp>(region->getParentOp()))) {
hasUserInRewrite = true;
break;
}
}
// If the operation does not have a user in `pdl.rewrite`, ignore it.
if (!hasUserInRewrite)
continue;
if (first) {
// For the first operation, invoke visit.
visit(&op, visited);
first = false;
} else if (!visited.count(&op)) {
// For the subsequent operations, check if already visited.
return emitOpError("the operations must form a connected component")
.attachNote(op.getLoc())
.append("see a disconnected value / operation here");
}
}
return success();
}
void PatternOp::build(OpBuilder &builder, OperationState &state,
std::optional<uint16_t> benefit,
std::optional<StringRef> name) {
build(builder, state, builder.getI16IntegerAttr(benefit ? *benefit : 0),
name ? builder.getStringAttr(*name) : StringAttr());
state.regions[0]->emplaceBlock();
}
/// Returns the rewrite operation of this pattern.
RewriteOp PatternOp::getRewriter() {
return cast<RewriteOp>(getBodyRegion().front().getTerminator());
}
/// The default dialect is `pdl`.
StringRef PatternOp::getDefaultDialect() {
return PDLDialect::getDialectNamespace();
}
//===----------------------------------------------------------------------===//
// pdl::RangeOp
//===----------------------------------------------------------------------===//
static ParseResult parseRangeType(OpAsmParser &p, TypeRange argumentTypes,
Type &resultType) {
// If arguments were provided, infer the result type from the argument list.
if (!argumentTypes.empty()) {
resultType = RangeType::get(getRangeElementTypeOrSelf(argumentTypes[0]));
return success();
}
// Otherwise, parse the type as a trailing type.
return p.parseColonType(resultType);
}
static void printRangeType(OpAsmPrinter &p, RangeOp op, TypeRange argumentTypes,
Type resultType) {
if (argumentTypes.empty())
p << ": " << resultType;
}
LogicalResult RangeOp::verify() {
Type elementType = getType().getElementType();
for (Type operandType : getOperandTypes()) {
Type operandElementType = getRangeElementTypeOrSelf(operandType);
if (operandElementType != elementType) {
return emitOpError("expected operand to have element type ")
<< elementType << ", but got " << operandElementType;
}
}
return success();
}
//===----------------------------------------------------------------------===//
// pdl::ReplaceOp
//===----------------------------------------------------------------------===//
LogicalResult ReplaceOp::verify() {
if (getReplOperation() && !getReplValues().empty())
return emitOpError() << "expected no replacement values to be provided"
" when the replacement operation is present";
return success();
}
//===----------------------------------------------------------------------===//
// pdl::ResultsOp
//===----------------------------------------------------------------------===//
static ParseResult parseResultsValueType(OpAsmParser &p, IntegerAttr index,
Type &resultType) {
if (!index) {
resultType = RangeType::get(p.getBuilder().getType<ValueType>());
return success();
}
if (p.parseArrow() || p.parseType(resultType))
return failure();
return success();
}
static void printResultsValueType(OpAsmPrinter &p, ResultsOp op,
IntegerAttr index, Type resultType) {
if (index)
p << " -> " << resultType;
}
LogicalResult ResultsOp::verify() {
if (!getIndex() && llvm::isa<pdl::ValueType>(getType())) {
return emitOpError() << "expected `pdl.range<value>` result type when "
"no index is specified, but got: "
<< getType();
}
return success();
}
//===----------------------------------------------------------------------===//
// pdl::RewriteOp
//===----------------------------------------------------------------------===//
LogicalResult RewriteOp::verifyRegions() {
Region &rewriteRegion = getBodyRegion();
// Handle the case where the rewrite is external.
if (getName()) {
if (!rewriteRegion.empty()) {
return emitOpError()
<< "expected rewrite region to be empty when rewrite is external";
}
return success();
}
// Otherwise, check that the rewrite region only contains a single block.
if (rewriteRegion.empty()) {
return emitOpError() << "expected rewrite region to be non-empty if "
"external name is not specified";
}
// Check that no additional arguments were provided.
if (!getExternalArgs().empty()) {
return emitOpError() << "expected no external arguments when the "
"rewrite is specified inline";
}
return success();
}
/// The default dialect is `pdl`.
StringRef RewriteOp::getDefaultDialect() {
return PDLDialect::getDialectNamespace();
}
//===----------------------------------------------------------------------===//
// pdl::TypeOp
//===----------------------------------------------------------------------===//
LogicalResult TypeOp::verify() {
if (!getConstantTypeAttr())
return verifyHasBindingUse(*this);
return success();
}
//===----------------------------------------------------------------------===//
// pdl::TypesOp
//===----------------------------------------------------------------------===//
LogicalResult TypesOp::verify() {
if (!getConstantTypesAttr())
return verifyHasBindingUse(*this);
return success();
}
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
#define GET_OP_CLASSES
#include "mlir/Dialect/PDL/IR/PDLOps.cpp.inc"
|