|
32 | 32 | #include "llvm/Support/Regex.h"
|
33 | 33 | #include "llvm/Support/SourceMgr.h"
|
34 | 34 | #include "llvm/Support/StringSaver.h"
|
| 35 | +#include "llvm/Support/ThreadPool.h" |
35 | 36 | #include "llvm/Support/ToolOutputFile.h"
|
36 | 37 |
|
37 | 38 | using namespace mlir;
|
@@ -93,19 +94,22 @@ static LogicalResult performActions(raw_ostream &os, bool verifyDiagnostics,
|
93 | 94 |
|
94 | 95 | /// Parses the memory buffer. If successfully, run a series of passes against
|
95 | 96 | /// it and print the result.
|
96 |
| -static LogicalResult processBuffer(raw_ostream &os, |
97 |
| - std::unique_ptr<MemoryBuffer> ownedBuffer, |
98 |
| - bool verifyDiagnostics, bool verifyPasses, |
99 |
| - bool allowUnregisteredDialects, |
100 |
| - bool preloadDialectsInContext, |
101 |
| - const PassPipelineCLParser &passPipeline, |
102 |
| - DialectRegistry ®istry) { |
| 97 | +static LogicalResult |
| 98 | +processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer, |
| 99 | + bool verifyDiagnostics, bool verifyPasses, |
| 100 | + bool allowUnregisteredDialects, bool preloadDialectsInContext, |
| 101 | + const PassPipelineCLParser &passPipeline, |
| 102 | + DialectRegistry ®istry, llvm::ThreadPool &threadPool) { |
103 | 103 | // Tell sourceMgr about this buffer, which is what the parser will pick up.
|
104 | 104 | SourceMgr sourceMgr;
|
105 | 105 | sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
|
106 | 106 |
|
| 107 | + // Create a context just for the current buffer. Disable threading on creation |
| 108 | + // since we'll inject the thread-pool separately. |
| 109 | + MLIRContext context(registry, MLIRContext::Threading::DISABLED); |
| 110 | + context.setThreadPool(threadPool); |
| 111 | + |
107 | 112 | // Parse the input file.
|
108 |
| - MLIRContext context(registry); |
109 | 113 | if (preloadDialectsInContext)
|
110 | 114 | context.loadAllAvailableDialects();
|
111 | 115 | context.allowUnregisteredDialects(allowUnregisteredDialects);
|
@@ -143,20 +147,24 @@ LogicalResult mlir::MlirOptMain(raw_ostream &outputStream,
|
143 | 147 | bool preloadDialectsInContext) {
|
144 | 148 | // The split-input-file mode is a very specific mode that slices the file
|
145 | 149 | // up into small pieces and checks each independently.
|
| 150 | + // We use an explicit threadpool to avoid creating and joining/destroying |
| 151 | + // threads for each of the split. |
| 152 | + llvm::ThreadPool threadPool; |
146 | 153 | if (splitInputFile)
|
147 | 154 | return splitAndProcessBuffer(
|
148 | 155 | std::move(buffer),
|
149 | 156 | [&](std::unique_ptr<MemoryBuffer> chunkBuffer, raw_ostream &os) {
|
150 | 157 | return processBuffer(os, std::move(chunkBuffer), verifyDiagnostics,
|
151 | 158 | verifyPasses, allowUnregisteredDialects,
|
152 |
| - preloadDialectsInContext, passPipeline, |
153 |
| - registry); |
| 159 | + preloadDialectsInContext, passPipeline, registry, |
| 160 | + threadPool); |
154 | 161 | },
|
155 | 162 | outputStream);
|
156 | 163 |
|
157 | 164 | return processBuffer(outputStream, std::move(buffer), verifyDiagnostics,
|
158 | 165 | verifyPasses, allowUnregisteredDialects,
|
159 |
| - preloadDialectsInContext, passPipeline, registry); |
| 166 | + preloadDialectsInContext, passPipeline, registry, |
| 167 | + threadPool); |
160 | 168 | }
|
161 | 169 |
|
162 | 170 | LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
|
|
0 commit comments