|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "PassDetail.h" |
| 10 | +#include "mlir/IR/AsmState.h" |
10 | 11 | #include "mlir/IR/SymbolTable.h" |
11 | 12 | #include "mlir/Pass/PassManager.h" |
12 | 13 | #include "mlir/Support/FileUtilities.h" |
@@ -345,6 +346,67 @@ struct FileTreeIRPrinterConfig : public PassManager::IRPrinterConfig { |
345 | 346 | llvm::DenseMap<Operation *, unsigned> counters; |
346 | 347 | }; |
347 | 348 |
|
| 349 | +/// A pass instrumenation to dump the IR before each pass into |
| 350 | +/// numbered files. |
| 351 | +/// It includes a mlir_reproducer info to rerun the pass. |
| 352 | +class ReproducerBeforeAll : public PassInstrumentation { |
| 353 | +public: |
| 354 | + ReproducerBeforeAll(mlir::StringRef outputDir) : outputDir(outputDir) {} |
| 355 | + void runBeforePass(Pass *pass, Operation *op) override; |
| 356 | + |
| 357 | + std::string outputDir; |
| 358 | + |
| 359 | + uint32_t counter = 0; |
| 360 | +}; |
| 361 | + |
| 362 | +void ReproducerBeforeAll::runBeforePass(Pass *pass, Operation *op) { |
| 363 | + // Skip adator passes (which adopt FuncOp passes to ModuleOp pass managers). |
| 364 | + if (isa<OpToOpPassAdaptor>(pass)) |
| 365 | + return; |
| 366 | + |
| 367 | + llvm::SmallString<128> path(outputDir); |
| 368 | + if (failed(createDirectoryOrPrintErr(path))) |
| 369 | + return; |
| 370 | + |
| 371 | + // Open output file. |
| 372 | + std::string fileName = |
| 373 | + llvm::formatv("{0,0+2}_{1}.mlir", counter++, pass->getArgument()); |
| 374 | + llvm::sys::path::append(path, fileName); |
| 375 | + |
| 376 | + std::string error; |
| 377 | + std::unique_ptr<llvm::ToolOutputFile> file = openOutputFile(path, &error); |
| 378 | + if (!file) { |
| 379 | + llvm::errs() << "Error opening output file " << path << ": " << error |
| 380 | + << "\n"; |
| 381 | + return; |
| 382 | + } |
| 383 | + |
| 384 | + SmallVector<OperationName> scopes; |
| 385 | + scopes.push_back(op->getName()); |
| 386 | + while (Operation *parentOp = op->getParentOp()) { |
| 387 | + scopes.push_back(parentOp->getName()); |
| 388 | + op = parentOp; |
| 389 | + } |
| 390 | + |
| 391 | + std::string pipelineStr; |
| 392 | + llvm::raw_string_ostream passOS(pipelineStr); |
| 393 | + // Add pass scopes like 'builtin.module(emitc.tu(' |
| 394 | + for (OperationName scope : llvm::reverse(scopes)) |
| 395 | + passOS << scope << "("; |
| 396 | + pass->printAsTextualPipeline(passOS); |
| 397 | + for (unsigned i = 0, e = scopes.size(); i < e; ++i) |
| 398 | + passOS << ")"; |
| 399 | + |
| 400 | + AsmState state(op); |
| 401 | + state.attachResourcePrinter("mlir_reproducer", |
| 402 | + [&](Operation *op, AsmResourceBuilder &builder) { |
| 403 | + builder.buildString("pipeline", pipelineStr); |
| 404 | + builder.buildBool("disable_threading", true); |
| 405 | + builder.buildBool("verify_each", true); |
| 406 | + }); |
| 407 | + op->print(file->os(), state); |
| 408 | + file->keep(); |
| 409 | +} |
348 | 410 | } // namespace |
349 | 411 |
|
350 | 412 | /// Add an instrumentation to print the IR before and after pass execution, |
@@ -383,3 +445,11 @@ void PassManager::enableIRPrintingToFileTree( |
383 | 445 | printModuleScope, printAfterOnlyOnChange, printAfterOnlyOnFailure, |
384 | 446 | opPrintingFlags, printTreeDir)); |
385 | 447 | } |
| 448 | + |
| 449 | +/// Add an instrumentation to print the IR before and after pass execution. |
| 450 | +void PassManager::enableReproducerBeforeAll(StringRef outputDir) { |
| 451 | + if (getContext()->isMultithreadingEnabled()) |
| 452 | + llvm::report_fatal_error("IR printing can't be setup on a pass-manager " |
| 453 | + "without disabling multi-threading first."); |
| 454 | + addInstrumentation(std::make_unique<ReproducerBeforeAll>(outputDir)); |
| 455 | +} |
0 commit comments