diff --git a/mlir/include/mlir/Support/ToolUtilities.h b/mlir/include/mlir/Support/ToolUtilities.h --- a/mlir/include/mlir/Support/ToolUtilities.h +++ b/mlir/include/mlir/Support/ToolUtilities.h @@ -32,10 +32,15 @@ /// all results to `os`. /// /// This is used to allow a large number of small independent tests to be put -/// into a single file. +/// into a single file. `enableSplitting` can be used to toggle if splitting +/// should be enabled, e.g. to allow for merging split and non-split code paths. +/// When `insertMarkerInOutput` is true, split markers (`//-----`) are placed +/// between each of the processed output chunks. LogicalResult splitAndProcessBuffer(std::unique_ptr originalBuffer, - ChunkBufferHandler processChunkBuffer, raw_ostream &os); + ChunkBufferHandler processChunkBuffer, raw_ostream &os, + bool enableSplitting = true, + bool insertMarkerInOutput = false); } // namespace mlir #endif // MLIR_SUPPORT_TOOLUTILITIES_H diff --git a/mlir/lib/Support/ToolUtilities.cpp b/mlir/lib/Support/ToolUtilities.cpp --- a/mlir/lib/Support/ToolUtilities.cpp +++ b/mlir/lib/Support/ToolUtilities.cpp @@ -21,7 +21,12 @@ LogicalResult mlir::splitAndProcessBuffer(std::unique_ptr originalBuffer, ChunkBufferHandler processChunkBuffer, - raw_ostream &os) { + raw_ostream &os, bool enableSplitting, + bool insertMarkerInOutput) { + // If splitting is disabled, we process the full input buffer. + if (!enableSplitting) + return processChunkBuffer(std::move(originalBuffer), os); + const char splitMarkerConst[] = "// -----"; StringRef splitMarker(splitMarkerConst); const int splitMarkerLen = splitMarker.size(); @@ -73,7 +78,7 @@ // Process each chunk in turn. bool hadFailure = false; - for (auto &subBuffer : sourceBuffers) { + auto interleaveFn = [&](StringRef subBuffer) { auto splitLoc = SMLoc::getFromPointer(subBuffer.data()); unsigned splitLine = fileSourceMgr.getLineAndColumn(splitLoc).first; auto subMemBuffer = llvm::MemoryBuffer::getMemBufferCopy( @@ -82,7 +87,9 @@ Twine(splitLine) + " offset "); if (failed(processChunkBuffer(std::move(subMemBuffer), os))) hadFailure = true; - } + }; + llvm::interleave(sourceBuffers, os, interleaveFn, + insertMarkerInOutput ? "\n// -----\n" : ""); // If any fails, then return a failure of the tool. return failure(hadFailure); diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp --- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp +++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp @@ -145,6 +145,7 @@ // We use an explicit threadpool to avoid creating and joining/destroying // threads for each of the split. ThreadPool *threadPool = nullptr; + // Create a temporary context for the sake of checking if // --mlir-disable-threading was passed on the command line. // We use the thread-pool this context is creating, and avoid @@ -153,23 +154,15 @@ if (threadPoolCtx.isMultithreadingEnabled()) threadPool = &threadPoolCtx.getThreadPool(); - if (splitInputFile) - return splitAndProcessBuffer( - std::move(buffer), - [&](std::unique_ptr chunkBuffer, raw_ostream &os) { - LogicalResult result = processBuffer( - os, std::move(chunkBuffer), verifyDiagnostics, verifyPasses, - allowUnregisteredDialects, preloadDialectsInContext, - passManagerSetupFn, registry, threadPool); - os << "// -----\n"; - return result; - }, - outputStream); - - return processBuffer(outputStream, std::move(buffer), verifyDiagnostics, - verifyPasses, allowUnregisteredDialects, - preloadDialectsInContext, passManagerSetupFn, registry, - threadPool); + auto chunkFn = [&](std::unique_ptr chunkBuffer, + raw_ostream &os) { + return processBuffer(os, std::move(chunkBuffer), verifyDiagnostics, + verifyPasses, allowUnregisteredDialects, + preloadDialectsInContext, passManagerSetupFn, registry, + threadPool); + }; + return splitAndProcessBuffer(std::move(buffer), chunkFn, outputStream, + splitInputFile, /*insertMarkerInOutput=*/true); } LogicalResult mlir::MlirOptMain(raw_ostream &outputStream, diff --git a/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp b/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp --- a/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp +++ b/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp @@ -98,13 +98,9 @@ return sourceMgrHandler.verify(); }; - if (splitInputFile) { - if (failed(splitAndProcessBuffer(std::move(input), processBuffer, - output->os()))) - return failure(); - } else if (failed(processBuffer(std::move(input), output->os()))) { + if (failed(splitAndProcessBuffer(std::move(input), processBuffer, + output->os(), splitInputFile))) return failure(); - } output->keep(); return success(); diff --git a/mlir/tools/mlir-pdll/mlir-pdll.cpp b/mlir/tools/mlir-pdll/mlir-pdll.cpp --- a/mlir/tools/mlir-pdll/mlir-pdll.cpp +++ b/mlir/tools/mlir-pdll/mlir-pdll.cpp @@ -186,13 +186,9 @@ return processBuffer(os, std::move(chunkBuffer), outputType, includeDirs, dumpODS, includedFiles); }; - if (splitInputFile) { - if (failed(splitAndProcessBuffer(std::move(inputFile), processFn, - outputStrOS))) - return 1; - } else if (failed(processFn(std::move(inputFile), outputStrOS))) { + if (failed(splitAndProcessBuffer(std::move(inputFile), processFn, outputStrOS, + splitInputFile))) return 1; - } // Write the output. bool shouldWriteOutput = true;