[EZ] Add logic to build Metal shader with debug info (#146768)

By appending `-frecord-sources -gline-tables-only` to the compilation command

Helpful when debugging shaders compiled into libtorch

Test plan: Run
`python ../tools/build_with_debinfo.py ../aten/src/ATen/native/mps/kernels/UpSample.metal ../aten/src/ATen/native/mps/operations/UpSample.mm`
And then run following to capture shader and check that it contains debug info
```python
import torch
import os
os.environ["MTL_CAPTURE_ENABLED"]="1"
inp = torch.rand(size=(6, 3, 10, 20), device="mps", dtype=torch.float32)
with torch.mps.profiler.metal_capture("bilinear2d"):
    out = torch.nn.functional.interpolate(x, scale_factor=(1.7,0.9), mode="bilinear")
```
<img width="769" alt="image" src="https://github.com/user-attachments/assets/e0316c1c-07a4-4da5-97b9-886c56857c1d" />

Pull Request resolved: https://github.com/pytorch/pytorch/pull/146768
Approved by: https://github.com/dcci
This commit is contained in:
Nikita Shulga 2025-02-08 23:40:23 +00:00 committed by PyTorch MergeBot
parent 6a9a02acbe
commit 0e83e7d56e

View file

@ -78,6 +78,9 @@ def create_build_plan() -> list[tuple[str, str]]:
if line.startswith(": &&") and line.endswith("&& :"): if line.startswith(": &&") and line.endswith("&& :"):
line = line[4:-4] line = line[4:-4]
line = line.replace("-O2", "-g").replace("-O3", "-g") line = line.replace("-O2", "-g").replace("-O3", "-g")
# Build Metal shaders with debug infomation
if "xcrun metal " in line and "-frecord-sources" not in line:
line += " -frecord-sources -gline-tables-only"
try: try:
name = line.split("-o ", 1)[1].split(" ")[0] name = line.split("-o ", 1)[1].split(" ")[0]
rc.append((name, line)) rc.append((name, line))