diff --git a/mlir/include/mlir/Dialect/IRDL/IRDLRegistration.h b/mlir/include/mlir/Dialect/IRDL/IRDLRegistration.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/Dialect/IRDL/IRDLRegistration.h @@ -0,0 +1,28 @@ +//===- IRDLRegistration.h - IRDL registration -------------------*- 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 registration of MLIR objects from IRDL operations. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_DIALECT_IRDL_IRDLREGISTRATION_H +#define MLIR_DIALECT_IRDL_IRDLREGISTRATION_H + +#include "mlir/IR/BuiltinOps.h" +#include "mlir/Support/LogicalResult.h" + +namespace mlir { +namespace irdl { + +/// Register all the dialects in a module. +LogicalResult registerDialects(ModuleOp op); + +} // namespace irdl +} // namespace mlir + +#endif // MLIR_DIALECT_IRDL_IRDLREGISTRATION_H diff --git a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h --- a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h +++ b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h @@ -53,25 +53,28 @@ /// - emitBytecode will generate bytecode output instead of text. /// - implicitModule will enable implicit addition of a top-level /// 'builtin.module' if one doesn't already exist. -/// - dumpPassPipeline will dump the pipeline being run to stderr -LogicalResult -MlirOptMain(llvm::raw_ostream &outputStream, - std::unique_ptr buffer, - const PassPipelineCLParser &passPipeline, DialectRegistry ®istry, - bool splitInputFile, bool verifyDiagnostics, bool verifyPasses, - bool allowUnregisteredDialects, - bool preloadDialectsInContext = false, bool emitBytecode = false, - bool implicitModule = false, bool dumpPassPipeline = false); - -/// Support a callback to setup the pass manager. -/// - passManagerSetupFn is the callback invoked to setup the pass manager to -/// apply on the loaded IR. +/// - dumpPassPipeline will dump the pipeline being run to stderr. +/// - irdlFile is the path to an IRDL file to load. LogicalResult MlirOptMain( llvm::raw_ostream &outputStream, std::unique_ptr buffer, - PassPipelineFn passManagerSetupFn, DialectRegistry ®istry, + const PassPipelineCLParser &passPipeline, DialectRegistry ®istry, bool splitInputFile, bool verifyDiagnostics, bool verifyPasses, bool allowUnregisteredDialects, bool preloadDialectsInContext = false, - bool emitBytecode = false, bool implicitModule = false); + bool emitBytecode = false, bool implicitModule = false, + bool dumpPassPipeline = false, StringRef irdlFile = ""); + +/// Support a callback to setup the pass manager. +/// - passManagerSetupFn is the callback invoked to setup the pass manager to +/// apply on the loaded IR. +LogicalResult MlirOptMain(llvm::raw_ostream &outputStream, + std::unique_ptr buffer, + PassPipelineFn passManagerSetupFn, + DialectRegistry ®istry, bool splitInputFile, + bool verifyDiagnostics, bool verifyPasses, + bool allowUnregisteredDialects, + bool preloadDialectsInContext = false, + bool emitBytecode = false, + bool implicitModule = false, StringRef irdlFile = ""); /// Implementation for tools like `mlir-opt`. /// - toolName is used for the header displayed by `--help`. diff --git a/mlir/lib/Dialect/IRDL/CMakeLists.txt b/mlir/lib/Dialect/IRDL/CMakeLists.txt --- a/mlir/lib/Dialect/IRDL/CMakeLists.txt +++ b/mlir/lib/Dialect/IRDL/CMakeLists.txt @@ -1,6 +1,7 @@ add_mlir_dialect_library(MLIRIRDL IR/IRDL.cpp IR/IRDLOps.cpp + IRDLRegistration.cpp DEPENDS MLIRIRDLIncGen diff --git a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp --- a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp +++ b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "mlir/Dialect/IRDL/IR/IRDL.h" +#include "mlir/Dialect/IRDL/IRDLRegistration.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/DialectImplementation.h" diff --git a/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp b/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp --- a/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp +++ b/mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp @@ -7,6 +7,10 @@ //===----------------------------------------------------------------------===// #include "mlir/Dialect/IRDL/IR/IRDL.h" +#include "mlir/Support/LogicalResult.h" +#include "mlir/Support/TypeID.h" +#include "llvm/ADT/SmallPtrSet.h" +#include using namespace mlir; using namespace mlir::irdl; diff --git a/mlir/lib/Dialect/IRDL/IRDLRegistration.cpp b/mlir/lib/Dialect/IRDL/IRDLRegistration.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/Dialect/IRDL/IRDLRegistration.cpp @@ -0,0 +1,133 @@ +//===- IRDLRegistration.cpp - IRDL dialect registration ----------- 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 registration of MLIR objects from IRDL operations. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Dialect/IRDL/IRDLRegistration.h" +#include "mlir/Dialect/IRDL/IR/IRDL.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/ExtensibleDialect.h" +#include "mlir/Support/LogicalResult.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/Support/SMLoc.h" + +using namespace mlir; +using namespace mlir::irdl; + +/// Define and register an operation represented by a `irdl.operation` +/// operation. +static WalkResult registerOperation(OperationOp op, + ExtensibleDialect *dialect) { + // 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 = [](Operation *op) { return success(); }; + + // IRDL does not support defining regions. + auto regionVerifier = [](Operation *op) { return 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(); +} + +/// Register all dialects in the given module, without registering any +/// operation, type or attribute definitions. +static DenseMap +registerEmptyDialects(ModuleOp op) { + DenseMap 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> +preallocateTypeDefs(ModuleOp op, + DenseMap dialects) { + DenseMap> typeDefs; + op.walk([&](TypeOp typeOp) { + ExtensibleDialect *dialect = dialects[typeOp.getDialectOp()]; + auto typeDef = DynamicTypeDefinition::get( + typeOp.getName(), dialect, + [](function_ref, ArrayRef) { + 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> +preallocateAttrDefs(ModuleOp op, + DenseMap dialects) { + DenseMap> attrDefs; + op.walk([&](AttributeOp attrOp) { + ExtensibleDialect *dialect = dialects[attrOp.getDialectOp()]; + auto attrDef = DynamicAttrDefinition::get( + attrOp.getName(), dialect, + [](function_ref, ArrayRef) { + return success(); + }); + attrDefs.try_emplace(attrOp, std::move(attrDef)); + }); + return attrDefs; +} + +LogicalResult mlir::irdl::registerDialects(ModuleOp op) { + // 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 dialects = registerEmptyDialects(op); + DenseMap> types = + preallocateTypeDefs(op, dialects); + DenseMap> attrs = + preallocateAttrDefs(op, dialects); + + // Define and register all operations. + WalkResult res = op.walk([&](OperationOp opOp) { + return registerOperation(opOp, dialects[opOp.getDialectOp()]); + }); + if (res.wasInterrupted()) + return failure(); + + // Register all types to their dialects. + for (auto &pair : types) { + ExtensibleDialect *dialect = dialects[pair.first.getDialectOp()]; + dialect->registerDynamicType(std::move(pair.second)); + } + + // Register all attributes to their dialects. + for (auto &pair : attrs) { + ExtensibleDialect *dialect = dialects[pair.first.getDialectOp()]; + dialect->registerDynamicAttr(std::move(pair.second)); + } + + return success(); +} diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp --- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp +++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp @@ -13,6 +13,8 @@ #include "mlir/Tools/mlir-opt/MlirOptMain.h" #include "mlir/Bytecode/BytecodeWriter.h" +#include "mlir/Dialect/IRDL/IR/IRDL.h" +#include "mlir/Dialect/IRDL/IRDLRegistration.h" #include "mlir/IR/AsmState.h" #include "mlir/IR/Attributes.h" #include "mlir/IR/BuiltinOps.h" @@ -104,6 +106,43 @@ return success(); } +LogicalResult registerIRDL(StringRef irdlFile, MLIRContext &ctx) { + DialectRegistry registry; + registry.insert(); + ctx.appendDialectRegistry(registry); + + // Set up the input file. + std::string errorMessage; + std::unique_ptr file = openInputFile(irdlFile, &errorMessage); + if (!file) { + emitError(UnknownLoc::get(&ctx)) << errorMessage; + return failure(); + } + + // Give the buffer to the source manager. + // This will be picked up by the parser. + SourceMgr sourceMgr; + sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc()); + + SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &ctx); + + // Disable multi-threading when parsing the input file. This removes the + // unnecessary/costly context synchronization when parsing. + // We also disable it during registration of the IRDL dialects. + bool wasThreadingEnabled = ctx.isMultithreadingEnabled(); + ctx.disableMultithreading(); + + // Parse the input file. + OwningOpRef module(parseSourceFile(sourceMgr, &ctx)); + + // Register IRDL dialects. + if (irdl::registerDialects(module.get()).failed()) + return failure(); + ctx.enableMultithreading(wasThreadingEnabled); + + return success(); +} + /// Parses the memory buffer. If successfully, run a series of passes against /// it and print the result. static LogicalResult @@ -112,7 +151,7 @@ bool allowUnregisteredDialects, bool preloadDialectsInContext, bool emitBytecode, bool implicitModule, PassPipelineFn passManagerSetupFn, DialectRegistry ®istry, - llvm::ThreadPool *threadPool) { + llvm::ThreadPool *threadPool, StringRef irdlFile) { // Tell sourceMgr about this buffer, which is what the parser will pick up. auto sourceMgr = std::make_shared(); sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc()); @@ -123,6 +162,11 @@ if (threadPool) context.setThreadPool(*threadPool); + if (!irdlFile.empty()) { + if (failed(registerIRDL(irdlFile, context))) + return failure(); + } + // Parse the input file. if (preloadDialectsInContext) context.loadAllAvailableDialects(); @@ -153,14 +197,12 @@ return sourceMgrHandler.verify(); } -LogicalResult mlir::MlirOptMain(raw_ostream &outputStream, - std::unique_ptr buffer, - PassPipelineFn passManagerSetupFn, - DialectRegistry ®istry, bool splitInputFile, - bool verifyDiagnostics, bool verifyPasses, - bool allowUnregisteredDialects, - bool preloadDialectsInContext, - bool emitBytecode, bool implicitModule) { +LogicalResult mlir::MlirOptMain( + raw_ostream &outputStream, std::unique_ptr buffer, + PassPipelineFn passManagerSetupFn, DialectRegistry ®istry, + bool splitInputFile, bool verifyDiagnostics, bool verifyPasses, + bool allowUnregisteredDialects, bool preloadDialectsInContext, + bool emitBytecode, bool implicitModule, StringRef irdlFile) { // The split-input-file mode is a very specific mode that slices the file // up into small pieces and checks each independently. // We use an explicit threadpool to avoid creating and joining/destroying @@ -180,18 +222,21 @@ return processBuffer(os, std::move(chunkBuffer), verifyDiagnostics, verifyPasses, allowUnregisteredDialects, preloadDialectsInContext, emitBytecode, implicitModule, - passManagerSetupFn, registry, threadPool); + passManagerSetupFn, registry, threadPool, irdlFile); }; return splitAndProcessBuffer(std::move(buffer), chunkFn, outputStream, splitInputFile, /*insertMarkerInOutput=*/true); } -LogicalResult mlir::MlirOptMain( - raw_ostream &outputStream, std::unique_ptr buffer, - const PassPipelineCLParser &passPipeline, DialectRegistry ®istry, - bool splitInputFile, bool verifyDiagnostics, bool verifyPasses, - bool allowUnregisteredDialects, bool preloadDialectsInContext, - bool emitBytecode, bool implicitModule, bool dumpPassPipeline) { +LogicalResult mlir::MlirOptMain(raw_ostream &outputStream, + std::unique_ptr buffer, + const PassPipelineCLParser &passPipeline, + DialectRegistry ®istry, bool splitInputFile, + bool verifyDiagnostics, bool verifyPasses, + bool allowUnregisteredDialects, + bool preloadDialectsInContext, + bool emitBytecode, bool implicitModule, + bool dumpPassPipeline, StringRef irdlFile) { auto passManagerSetupFn = [&](PassManager &pm) { auto errorHandler = [&](const Twine &msg) { emitError(UnknownLoc::get(pm.getContext())) << msg; @@ -208,7 +253,7 @@ return MlirOptMain(outputStream, std::move(buffer), passManagerSetupFn, registry, splitInputFile, verifyDiagnostics, verifyPasses, allowUnregisteredDialects, preloadDialectsInContext, - emitBytecode, implicitModule); + emitBytecode, implicitModule, irdlFile); } LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName, @@ -260,6 +305,9 @@ "dump-pass-pipeline", cl::desc("Print the pipeline that will be run"), cl::init(false)}; + static cl::opt irdlFile("irdl-file", cl::desc("IRDL file"), + cl::value_desc("filename")); + InitLLVM y(argc, argv); // Register any command line options. @@ -306,7 +354,7 @@ splitInputFile, verifyDiagnostics, verifyPasses, allowUnregisteredDialects, preloadDialectsInContext, emitBytecode, /*implicitModule=*/!noImplicitModule, - dumpPassPipeline))) + dumpPassPipeline, irdlFile))) return failure(); // Keep the output file if the invocation of MlirOptMain was successful. diff --git a/mlir/test/Dialect/IRDL/test-cmath.mlir b/mlir/test/Dialect/IRDL/test-cmath.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/IRDL/test-cmath.mlir @@ -0,0 +1,27 @@ +// RUN: mlir-opt %s --irdl-file=%S/cmath.irdl.mlir | mlir-opt --irdl-file=%S/cmath.irdl.mlir | FileCheck %s + +module { + // CHECK: func.func @conorm(%[[p:[^:]*]]: !cmath.complex, %[[q:[^:]*]]: !cmath.complex) -> f32 { + // CHECK: %[[norm_p:[^ ]*]] = "cmath.norm"(%[[p]]) : (!cmath.complex) -> f32 + // CHECK: %[[norm_q:[^ ]*]] = "cmath.norm"(%[[q]]) : (!cmath.complex) -> f32 + // CHECK: %[[pq:[^ ]*]] = arith.mulf %[[norm_p]], %[[norm_q]] : f32 + // CHECK: return %[[pq]] : f32 + // CHECK: } + func.func @conorm(%p: !cmath.complex, %q: !cmath.complex) -> f32 { + %norm_p = "cmath.norm"(%p) : (!cmath.complex) -> f32 + %norm_q = "cmath.norm"(%q) : (!cmath.complex) -> f32 + %pq = arith.mulf %norm_p, %norm_q : f32 + return %pq : f32 + } + + // CHECK: func.func @conorm2(%[[p:[^:]*]]: !cmath.complex, %[[q:[^:]*]]: !cmath.complex) -> f32 { + // CHECK: %[[pq:[^ ]*]] = "cmath.mul"(%[[p]], %[[q]]) : (!cmath.complex, !cmath.complex) -> !cmath.complex + // CHECK: %[[conorm:[^ ]*]] = "cmath.norm"(%[[pq]]) : (!cmath.complex) -> f32 + // CHECK: return %[[conorm]] : f32 + // CHECK: } + func.func @conorm2(%p: !cmath.complex, %q: !cmath.complex) -> f32 { + %pq = "cmath.mul"(%p, %q) : (!cmath.complex, !cmath.complex) -> !cmath.complex + %conorm = "cmath.norm"(%pq) : (!cmath.complex) -> f32 + return %conorm : f32 + } +} diff --git a/mlir/test/Dialect/IRDL/testd.mlir b/mlir/test/Dialect/IRDL/testd.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/IRDL/testd.mlir @@ -0,0 +1,108 @@ +// RUN: mlir-opt %s --irdl-file=%S/testd.irdl.mlir -split-input-file -verify-diagnostics | FileCheck %s + +//===----------------------------------------------------------------------===// +// Type or attribute constraint +//===----------------------------------------------------------------------===// + +func.func @typeFitsType() { + // CHECK: "testd.any"() : () -> !testd.parametric + "testd.any"() : () -> !testd.parametric + return +} + +// ----- + +func.func @attrDoesntFitType() { + "testd.any"() : () -> !testd.parametric<"foo"> + return +} + +// ----- + +func.func @attrFitsAttr() { + // CHECK: "testd.any"() : () -> !testd.attr_in_type_out<"foo"> + "testd.any"() : () -> !testd.attr_in_type_out<"foo"> + return +} + +// ----- + +func.func @typeFitsAttr() { + // CHECK: "testd.any"() : () -> !testd.attr_in_type_out + "testd.any"() : () -> !testd.attr_in_type_out + return +} + +// ----- + +//===----------------------------------------------------------------------===// +// Equality constraint +//===----------------------------------------------------------------------===// + +func.func @succeededEqConstraint() { + // CHECK: "testd.eq"() : () -> i32 + "testd.eq"() : () -> i32 + return +} + + +// ----- + +//===----------------------------------------------------------------------===// +// Any constraint +//===----------------------------------------------------------------------===// + +func.func @succeededAnyConstraint() { + // CHECK: "testd.any"() : () -> i32 + "testd.any"() : () -> i32 + // CHECK: "testd.any"() : () -> i64 + "testd.any"() : () -> i64 + return +} + +// ----- + +//===----------------------------------------------------------------------===// +// Dynamic base constraint +//===----------------------------------------------------------------------===// + +func.func @succeededDynBaseConstraint() { + // CHECK: "testd.dynbase"() : () -> !testd.parametric + "testd.dynbase"() : () -> !testd.parametric + // CHECK: "testd.dynbase"() : () -> !testd.parametric> + "testd.dynbase"() : () -> !testd.parametric> + return +} + + +// ----- + +//===----------------------------------------------------------------------===// +// Dynamic parameters constraint +//===----------------------------------------------------------------------===// + +func.func @succeededDynParamsConstraint() { + // CHECK: "testd.dynparams"() : () -> !testd.parametric + "testd.dynparams"() : () -> !testd.parametric + return +} + +// ----- + +//===----------------------------------------------------------------------===// +// Constraint variables +//===----------------------------------------------------------------------===// + +func.func @succeededConstraintVars() { + // CHECK: "testd.constraint_vars"() : () -> (i32, i32) + "testd.constraint_vars"() : () -> (i32, i32) + return +} + +// ----- + +func.func @succeededConstraintVars2() { + // CHECK: "testd.constraint_vars"() : () -> (i64, i64) + "testd.constraint_vars"() : () -> (i64, i64) + return +}