Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 560cd8c

Browse files
committed
Add explicit dialect registration
The constructing MLIRContext no longer globally loads the registered dialects on construction. Thus dialects have to be loaded explicitly on demand, addressed by this commit. Upstream change was: llvm/llvm-project@f9dc2b7 https://reviews.llvm.org/D85622 Picked from cgo branch, ignoring MHLO dialect and EmitC conversion passes.
1 parent ccc8921 commit 560cd8c

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

‎include/emitc/InitDialect.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919

2020
namespace mlir {
2121

22+
// Add all the MLIR dialects to the provided registry.
23+
inline void registerEmitCDialect(DialectRegistry &registry) {
24+
registry.insert<emitc::EmitCDialect>();
25+
}
26+
2227
// This function should be called before creating any MLIRContext if one expect
2328
// all the possible dialects to be made available to the context automatically.
2429
inline void registerEmitCDialect() {
25-
static bool init_once = []() {
26-
registerDialect<emitc::EmitCDialect>();
27-
return true;
28-
}();
29-
(void)init_once;
30+
static bool initOnce =
31+
([]() { registerEmitCDialect(getGlobalDialectRegistry()); }(), true);
32+
(void)initOnce;
3033
}
3134
} // namespace mlir
3235

‎tools/emitc-opt/emitc-opt.cpp‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ static cl::opt<bool>
6868
cl::init(false));
6969

7070
int main(int argc, char **argv) {
71+
mlir::DialectRegistry registry;
7172
#ifdef IREE_BUILD_EMITC
72-
registerMlirDialects();
73+
registerMlirDialects(registry);
7374
registerMlirPasses();
7475
#else
75-
registerAllDialects();
76+
registerAllDialects(registry);
7677
registerAllPasses();
77-
#endif
78-
registerEmitCDialect();
78+
#endif // IREE_BUILD_EMITC
79+
registerEmitCDialect(registry);
80+
7981
InitLLVM y(argc, argv);
8082

8183
// Register any command line options.
@@ -88,11 +90,10 @@ int main(int argc, char **argv) {
8890
cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
8991

9092
if (showDialects) {
91-
llvm::outs() << "Registered Dialects:\n";
92-
MLIRContext context;
93-
for (Dialect *dialect : context.getRegisteredDialects()) {
94-
llvm::outs() << dialect->getNamespace() << "\n";
95-
}
93+
llvm::outs() << "Available Dialects:\n";
94+
interleave(
95+
registry, llvm::outs(),
96+
[](auto &registryEntry) { llvm::outs() << registryEntry.first; }, "\n");
9697
return 0;
9798
}
9899

@@ -110,9 +111,10 @@ int main(int argc, char **argv) {
110111
exit(1);
111112
}
112113

113-
if (failed(MlirOptMain(output->os(), std::move(file), passPipeline,
114+
if (failed(MlirOptMain(output->os(), std::move(file), passPipeline, registry,
114115
splitInputFile, verifyDiagnostics, verifyPasses,
115-
allowUnregisteredDialects))) {
116+
allowUnregisteredDialects,
117+
/*preloadDialectsInContext=*/false))) {
116118
return 1;
117119
}
118120
// Keep the output file if the invocation of MlirOptMain was successful.

‎tools/emitc-translate/emitc-translate.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ int main(int argc, char **argv) {
8888
// Processes the memory buffer with a new MLIRContext.
8989
auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
9090
raw_ostream &os) {
91-
MLIRContext context;
91+
MLIRContext context(false);
92+
registerAllDialects(&context);
9293
context.allowUnregisteredDialects();
9394
context.printOpOnDiagnostic(!verifyDiagnostics);
9495
llvm::SourceMgr sourceMgr;

0 commit comments

Comments
 (0)