diff --git a/mlir/include/mlir/IR/MLIRContext.h b/mlir/include/mlir/IR/MLIRContext.h index 40b332698c44..da0b0bd826ce 100644 --- a/mlir/include/mlir/IR/MLIRContext.h +++ b/mlir/include/mlir/IR/MLIRContext.h @@ -60,6 +60,9 @@ public: /// Set the flag specifying if multi-threading is disabled by the context. void disableMultithreading(bool disable = true); + void enableMultithreading(bool enable = true) { + disableMultithreading(!enable); + } /// Return true if we should attach the operation to diagnostics emitted via /// Operation::emit. diff --git a/mlir/lib/Support/MlirOptMain.cpp b/mlir/lib/Support/MlirOptMain.cpp index 5c21e19c4bd2..25e197083b62 100644 --- a/mlir/lib/Support/MlirOptMain.cpp +++ b/mlir/lib/Support/MlirOptMain.cpp @@ -40,7 +40,14 @@ static LogicalResult performActions(raw_ostream &os, bool verifyDiagnostics, bool verifyPasses, SourceMgr &sourceMgr, MLIRContext *context, const PassPipelineCLParser &passPipeline) { + // Disable multi-threading when parsing the input file. This removes the + // unnecessary/costly context synchronization when parsing. + bool wasThreadingEnabled = context->isMultithreadingEnabled(); + context->disableMultithreading(); + + // Parse the input file and reset the context threading state. OwningModuleRef module(parseSourceFile(sourceMgr, context)); + context->enableMultithreading(wasThreadingEnabled); if (!module) return failure(); diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp index d9c822169932..c5cc533ab119 100644 --- a/mlir/tools/mlir-opt/mlir-opt.cpp +++ b/mlir/tools/mlir-opt/mlir-opt.cpp @@ -150,9 +150,9 @@ int main(int argc, char **argv) { // Parse pass names in main to ensure static initialization completed. cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); - MLIRContext context; if(showDialects) { llvm::outs() << "Registered Dialects:\n"; + MLIRContext context; for(Dialect *dialect : context.getRegisteredDialects()) { llvm::outs() << dialect->getNamespace() << "\n"; }