Skip to content

Commit

Permalink
[TOOLING] Implement $MLIR_DUMP_PATH to redirect $MLIR_ENABLE_DUMP (#5457
Browse files Browse the repository at this point in the history
)

## What
`$MLIR_DUMP_PATH` redirects the dump output to the specified filepath.

## Why
`$MLIR_ENABLE_DUMP` is incredibly useful when debugging passes, however
it's not the easiest to work with when various other logs are enabled.

<!---
The core Triton is a small number of people, and we receive many PRs
(thank
you!).  To help us review your code more quickly, **if you are a new
contributor (less than 3 PRs merged) we ask that you complete the
following
tasks and include the filled-out checklist in your PR description.**

Complete the following tasks before sending your PR, and replace `[ ]`
with
`[x]` to indicate you have done them.
-->

# New contributor declaration
- [x] I am not making a trivial change, such as fixing a typo in a
comment.

- [x] I have written a PR description following these
  [rules](https://cbea.ms/git-commit/#why-not-how).

- [x] I have run `pre-commit run --from-ref origin/main --to-ref HEAD`.

- Select one of the following.
  - [ ] I have added tests.
    - `/test` for `lit` tests
    - `/unittest` for C++ tests
    - `/python/test` for end-to-end tests
- [x] This PR does not need a test because `This is a debugging env
var`.

- Select one of the following.
  - [x] I have not added any `lit` tests.
- [ ] The `lit` tests I have added follow these [best
practices](https://mlir.llvm.org/getting_started/TestingGuide/#filecheck-best-practices),
including the "tests should be minimal" section. (Usually running Python
code
    and using the instructions it generates is not minimal.)
  • Loading branch information
danzimm authored Jan 7, 2025
1 parent 01aa5b2 commit 3d7d9e3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ For detailed instructions on how to debug Triton's frontend, please refer to thi
- `MLIR_ENABLE_DUMP=1` dumps the IR before every MLIR pass Triton runs, for all
kernels. Use `MLIR_ENABLE_DUMP=kernelName` to dump for a specific kernel only.
- Triton cache can interfere with the dump. In cases where `MLIR_ENABLE_DUMP=1` does not work, try cleaning your triton cache: `rm -r ~/.triton/cache/*`
- `MLIR_DUMP_PATH` specifies where `MLIR_ENABLE_DUMP` will dump to. If unset will dump to stderr.
- `LLVM_IR_ENABLE_DUMP=1` dumps the IR before every pass run over the LLVM IR.
- `TRITON_REPRODUCER_PATH=<reproducer_path>` will generate an MLIR reproducer file
at `<reproducer_path>` before each MLIR compiler stage. If any of the stages fail,
Expand Down
1 change: 1 addition & 0 deletions include/triton/Tools/Sys/GetEnv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ inline const std::set<std::string> CACHE_INVALIDATING_ENV_VARS = {
"LLVM_PASS_PLUGIN_PATH",
"MLIR_ENABLE_DIAGNOSTICS",
"MLIR_ENABLE_DUMP",
"MLIR_DUMP_PATH",
"MLIR_ENABLE_TIMING",
"TRITON_DEFAULT_FP_FUSION",
"TRITON_DISABLE_LINE_INFO",
Expand Down
19 changes: 18 additions & 1 deletion python/src/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "triton/Dialect/Triton/IR/Utility.h"
#include "triton/Dialect/TritonGPU/IR/Dialect.h"
#include "triton/Tools/Sys/GetEnv.hpp"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/SourceMgr.h"

#include "third_party/proton/dialect/include/Dialect/Proton/IR/Dialect.h"
Expand All @@ -39,6 +40,22 @@ namespace py = pybind11;
using namespace mlir;
using namespace triton;

llvm::raw_fd_ostream &mlir_dumps() {
std::error_code EC;
static llvm::raw_fd_ostream S(::triton::tools::getStrEnv("MLIR_DUMP_PATH"),
EC, llvm::sys::fs::CD_CreateAlways);
assert(!EC);
return S;
}

llvm::raw_ostream &mlir_dumps_or_dbgs() {
if (!::triton::tools::getStrEnv("MLIR_DUMP_PATH").empty()) {
return mlir_dumps();
} else {
return llvm::dbgs();
}
}

// A custom op builder that keeps track of the last location
class TritonOpBuilder {
public:
Expand Down Expand Up @@ -1711,7 +1728,7 @@ void init_triton_ir(py::module &&m) {
/*shouldPrintAfterPass=*/printAlways,
/*printModuleScope=*/true,
/*printAfterOnlyOnChange=*/false,
/*printAfterOnlyOnFailure*/ true, llvm::dbgs(),
/*printAfterOnlyOnFailure*/ true, mlir_dumps_or_dbgs(),
printingFlags);
}
})
Expand Down

0 comments on commit 3d7d9e3

Please sign in to comment.