UnifiedIR for Julia
摘要
正文指出当前 Base IR 存在使用难度高、缺乏扩展性、未设计为外部系统等问题。该 PR 引入 UnifiedIR 作为独立包,提供 dialect-aware、region-based 的 IR 表示,保留部分原有设计,计划替换 SyntaxTree、SyntaxGraph、CodeInfo、IRCode 等结构。核心支持列扩展与分层视图(IncrementalCompact 式、LLVM-like 链表、graph-like、tree-like),并已迁移 inference、optimizer 和 JuliaLowering,但尚未启用 bootstrap。
荐读理由
UnifiedIR 的区域化设计、分层视图(LLVM 式、图嵌入、树编码)和方言扩展支持,直接展示了 Julia 编译器如何解决旧 IR 的扩展性与外部可用性问题,能更新你对 AI 工程中 IR 架构演进的判断。
原文
Our current IR data structures in Base are about 10 years old at this point. They've held up relatively well, but there are also big problems with. I think the three biggest are that the IR is somewhat hard to use (both for general API design reasons as well as for semantic reasons relating to ownership and invalidation), that it is not extensible to various other use cases that have grown in the ecosystem (leading to people either suffering through hacks or just building their own IR data structures) and really that it was never design to be an externally facing system at all.
This PR tries to fix that by introducing UnifiedIR, a new top-level package alongside Compiler, JuliaSyntax and JuliaLowering (and depended upon by all three) that provides a self-contained, extensible set of IR data structures and operations for use both in Julia base and in the larger ecosystem.
It is a dialect-aware, region-based IR representation (similar to MLIR) but retains a fair bit of the design of the original Julia IR (and JuliaLowering) representation that I thought worked well. It is in particular designed to replace the following existing data structures:
SyntaxTree (from JuliaSyntax)
SyntaxGraph (from JuliaSyntax/Lowering)
CodeInfo (for inference purposes, codegen/interepreter are out of scope for the moment but should be adapted in the future)
IRCode
Various IRs within the ecosystem
To facilitate this, there is a core data structure that provides an efficient representation of the IR along with core utilities. Any particular user of this core data structure can expand it with additional columns (one entry per statement) and there is also a concept of layered views to make it easier to work with the IR in a particular application.
The currently defined layered views are:
An IncrementalCompact like extension that allows fast one-pass replacement passes
An LLVM-like view that maintains ordering through linked lists (thus making complicated CFG transforms a lot easier). To avoid the usual LLVM pitfall of quadratic order maintenance for large functions (which are important to some downstream applications), there's a fancy order maintenance algorithm that claude and I came up with (but still needs validation).
A graph like embedding for use by array compilers (as well as lowering) (Graphs are IR with no control flow dependency)
A tree-like encoding for use by lowering (trees are IR where every statement has exactly one user, except for the root, which has zero).
Major features include first class support for regions (making features like #58532 easy to implement as well as facilitating better integration will MLIR) as well as first class support for dialect extensions.
This PR ports inference, the optimizer and JuliaLowering to the new data structure, but does not yet enable it for bootstrap. That said, I ran about half of the test suite with this version of the compiler and things were working. This should be considered a bit of a preview PR and a basis for discussion. There's many things that still need to be done, but I wanted to put this up so I had something to point to as a basis for discussion.
这条对你有帮助吗?
