#include "Standalone/StandaloneDialect.h"
int main(int argc, char **argv) {
+ mlir::registerAllDialects();
mlir::registerAllPasses();
// TODO: Register standalone passes here.
}
int dumpMLIR() {
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
}
int dumpMLIR() {
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
}
int dumpMLIR() {
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
}
int dumpMLIR() {
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
}
int main(int argc, char **argv) {
+ mlir::registerAllDialects();
+
// Register any command line options.
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
}
int main(int argc, char **argv) {
+ mlir::registerAllDialects();
+
// Register any command line options.
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
// If we aren't dumping the AST, then we are compiling with/to MLIR.
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
}
int main(int argc, char **argv) {
+ mlir::registerAllDialects();
+
// Register any command line options.
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
// If we aren't dumping the AST, then we are compiling with/to MLIR.
- mlir::MLIRContext context;
+ mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
MapTy registry;
};
+/// Deprecated: this provides a global registry for convenience, while we're
+/// transitioning the registration mechanism to a stateless approach.
+DialectRegistry &getGlobalDialectRegistry();
+
+/// This controls globally whether the dialect registry is / isn't enabled.
+/// This is deprecated and only intended to help the transition. It'll be
+/// removed soon.
+void enableGlobalDialectRegistry(bool);
+bool isGlobalDialectRegistryEnabled();
+
+/// Registers all dialects from the global registries with the
+/// specified MLIRContext. This won't load the dialects in the context,
+/// but only make them available for lazy loading by name.
+/// Note: This method is not thread-safe.
+/// Deprecated: this method will be deleted soon.
+void registerAllDialects(MLIRContext *context);
+
+/// Register and return the dialect with the given namespace in the provided
+/// context. Returns nullptr is there is no constructor registered for this
+/// dialect.
+inline Dialect *registerDialect(StringRef name, MLIRContext *context) {
+ return getGlobalDialectRegistry().loadByName(name, context);
+}
+
+/// Utility to register a dialect. Client can register their dialect with the
+/// global registry by calling registerDialect<MyDialect>();
+/// Note: This method is not thread-safe.
+template <typename ConcreteDialect> void registerDialect() {
+ getGlobalDialectRegistry().insert<ConcreteDialect>();
+}
+
+/// DialectRegistration provides a global initializer that registers a Dialect
+/// allocation routine.
+///
+/// Usage:
+///
+/// // At namespace scope.
+/// static DialectRegistration<MyDialect> Unused;
+template <typename ConcreteDialect> struct DialectRegistration {
+ DialectRegistration() { registerDialect<ConcreteDialect>(); }
+};
+
} // namespace mlir
namespace llvm {
class Location;
class MLIRContextImpl;
class StorageUniquer;
+DialectRegistry &getGlobalDialectRegistry();
/// MLIRContext is the top-level object for a collection of MLIR modules. It
/// holds immortal uniqued objects like types, and the tables used to unique
/// The loadAllDialects parameters allows to load all dialects from the global
/// registry on Context construction. It is deprecated and will be removed
/// soon.
- explicit MLIRContext();
+ explicit MLIRContext(bool loadAllDialects = true);
~MLIRContext();
/// Return information about all IR dialects loaded in the context.
loadDialect<OtherDialect, MoreDialects...>();
}
+ /// Deprecated: load all globally registered dialects into this context.
+ /// This method will be removed soon, it can be used temporarily as we're
+ /// phasing out the global registry.
+ void loadAllGloballyRegisteredDialects();
+
/// Get (or create) a dialect for the given derived dialect name.
/// The dialect will be loaded from the registry if no dialect is found.
/// If no dialect is loaded for this name and none is available in the
// clang-format on
}
+// This function should be called before creating any MLIRContext if one expect
+// all the possible dialects to be made available to the context automatically.
+inline void registerAllDialects() {
+ static bool initOnce =
+ ([]() { registerAllDialects(getGlobalDialectRegistry()); }(), true);
+ (void)initOnce;
+}
} // namespace mlir
#endif // MLIR_INITALLDIALECTS_H_
/* ========================================================================== */
MlirContext mlirContextCreate() {
- auto *context = new MLIRContext;
+ auto *context = new MLIRContext(/*loadAllDialects=*/false);
return wrap(context);
}
if (failed(spirv::serialize(*spirvModules.begin(), binary, emitDebugInfo)))
return failure();
- MLIRContext deserializationContext;
+ MLIRContext deserializationContext(false);
context->getDialectRegistry().loadAll(&deserializationContext);
// Then deserialize to get back a SPIR-V module.
spirv::OwningSPIRVModuleRef spirvModule =
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Module.h"
#include "mlir/IR/StandardTypes.h"
-#include "mlir/InitAllDialects.h"
#include "mlir/Parser.h"
#include "mlir/Support/FileUtilities.h"
}
}
- MLIRContext context;
- registerAllDialects(context.getDialectRegistry());
+ MLIRContext context(/*loadAllDialects=*/false);
+ registerAllDialects(&context);
auto m = parseMLIRInput(options.inputFilename, &context);
if (!m) {
DialectAsmParser::~DialectAsmParser() {}
+//===----------------------------------------------------------------------===//
+// Dialect Registration (DEPRECATED)
+//===----------------------------------------------------------------------===//
+
+/// Registry for all dialect allocation functions.
+static llvm::ManagedStatic<DialectRegistry> dialectRegistry;
+DialectRegistry &mlir::getGlobalDialectRegistry() { return *dialectRegistry; }
+
+// Note: deprecated, will be removed soon.
+static bool isGlobalDialectRegistryEnabledFlag = false;
+void mlir::enableGlobalDialectRegistry(bool enable) {
+ isGlobalDialectRegistryEnabledFlag = enable;
+}
+bool mlir::isGlobalDialectRegistryEnabled() {
+ return isGlobalDialectRegistryEnabledFlag;
+}
+
+void mlir::registerAllDialects(MLIRContext *context) {
+ dialectRegistry->appendTo(context->getDialectRegistry());
+}
+
Dialect *DialectRegistry::loadByName(StringRef name, MLIRContext *context) {
auto it = registry.find(name.str());
if (it == registry.end())
};
} // end namespace mlir
-MLIRContext::MLIRContext() : impl(new MLIRContextImpl()) {
+MLIRContext::MLIRContext(bool loadAllDialects) : impl(new MLIRContextImpl()) {
// Initialize values based on the command line flags if they were provided.
if (clOptions.isConstructed()) {
disableMultithreading(clOptions->disableThreading);
printStackTraceOnDiagnostic(clOptions->printStackTraceOnDiagnostic);
}
- // Ensure the builtin dialect is always pre-loaded.
+ // Register dialects with this context.
getOrLoadDialect<BuiltinDialect>();
+ if (loadAllDialects)
+ loadAllGloballyRegisteredDialects();
// Initialize several common attributes and types to avoid the need to lock
// the context when accessing them.
return dialect.get();
}
+void MLIRContext::loadAllGloballyRegisteredDialects() {
+ if (!isGlobalDialectRegistryEnabled())
+ return;
+ getGlobalDialectRegistry().loadAll(this);
+}
+
bool MLIRContext::allowsUnregisteredDialects() {
return impl->allowUnregisteredDialects;
}
sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
// Parse the input file.
- MLIRContext context;
+ MLIRContext context(/*loadAllDialects=*/preloadDialectsInContext);
registry.appendTo(context.getDialectRegistry());
if (preloadDialectsInContext)
registry.loadAll(&context);
// Processes the memory buffer with a new MLIRContext.
auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
raw_ostream &os) {
- MLIRContext context;
+ MLIRContext context(false);
context.printOpOnDiagnostic(!verifyDiagnostics);
llvm::SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), llvm::SMLoc());
using namespace mlir::edsc::intrinsics;
static MLIRContext &globalContext() {
- static thread_local MLIRContext context;
+ static thread_local MLIRContext context(/*loadAllDialects=*/false);
static thread_local bool initOnce = [&]() {
// clang-format off
context.loadDialect<AffineDialect,
static MLIRContext *ctx() {
- static thread_local MLIRContext context;
+ static thread_local MLIRContext context(/*loadAllDialects=*/false);
static thread_local bool once =
(context.getOrLoadDialect<SDBMDialect>(), true);
(void)once;
#include "llvm/Support/TargetSelect.h"
int main(int argc, char **argv) {
+ mlir::registerAllDialects();
llvm::InitLLVM y(argc, argv);
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
int main(int argc, char **argv) {
registerPassManagerCLOptions();
+ mlir::registerAllDialects();
llvm::InitLLVM y(argc, argv);
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
if (testEmitIncludeTdHeader)
output->os() << "include \"mlir/Dialect/Linalg/IR/LinalgStructuredOps.td\"";
- MLIRContext context;
+ MLIRContext context(/*loadAllDialects=*/false);
llvm::SourceMgr mgr;
mgr.AddNewSourceBuffer(std::move(file), llvm::SMLoc());
Parser parser(mgr, &context);
#endif
int main(int argc, char **argv) {
+ registerAllDialects();
registerAllPasses();
#ifdef MLIR_INCLUDE_TESTS
registerTestPasses();
llvm::InitLLVM y(argc, argv);
+ registerAllDialects();
registerMLIRContextCLOptions();
registerPassManagerCLOptions();
int main(int argc, char **argv) {
registerPassManagerCLOptions();
+ mlir::registerAllDialects();
llvm::InitLLVM y(argc, argv);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::llvm_shutdown_obj x;
registerPassManagerCLOptions();
+ mlir::registerAllDialects();
llvm::InitLLVM y(argc, argv);
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
}
TEST(QuantizationUtilsTest, convertFloatAttrUniform) {
- MLIRContext ctx;
+ MLIRContext ctx(/*loadAllDialects=*/false);
ctx.getOrLoadDialect<QuantizationDialect>();
IntegerType convertedType = IntegerType::get(8, &ctx);
auto quantizedType = getTestQuantizedType(convertedType, &ctx);
}
TEST(QuantizationUtilsTest, convertRankedDenseAttrUniform) {
- MLIRContext ctx;
+ MLIRContext ctx(/*loadAllDialects=*/false);
ctx.getOrLoadDialect<QuantizationDialect>();
IntegerType convertedType = IntegerType::get(8, &ctx);
auto quantizedType = getTestQuantizedType(convertedType, &ctx);
}
TEST(QuantizationUtilsTest, convertRankedSplatAttrUniform) {
- MLIRContext ctx;
+ MLIRContext ctx(/*loadAllDialects=*/false);
ctx.getOrLoadDialect<QuantizationDialect>();
IntegerType convertedType = IntegerType::get(8, &ctx);
auto quantizedType = getTestQuantizedType(convertedType, &ctx);
}
TEST(QuantizationUtilsTest, convertRankedSparseAttrUniform) {
- MLIRContext ctx;
+ MLIRContext ctx(/*loadAllDialects=*/false);
ctx.getOrLoadDialect<QuantizationDialect>();
IntegerType convertedType = IntegerType::get(8, &ctx);
auto quantizedType = getTestQuantizedType(convertedType, &ctx);
using namespace mlir;
+/// Load the SPIRV dialect.
+static DialectRegistration<spirv::SPIRVDialect> SPIRVRegistration;
+
using ::testing::StrEq;
//===----------------------------------------------------------------------===//
/// diagnostic checking utilities.
class DeserializationTest : public ::testing::Test {
protected:
- DeserializationTest() {
+ DeserializationTest() : context(/*loadAllDialects=*/false) {
context.getOrLoadDialect<mlir::spirv::SPIRVDialect>();
// Register a diagnostic handler to capture the diagnostic so that we can
// check it later.
class SerializationTest : public ::testing::Test {
protected:
- SerializationTest() {
+ SerializationTest() : context(/*loadAllDialects=*/false) {
context.getOrLoadDialect<mlir::spirv::SPIRVDialect>();
createModuleOp();
}
namespace {
TEST(DenseSplatTest, BoolSplat) {
- MLIRContext context;
+ MLIRContext context(false);
IntegerType boolTy = IntegerType::get(1, &context);
RankedTensorType shape = RankedTensorType::get({2, 2}, boolTy);
TEST(DenseSplatTest, LargeBoolSplat) {
constexpr int64_t boolCount = 56;
- MLIRContext context;
+ MLIRContext context(false);
IntegerType boolTy = IntegerType::get(1, &context);
RankedTensorType shape = RankedTensorType::get({boolCount}, boolTy);
}
TEST(DenseSplatTest, BoolNonSplat) {
- MLIRContext context;
+ MLIRContext context(false);
IntegerType boolTy = IntegerType::get(1, &context);
RankedTensorType shape = RankedTensorType::get({6}, boolTy);
TEST(DenseSplatTest, OddIntSplat) {
// Test detecting a splat with an odd(non 8-bit) integer bitwidth.
- MLIRContext context;
+ MLIRContext context(false);
constexpr size_t intWidth = 19;
IntegerType intTy = IntegerType::get(intWidth, &context);
APInt value(intWidth, 10);
}
TEST(DenseSplatTest, Int32Splat) {
- MLIRContext context;
+ MLIRContext context(false);
IntegerType intTy = IntegerType::get(32, &context);
int value = 64;
}
TEST(DenseSplatTest, IntAttrSplat) {
- MLIRContext context;
+ MLIRContext context(false);
IntegerType intTy = IntegerType::get(85, &context);
Attribute value = IntegerAttr::get(intTy, 109);
}
TEST(DenseSplatTest, F32Splat) {
- MLIRContext context;
+ MLIRContext context(false);
FloatType floatTy = FloatType::getF32(&context);
float value = 10.0;
}
TEST(DenseSplatTest, F64Splat) {
- MLIRContext context;
+ MLIRContext context(false);
FloatType floatTy = FloatType::getF64(&context);
double value = 10.0;
}
TEST(DenseSplatTest, FloatAttrSplat) {
- MLIRContext context;
+ MLIRContext context(false);
FloatType floatTy = FloatType::getF32(&context);
Attribute value = FloatAttr::get(floatTy, 10.0);
}
TEST(DenseSplatTest, BF16Splat) {
- MLIRContext context;
+ MLIRContext context(false);
FloatType floatTy = FloatType::getBF16(&context);
Attribute value = FloatAttr::get(floatTy, 10.0);
}
TEST(DenseSplatTest, StringSplat) {
- MLIRContext context;
+ MLIRContext context(false);
Type stringType =
OpaqueType::get(Identifier::get("test", &context), "string", &context);
StringRef value = "test-string";
}
TEST(DenseSplatTest, StringAttrSplat) {
- MLIRContext context;
+ MLIRContext context(false);
Type stringType =
OpaqueType::get(Identifier::get("test", &context), "string", &context);
Attribute stringAttr = StringAttr::get("test-string", stringType);
}
TEST(DenseComplexTest, ComplexFloatSplat) {
- MLIRContext context;
+ MLIRContext context(false);
ComplexType complexType = ComplexType::get(FloatType::getF32(&context));
std::complex<float> value(10.0, 15.0);
testSplat(complexType, value);
}
TEST(DenseComplexTest, ComplexIntSplat) {
- MLIRContext context;
+ MLIRContext context(false);
ComplexType complexType = ComplexType::get(IntegerType::get(64, &context));
std::complex<int64_t> value(10, 15);
testSplat(complexType, value);
}
TEST(DenseComplexTest, ComplexAPFloatSplat) {
- MLIRContext context;
+ MLIRContext context(false);
ComplexType complexType = ComplexType::get(FloatType::getF32(&context));
std::complex<APFloat> value(APFloat(10.0f), APFloat(15.0f));
testSplat(complexType, value);
}
TEST(DenseComplexTest, ComplexAPIntSplat) {
- MLIRContext context;
+ MLIRContext context(false);
ComplexType complexType = ComplexType::get(IntegerType::get(64, &context));
std::complex<APInt> value(APInt(64, 10), APInt(64, 15));
testSplat(complexType, value);
};
TEST(DialectDeathTest, MultipleDialectsWithSameNamespace) {
- MLIRContext context;
+ MLIRContext context(false);
// Registering a dialect with the same namespace twice should result in a
// failure.
namespace {
TEST(OperandStorageTest, NonResizable) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
Operation *useOp =
}
TEST(OperandStorageTest, Resizable) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
Operation *useOp =
}
TEST(OperandStorageTest, RangeReplace) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
Operation *useOp =
}
TEST(OperandStorageTest, MutableRange) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
Operation *useOp =
}
TEST(OperationOrderTest, OrderIsAlwaysValid) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
Operation *containerOp =
};
TEST(AnalysisManagerTest, FineGrainModuleAnalysisPreservation) {
- MLIRContext context;
+ MLIRContext context(false);
// Test fine grain invalidation of the module analysis manager.
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
}
TEST(AnalysisManagerTest, FineGrainFunctionAnalysisPreservation) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
// Create a function and a module.
}
TEST(AnalysisManagerTest, FineGrainChildFunctionAnalysisPreservation) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
// Create a function and a module.
};
TEST(AnalysisManagerTest, CustomInvalidation) {
- MLIRContext context;
+ MLIRContext context(false);
Builder builder(&context);
// Create a function and a module.
static MLIRContext *ctx() {
- static thread_local MLIRContext context;
+ static thread_local MLIRContext context(false);
context.getOrLoadDialect<SDBMDialect>();
return &context;
}
//===----------------------------------------------------------------------===//
static MLIRContext &getContext() {
- static MLIRContext ctx;
+ static MLIRContext ctx(false);
ctx.getOrLoadDialect<TestDialect>();
return ctx;
}
/// Validates that test::TestStruct::classof correctly identifies a valid
/// test::TestStruct.
TEST(StructsGenTest, ClassofTrue) {
- mlir::MLIRContext context;
+ mlir::MLIRContext context(false);
auto structAttr = getTestStruct(&context);
ASSERT_TRUE(test::TestStruct::classof(structAttr));
}