diff options
author | Mathieu Fehr <mathieu.fehr@gmail.com> | 2023-05-12 19:45:46 +0100 |
---|---|---|
committer | Mathieu Fehr <mathieu.fehr@gmail.com> | 2023-05-12 19:48:00 +0100 |
commit | 648d1920404566d66cef72cd378c755f09d93d0e (patch) | |
tree | 9d7330e27f1171b33e46568d19d5cdb29d13267f /mlir/lib | |
parent | 49b87b05726b4395503230c3d400f93c626e6dce (diff) | |
download | llvm-648d1920404566d66cef72cd378c755f09d93d0e.tar.gz |
Revert "[mlir][irdl] Add verification of IRDL ops"
This reverts commit 52761cb99164acd4ea76f91fc16a3e40ec94b898.
Diffstat (limited to 'mlir/lib')
-rw-r--r-- | mlir/lib/Dialect/IRDL/CMakeLists.txt | 1 | ||||
-rw-r--r-- | mlir/lib/Dialect/IRDL/IR/IRDL.cpp | 2 | ||||
-rw-r--r-- | mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp | 61 | ||||
-rw-r--r-- | mlir/lib/Dialect/IRDL/IRDLLoading.cpp | 222 |
4 files changed, 4 insertions, 282 deletions
diff --git a/mlir/lib/Dialect/IRDL/CMakeLists.txt b/mlir/lib/Dialect/IRDL/CMakeLists.txt index d25760e5d29b..7af0e4229357 100644 --- a/mlir/lib/Dialect/IRDL/CMakeLists.txt +++ b/mlir/lib/Dialect/IRDL/CMakeLists.txt @@ -1,6 +1,5 @@ add_mlir_dialect_library(MLIRIRDL IR/IRDL.cpp - IR/IRDLOps.cpp IRDLLoading.cpp IRDLVerifiers.cpp diff --git a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp index 01e58ccf0a5b..e2649f22094c 100644 --- a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp +++ b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp @@ -71,8 +71,6 @@ LogicalResult DialectOp::verify() { return success(); } -#include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc" - #define GET_TYPEDEF_CLASSES #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc" diff --git a/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp b/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp deleted file mode 100644 index a9956cc630cc..000000000000 --- a/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp +++ /dev/null @@ -1,61 +0,0 @@ -//===- IRDLOps.cpp - IRDL dialect -------------------------------*- 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 -// -//===----------------------------------------------------------------------===// - -#include "mlir/Dialect/IRDL/IR/IRDL.h" - -using namespace mlir; -using namespace mlir::irdl; - -std::unique_ptr<Constraint> Is::getVerifier( - SmallVector<Value> const &valueToConstr, - DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types, - DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> &attrs) { - return std::make_unique<IsConstraint>(getExpectedAttr()); -} - -std::unique_ptr<Constraint> Parametric::getVerifier( - SmallVector<Value> const &valueToConstr, - DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types, - DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> &attrs) { - SmallVector<unsigned> constraints; - for (Value arg : getArgs()) { - for (auto [i, value] : enumerate(valueToConstr)) { - if (value == arg) { - constraints.push_back(i); - break; - } - } - } - - // Symbol reference case for the base - SymbolRefAttr symRef = getBaseType(); - Operation *defOp = - SymbolTable::lookupNearestSymbolFrom(getOperation(), symRef); - if (!defOp) { - emitError() << symRef << " does not refer to any existing symbol"; - return nullptr; - } - - if (auto typeOp = dyn_cast<TypeOp>(defOp)) - return std::make_unique<DynParametricTypeConstraint>(types[typeOp].get(), - constraints); - - if (auto attrOp = dyn_cast<AttributeOp>(defOp)) - return std::make_unique<DynParametricAttrConstraint>(attrs[attrOp].get(), - constraints); - - llvm_unreachable("verifier should ensure that the referenced operation is " - "either a type or an attribute definition"); -} - -std::unique_ptr<Constraint> Any::getVerifier( - SmallVector<Value> const &valueToConstr, - DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types, - DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> &attrs) { - return std::make_unique<AnyAttributeConstraint>(); -} diff --git a/mlir/lib/Dialect/IRDL/IRDLLoading.cpp b/mlir/lib/Dialect/IRDL/IRDLLoading.cpp index b4b6f01466f3..fb00085a7ee0 100644 --- a/mlir/lib/Dialect/IRDL/IRDLLoading.cpp +++ b/mlir/lib/Dialect/IRDL/IRDLLoading.cpp @@ -12,7 +12,6 @@ #include "mlir/Dialect/IRDL/IRDLLoading.h" #include "mlir/Dialect/IRDL/IR/IRDL.h" -#include "mlir/Dialect/IRDL/IR/IRDLInterfaces.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/ExtensibleDialect.h" #include "mlir/Support/LogicalResult.h" @@ -23,130 +22,9 @@ 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(); -} - -/// 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, ArrayRef<std::unique_ptr<Constraint>> constraints, - ArrayRef<size_t> operandConstrs, - ArrayRef<size_t> resultConstrs) { - /// Check that we have the right number of operands. - unsigned numOperands = op->getNumOperands(); - size_t numExpectedOperands = operandConstrs.size(); - if (numOperands != numExpectedOperands) - return op->emitOpError() << numExpectedOperands - << " operands expected, but got " << numOperands; - - /// Check that we have the right number of results. - unsigned numResults = op->getNumResults(); - size_t numExpectedResults = resultConstrs.size(); - if (numResults != numExpectedResults) - return op->emitOpError() - << numExpectedResults << " results expected, but got " << numResults; - - auto emitError = [op]() { return op->emitError(); }; - - ConstraintVerifier verifier(constraints); - - /// Check that all operands satisfy the constraints. - for (auto [i, operandType] : enumerate(op->getOperandTypes())) - if (failed(verifier.verify({emitError}, TypeAttr::get(operandType), - operandConstrs[i]))) - return failure(); - - /// Check that all results satisfy the constraints. - for (auto [i, resultType] : enumerate(op->getResultTypes())) - if (failed(verifier.verify({emitError}, TypeAttr::get(resultType), - resultConstrs[i]))) - return failure(); - - return success(); -} - /// Define and load an operation represented by a `irdl.operation` /// operation. -static WalkResult loadOperation( - OperationOp op, ExtensibleDialect *dialect, - DenseMap<TypeOp, std::unique_ptr<DynamicTypeDefinition>> &types, - DenseMap<AttributeOp, std::unique_ptr<DynamicAttrDefinition>> &attrs) { - // Resolve SSA values to verifier constraint slots - SmallVector<Value> constrToValue; - for (Operation &op : op->getRegion(0).getOps()) { - if (isa<VerifyConstraintInterface>(op)) { - if (op.getNumResults() != 1) - return op.emitError() - << "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 WalkResult::interrupt(); - constraints.push_back(std::move(verifier)); - } - - SmallVector<size_t> operandConstraints; - SmallVector<size_t> resultConstraints; - - // 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 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; - } - } - } - } - +static WalkResult loadOperation(OperationOp op, ExtensibleDialect *dialect) { // IRDL does not support defining custom parsers or printers. auto parser = [](OpAsmParser &parser, OperationState &result) { return failure(); @@ -155,13 +33,7 @@ static WalkResult loadOperation( printer.printGenericOp(op); }; - auto verifier = - [constraints{std::move(constraints)}, - operandConstraints{std::move(operandConstraints)}, - resultConstraints{std::move(resultConstraints)}](Operation *op) { - return irdlOpVerifier(op, constraints, operandConstraints, - resultConstraints); - }; + auto verifier = [](Operation *op) { return success(); }; // IRDL does not support defining regions. auto regionVerifier = [](Operation *op) { return success(); }; @@ -174,68 +46,6 @@ static WalkResult loadOperation( 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); - }; - return verifier; -} - /// Load all dialects in the given module, without loading any operation, type /// or attribute definitions. static DenseMap<DialectOp, ExtensibleDialect *> loadEmptyDialects(ModuleOp op) { @@ -298,33 +108,9 @@ LogicalResult mlir::irdl::loadDialects(ModuleOp op) { 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); + WalkResult res = op.walk([&](OperationOp opOp) { + return loadOperation(opOp, dialects[opOp.getParentOp()]); }); if (res.wasInterrupted()) return failure(); |