Source Reading 012 · ROCm/FlyDSL

270 Fences

Putting the compiler back in the FlyDSL kernel loop: how one in-process LLVM compiles MI355X and MI455X kernels, why 62 of 180 kernel files override it by hand, which layer of the compiler chain fixes each break in the optimization loop, and how a CAKE-style outer loop could turn every override into a compiler feature.

让编译器回到 FlyDSL 的 kernel 优化循环: 一套进程内 LLVM 怎样编译 MI355X 和 MI455X 的 kernel, 为什么 180 个 kernel 文件里有 62 个在手工覆盖它, compiler chain 的哪一层能修好优化循环里的每一处断点, 以及 CAKE 式的外层循环怎样把每一处手工覆盖变成编译器能力。

RepoROCm/FlyDSL
Snapshotmain @ aae155359ddc9b2bb1064ec8d391bc25aa4c0d3e
LLVMllvm-project 941a04e + 9 patches
Date2026-11-16

01The short version先给结论

"Think about the compiler chain" is good advice that comes without a next step. A kernel engineer knows how to change a kernel. What would it mean to change the compiler: which part, how, and how would anyone know it helped?

「多想想 compiler chain」 是一句好建议, 但它没有告诉你下一步做什么。 kernel 工程师知道怎么改 kernel。 那么改编译器是什么意思: 改哪一块、 怎么改, 又怎么知道改了有用?

Start from what sets a kernel's speed. Once the algorithm and the tiling are fixed, performance comes down to a few decisions: which instructions run, in what order, in which registers, and where each wave stops to wait for memory. Each of these decisions is made by someone, either the kernel author or the compiler. The compiler chain matters exactly where it makes a decision badly, or makes it in a way nobody can see. This reading finds those places in FlyDSL, the Python kernel DSL that AMD uses for MI355X and MI455X kernels, and asks how the compiler could improve together with the kernels instead of being worked around.

先从决定 kernel 速度的因素说起。 算法和分块确定之后, 性能取决于少数几个决定: 执行哪些指令、 按什么顺序、 放在哪些寄存器里, 以及每个 wave 在哪里停下来等内存。 每个决定都有人负责, 要么是 kernel 作者, 要么是编译器。 compiler chain 真正重要的地方, 正是它做错决定、 或者做了决定却没人看得见的地方。 本文在 FlyDSL(AMD 用来写 MI355X 和 MI455X kernel 的 Python DSL) 里找出这些地方, 并讨论怎样让编译器跟着 kernel 一起进步, 而不是被绕开。

62of 180 kernel files override the compiler by hand180 个 kernel 文件中手工覆盖编译器的文件数
270full scheduling fences, sched_barrier(0)完整的调度屏障 sched_barrier(0)
9LLVM patches FlyDSL already carriesFlyDSL 已经在维护的 LLVM 补丁数

The short version. FlyDSL owns every layer of its compiler, and its kernels override the backend's scheduling, register and wait decisions in about a third of all kernel files. The most useful compiler work removes the need for those overrides, one recurring pattern at a time, and accepts a change only if every kernel agrees.

简短的结论。 FlyDSL 掌握自己编译器的每一层, 而它的 kernel 在大约三分之一的文件里手工覆盖了后端的调度、 寄存器和等待决定。 最有价值的编译器工作, 是一次消除一种反复出现的手工覆盖, 并且只在所有 kernel 都认可时才接受改动。

All FlyDSL references are pinned to commit aae1553 (16 November 2026). For the DSL and its layout algebra, see Source Reading 006.

文中所有 FlyDSL 引用都固定在 commit aae1553(2026 年 11 月 16 日)。 DSL 本身和它的 layout algebra 见 Source Reading 006。

02One toolchain, and FlyDSL owns it一套工具链, 归 FlyDSL 自己所有

FlyDSL does not call hipcc, amdclang, comgr or hipRTC. It compiles inside the Python process with its own MLIR and LLVM: upstream llvm-project pinned at 941a04e (LLVM 24 development), plus nine FlyDSL patches (thirdparty/llvm-build-info.json). The pipeline lowers the Fly dialect through FlyDSL's own passes, converts the GPU module to ROCDL, and then lets LLVM optimize at -O2, generate AMDGPU code and assemble it (backends/rocm.py:323). The object becomes an HSA code object through an LLD linked into the same library since #987, or through ROCm's ld.lld when that LLD is not built in. ROCm contributes only the device bitcode libraries, the fallback linker and the HIP runtime that loads the result.

FlyDSL 不调用 hipcc、 amdclang、 comgr 或 hipRTC。 它在 Python 进程里用自带的 MLIR 和 LLVM 编译: upstream llvm-project 固定在 941a04e(LLVM 24 开发版), 外加 9 个 FlyDSL 补丁(thirdparty/llvm-build-info.json)。 pipeline 先用 FlyDSL 自己的 pass 降低 Fly dialect, 再把 GPU module 转成 ROCDL, 然后由 LLVM 做 -O2 优化、 生成 AMDGPU 代码并汇编(backends/rocm.py:323)。 从 #987 起, object 由链接进同一个库的 LLD 变成 HSA code object; 构建时没有带 LLD 的话, 就退回 ROCm 的 ld.lld。 ROCm 只提供 device bitcode 库、 备用链接器, 以及加载结果的 HIP runtime。

MI355X (gfx950) and MI455X (gfx1250, the MI450 series) take the same path. The target attribute changes the chip name and, for gfx1250, marks the target as wave32 (backends/rocm.py:425). The kernels themselves use different instruction families: MFMA on gfx950, WMMA and the tensor data mover on gfx1250 (arch-gfx1250.md). gfx1250 also leans harder on the patches: six of the nine carry gfx1250-specific changes, including a backport of the gfx1250-strict target that upstream LLVM added after FlyDSL's pin (llvm-project #218543).

MI355X(gfx950) 和 MI455X(gfx1250, 即 MI450 系列) 走同一条路径。 target 属性只改变 chip 名称, 并把 gfx1250 标记为 wave32(backends/rocm.py:425)。 kernel 本身用的指令族不同: gfx950 上是 MFMA, gfx1250 上是 WMMA 和 tensor data mover(arch-gfx1250.md)。 gfx1250 也更依赖这些补丁: 9 个里有 6 个包含 gfx1250 专用的改动, 其中包括把 upstream LLVM 在 FlyDSL pin 之后才加入的 gfx1250-strict target 移植回来(llvm-project #218543)。

Plate IWho decides what谁做哪个决定
The FlyDSL compile chain and the decision each stage owns Python kernel Fly MLIR passes LLVM IR, -O2 AMDGPU backend AMDGPU 后端 MC assembler + LLD MC 汇编器 + LLD HIP runtime @flyc.kernel source @flyc.kernel 源码 FlyDSL's own C++ passes FlyDSL 自己的 C++ pass upstream LLVM middle end upstream LLVM 中端 upstream + 9 FlyDSL patches upstream + 9 个 FlyDSL 补丁 in process since #987 #987 起在进程内 hipModuleLoadData decides: algorithm, tiling, instruction family 决定: 算法、 分块、 指令族 decides: layout to address math, swizzles 决定: 把 layout 变成地址计算、 swizzle decides: unrolling, LICM, CSE, inlining 决定: 循环展开、 LICM、 CSE、 内联 decides: instruction order, registers, waits 决定: 指令顺序、 寄存器、 等待点 decides: encoding and ELF only; ISA unchanged 决定: 只有编码和 ELF, 不改 ISA loads the code object and launches it 加载 code object 并启动 owner: kernel author 负责方: kernel 作者 owner: FlyDSL, no LLVM rebuild needed 负责方: FlyDSL, 不用重编 LLVM owner: upstream LLVM, steered by flags 负责方: upstream LLVM, 只能用开关引导 owner: LLVM plus FlyDSL patches 负责方: LLVM 加 FlyDSL 补丁 overridden by hand in 62 of 180 kernel files 180 个 kernel 文件中有 62 个在手工覆盖 owner: same LLVM, or ROCm ld.lld 负责方: 同一个 LLVM, 或 ROCm ld.lld owner: ROCm 负责方: ROCm IN ONE PROCESS · FLYDSL MLIR + LLVM 同一进程 · FlyDSL 自带 MLIR + LLVM MI355X (gfx950) and MI455X (gfx1250) run the same stages; only the chip, wave size and instruction families differ. MI355X(gfx950) 和 MI455X(gfx1250) 走同样的阶段, 只有 chip、 wave 大小和指令族不同。

The three decisions that kernel authors override by hand (order, registers, waits) all live in one stage: the AMDGPU backend. The linker after it changes no instruction.

kernel 作者手工覆盖的三个决定(顺序、 寄存器、 等待) 都在同一个阶段: AMDGPU 后端。 它之后的链接器不改任何指令。

Why the design matters more than the details. A compiler that ships as a binary can only be steered with flags. A compiler built from source, with your own passes in front of it and your own patches inside it, can be changed at every stage, and FlyDSL already does this routinely. That is the precondition for putting the compiler in the optimization loop at all.

为什么设计比细节更重要。 以二进制形式发布的编译器只能用开关来引导。 从源码构建、 前面有自己的 pass、 内部有自己补丁的编译器, 每一层都可以改, 而 FlyDSL 已经在经常这样做。 这是把编译器放进优化循环的前提。

03What the kernels are telling uskernel 在告诉我们什么

When a kernel author overrides the compiler, either the compiler decided badly or it could not be told what the author wanted. Both leave a trace in the source. These are the explicit overrides in the 180 Python files under kernels/ (method in section 08):

kernel 作者手工覆盖编译器, 要么是因为编译器决定错了, 要么是因为没法把作者的意图告诉编译器。 两种情况都会在源码里留下痕迹。 下面是 kernels/ 下 180 个 Python 文件里的显式覆盖(统计方法见第 08 节):

Override覆盖方式 Occurrences出现次数 What the author is doing作者在做什么
sched_barrier(0) 270 of 279 calls, 35 files279 次调用中的 270 次, 35 个文件 forbid the scheduler to move any instruction across this point禁止调度器把任何指令移过这个点
sched_mfma / sched_dsrd / sched_dswr / sched_vmem 66 lines, 12 files66 行, 12 个文件 hand-write the interleave of matrix and memory instructions手写矩阵指令和访存指令的交错
s_waitcnt, s_wait_*cnt 187 lines, 45 files187 行, 45 个文件 choose by hand where a wave waits for memory手工决定 wave 在哪里等内存
s_setprio 49 lines, 14 files49 行, 14 个文件 raise and lower wave priority by hand手工调高或调低 wave 优先级
inline assemblyinline assembly 29 lines, 12 files29 行, 12 个文件 write instructions the compiler cannot analyze直接写编译器无法分析的指令
"enable-post-misched": False 4 files4 个文件 switch off scheduling after register allocation关掉寄存器分配之后的调度

62 of the 180 files contain at least one of them.

180 个文件中, 有 62 个至少包含其中一种。

Why would an author forbid the scheduler to move anything? Because the scheduler optimizes a generic objective, trading register pressure against latency and occupancy, without knowing the pipeline the author designed: that a global load must issue several matrix instructions before its data is needed, and that the LDS writes for the next tile belong at the tail of the loop (kernel_tuning_guide.md:311). The hand-written order encodes that pipeline, and the fence protects it from an optimizer that cannot see it. The same reasoning switches off post-RA scheduling in several attention kernels, "to protect a hand-tuned schedule" (mllvm-flags.md).

作者为什么要禁止调度器移动任何东西? 因为调度器优化的是一个通用目标, 在寄存器压力、 延迟和 occupancy 之间做取舍, 却不知道作者设计的 pipeline: 一条 global load 要在数据被用到之前, 提前若干条矩阵指令发出; 下一个 tile 的 LDS 写要放在循环末尾(kernel_tuning_guide.md:311)。 手写的顺序编码了这条 pipeline, 屏障则保护它不被一个看不见它的优化器打乱。 同样的原因也让几个 attention kernel 关掉了 post-RA 调度, 用仓库文档的话说, 是为了保护手工调好的调度(mllvm-flags.md)。

Plate IIEach override replaces one backend decision每种覆盖替代一个后端决定
Six kinds of hand override in FlyDSL kernels, each mapped to the backend decision it replaces sched_barrier(0) sched_mfma / dsrd / dswr / vmem s_setprio "enable-post-misched": False inline assembly inline assembly s_waitcnt, s_wait_*cnt 270 calls · 35 files 270 次调用 · 35 个文件 66 lines · 12 files 66 行 · 12 个文件 49 lines · 14 files 49 行 · 14 个文件 4 files 4 个文件 29 lines · 12 files 29 行 · 12 个文件 187 lines · 45 files 187 行 · 45 个文件 Instruction order 指令顺序 scheduler, before and after RA 机器调度器, 寄存器分配前后 Instruction choice, registers 指令选择和寄存器 ISel, register allocator ISel、 寄存器分配器 Where waves wait wave 在哪里等待 SIInsertWaitcnts

Four of the six override kinds fight the scheduler. The fix for them is not a better fence but a way to state the pipeline the fence protects.

六种覆盖里有四种是在对抗调度器。 对它们来说, 修法不是更好的屏障, 而是一种能说清楚屏障所保护的 pipeline 的方式。

Leaving it this way has three costs. An override is paid again for every kernel, every tile shape and every architecture. A full fence also stops the compiler from improving the code around it. And the right override is not even fixed: FlyDSL's tuning guide reports that the hand interleave wins 8–10% on async "fat" tiles and loses on some f16/bf16 tiles with few N accumulators (kernel_tuning_guide.md:336). A decision that changes with the tile should not be written by hand in each file.

维持现状有三个代价。 每个 kernel、 每种 tile 形状、 每个架构都要重新付一次覆盖的成本。 一个完整的屏障还会阻止编译器优化它周围的代码。 而且正确的覆盖本身并不固定: FlyDSL 的调优文档记录, 手写交错在 async 「fat」 tile 上快 8–10%, 在部分 N 方向累加器很少的 f16 / bf16 tile 上反而更慢(kernel_tuning_guide.md:336)。 一个随 tile 变化的决定, 不应该在每个文件里手写。

Case: the AGPR accumulator in #1368案例: #1368 里的 AGPR 累加器

The clearest case merged on 16 November. The gfx950 FP4 and FP8 GEMMs kept their matrix accumulators in AGPRs with inline assembly, because the compiler's own pinning primitive, sched_agpr, did not hold: the accumulator could land in the wrong register file, and instruction selection picked the VGPR form of the MFMA. On an FP4 8192³ GEMM, the primitive took 674.8 µs against 221.4 µs for the assembly (#1368). The fix was two changes in the LLVM backend, after which the primitive matches or beats the assembly.

最清楚的例子在 11 月 16 日合入。 gfx950 的 FP4 和 FP8 GEMM 用 inline assembly 把矩阵累加器放在 AGPR 里, 因为编译器自己的固定原语 sched_agpr 不起作用: 累加器可能落进错误的寄存器文件, 指令选择也选了 VGPR 形式的 MFMA。 在 FP4 8192³ GEMM 上, 用这个原语要 674.8 µs, 用 assembly 只要 221.4 µs(#1368)。 修复是 LLVM 后端里的两处改动, 之后这个原语追平或超过了 assembly。

The gap was never in the kernel. It was two compiler decisions, and once they are fixed in the compiler, the next kernel gets in-place AGPR accumulation without writing assembly.

差距从来不在 kernel 里, 而在两个编译器决定里。 在编译器里修好之后, 下一个 kernel 不写 assembly 也能在 AGPR 里原地累加。

04Four ways the loop breaks循环断开的四种方式

Kernel optimization, by hand or by an agent, is a loop: change the kernel, compile, measure, keep the better version. The loop breaks in four distinct ways, and each points at a different layer of the chain. They are ordered here from the cheapest fix to the most expensive, and Plate III shows why the order matters: every later fix needs the cheap signal that the first one provides.

kernel 优化不管是人做还是 agent 做, 都是一个循环: 改 kernel、 编译、 测量、 保留更好的版本。 这个循环有四种不同的断法, 每一种都指向 compiler chain 的不同一层。 下面按修复成本从低到高排列。 图版 III 说明了这个顺序为什么重要: 后面每一种修法, 都需要第一种修法提供的便宜信号。

Plate IIIFour breaks, five fixes, one signal四种断法、 五种修法、 一个信号
The structured compile report feeds search, new abstractions and backend changes; ISA-level search feeds backend changes 1 · CANNOT SEE 1 · 看不见 Structured compile report 结构化编译报告 registers · spills · waits · hints 寄存器 · spill · wait · hint prune candidates 筛掉候选 verify the ISA 核对 ISA size the gap 量出差距 2 · CANNOT CHOOSE 2 · 选不了 Search existing options 搜索已有选项 autotune: options, templates autotune: 选项、 模板 no rebuild · per kernel 不用重编 · 逐个 kernel 3 · CANNOT EXPRESS 3 · 表达不了 DSL primitives, Fly passes DSL 原语、 Fly pass declared pipelines, waits 声明式 pipeline、 wait no LLVM rebuild · per pattern 不用重编 LLVM · 按模式生效 4 · DECIDES WRONG 4 · 决定错了 LLVM backend heuristics LLVM 后端启发式 schedule, registers, waits 调度、 寄存器、 wait LLVM rebuild · every kernel 要重编 LLVM · 所有 kernel 5 · MEASURE THE CEILING 5 · 量出上限 ISA-level search ISA 级搜索 inject_asm + agent or RL inject_asm 加 agent 或 RL the diff names the heuristic diff 指出该改哪条启发式 CHEAPER 更便宜 MORE EXPENSIVE 更昂贵

Fix 3 is outlined in red because it is the best main direction: it targets the most common overrides, needs no LLVM rebuild, and can be checked kernel by kernel.

修法 3 用红框标出, 因为它最适合作为主方向: 它针对最常见的覆盖, 不用重编 LLVM, 并且能逐个 kernel 验证。

1 · It cannot see1 · 看不见

A compile returns a binary or an error. To learn the register count, the spills, the occupancy, or whether a hint took effect, someone dumps about twenty intermediate files and runs a script over the assembly. Some knobs also fail silently. The repository's own LLVM notes record four knobs that never reached code generation, and an unknown compile_hints key is accepted without complaint while still changing the cache key, so the recompile looks real (.claude/skills/llvm/SKILL.md).

一次编译只返回二进制或者报错。 想知道寄存器数、 spill、 occupancy, 或者某个 hint 有没有生效, 就得 dump 大约二十个中间文件, 再用脚本解析 assembly。 有些开关还会静默失效。 仓库自己的 LLVM 笔记记录了 4 个从未到达代码生成的开关; 一个未知的 compile_hints key 也会被默默接受, 同时还改变 cache key, 所以重新编译看起来像是真的生效了(.claude/skills/llvm/SKILL.md)。

The fix is a structured report from every compile: registers, spills, occupancy, LDS, instruction and wait counts, and which hints reached the IR. LLVM can already emit the resource half as YAML through the kernel-resource-usage remark; the rest comes from the ISA that FlyDSL already dumps. Unknown hints should be errors.

修法是每次编译都输出一份结构化报告: 寄存器、 spill、 occupancy、 LDS、 指令数和 wait 数, 以及哪些 hint 真的到达了 IR。 LLVM 已经能通过 kernel-resource-usage remark 用 YAML 输出资源这一半, 其余部分可以从 FlyDSL 已经 dump 出来的 ISA 里取。 未知的 hint 应该直接报错。

This comes first because every other fix needs a cheap signal. GPU timing is expensive and noisy (the same notes measure ±14% clock noise on gfx950), while a static report rejects a spilling candidate in milliseconds. FlyDSL already maps compile errors back to the Python file and line that caused them (compiler/diagnostics.py:30); performance facts get no such treatment yet.

这一项排在最前面, 因为其他所有修复都需要一个便宜的信号。 GPU 计时又贵又吵(同一份笔记测到 gfx950 上有 ±14% 的时钟噪声), 而静态报告几毫秒就能筛掉一个会 spill 的候选。 FlyDSL 已经能把编译错误映射回出错的 Python 文件和行号(compiler/diagnostics.py:30), 但性能信息还没有这种待遇。

2 · It cannot choose2 · 选不了

When the best schedule template depends on the tile, the choice is a search problem, not a rule. FlyDSL's autotuner searches kernel parameters, the warp count and waves_per_eu (autotune.py:130). LLVM options and template switches are written into kernels by hand. Adding them to the search space, and pruning it with the report above, turns a per-tile guess into a measurement.

当最好的调度模板取决于 tile 时, 这个选择是搜索问题, 不是规则。 FlyDSL 的 autotuner 搜索 kernel 参数、 warp 数和 waves_per_eu(autotune.py:130)。 LLVM 选项和模板开关都是手工写进 kernel 的。 把它们加入搜索空间, 再用上面的报告剪枝, 就能把按 tile 的猜测变成测量。

Search has a ceiling: it only picks among options that exist. It cannot create the one that is missing.

搜索有上限: 它只能在已有的选项里挑, 造不出缺少的那个选项。

3 · It cannot express3 · 表达不了

270 fences and 187 hand-placed waits are not 457 different ideas. They are a few ideas written again in every kernel, and an idea written hundreds of times is a missing abstraction. Three are visible in the counts:

270 个屏障和 187 处手工 wait 并不是 457 个不同的想法。 它们是少数几个想法, 在每个 kernel 里重新写一遍; 一个被写了几百遍的想法, 就是一个缺失的抽象。 统计里能看到三个:

  • A declared pipeline instead of fences. The author states the intended schedule once (which loads run how many matrix instructions ahead, where the LDS writes go), and the compiler emits the scheduling hints and checks that the final ISA respects them. A constraint lets the compiler keep optimizing everything else; a full fence does not.
  • 声明 pipeline, 而不是加屏障。 作者只描述一次想要的调度(哪些 load 要提前多少条矩阵指令、 LDS 写放在哪里), 由编译器生成调度提示, 并检查最终 ISA 是否遵守。 约束让编译器还能继续优化其余部分, 完整的屏障做不到。
  • Waits derived from data flow. The Fly level sees every asynchronous copy and the operation that consumes it, so it can compute where the waits must go and check that none is missing. Missing and misplaced waits were a recurring source of silent wrong answers during the gfx1250 bring-up (Gap 5 in "Green Test, Red Model").
  • 从数据流推出 wait。 Fly 层能看到每个异步拷贝和消费它的操作, 所以能算出 wait 必须放在哪里, 并检查有没有漏掉。 在 gfx1250 上线期间, 漏掉或放错的 wait 反复造成静默的错误结果(「单测通过, 模型出错」 里的缺口 5)。
  • Register files as an API, not assembly. #1349 added sched_bank and sched_agpr, and #1368 made sched_agpr hold.
  • 用 API 而不是 assembly 控制寄存器文件。 #1349 加入了 sched_bank 和 sched_agpr, #1368 让 sched_agpr 真正生效。

Why not ask LLVM to do all of this? Because LLVM sees instructions, not pipelines. Stages, producers and consumers exist at the Fly level and are gone by the time the backend schedules. That is also why this layer is cheap to change: the passes live in the FlyDSL repository, a change needs no LLVM rebuild, and each change can be checked kernel by kernel, with the same or better speed and fewer overrides.

为什么不让 LLVM 来做这些? 因为 LLVM 看到的是指令, 不是 pipeline。 stage、 生产者和消费者只存在于 Fly 层, 到后端调度时已经消失了。 这也是这一层改起来便宜的原因: pass 就在 FlyDSL 仓库里, 改动不用重编 LLVM, 每次改动都能逐个 kernel 验证, 标准是速度相同或更快, 手工覆盖更少。

4 · It decides wrong4 · 决定错了

Some decisions only the backend makes: the final instruction order, register allocation, the MFMA form, wait insertion. #1368 was fixed there. This layer has the highest ceiling, because one fix reaches every kernel, and the highest cost. Each change means rebuilding LLVM, and LLVM changes move kernel performance in ways that are hard to predict: #1349 kept the LLVM pin on purpose instead of pulling in 1,807 upstream commits, "to avoid ... shifting kernel performance". Backend work should therefore go where measurement shows a large gap, and every change must pass a performance gate across the whole kernel set.

有些决定只有后端能做: 最终指令顺序、 寄存器分配、 MFMA 形式、 wait 插入。 #1368 就是在这一层修好的。 这一层的上限最高, 因为一次修复能惠及所有 kernel; 成本也最高。 每次改动都要重编 LLVM, 而 LLVM 的变化会以难以预测的方式挪动 kernel 性能: #1349 刻意没有升级 LLVM pin, 不引入 1,807 个 upstream 提交, 理由就是避免改变 kernel 性能。 所以后端工作应该投到测量显示差距大的地方, 而且每次改动都要通过覆盖全部 kernel 的性能门禁。

5 · Measuring the ceiling5 · 量出上限

One more tool turns guesses into numbers. FlyDSL's tools/inject_asm reassembles a hand-edited .s, swaps it into the cache and reruns the unchanged test, with a baseline run under the same conditions. Today it is a debugging aid. Driven by an agent or a reinforcement-learning policy that reorders instructions and moves waits, it becomes a search for the best schedule a given binary admits. CuAsmRL did this on NVIDIA SASS and improved expert kernels by 9% on average and up to 26%. The result is tied to one binary and one shape, so it is not what you ship. What it gives you is the size of the gap the compiler left, and a concrete diff that says which heuristic to change.

还有一个工具能把猜测变成数字。 FlyDSL 的 tools/inject_asm 能把手工改过的 .s 重新汇编、 换进 cache, 再跑原来的测试, 并在相同条件下跑一次基线。 它现在只是调试工具。 如果由 agent 或强化学习策略来重排指令、 移动 wait, 它就变成了对某个二进制的最优调度的搜索。 CuAsmRL 在 NVIDIA SASS 上这样做, 让专家写的 kernel 平均快 9%, 最高快 26%。 结果绑定单个二进制和单个形状, 所以不是用来上线的。 它给出的是编译器留下了多大差距, 以及一个具体的 diff, 告诉你该改哪条启发式。

05Letting the compiler improve itself让编译器自己进步

CAKE, from researchers at NVIDIA and CMU, turns this into a system. Three commitments carry it. Agents edit a typed IR in which hardware decisions are explicit, not raw CUDA. The compiler answers with localized diagnostics rather than a pass/fail bit: pre-compile gates for synchronization safety, hardware conformance, data consistency and schedule semantics, plus a calibrated cost model that ranks candidates before any GPU run (§3.1). And the harness itself evolves: "an opaque runtime crash becomes a verifier rule, a repeated illegal lowering pattern becomes a static check, a systematic misprediction becomes a calibration target" (§3.2). On a clean-start Flash-KMeans task on B200, the best Cake IR candidate reached a median 1.144× of a tuned baseline, against 0.928× when agents wrote CUDA/PTX directly with the same budget.

来自 NVIDIA 和 CMU 的研究者提出的 CAKE, 把这件事做成了一个系统。 它靠三条原则支撑。 agent 修改的是类型化、 硬件决定显式的 IR, 而不是裸 CUDA。 编译器返回的是定位到具体位置的诊断, 而不是 pass / fail: 编译前的门禁检查同步安全、 硬件契约、 数据一致性和调度语义, 再加一个校准过的 cost model, 在任何 GPU 运行之前给候选排序(§3.1)。 harness 本身也在演化: 不透明的运行时崩溃变成 verifier 规则, 反复出现的非法 lowering 模式变成静态检查, 系统性的预测偏差变成校准目标(§3.2)。 在 B200 上从零开始的 Flash-KMeans 任务里, 同样预算下, Cake IR 的最好候选中位数达到调优基线的 1.144×, agent 直接写 CUDA / PTX 只有 0.928×。

Plate IVTwo loops两个循环
The inner kernel-optimization loop and the outer compiler-improvement loop INNER LOOP · OPTIMIZE ONE KERNEL 内层循环 · 优化一个 kernel OUTER LOOP · IMPROVE THE COMPILER 外层循环 · 改进编译器 Compile 编译 Report 报告 Measure 测量 Edit kernel 改 kernel MLIR + LLVM spills, waits, hints spill、 wait、 hint idle GPU, repeats 空闲 GPU, 多次重复 agent or engineer agent 或工程师 Record, group 记录、 归类 Fix 修复 Corpus gate 语料门禁 failures, overrides 失败和手工覆盖 cheapest layer first 先用最便宜的一层 every kernel passes 所有 kernel 都通过 repeat, keep the best version 重复, 保留最好的版本 failures, overrides, gaps 失败、 手工覆盖、 差距 merged: every later kernel benefits 合并后, 之后的 kernel 都受益 option → Fly pass → LLVM patch 开关 → Fly pass → LLVM 补丁

The inner loop optimizes one kernel. The outer loop turns what the inner loop runs into (failures, hand overrides, measured gaps) into compiler changes, and merges only the changes that every kernel passes.

内层循环优化一个 kernel。 外层循环把内层循环遇到的问题(失败、 手工覆盖、 测出的差距) 变成编译器改动, 并且只合并所有 kernel 都通过的改动。

FlyDSL already has most of the pieces:

FlyDSL 已经有了大部分组件:

CAKE commitmentCAKE 原则 FlyDSL todayFlyDSL 现状 Missing还缺什么
hardware-explicit IR硬件显式的 IR atoms, layouts and scheduling primitives are explicitatom、 layout 和调度原语都是显式的 errors in layout code are hard to localizelayout 代码出错时难以定位
localized diagnostics定位到具体位置的诊断 compile errors map to Python lines编译错误能映射到 Python 行号 structured performance diagnostics结构化的性能诊断
evolving harness自我演化的 harness repository skills, an LLVM patch pipeline, CI that benchmarks against main and the latest tag (flydsl.yaml)仓库 skill、 LLVM 补丁流程、 对比 main 和最新 tag 的 benchmark CI(flydsl.yaml) a log of failures and overrides; a compiler-change gate across the kernel set失败和手工覆盖的记录; 覆盖全部 kernel 的编译器改动门禁

One design difference deserves attention. CAKE deliberately does not make layout a first-class abstraction (§2.2), because a layout calculus makes agent errors likely and hard to localize. FlyDSL is built on a layout algebra. For FlyDSL, then, the most valuable diagnostic is one that turns a layout error into a repair an agent can make.

有一个设计差异值得注意。 CAKE 刻意不把 layout 作为一等抽象(§2.2), 理由是 layout 演算让 agent 容易出错, 而且错误难以定位。 FlyDSL 恰好建立在 layout algebra 之上。 所以对 FlyDSL 来说, 最有价值的诊断, 是把 layout 错误翻译成 agent 能动手修的问题。

The corpus gate is not bureaucracy. CAKE notes that a new verifier rule without corpus validation can reject valid kernels, and that a new primitive without legality rules makes the IR harder to analyze (§3.2). A compiler change touches every kernel, so it has to be judged on every kernel.

语料门禁不是官僚流程。 CAKE 指出, 没有经过语料验证的新 verifier 规则可能拒绝合法的 kernel, 没有合法性规则的新原语会让 IR 更难分析(§3.2)。 编译器改动会影响每个 kernel, 所以必须用每个 kernel 来评判。

What gets automated, and how, depends on what is being changed:

自动化什么、 怎么自动化, 取决于要改的是什么:

Automation自动化方式 What it changes改什么 Example例子
search搜索 options and kernel parameters选项和 kernel 参数 FlyDSL's autotunerFlyDSL 的 autotuner
learned heuristic学习型启发式 one decision inside the compiler编译器里的单个决定 MLGO replaced LLVM's inlining-for-size heuristic with a trained policy: up to 7% smaller codeMLGO 用训练出的策略替换了 LLVM 的 inlining-for-size 启发式, 代码最多小 7%
LLM-evolved code with an automatic evaluatorLLM 演化代码 + 自动评估 the source of a kernel or a compiler heuristickernel 或编译器启发式的源码 AlphaEvolve sped up a Gemini kernel by 23%, and FlashAttention by up to 32.5% at the GPU-instruction levelAlphaEvolve 让 Gemini 的一个 kernel 快 23%, 在 GPU 指令层面让 FlashAttention 最多快 32.5%
reinforcement learning on the ISAISA 上的强化学习 the final instruction order最终指令顺序 CuAsmRL: 9% on average, up to 26%CuAsmRL: 平均快 9%, 最高快 26%

All four need the same thing first: an evaluator you can trust, meaning correctness tests plus timing on an isolated GPU with enough repeats to beat the noise. Without it, automation optimizes noise.

四种方式都先需要同一样东西: 一个可信的评估器, 也就是正确性测试, 加上在隔离 GPU 上重复足够多次、 能压过噪声的计时。 没有它, 自动化只会去优化噪声。

06A first experiment: a compiler-gap census第一个实验: 编译器差距普查

"Which part should we change?" stays vague until the gaps are measured. A census answers it:

在测出差距之前, 「该改哪一块」 始终是模糊的。 一次普查可以回答它:

  1. Pick three to five important kernels on MI355X, where CI runners exist: a GEMM, an attention kernel and a MoE kernel.
  2. 在 MI355X 上选 3 到 5 个重要的 kernel, 因为那里有 CI runner: 一个 GEMM、 一个 attention kernel 和一个 MoE kernel。
  3. Build each one three ways: as written; with the hand overrides removed and a sweep of LLVM scheduler strategies; and, optionally, with the best schedule that an inject_asm search finds.
  4. 每个 kernel 构建三个版本: 原样; 去掉手工覆盖并扫几种 LLVM 调度策略; 以及可选的、 用 inject_asm 搜索找到的最好调度。
  5. Attribute each performance gap to one stage: scheduling, register allocation, waits, instruction selection or address arithmetic.
  6. 把每一段性能差距归到一个阶段: 调度、 寄存器分配、 wait、 指令选择或地址计算。
  7. Tabulate each gap class: how much performance it costs, in how many kernels, and which layer would fix it.
  8. 把每类差距列成表: 它损失多少性能、 出现在几个 kernel 里、 由哪一层来修。
Plate VCensus design普查设计
Three to five kernels, three builds each, every gap attributed to one stage, one decision table KERNELS · MI355X KERNEL · MI355X GEMM attention MoE THREE BUILDS EACH 每个构建三个版本 As written 原样 all hand overrides kept 保留全部手工覆盖 Overrides removed 去掉手工覆盖 sweep scheduler strategies 扫几种 LLVM 调度策略 ISA search (optional) ISA 搜索(可选) best inject_asm schedule inject_asm 找到的最好调度 Attribute gaps 归因差距 scheduling 调度 registers 寄存器 waits wait selection 指令选择 addresses 地址计算 Decision table 决策表 cost per gap 每类差距的损失 kernel count kernel 数量 layer to fix 由哪一层来修 Done when the table answers "which layer first, and for how much". 当这张表能回答 「先改哪一层、 能拿到多少」 时, 普查就完成了。 instrument: the structured compile report from fix 1 工具: 修法 1 里的结构化编译报告

The census measures before it chooses. Its output is a ranking of layers by measured gain, which is the concrete answer to "which part of the compiler chain should we change".

普查先测量, 再选择。 它的产出是按实测收益给各层排序, 这就是 「该改 compiler chain 的哪一块」 的具体答案。

The census is done when that table answers "which layer first, and for how much". The structured compile report from the first fix is the instrument that makes the census cheap, so the two can start together.

当这张表能回答 「先改哪一层、 能拿到多少」 时, 普查就完成了。 第一项修复里的结构化编译报告正是让普查变便宜的工具, 所以两者可以同时开始。

07Closing结语

"Think about the compiler chain" becomes concrete once you ask, for each decision that sets a kernel's speed, who made it and whether anyone can see it. In FlyDSL the kernels answer for themselves: scheduling, registers and waits are decided by hand in 62 of 180 files. Each override is a bug report against the compiler that nobody filed. The outer loop is a way to file them: record them, group them, fix each one at the cheapest layer that can express it, and merge only what the whole kernel set agrees with.

只要对每个决定 kernel 速度的因素问一句 「是谁做的决定、 有没有人看得见」, 「多想想 compiler chain」 就不再模糊。 在 FlyDSL 里, kernel 自己给出了答案: 180 个文件里有 62 个在手工决定调度、 寄存器和等待。 每一处手工覆盖, 都是一份没人提交的编译器 bug 报告。 外层循环就是提交它们的方式: 记录、 归类, 在能表达它的最便宜的一层修好, 并且只合并整套 kernel 都认可的改动。

Open questions开放问题

  • Does a declared pipeline keep the 8–10% that hand interleaves win on fat tiles, or does giving the scheduler freedom give some of it back?
  • 声明式 pipeline 能不能保住手写交错在 fat tile 上的 8–10%? 还是把自由交还给调度器之后, 会吐回一部分?
  • Can a layout error in FlyDSL be reported as a repair (which mode, which stride) instead of an MLIR verifier message?
  • FlyDSL 里的 layout 错误, 能不能报告成一个修法(哪个 mode、 哪个 stride), 而不是一条 MLIR verifier 报错?
  • With ±14% clock noise on gfx950, how many repeats does a corpus gate need before it can accept a 2–3% compiler win?
  • gfx950 上有 ±14% 的时钟噪声, 语料门禁要重复多少次, 才能可靠地接受一个 2–3% 的编译器收益?

08Method and sources方法与来源

The counts are static matches in kernels/ at aae1553. They include a few comments, so read them as an indicator, not an exact call count:

数字是 aae1553 上 kernels/ 目录的静态匹配结果。 其中包含少量注释, 所以应该把它看作指标, 而不是精确的调用次数:

git clone https://github.com/ROCm/FlyDSL.git && cd FlyDSL
git checkout aae155359ddc9b2bb1064ec8d391bc25aa4c0d3e
rg --files -g '*.py' kernels | wc -l               # 180 kernel files
rg -o 'sched_barrier\(\s*0\s*\)' kernels | wc -l   # 270 full fences
rg -o 'sched_barrier\(' kernels | wc -l            # 279 sched_barrier calls
rg -c 's_waitcnt|s_wait_[a-z]+cnt' kernels         # hand-placed waits, per file
rg -l 'sched_barrier|sched_group_barrier|sched_(mfma|dsrd|dswr|vmem)\(|s_setprio|s_waitcnt|s_wait_[a-z]+cnt|inline_asm|InlineAsm' kernels | wc -l   # 62 files

The 8–10% and ±14% figures come from FlyDSL's own documentation and were not re-measured for this reading. None of the directions above has been tried yet; the census is the first step that would produce numbers.

文中 8–10% 和 ±14% 两个数字来自 FlyDSL 自己的文档, 本文没有复测。 上面的方向都还没有实验数据; 普查是第一个能产生数字的步骤。