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 @@ -147,6 +147,13 @@ /// this call in this case. void setThreadPool(llvm::ThreadPool &pool); + /// Return the number of threads used by the thread pool in this context. The + /// number of computed hardware threads can change over the lifetime of a + /// process based on affinity changes, so users should use the number of + /// threads actually in the thread pool for dispatching work. Returns 1 if + /// multithreading is disabled. + unsigned getNumThreads(); + /// Return the thread pool used by this context. This method requires that /// multithreading be enabled within the context, and should generally not be /// used directly. Users should instead prefer the threading utilities within 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 @@ -518,6 +518,13 @@ enableMultithreading(); } +unsigned MLIRContext::getNumThreads() { + if (isMultithreadingEnabled() && impl->threadPool) + return impl->threadPool->getThreadCount(); + // No multithreading or active thread pool. Return 1 thread. + return 1; +} + llvm::ThreadPool &MLIRContext::getThreadPool() { assert(isMultithreadingEnabled() && "expected multi-threading to be enabled within the context"); diff --git a/mlir/lib/Transforms/Inliner.cpp b/mlir/lib/Transforms/Inliner.cpp --- a/mlir/lib/Transforms/Inliner.cpp +++ b/mlir/lib/Transforms/Inliner.cpp @@ -678,7 +678,7 @@ // parallel. Note: The number of pass managers here needs to remain constant // to prevent issues with pass instrumentations that rely on having the same // pass manager for the main thread. - size_t numThreads = llvm::hardware_concurrency().compute_thread_count(); + size_t numThreads = ctx->getNumThreads(); if (opPipelines.size() < numThreads) { // Reserve before resizing so that we can use a reference to the first // element. diff --git a/mlir/test/Transforms/inlining.mlir b/mlir/test/Transforms/inlining.mlir --- a/mlir/test/Transforms/inlining.mlir +++ b/mlir/test/Transforms/inlining.mlir @@ -1,4 +1,5 @@ // RUN: mlir-opt %s -inline='default-pipeline=''' | FileCheck %s +// RUN: mlir-opt %s --mlir-disable-threading -inline='default-pipeline=''' | FileCheck %s // RUN: mlir-opt %s -inline='default-pipeline=''' -mlir-print-debuginfo -mlir-print-local-scope | FileCheck %s --check-prefix INLINE-LOC // RUN: mlir-opt %s -inline | FileCheck %s --check-prefix INLINE_SIMPLIFY