Skip to content
ambujonePublic

About

An MCP server that helps coding agents debug LLVM, clang, MLIR and CIRCT crashes: run, shrink to a minimal reproducer, bisect to the commit and find duplicate issues.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

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 checks with Alive2 that a transformation is correct, and can also start from the report that clang prints when it crashes.

Status: early (0.4.0). Used with real LLVM builds on macOS (Apple silicon). CI runs the tests on macOS and Linux with Python 3.11, 3.13 and 3.14.

Demo

demo/pr248279.ll is the C test from llvm/llvm-project#248279, an open InstCombine crash that a maintainer confirmed, as clang -O2 hands it to the optimizer. demo/run_demo.py drives irsmith over MCP stdio the way an agent would. A summary of one run, against an LLVM build with assertions from 2026-12-03, on an M4 Mac with other jobs running:

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.

Quickstart

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

This needs uv. 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. Without an LLVM build, irsmith uses Homebrew LLVM, with some limits that the same page lists.

To run from source instead:

git clone https://github.com/ambujone/irsmith && cd irsmith && uv sync
claude mcp add irsmith -e IRSMITH_LLVM_BUILD=/path/to/llvm-project/build -- 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.
prove Checks with Alive2's alive-tv that a transformation is correct. Takes the IR before and after, or the IR and an opt pipeline that irsmith runs with your opt. Returns correct, incorrect, unknown or error, and for a miscompile the function, the reason and a counterexample: the inputs and the source and target values.
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. With oracle: {"kind": "alive2"} it shrinks a miscompile instead: alive-tv must still find a counterexample after the opt command, which must not have -o, runs on each candidate. 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.
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.

Checking a transformation with Alive2

prove runs Alive2's alive-tv on the IR before and after a transformation. Give it two files, one file and an opt pipeline, or one file with functions named src and tgt. For this file, where @tgt adds nsw:

define i32 @src(i32 %x) {
  %r = add i32 %x, 1
  ret i32 %r
}

define i32 @tgt(i32 %x) {
  %r = add nsw i32 %x, 1
  ret i32 %r
}

it returns verdict incorrect, reason Target is more poisonous than source and the input i32 %x = #x7fffffff (2147483647), for which the source returns #x80000000 and the target returns poison. timeout_s limits the whole run, and smt_timeout_ms and smt_memory_mb are passed to alive-tv. An SMT timeout gives verdict unknown.

On llvm/llvm-project#248754, where InstCombine puts poison into unused lanes of a noundef vector argument, the reporter's 9-line IR and the pipeline instcombine were enough: prove returned verdict incorrect in 0.2 s, named the function f and the reason Source is more defined than target, and showed the target ending in UB triggered.

To get alive-tv, build LLVM with -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON, then build Alive2 against it with -DBUILD_TV=1 as its README shows. Set IRSMITH_ALIVE_TV to the alive-tv binary.

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:

[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:

  • cvise in reduce for C and C++ files that do not come from a clang crash report, and .fir input for circt-reduce.
  • Testing against real LLVM builds on Linux.

Development

uv run pytest

The integration tests use the builds in IRSMITH_LLVM_BUILD and IRSMITH_CIRCT_BUILD and the alive-tv in IRSMITH_ALIVE_TV, 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.

About

An MCP server that helps coding agents debug LLVM, clang, MLIR and CIRCT crashes: run, shrink to a minimal reproducer, bisect to the commit and find duplicate issues.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages