Metadata-Version: 2.5
Name: irsmith
Version: 0.3.0
Summary: MCP server that helps coding agents debug LLVM, MLIR and CIRCT crashes and pass pipelines
Project-URL: Homepage, https://github.com/ambujone/irsmith
Project-URL: Issues, https://github.com/ambujone/irsmith/issues
Project-URL: Source, https://github.com/ambujone/irsmith
Author-email: "Ambuj A." <ambujone@gmail.com>
License-Expression: MIT OR Apache-2.0
License-File: LICENSE-APACHE
License-File: LICENSE-MIT
Keywords: bisect,circt,compiler,crash,debugging,llvm,mcp,mcp-server,mlir,reduce
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Compilers
Classifier: Topic :: Software Development :: Debuggers
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: anyio>=4.5
Requires-Dist: mcp>=2.3.0
Description-Content-Type: text/markdown

# irsmith

irsmith is an MCP server that lets coding agents debug LLVM, MLIR and CIRCT tools using your own local builds. It runs a compiler command and reads the crash signature, shrinks a crashing input to a small reproducer, and shows which pass changed the IR and how. It can also start from the report that clang prints when it crashes.

<!-- mcp-name: io.github.ambujone/irsmith -->

Status: early (v0.1). Tested on macOS (Apple silicon) and Linux, with Python 3.11 and 3.13 in CI.

## Demo

[demo/pr248279.ll](https://github.com/ambujone/irsmith/blob/main/demo/pr248279.ll) is the C test from [llvm/llvm-project#248279](https://github.com/llvm/llvm-project/issues/248279), an open InstCombine crash that a maintainer confirmed, as clang -O2 hands it to the optimizer. [demo/run_demo.py](https://github.com/ambujone/irsmith/blob/main/demo/run_demo.py) drives irsmith over MCP stdio the way an agent would. One run, against an LLVM build with assertions from 2026-12-03, on an M4 Mac with other jobs running:

```
$ uv run python demo/run_demo.py --build $IRSMITH_LLVM_BUILD
run               0.1s  opt -passes=default<O2> -disable-output pr248279.ll
                        SIGABRT, assertion at Value.cpp:519, pass instcombine, sig:557d7f91
                        New->getType() == getType() && "replaceAllUses of value with new value of different type!"
reduce           40.0s  154 -> 53 lines (llvm-reduce, 1767 tests), same signature in 3 of 3 checks
diff_passes       0.1s  192 passes finished before the crash, the last was InferAlignmentPass on main
find_duplicates   7.3s  #173177 0.91 closed  [InstCombine] Assertion `New->getType() == getType() && "replaceAllUses...`
                        #248279 0.87 open    [InstCombine] Assertion `New->getType() == getType() && "replaceAllUses...`
```

Plain llvm-reduce with a grep test reached the same 53 lines in 16.7 s on that machine. The extra time is a Python check per candidate that the crash signature has not changed.

The full transcript, with the reduced file, is in [demo/README.md](https://github.com/ambujone/irsmith/blob/main/demo/README.md).

## Quickstart

```sh
claude mcp add irsmith -e IRSMITH_LLVM_BUILD=/path/to/llvm-project/build -- uvx irsmith
```

This needs [uv](https://docs.astral.sh/uv/getting-started/installation/). Ask the agent to call `doctor` first. It lists the toolchains and tools it found. For Claude Desktop, Cursor, VS Code, Codex CLI and Windsurf, see [docs/clients.md](https://github.com/ambujone/irsmith/blob/main/docs/clients.md). Without an LLVM build, irsmith uses Homebrew LLVM, with some limits that the same page lists.

To run from source instead:

```sh
git clone https://github.com/ambujone/irsmith && cd irsmith && uv sync
export IRSMITH_LLVM_BUILD=/path/to/llvm-project/build
claude mcp add irsmith -- uv --directory "$PWD" run irsmith
```

Any MCP client that can launch a stdio server works the same way: the command is `uv --directory /path/to/irsmith run irsmith`.

## Tools

| Tool | What it does |
|---|---|
| `doctor` | Lists toolchains (version, git revision, assertions on or off), optional tools (alive-tv, cvise, gh) and state dir usage. |
| `run` | Runs one command under time, memory and output limits. Returns the exit status, peak memory and a crash signature: kind, message, pass, function and top stack frames, with a stable id. |
| `diff_passes` | Runs a pipeline with `opt -print-changed`, or with `--mlir-print-ir-after-all --mlir-print-ir-after-change` for `mlir-opt` and `circt-opt`, and returns one diff hunk per pass that changed the IR, within a token budget. If the tool crashes partway, it returns the passes that finished and the crash signature. |
| `reduce` | Shrinks a crashing `.ll`, `.bc` or `.mir` file with llvm-reduce, or a `.mlir` file with circt-reduce. Each candidate must crash with the same signature as the original, so the reducer cannot drift to a different bug. Runs as a job. |
| `bisect` | Finds the commit that introduced a crash, or with `find: "fix"` the commit that fixed it. At each step it checks out the commit, runs `ninja -C <build> <targets>` in your build dir and runs the command. A failed build or an unclear result skips the commit. Runs as a job. |
| `from_clang_crash` | Starts from the run script that clang prints when it crashes. Reruns its `clang -cc1` command, returns the crash signature and whether the crash is in the frontend or in LLVM, and can reduce it. See [When clang crashes](https://github.com/ambujone/irsmith#when-clang-crashes). |
| `find_duplicates` | Searches GitHub issues for the same crash, given a signature id, a stderr handle or pasted text. Runs up to 3 searches (the assertion text, the top frame, the pass) and ranks the issues by how well they match. Uses `gh` when it is logged in, else the public API. Results are cached for 24 hours. |
| `job_status`, `job_cancel` | Follow or stop a `reduce` or `bisect` job. Calls wait at most 55 seconds, because many clients drop longer calls. |
| `read_artifact` | Reads part of a stored output (stderr, IR dumps, the reduced file) by handle, with offset, limit and grep. |

Commands are argv arrays, never shell strings. The first element must be a tool found in a configured toolchain, such as `opt` or `circt-opt`. Use `{input}`, `{out}` and `{tmp}` as placeholders.

## When clang crashes

When clang crashes, it prints the paths of a preprocessed source and a run script, such as `/tmp/foo-abc123.c` and `/tmp/foo-abc123.sh`. Pass the script, or the whole crash output, to `from_clang_crash`. It reruns the script's `clang -cc1` command, returns the crash signature and says where the crash is:

- `backend`: in the LLVM optimizer or code generator. irsmith emits IR with the same flags plus `-emit-llvm -disable-llvm-passes`, and checks that compiling that IR with the same flags and `-x ir` crashes the same way. llvm-reduce then reduces the IR.
- `frontend`: in parsing, Sema or IR generation. cvise reduces the preprocessed source, if it is installed.

irsmith reads the stage from the stack trace when clang prints one. Unless that shows a frontend crash, it also emits the IR: if emitting it already crashes the same way, the crash is in the frontend. Apple clang prints no stack trace, so there the IR check alone decides. With `reduce: true`, the call also starts the reduce job. Each candidate must crash with the same signature, and `find_duplicates` takes the signature id as usual.

On llvm/llvm-project#151623, a "Cannot select" error in the x86 backend, Apple clang 21's crash script was enough: irsmith placed the crash in the backend and reduced the 63-line IR to 12 lines around one SSE4.1 intrinsic call in 12 s.

irsmith runs the clang that the script names, which must be an absolute path to a file named `clang`, `clang++` or `clang-<version>`. With `toolchain` set, it runs that toolchain's clang instead.

## Configuration

Environment variables: `IRSMITH_LLVM_BUILD`, `IRSMITH_CIRCT_BUILD`, `IRSMITH_STATE_DIR` (default `~/.cache/irsmith`), `IRSMITH_ALIVE_TV` and `IRSMITH_CONFIG`. Without them, irsmith looks for Homebrew LLVM, `/usr/lib/llvm-*` and tools on `PATH`. When `gh` is not logged in, `GITHUB_TOKEN` raises the GitHub search rate limit.

The config file is `$IRSMITH_CONFIG`, else `.irsmith.toml` in the working directory or a parent, else `~/.config/irsmith/config.toml`:

```toml
[limits]
timeout_s = 120
memory_mb = 8192
disk_quota_mb = 4096
max_heavy_jobs = 2

[toolchains.llvm]
build_dir = "/path/to/llvm-project/build"

[toolchains.circt]
build_dir = "/path/to/circt/build"

[env]
# extra variables passed to every child
```

## Safety

- Every child runs in its own process group. Timeouts, cancellation and server shutdown kill the whole tree, including children that called `setsid`.
- Memory is limited by sampling the tree's RSS once a second, because macOS ignores memory rlimits.
- Children get an allowlisted environment (`PATH`, `HOME`, `LANG`, `CCACHE_*` and your `[env]` table). Tokens in the server's environment are not passed on.
- Output streams are capped at 64 MiB each. Each job has a disk quota, and the state dir is capped at 10 GiB, with the oldest finished jobs removed first.
- If the server dies, the next one kills the process groups it left behind.
- `bisect` refuses to start on a tree with uncommitted changes to tracked files, during a bisect, rebase or merge, or with a build that is not Ninja. It checks that the tree is still clean before each checkout and stops if you edited a file. At the end, including on cancel, it puts HEAD back on your branch. Only one bisect runs per repository. If the server dies mid-bisect, the next server restores the branch on startup.
- GitHub access is read-only. `gh` runs only `auth status` and `search issues`, and HTTP requests are GET requests to the issue search endpoint. `gh` is the only child that receives `GH_TOKEN` or `GITHUB_TOKEN`.

## Planned

Not done yet:

- `prove`: check a transformation with Alive2. The `reduce` oracle for miscompiles also needs it.
- cvise in `reduce` for C and C++ files that do not come from a clang crash report, and `.fir` input for circt-reduce.
- Testing on Linux.

## Development

```sh
uv run pytest
```

The integration tests use the builds in `IRSMITH_LLVM_BUILD` and `IRSMITH_CIRCT_BUILD` and are skipped when those are missing. The clang tests use the clang in `IRSMITH_LLVM_BUILD`, else the one on `PATH`.

## License

MIT or Apache-2.0, at your option.
