Marmot 用 MLIR 与 LLVM 重写 Go 编译器

原标题:Marmot, an Alternative Go Compiler
58AI 研判分

Marmot 是一个基于 MLIR 与 LLVM 的实验性 Go 编译器,保留 Go 代码与 go.mod 不变,仅替换代码生成环节,产出体积更小、运行更快的原生二进制,并给出可直接照做的报错提示。它复用 Go 工具链的类型检查器、SSA 构建器与标准库,通过自定义 go dialect 在 MLIR 中保留 Go 语义,从而消除边界检查、让对象留在栈上并直接调用接口方法。项目已能编译包括自身在内的真实 Go 程序,但作者明确表示尚不可用于生产环境。

关联知识实体

报道正文

本文目录5 个章节

Go, lowered to machine code

marmot is a Go compiler built on MLIR and LLVM. Keep your Go code and your go.mod, switch the compiler, and get small, fast native binaries and errors that show you the fix.

Get startedRead the benchmarksSee the progress

Experimental: marmot compiles real Go programs, including itself, and checks every test program against Go, but it is not ready for production.

  1. Go

Your code, unchanged.

GO
var s int32
for _, x := range xs {
    s += x
}
return s
  1. MLIR, with the go dialect

Still Go: an index into a slice, with its bounds check.

YAML
%v9 = llvm.extractvalue %a0[0] : !llvm.struct<(ptr, i64, i64)>
%v10 = llvm.extractvalue %a0[1] : !llvm.struct<(ptr, i64, i64)>
%v11 = go.index %v9[%v7] len %v10, i32 : i64
%v12 = llvm.load %v11 {alignment = 4 : i64, go.unsafe} : !llvm.ptr -> i32
%v13 = llvm.add %v1, %v12 : i32
  1. LLVM IR

The check is gone; four int32 values at a time.

文本
%wide.load = load <4 x i32>, ptr %5, align 4
%wide.load12 = load <4 x i32>, ptr %6, align 4
%wide.load13 = load <4 x i32>, ptr %7, align 4
%wide.load14 = load <4 x i32>, ptr %8, align 4
%9 = add <4 x i32> %wide.load, %vec.phi
  1. arm64 machine code

Two 128 bit loads, four vector adds.

文本
ldp q4, q5, [x8, #-0x20]
ldp q6, q7, [x8], #0x40
add.4s v0, v4, v0
add.4s v1, v5, v1
add.4s v2, v6, v2
add.4s v3, v7, v3

Against go build

Measured on an Apple M3 Pro, newest measurement of each. Every program’s output is checked against Go. Shorter bars are better.

MeasurementmarmotgoTo scale
hello world with println, ‑releasebinary size201 KB1.83 MB
hello world with fmt.Println, ‑releasebinary size698 KB2.43 MB
run time, 45 programs, ‑releasegeometric mean; 37 faster than Go, 8 slower1.54x faster1x
first build of cmd/marmot, empty caches325 packages, 2026-11-144.40 to 4.57 s6.14 to 6.18 s
rebuild, nothing changed325 packages, 2026-11-140.05 s0.15 s
one line edit, leaf package325 packages, 2026-11-140.59 s0.66 to 0.68 s
one line edit, deep package325 packages, 2026-11-140.72 to 0.76 s1.18 to 1.23 s
  • hello world with println, ‑releasebinary sizemarmot201 KBgo1.83 MB
  • hello world with fmt.Println, ‑releasebinary sizemarmot698 KBgo2.43 MB
  • run time, 45 programs, ‑releasegeometric mean; 37 faster than Go, 8 slowermarmot1.54x fastergo1x
  • first build of cmd/marmot, empty caches325 packages, 2026-11-14marmot4.40 to 4.57 sgo6.14 to 6.18 s
  • rebuild, nothing changed325 packages, 2026-11-14marmot0.05 sgo0.15 s
  • one line edit, leaf package325 packages, 2026-11-14marmot0.59 sgo0.66 to 0.68 s
  • one line edit, deep package325 packages, 2026-11-14marmot0.72 to 0.76 sgo1.18 to 1.23 s

Every number and how it was measured

Errors that show the fix

Marmots stand guard and whistle when something is wrong. marmot’s errors show the source, mark the exact span, label every place involved, and suggest a fix that marmot fix can apply.

Off a terminal they are the plain lines the go tool prints. Under a coding agent they are JSON, with the spans, the source around them and the fixes as edits. Every error has a stable code, and all 44 are explained.

shapes

marmot build: error M0014, Square does not implement Shape, with labels on the interface and on the method with a pointer receiver, and a fix that adds &

shapes

marmot build: error M0014, Square does not implement Shape, with labels on the interface and on the method with a pointer receiver, and a fix that adds &

One command for the whole loop

marmot works like the go command, and does what people usually install next to it.

buildCompile a program. Dev builds for fast rebuilds, -release for whole program optimization.runCompile and run a program.testRun package tests like go test, with each failure shown at its line.checkRun vet, lint, gopls, vuln, tidy, cross builds and more in one cached pass.fixApply the suggested fixes of errors and findings, and keep only what still builds.fmtFormat source and fix imports, as gofmt and goimports do.watchBuild, run or test again after every save.replEvaluate Go expressions and statements as you type.benchRun benchmarks against go test and report which changes are significant.profRead a pprof profile in the terminal, as a tree or line by line.sizeShow what a binary is made of, and why a function is in it.generateRun go directives, restoring cached outputs.explainDescribe an error code, with the mistake and the fix.doctorCheck the installation and say what to fix.mcpServe build, run, test, fix and explain to coding agents.

Go’s front end, MLIR and LLVM behind it

marmot keeps the parts of the Go toolchain that define the language: the type checker, the SSA builder and Go’s own standard library. It replaces the parts that generate code. A small go dialect keeps Go’s meaning in MLIR long enough to remove bounds checks, keep objects on the stack and call interface methods directly.