0xMiden / 0xMiden/air-script

Potential Mir improvements

未關閉
#504 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Rust
星號
96
分支
39
PR 合併指標
30 天內沒有已合併 PR

描述

This post aims to describe several potential refactors that would simplify the current codebase and to discuss them.

Note that some proposals should not be done independently (especially regarding the MIR’s API).

The proposals are sorted by estimated benefit to cost ratio.

1. simplify the builder pattern
- Why? The type level mechanism to enforce no field is set twice slows down the LSP, sometimes causing crashes. remove duplicate code, enforce a unique way to build new nodes
- What? Simplify the builder pattern to only have a single intermediary step. Enforce double values set via .expect instead of at the type-level. Only expose build or create methods on MIR operations, depending on if it’s needed. (impacts the MIR)
- Estimation: 3 days

2. Remove Matrices
- Why? Matrices are not needed to describe Miden’s constraints, and if needed they can be replaced by multiple Vector constraints (especially as no operation on matrices are currently supported in AirScript, only accessing specific matrix indices).
The language would be simplified (simplify typing checks implementations, removing Op::Matrix in the MIR..), and that would eliminate some currently hard to debug issues with nesting.
- What? Remove all instances of Matrix and type::Matrix (impacts parser / AST / MIR)
- Estimation: 3-5 days

3. unify Node, Owner, root, op, into Op
- Why? Most of the complexity in the MIR stem from the difference in usage between Root and Op, and their “virtual representation” as Node and Owner. Unifying both under a single Op enum will eliminate most conversions.
- What? Unify all MIR enums under a single Op enum (impacts the MIR)
- Estimation: 5 days dev \- potentially a few more for debugging if the way we reference nodes in the graph breaks.

4. Try to move most of the sema to mir
- Why? most passes rely on information given by the sema (enforced invariants), but at the same time most also provide new information for further semantic analysis. This bidirectional coupling currently relies on duplicated logic happening before and after the sema and other transformations. Moving elements of this pass to happen in the Mir would let us enforce invariants as we encounter them by easily reordering passes. Reordering is tricky currently, as the semantic analysis and Mir transformations operate on different representations of the same expressions (Ast vs Mir)
- What? Progressively extract pieces of the semantic analysis and move them to where the information is introduced (after/before specific passes in the Mir) (impacts the Sema and the MIR)
- Estimation: 10 days (background task)

5. Visitor API: make visit order explicit
- Why? Reduce the need for multiple sub-passes per pass (scan, rebuild, unify, etc.)
- What? call a generic visit method explicitly, simplify affected passes. (impacts the MIR)
- Estimation: \~ (we need to design the API first)

6. Check assumptions earlier (cf [https://github.com/0xMiden/air-script/pull/440\#discussion\_r2348037326](https://github.com/0xMiden/air-script/pull/440#discussion_r2348037326)), before undefined behaviour (unreachable), pass checks ahead of running, via a trait method for all passes Pass::check\_assumptions.
- Why? Debugging where things broke is harder than when a check fails. Having more info would speed up debugging, and allow us to enforce invariants globally rather than through documentation/comments
- What? add a trait method for all passes where all invariants are checked, move existing scattered invariants checks to this impl. (impacts all passes)
- Note: assumptions should also include graph consistancy (see PR https://github.com/0xMiden/air-script/pull/496 for an example of sub-optimal consistancy in the handling of Enf nodes)
- Estimation: \~ (we need to design the API first)

7. maybe add ssa id to all expressions: potentially add asserts to enforce this invariant
- Why? To help debugging MIR transformation, having a unique ssa id per immutable expression would allow better tracking of transformations.
- What? Add SSA ID to all Mir::Op, enforce that all updates bump the ssa id to \> last\_ssa\_id. Store a ssa id per Op, as well as a Link to a shared global ssa id. (impacts the MIR), on every Link\::borrow\_mut, store the max ssa\_id \+ 1 as the Op’s ssa id, increment the max ssa\_id. On duplications, increment too.
Change backrefs to use ssa ids (in parameters, calls, iterator bindings, …)
- Estimation: 5 days

8. arena allocator to have the same lifetimes
- Why? some structs are hard to express in rust, such as linked lists and graph datastructures (to store back pointers from children to their parents). Using an arena allocator instead of the standard Global allocator would enable using linked lists as well as expressing mutable recursive structures more easily.
- What? drop in replacement for the allocator. implement automatic parent setting using the linked list. (impacts the MIR)
- Estimation: 10 days

A general principle to follow:
Favor simplicity & correctness over perf.
- Why? Performance is a non-issue in the current compiler, and that can lead to complications in the compiler: for example, we currently perform all transformations in a single pass.
- What? fixed point compilation if transformations are missed, etc.

Background tasks:
4. progressively migrate sema to mir
6. check assumptions earlier

貢獻指南

開啟貢獻指南

研究方向

The issue lists multiple refactoring proposals for the MIR (Mid-level IR) in the AirScript compiler. Start by understanding the current MIR structure in the codebase, likely in a `mir` module. Each proposal (e.g., simplifying the builder pattern, removing matrices, unifying enums) requires deep familiarity with the compiler's internals and design trade-offs. 'Done' means implementing one proposal without breaking existing functionality, which involves changes across parser, AST, MIR, and possibly Sema.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
rust
領域
compilers
Issue 類型
重構
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。