Metadata-Version: 2.5
Name: irsmith
Version: 0.1.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 Agrawal <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.

<!-- 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/pr248058.ll](https://github.com/ambujone/irsmith/blob/main/demo/pr248058.ll) is the C test from [llvm/llvm-project#248058](https://github.com/llvm/llvm-project/issues/248058), an open LICM crash, compiled to IR at -O0. [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 pr248058.ll
                        SIGABRT, assertion at LoopPassManager.cpp:265, pass loop-mssa, sig:d48af5be
                        L->isRecursivelyLCSSAForm(LAR.DT, LI) && "Loops must remain in LCSSA form!"
reduce          273.0s  109 -> 38 lines (llvm-reduce, 1449 tests), same signature in 3 of 3 checks
diff_passes       0.2s  45 passes finished before the crash, the last was LoopSimplifyCFGPass on loop %do.body.outer
find_duplicates   7.8s  #248058 0.63 open    [LICM] Crash at -O2 and above: Assertion `L->isRecursivelyLCSSAForm(...)`
                        #156334 0.62 closed  Assertion `L->isRecursivelyLCSSAForm(LAR.DT, LI) && "Loops must remain...`
```

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. |
| `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.

## 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 for C and C++ inputs, 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.

## License

MIT or Apache-2.0, at your option.
