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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
//===- IRDLLoading.cpp - IRDL dialect loading --------------------- C++ -*-===//
//
// This file is licensed 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
//
//===----------------------------------------------------------------------===//
//
// Manages the loading of MLIR objects from IRDL operations.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/IRDL/IRDLLoading.h"
#include "mlir/Dialect/IRDL/IR/IRDL.h"
#include "mlir/Dialect/IRDL/IR/IRDLInterfaces.h"
#include "mlir/Dialect/IRDL/IRDLSymbols.h"
#include "mlir/Dialect/IRDL/IRDLVerifiers.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/ExtensibleDialect.h"
#include "mlir/IR/OperationSupport.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/SMLoc.h"
#include <numeric>
using namespace mlir;
using namespace mlir::irdl;
/// Verify that the given list of parameters satisfy the given constraints.
/// This encodes the logic of the verification method for attributes and types
/// defined with IRDL.
static LogicalResult
irdlAttrOrTypeVerifier(function_ref<InFlightDiagnostic()> emitError,
ArrayRef<Attribute> params,
ArrayRef<std::unique_ptr<Constraint>> constraints,
ArrayRef<size_t> paramConstraints) {
if (params.size() != paramConstraints.size()) {
emitError() << "expected " << paramConstraints.size()
<< " type arguments, but had " << params.size();
return failure();
}
ConstraintVerifier verifier(constraints);
// Check that each parameter satisfies its constraint.
for (auto [i, param] : enumerate(params))
if (failed(verifier.verify(emitError, param, paramConstraints[i])))
return failure();
return success();
}
/// Get the operand segment sizes from the attribute dictionary.
LogicalResult getSegmentSizesFromAttr(Operation *op, StringRef elemName,
StringRef attrName, unsigned numElements,
ArrayRef<Variadicity> variadicities,
SmallVectorImpl<int> &segmentSizes) {
// Get the segment sizes attribute, and check that it is of the right type.
Attribute segmentSizesAttr = op->getAttr(attrName);
if (!segmentSizesAttr) {
return op->emitError() << "'" << attrName
<< "' attribute is expected but not provided";
}
auto denseSegmentSizes = dyn_cast<DenseI32ArrayAttr>(segmentSizesAttr);
if (!denseSegmentSizes) {
return op->emitError() << "'" << attrName
<< "' attribute is expected to be a dense i32 array";
}
if (denseSegmentSizes.size() != (int64_t)variadicities.size()) {
return op->emitError() << "'" << attrName << "' attribute for specifying "
<< elemName << " segments must have "
<< variadicities.size() << " elements, but got "
<< denseSegmentSizes.size();
}
// Check that the segment sizes are corresponding to the given variadicities,
for (auto [i, segmentSize, variadicity] :
enumerate(denseSegmentSizes.asArrayRef(), variadicities)) {
if (segmentSize < 0)
return op->emitError()
<< "'" << attrName << "' attribute for specifying " << elemName
<< " segments must have non-negative values";
if (variadicity == Variadicity::single && segmentSize != 1)
return op->emitError() << "element " << i << " in '" << attrName
<< "' attribute must be equal to 1";
if (variadicity == Variadicity::optional && segmentSize > 1)
return op->emitError() << "element " << i << " in '" << attrName
<< "' attribute must be equal to 0 or 1";
segmentSizes.push_back(segmentSize);
}
// Check that the sum of the segment sizes is equal to the number of elements.
int32_t sum = 0;
for (int32_t segmentSize : denseSegmentSizes.asArrayRef())
sum += segmentSize;
if (sum != static_cast<int32_t>(numElements))
return op->emitError() << "sum of elements in '" << attrName
<< "' attribute must be equal to the number of "
<< elemName << "s";
return success();
}
/// Compute the segment sizes of the given element (operands, results).
/// If the operation has more than two non-single elements (optional or
/// variadic), then get the segment sizes from the attribute dictionary.
/// Otherwise, compute the segment sizes from the number of elements.
/// `elemName` should be either `"operand"` or `"result"`.
LogicalResult getSegmentSizes(Operation *op, StringRef elemName,
StringRef attrName, unsigned numElements,
ArrayRef<Variadicity> variadicities,
SmallVectorImpl<int> &segmentSizes) {
// If we have more than one non-single variadicity, we need to get the
// segment sizes from the attribute dictionary.
int numberNonSingle = count_if(
variadicities, [](Variadicity v) { return v != Variadicity::single; });
if (numberNonSingle > 1)
return getSegmentSizesFromAttr(op, elemName, attrName, numElements,
variadicities, segmentSizes);
// If we only have single variadicities, the segments sizes are all 1.
if (numberNonSingle == 0) {
if (numElements != variadicities.size()) {
return op->emitError() << "op expects exactly " << variadicities.size()
<< " " << elemName << "s, but got " << numElements;
}
for (size_t i = 0, e = variadicities.size(); i < e; ++i)
segmentSizes.push_back(1);
return success();
}
assert(numberNonSingle == 1);
// There is exactly one non-single element, so we can
// compute its size and check that it is valid.
int nonSingleSegmentSize = static_cast<int>(numElements) -
static_cast<int>(variadicities.size()) + 1;
if (nonSingleSegmentSize < 0) {
return op->emitError() << "op expects at least " << variadicities.size() - 1
<< " " << elemName << "s, but got " << numElements;
}
// Add the segment sizes.
for (Variadicity variadicity : variadicities) {
if (variadicity == Variadicity::single) {
segmentSizes.push_back(1);
continue;
}
// If we have an optional element, we should check that it represents
// zero or one elements.
if (nonSingleSegmentSize > 1 && variadicity == Variadicity::optional)
return op->emitError() << "op expects at most " << variadicities.size()
<< " " << elemName << "s, but got " << numElements;
segmentSizes.push_back(nonSingleSegmentSize);
}
return success();
}
/// Compute the segment sizes of the given operands.
/// If the operation has more than two non-single operands (optional or
/// variadic), then get the segment sizes from the attribute dictionary.
/// Otherwise, compute the segment sizes from the number of operands.
LogicalResult getOperandSegmentSizes(Operation *op,
ArrayRef<Variadicity> variadicities,
SmallVectorImpl<int> &segmentSizes) {
return getSegmentSizes(op, "operand", "operand_segment_sizes",
op->getNumOperands(), variadicities, segmentSizes);
}
/// Compute the segment sizes of the given results.
/// If the operation has more than two non-single results (optional or
/// variadic), then get the segment sizes from the attribute dictionary.
/// Otherwise, compute the segment sizes from the number of results.
LogicalResult getResultSegmentSizes(Operation *op,
ArrayRef<Variadicity> variadicities,
SmallVectorImpl<int> &segmentSizes) {
return getSegmentSizes(op, "result", "result_segment_sizes",
op->getNumResults(), variadicities, segmentSizes);
}
/// Verify that the given operation satisfies the given constraints.
/// This encodes the logic of the verification method for operations defined
/// with IRDL.
static LogicalResult irdlOpVerifier(
Operation *op, ConstraintVerifier &verifier,
ArrayRef<size_t> operandConstrs, ArrayRef<Variadicity> operandVariadicity,
ArrayRef<size_t> resultConstrs, ArrayRef<Variadicity> resultVariadicity,
const DenseMap<StringAttr, size_t> &attributeConstrs) {
// Get the segment sizes for the operands.
// This will check that the number of operands is correct.
SmallVector<int> operandSegmentSizes;
if (failed(
getOperandSegmentSizes(op, operandVariadicity, operandSegmentSizes)))
return failure();
// Get the segment sizes for the results.
// This will check that the number of results is correct.
SmallVector<int> resultSegmentSizes;
if (failed(getResultSegmentSizes(op, resultVariadicity, resultSegmentSizes)))
return failure();
auto emitError = [op] { return op->emitError(); };
/// Сheck that we have all needed attributes passed
/// and they satisfy the constraints.
DictionaryAttr actualAttrs = op->getAttrDictionary();
for (auto [name, constraint] : attributeConstrs) {
/// First, check if the attribute actually passed.
std::optional<NamedAttribute> actual = actualAttrs.getNamed(name);
if (!actual.has_value())
return op->emitOpError()
<< "attribute " << name << " is expected but not provided";
/// Then, check if the attribute value satisfies the constraint.
if (failed(verifier.verify({emitError}, actual->getValue(), constraint)))
return failure();
}
// Check that all operands satisfy the constraints
int operandIdx = 0;
for (auto [defIndex, segmentSize] : enumerate(operandSegmentSizes)) {
for (int i = 0; i < segmentSize; i++) {
if (failed(verifier.verify(
{emitError}, TypeAttr::get(op->getOperandTypes()[operandIdx]),
operandConstrs[defIndex])))
return failure();
++operandIdx;
}
}
// Check that all results satisfy the constraints
int resultIdx = 0;
for (auto [defIndex, segmentSize] : enumerate(resultSegmentSizes)) {
for (int i = 0; i < segmentSize; i++) {
if (failed(verifier.verify({emitError},
TypeAttr::get(op->getResultTypes()[resultIdx]),
resultConstrs[defIndex])))
return failure();
++resultIdx;
}
}
return success();
}
static LogicalResult irdlRegionVerifier(
Operation *op, ConstraintVerifier &verifier,
ArrayRef<std::unique_ptr<RegionConstraint>> regionsConstraints) {
if (op->getNumRegions() != regionsConstraints.size()) {
return op->emitOpError()
<< "unexpected number of regions: expected "
<< regionsConstraints.size() << " but got " << op->getNumRegions();
}
for (auto [constraint, region] :
llvm::zip(regionsConstraints, op->getRegions()))
if (failed(constraint->verify(region, verifier)))
return failure();
return success();
}
llvm::unique_function<LogicalResult(Operation *) const>
mlir::irdl::createVerifier(
OperationOp op,
const DenseMap<irdl::TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types,
const DenseMap<irdl::AttributeOp, std::unique_ptr<DynamicAttrDefinition>>
&attrs) {
// Resolve SSA values to verifier constraint slots
SmallVector<Value> constrToValue;
SmallVector<Value> regionToValue;
for (Operation &op : op->getRegion(0).getOps()) {
if (isa<VerifyConstraintInterface>(op)) {
if (op.getNumResults() != 1) {
op.emitError()
<< "IRDL constraint operations must have exactly one result";
return nullptr;
}
constrToValue.push_back(op.getResult(0));
}
if (isa<VerifyRegionInterface>(op)) {
if (op.getNumResults() != 1) {
op.emitError()
<< "IRDL constraint operations must have exactly one result";
return nullptr;
}
regionToValue.push_back(op.getResult(0));
}
}
// Build the verifiers for each constraint slot
SmallVector<std::unique_ptr<Constraint>> constraints;
for (Value v : constrToValue) {
VerifyConstraintInterface op =
cast<VerifyConstraintInterface>(v.getDefiningOp());
std::unique_ptr<Constraint> verifier =
op.getVerifier(constrToValue, types, attrs);
if (!verifier)
return nullptr;
constraints.push_back(std::move(verifier));
}
// Build region constraints
SmallVector<std::unique_ptr<RegionConstraint>> regionConstraints;
for (Value v : regionToValue) {
VerifyRegionInterface op = cast<VerifyRegionInterface>(v.getDefiningOp());
std::unique_ptr<RegionConstraint> verifier =
op.getVerifier(constrToValue, types, attrs);
regionConstraints.push_back(std::move(verifier));
}
SmallVector<size_t> operandConstraints;
SmallVector<Variadicity> operandVariadicity;
// Gather which constraint slots correspond to operand constraints
auto operandsOp = op.getOp<OperandsOp>();
if (operandsOp.has_value()) {
operandConstraints.reserve(operandsOp->getArgs().size());
for (Value operand : operandsOp->getArgs()) {
for (auto [i, constr] : enumerate(constrToValue)) {
if (constr == operand) {
operandConstraints.push_back(i);
break;
}
}
}
// Gather the variadicities of each operand
for (VariadicityAttr attr : operandsOp->getVariadicity())
operandVariadicity.push_back(attr.getValue());
}
SmallVector<size_t> resultConstraints;
SmallVector<Variadicity> resultVariadicity;
// Gather which constraint slots correspond to result constraints
auto resultsOp = op.getOp<ResultsOp>();
if (resultsOp.has_value()) {
resultConstraints.reserve(resultsOp->getArgs().size());
for (Value result : resultsOp->getArgs()) {
for (auto [i, constr] : enumerate(constrToValue)) {
if (constr == result) {
resultConstraints.push_back(i);
break;
}
}
}
// Gather the variadicities of each result
for (Attribute attr : resultsOp->getVariadicity())
resultVariadicity.push_back(cast<VariadicityAttr>(attr).getValue());
}
// Gather which constraint slots correspond to attributes constraints
DenseMap<StringAttr, size_t> attributeConstraints;
auto attributesOp = op.getOp<AttributesOp>();
if (attributesOp.has_value()) {
const Operation::operand_range values = attributesOp->getAttributeValues();
const ArrayAttr names = attributesOp->getAttributeValueNames();
for (const auto &[name, value] : llvm::zip(names, values)) {
for (auto [i, constr] : enumerate(constrToValue)) {
if (constr == value) {
attributeConstraints[cast<StringAttr>(name)] = i;
break;
}
}
}
}
return
[constraints{std::move(constraints)},
regionConstraints{std::move(regionConstraints)},
operandConstraints{std::move(operandConstraints)},
operandVariadicity{std::move(operandVariadicity)},
resultConstraints{std::move(resultConstraints)},
resultVariadicity{std::move(resultVariadicity)},
attributeConstraints{std::move(attributeConstraints)}](Operation *op) {
ConstraintVerifier verifier(constraints);
const LogicalResult opVerifierResult = irdlOpVerifier(
op, verifier, operandConstraints, operandVariadicity,
resultConstraints, resultVariadicity, attributeConstraints);
const LogicalResult opRegionVerifierResult =
irdlRegionVerifier(op, verifier, regionConstraints);
return LogicalResult::success(opVerifierResult.succeeded() &&
opRegionVerifierResult.succeeded());
};
}
/// Define and load an operation represented by a `irdl.operation`
/// operation.
static WalkResult loadOperation(
OperationOp op, ExtensibleDialect *dialect,
const DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types,
const DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>>
&attrs) {
// IRDL does not support defining custom parsers or printers.
auto parser = [](OpAsmParser &parser, OperationState &result) {
return failure();
};
auto printer = [](Operation *op, OpAsmPrinter &printer, StringRef) {
printer.printGenericOp(op);
};
auto verifier = createVerifier(op, types, attrs);
if (!verifier)
return WalkResult::interrupt();
// IRDL supports only checking number of blocks and argument constraints
// It is done in the main verifier to reuse `ConstraintVerifier` context
auto regionVerifier = [](Operation *op) { return LogicalResult::success(); };
auto opDef = DynamicOpDefinition::get(
op.getName(), dialect, std::move(verifier), std::move(regionVerifier),
std::move(parser), std::move(printer));
dialect->registerDynamicOp(std::move(opDef));
return WalkResult::advance();
}
/// Get the verifier of a type or attribute definition.
/// Return nullptr if the definition is invalid.
static DynamicAttrDefinition::VerifierFn getAttrOrTypeVerifier(
Operation *attrOrTypeDef, ExtensibleDialect *dialect,
DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types,
DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> &attrs) {
assert((isa<AttributeOp>(attrOrTypeDef) || isa<TypeOp>(attrOrTypeDef)) &&
"Expected an attribute or type definition");
// Resolve SSA values to verifier constraint slots
SmallVector<Value> constrToValue;
for (Operation &op : attrOrTypeDef->getRegion(0).getOps()) {
if (isa<VerifyConstraintInterface>(op)) {
assert(op.getNumResults() == 1 &&
"IRDL constraint operations must have exactly one result");
constrToValue.push_back(op.getResult(0));
}
}
// Build the verifiers for each constraint slot
SmallVector<std::unique_ptr<Constraint>> constraints;
for (Value v : constrToValue) {
VerifyConstraintInterface op =
cast<VerifyConstraintInterface>(v.getDefiningOp());
std::unique_ptr<Constraint> verifier =
op.getVerifier(constrToValue, types, attrs);
if (!verifier)
return {};
constraints.push_back(std::move(verifier));
}
// Get the parameter definitions.
std::optional<ParametersOp> params;
if (auto attr = dyn_cast<AttributeOp>(attrOrTypeDef))
params = attr.getOp<ParametersOp>();
else if (auto type = dyn_cast<TypeOp>(attrOrTypeDef))
params = type.getOp<ParametersOp>();
// Gather which constraint slots correspond to parameter constraints
SmallVector<size_t> paramConstraints;
if (params.has_value()) {
paramConstraints.reserve(params->getArgs().size());
for (Value param : params->getArgs()) {
for (auto [i, constr] : enumerate(constrToValue)) {
if (constr == param) {
paramConstraints.push_back(i);
break;
}
}
}
}
auto verifier = [paramConstraints{std::move(paramConstraints)},
constraints{std::move(constraints)}](
function_ref<InFlightDiagnostic()> emitError,
ArrayRef<Attribute> params) {
return irdlAttrOrTypeVerifier(emitError, params, constraints,
paramConstraints);
};
// While the `std::move` is not required, not adding it triggers a bug in
// clang-10.
return std::move(verifier);
}
/// Get the possible bases of a constraint. Return `true` if all bases can
/// potentially be matched.
/// A base is a type or an attribute definition. For instance, the base of
/// `irdl.parametric "!builtin.complex"(...)` is `builtin.complex`.
/// This function returns the following information through arguments:
/// - `paramIds`: the set of type or attribute IDs that are used as bases.
/// - `paramIrdlOps`: the set of IRDL operations that are used as bases.
/// - `isIds`: the set of type or attribute IDs that are used in `irdl.is`
/// constraints.
static bool getBases(Operation *op, SmallPtrSet<TypeID, 4> ¶mIds,
SmallPtrSet<Operation *, 4> ¶mIrdlOps,
SmallPtrSet<TypeID, 4> &isIds) {
// For `irdl.any_of`, we get the bases from all its arguments.
if (auto anyOf = dyn_cast<AnyOfOp>(op)) {
bool hasAny = false;
for (Value arg : anyOf.getArgs())
hasAny &= getBases(arg.getDefiningOp(), paramIds, paramIrdlOps, isIds);
return hasAny;
}
// For `irdl.all_of`, we get the bases from the first argument.
// This is restrictive, but we can relax it later if needed.
if (auto allOf = dyn_cast<AllOfOp>(op))
return getBases(allOf.getArgs()[0].getDefiningOp(), paramIds, paramIrdlOps,
isIds);
// For `irdl.parametric`, we get directly the base from the operation.
if (auto params = dyn_cast<ParametricOp>(op)) {
SymbolRefAttr symRef = params.getBaseType();
Operation *defOp = irdl::lookupSymbolNearDialect(op, symRef);
assert(defOp && "symbol reference should refer to an existing operation");
paramIrdlOps.insert(defOp);
return false;
}
// For `irdl.is`, we get the base TypeID directly.
if (auto is = dyn_cast<IsOp>(op)) {
Attribute expected = is.getExpected();
isIds.insert(expected.getTypeID());
return false;
}
// For `irdl.any`, we return `false` since we can match any type or attribute
// base.
if (auto isA = dyn_cast<AnyOp>(op))
return true;
llvm_unreachable("unknown IRDL constraint");
}
/// Check that an any_of is in the subset IRDL can handle.
/// IRDL uses a greedy algorithm to match constraints. This means that if we
/// encounter an `any_of` with multiple constraints, we will match the first
/// constraint that is satisfied. Thus, the order of constraints matter in
/// `any_of` with our current algorithm.
/// In order to make the order of constraints irrelevant, we require that
/// all `any_of` constraint parameters are disjoint. For this, we check that
/// the base parameters are all disjoints between `parametric` operations, and
/// that they are disjoint between `parametric` and `is` operations.
/// This restriction will be relaxed in the future, when we will change our
/// algorithm to be non-greedy.
static LogicalResult checkCorrectAnyOf(AnyOfOp anyOf) {
SmallPtrSet<TypeID, 4> paramIds;
SmallPtrSet<Operation *, 4> paramIrdlOps;
SmallPtrSet<TypeID, 4> isIds;
for (Value arg : anyOf.getArgs()) {
Operation *argOp = arg.getDefiningOp();
SmallPtrSet<TypeID, 4> argParamIds;
SmallPtrSet<Operation *, 4> argParamIrdlOps;
SmallPtrSet<TypeID, 4> argIsIds;
// Get the bases of this argument. If it can match any type or attribute,
// then our `any_of` should not be allowed.
if (getBases(argOp, argParamIds, argParamIrdlOps, argIsIds))
return failure();
// We check that the base parameters are all disjoints between `parametric`
// operations, and that they are disjoint between `parametric` and `is`
// operations.
for (TypeID id : argParamIds) {
if (isIds.count(id))
return failure();
bool inserted = paramIds.insert(id).second;
if (!inserted)
return failure();
}
// We check that the base parameters are all disjoints with `irdl.is`
// operations.
for (TypeID id : isIds) {
if (paramIds.count(id))
return failure();
isIds.insert(id);
}
// We check that all `parametric` operations are disjoint. We do not
// need to check that they are disjoint with `is` operations, since
// `is` operations cannot refer to attributes defined with `irdl.parametric`
// operations.
for (Operation *op : argParamIrdlOps) {
bool inserted = paramIrdlOps.insert(op).second;
if (!inserted)
return failure();
}
}
return success();
}
/// Load all dialects in the given module, without loading any operation, type
/// or attribute definitions.
static DenseMap<DialectOp, ExtensibleDialect *> loadEmptyDialects(ModuleOp op) {
DenseMap<DialectOp, ExtensibleDialect *> dialects;
op.walk([&](DialectOp dialectOp) {
MLIRContext *ctx = dialectOp.getContext();
StringRef dialectName = dialectOp.getName();
DynamicDialect *dialect = ctx->getOrLoadDynamicDialect(
dialectName, [](DynamicDialect *dialect) {});
dialects.insert({dialectOp, dialect});
});
return dialects;
}
/// Preallocate type definitions objects with empty verifiers.
/// This in particular allocates a TypeID for each type definition.
static DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>>
preallocateTypeDefs(ModuleOp op,
DenseMap<DialectOp, ExtensibleDialect *> dialects) {
DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> typeDefs;
op.walk([&](TypeOp typeOp) {
ExtensibleDialect *dialect = dialects[typeOp.getParentOp()];
auto typeDef = DynamicTypeDefinition::get(
typeOp.getName(), dialect,
[](function_ref<InFlightDiagnostic()>, ArrayRef<Attribute>) {
return success();
});
typeDefs.try_emplace(typeOp, std::move(typeDef));
});
return typeDefs;
}
/// Preallocate attribute definitions objects with empty verifiers.
/// This in particular allocates a TypeID for each attribute definition.
static DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>>
preallocateAttrDefs(ModuleOp op,
DenseMap<DialectOp, ExtensibleDialect *> dialects) {
DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> attrDefs;
op.walk([&](AttributeOp attrOp) {
ExtensibleDialect *dialect = dialects[attrOp.getParentOp()];
auto attrDef = DynamicAttrDefinition::get(
attrOp.getName(), dialect,
[](function_ref<InFlightDiagnostic()>, ArrayRef<Attribute>) {
return success();
});
attrDefs.try_emplace(attrOp, std::move(attrDef));
});
return attrDefs;
}
LogicalResult mlir::irdl::loadDialects(ModuleOp op) {
// First, check that all any_of constraints are in a correct form.
// This is to ensure we can do the verification correctly.
WalkResult anyOfCorrects = op.walk(
[](AnyOfOp anyOf) { return (WalkResult)checkCorrectAnyOf(anyOf); });
if (anyOfCorrects.wasInterrupted())
return op.emitError("any_of constraints are not in the correct form");
// Preallocate all dialects, and type and attribute definitions.
// In particular, this allocates TypeIDs so type and attributes can have
// verifiers that refer to each other.
DenseMap<DialectOp, ExtensibleDialect *> dialects = loadEmptyDialects(op);
DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> types =
preallocateTypeDefs(op, dialects);
DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> attrs =
preallocateAttrDefs(op, dialects);
// Set the verifier for types.
WalkResult res = op.walk([&](TypeOp typeOp) {
DynamicAttrDefinition::VerifierFn verifier = getAttrOrTypeVerifier(
typeOp, dialects[typeOp.getParentOp()], types, attrs);
if (!verifier)
return WalkResult::interrupt();
types[typeOp]->setVerifyFn(std::move(verifier));
return WalkResult::advance();
});
if (res.wasInterrupted())
return failure();
// Set the verifier for attributes.
res = op.walk([&](AttributeOp attrOp) {
DynamicAttrDefinition::VerifierFn verifier = getAttrOrTypeVerifier(
attrOp, dialects[attrOp.getParentOp()], types, attrs);
if (!verifier)
return WalkResult::interrupt();
attrs[attrOp]->setVerifyFn(std::move(verifier));
return WalkResult::advance();
});
if (res.wasInterrupted())
return failure();
// Define and load all operations.
res = op.walk([&](OperationOp opOp) {
return loadOperation(opOp, dialects[opOp.getParentOp()], types, attrs);
});
if (res.wasInterrupted())
return failure();
// Load all types in their dialects.
for (auto &pair : types) {
ExtensibleDialect *dialect = dialects[pair.first.getParentOp()];
dialect->registerDynamicType(std::move(pair.second));
}
// Load all attributes in their dialects.
for (auto &pair : attrs) {
ExtensibleDialect *dialect = dialects[pair.first.getParentOp()];
dialect->registerDynamicAttr(std::move(pair.second));
}
return success();
}
|