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