diff --git a/mlir/include/mlir/IR/ExtensibleDialect.h b/mlir/include/mlir/IR/ExtensibleDialect.h --- a/mlir/include/mlir/IR/ExtensibleDialect.h +++ b/mlir/include/mlir/IR/ExtensibleDialect.h @@ -540,6 +540,21 @@ /// Owns the TypeID generated at runtime for operations. TypeIDAllocator typeIDAllocator; }; + +//===----------------------------------------------------------------------===// +// Dynamic dialect +//===----------------------------------------------------------------------===// + +/// A dialect that can be defined at runtime. +/// It can be extended with new operations, types, and attributes at runtime. +class DynamicDialect : public SelfOwningTypeID, public ExtensibleDialect { +public: + DynamicDialect(StringRef name, MLIRContext *ctx) + : SelfOwningTypeID(), + ExtensibleDialect(name, ctx, SelfOwningTypeID::getTypeID()) {} + + TypeID getTypeID() { return SelfOwningTypeID::getTypeID(); } +}; } // namespace mlir namespace llvm { diff --git a/mlir/include/mlir/IR/MLIRContext.h b/mlir/include/mlir/IR/MLIRContext.h --- a/mlir/include/mlir/IR/MLIRContext.h +++ b/mlir/include/mlir/IR/MLIRContext.h @@ -24,6 +24,7 @@ class DiagnosticEngine; class Dialect; class DialectRegistry; +class DynamicDialect; class InFlightDiagnostic; class Location; class MLIRContextImpl; @@ -110,6 +111,11 @@ loadDialect(); } + /// Create and load a new dialect that will be populated with operations, + /// types, and attributes at runtime. Abort if a dialect with a similar name + /// is already loaded. + DynamicDialect *createDynamicDialect(StringRef dialectNamespace); + /// Load all dialects available in the registry in this context. void loadAllAvailableDialects(); diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -18,6 +18,7 @@ #include "mlir/IR/BuiltinDialect.h" #include "mlir/IR/Diagnostics.h" #include "mlir/IR/Dialect.h" +#include "mlir/IR/ExtensibleDialect.h" #include "mlir/IR/IntegerSet.h" #include "mlir/IR/Location.h" #include "mlir/IR/OpImplementation.h" @@ -456,6 +457,18 @@ return dialect.get(); } +DynamicDialect *MLIRContext::createDynamicDialect(StringRef dialectNamespace) { + auto name = StringAttr::get(this, dialectNamespace); + auto *dialect = new DynamicDialect(name, this); + auto *loadedDialect = + getOrLoadDialect(name, dialect->getTypeID(), [&dialect]() { + return std::unique_ptr(dialect); + }); + // This is the same result as `getOrLoadDialect` (if it didn't failed), + // since it has the same TypeID, and TypeIDs are unique. + return dialect; +} + void MLIRContext::loadAllAvailableDialects() { for (StringRef name : getAvailableDialects()) getOrLoadDialect(name);