regalloc2 makes too many memory allocations
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- rust
- 領域
- compilers, performance
調査の方向性
挙げられているホットスポットから始めます。try_to_allocate_bundle_to_reg、merge_bundles、insert_use_into_liverange、add_liverange_to_vreg、create_pregs_and_vregs、Env::new を調べてください。Env と、列挙されている SmallVec、HashSet、BTreeMap、Vec の各フィールドがどのように作成され再利用されているかを確認し、そのうえで cranelift-entity と cranelift-bforest を使用する Context 設計を評価してください。繰り返し発生するアロケーションを削減し、アロケータの動作を維持し、#62 との関係に対処できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
I used heaptrack to analyze the memory allocation patterns of my compiler which uses regalloc2. I used a large benchmark (LLVM library) which has 1257955 basic blocks in 104913 functions. Out of a total of 46780667 allocations, 46779011 of them come from regalloc2 and only 1656 come from other parts of the compiler.
My compiler uses a similar structure [^1] to Cranelift where a Context structure allows memory allocations to be reused across compilation units. Specifically, this allows various Vecs and HashMaps to grow to a sufficient size only once while being reused many times for compiling multiple functions.
[^1]: In fact I am using the cranelift-entity crate directly, it's very well written.
regalloc2 unfortunately doesn't follow this design:
- A fresh
Envstructure is allocated and freed for each function. No memory allocations are preserved. - Heavy use of temporary
SmallVecs andHashSets. Heaptrack reports that 19865290 allocations (42% of the total) are "temporary". These are allocations which are immediately followed by a free, with no other allocations in between. - Heavy use of
BTreeMapwhich needs to allocate many nodes separately and cannot reuse memory unlikeHashMapandVec.
Heaptrack results
Here are the major allocation hotspots shown by heaptrack:
In try_to_allocate_bundle_to_reg:
- 16158324 temporary allocations from the
FxHashSet. - 9677435 allocations from inserting liveranges into
PRegData::allocations(LiveRangeSetbacked by aBTreeMap).
In merge_bundles:
- 4478881 allocations from pushing a
VRegtoSpillSet::vregs(SmallVec). - 1895443 allocations from appending
LiveRangeListEntrys toLiveBundle::ranges(SmallVec).
In insert_use_into_liverange:
- 2578957 allocations from pushing a
UsetoLiveRange::uses(SmallVec).
In add_liverange_to_vreg:
- 1432853 allocations from pushing a
LiveRangeListEntrytoVRegData::ranges(SmallVec).
In create_pregs_and_vregs:
- 948582 allocations from pushing to
Env::vregsandEnv::allocsandEnv::inst_alloc_offsets. (Vec)
In Env::new:
- 1154043 allocations from initializing the various
Vecs withVec::with_capacity.
Performance impact
Benchmarking shows that approximately 10% of the time is spent in memory allocation functions (malloc, realloc, free). Additionally, another 10% of the time is spent in memcpy/memmove which could be due to vector reallocation.
Compiling with multiple threads can cause additional contention on the global allocator. This was noticed on musl targets where the global allocator doesn't have thread-local caches: compiler performance on 6 threads (on a 6-core machine) was reduced to that of a single thread whereas other allocators allow almost-perfect performance scaling.
Proposed solution
I believe the best way to improve regalloc2 would be to use a design similar to Cranelift with a Context structure that can preserve memory allocation across multiple invocations of the register allocator.
- Temporary
SmallVecs andHashSets should be replaced with a single instance inContext. - The many
BTreeMaps should be replaced withcranelift-bforestwhich maintains a pool of nodes that can be reused. - The many
SmallVecs should be replaced withEntityListfromcranelift-entityand aListPoolinContext.
Since this would require a dependency on the cranelift-entity and cranelift-bforest crates, this is also a good opportunity to resolve #62 and switch to using proper indexed types internally.
- 主要言語
- Rust
- スター
- 266
- フォーク
- 54
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bytecodealliance/regalloc2 のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
bytecodealliance/regalloc2#265 · コメント 8 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
bytecodealliance/regalloc2#247 · コメント 3 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
bytecodealliance/regalloc2#222 · コメント 4 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
bytecodealliance/regalloc2#206 · コメント 2 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
bytecodealliance/regalloc2#194 · コメント 7 件 ·
bytecodealliance/regalloc2 の issue をすべて見る
似ている issue
-
bug CLI custom-model
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
rust-bitcoin/rust-bitcoin#6930 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
fulcrumgenomics/ferro-hgvs#2251 ·
-
A-allocators A-docs C-enhancement T-libs
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100