Skip to content

Commit a7ffd7d

Browse files
authored
Adding IR dumps for better developer experience (#206)
Co-authored-by: Renat Idrisov <[email protected]>
1 parent ebe3845 commit a7ffd7d

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,36 @@ pytest <path-to-triton-shared>/python/examples
176176
```
177177
In addition to testing on the tutorial kernels, there are many lit tests covering various scenarios.
178178

179+
## Intermediate Representation (IR) Dumps
180+
181+
To facilitate debugging and analysis, the triton-shared project now supports emitting all intermediate representations (IRs) generated during the compilation process. This functionality is controlled via the environment variable `TRITON_SHARED_DUMP_PATH`.
182+
183+
### How It Works
184+
185+
By setting the `TRITON_SHARED_DUMP_PATH` environment variable, you specify a directory where all intermediate representations will be saved. The Triton compiler will emit IR dumps at various stages of compilation into the specified folder, allowing developers to inspect and analyze the transformations applied to the code.
186+
187+
### How to Use
188+
189+
Create a directory where the IR dumps will be stored (e.g., /path/to/dump_dir).
190+
Set the `TRITON_SHARED_DUMP_PATH` environment variable to the directory path:
191+
`export TRITON_SHARED_DUMP_PATH=/path/to/dump_dir`
192+
Run your Triton compilation as usual. The compiler will emit IR dumps into the specified directory.
193+
194+
### Example
195+
196+
Suppose your dump directory is `/tmp/ir_dumps`. Before running your code, set the environment variable:
197+
198+
```sh
199+
export TRITON_SHARED_DUMP_PATH=/tmp/ir_dumps
200+
```
201+
202+
After the compilation process completes, you can explore the `/tmp/ir_dumps` directory to find all the intermediate representation files.
203+
204+
```sh
205+
$ ls /tmp/ir_dumps
206+
ll.ir ll.mlir tt.mlir ttshared.mlir
207+
```
208+
179209
## Contributing
180210

181211
This project welcomes contributions and suggestions. Most contributions require you to agree to a

backend/compiler.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tempfile
88
import os
99
import re
10+
import shutil
1011
import subprocess
1112
import functools
1213
from pathlib import Path
@@ -25,6 +26,14 @@ def _get_llvm_bin_path(bin_name: str) -> str:
2526
return os.path.join(path, bin_name)
2627

2728

29+
def _dump_ir_if_needed(files):
30+
path = os.getenv("TRITON_SHARED_DUMP_PATH", "")
31+
if not path:
32+
return
33+
for f in files:
34+
shutil.copy(f, os.path.join(path, os.path.basename(f)))
35+
36+
2837
def _ttir_to_ttsharedir(mod):
2938
# Get Triton-MLIR as string
3039
ttir_code = str(mod)
@@ -33,8 +42,8 @@ def _ttir_to_ttsharedir(mod):
3342
dst_path = os.path.join(tmpdir, "ttshared.mlir")
3443
Path(src_path).write_text(ttir_code)
3544
triton_shared_opt_path = _get_triton_shared_opt_path()
36-
subprocess.check_call([triton_shared_opt_path, src_path,
37-
"--triton-to-linalg-experimental", "--mlir-print-debuginfo", "-o", dst_path])
45+
subprocess.check_call([triton_shared_opt_path, src_path, "--triton-to-linalg-experimental", "--mlir-print-debuginfo", "-o", dst_path])
46+
_dump_ir_if_needed([src_path])
3847
return Path(dst_path).read_text()
3948

4049

@@ -91,6 +100,7 @@ def _ttsharedir_to_llir(ttsharedir: str):
91100
"--mlir-to-llvmir",
92101
"-o",
93102
llir_path])
103+
_dump_ir_if_needed([ttshared_path, llmlir_path, llir_path])
94104
return Path(llir_path).read_text()
95105

96106

0 commit comments

Comments
 (0)