Inline calls to constant functions and evaluate them at compile time
まだ誰も着手していません。
- 主要言語
- Rust
- スター
- 3.4k
- フォーク
- 126
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
Originally posted by @nazar-pc in #300
The basic problem right now is that calls into const fn are not inlined recursively. Here is an example:
const K: u8 = 20;
const PARAM_EXT: u8 = 6;
const fn y_size_bits(k: u8) -> u32 {
k as u32 + PARAM_EXT as u32
}
const fn metadata_size_bits(k: u8, table_number: u8) -> u32 {
k as u32
* match table_number {
1 => 1,
2 => 2,
3 | 4 => 4,
5 => 3,
6 => 2,
7 => 0,
_ => unreachable!(),
}
}
fn compute_fn_impl<const PARENT_TABLE_NUMBER: u8>(
) -> u32 {
let parent_metadata_bits = metadata_size_bits(K, PARENT_TABLE_NUMBER);
let num_bytes_with_data = (y_size_bits(K) + parent_metadata_bits * 2).div_ceil(u8::BITS);
// ..
}
Right now this requires Int8 capability:
error: Missing required capabilities for types
|
= note: `u8` type used without `OpCapability Int8`
Even though the exact value of num_bytes_with_data is known at compile time for every instantiation of the function and no u8 should actually exist anywhere in runtime. Some workarounds exist:
-
This helps with the first variable, but doesn't scale further.let parent_metadata_bits = const { metadata_size_bits(K, PARENT_TABLE_NUMBER) }; - Making ^ a proper constant doesn't work due to "error[E0401]: can’t use generic parameters from outer function" error
The only real solution is to inline everything recursively manually and wrap with const {}, which hurts code readability a lot, but perhaps this is something compiler can do as a workaround without redesigning too many of things.
In the end, I expect that all constants, variables and const fn calls to be inlined into a single constant value at compile time automatically, including any possible integer casts (like u8 -> u32), which for constant values are also known at compile time.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、コンパイラまたはコード生成パイプラインで Rust の例と、Int8 capability が欠如していることによるエラーを再現します。const fn 呼び出し、ジェネリックのインスタンス化、キャスト、定数式がどのように低レベル化されるかを追跡し、その後、ランタイムの u8 値や Int8 capability なしで例がコンパイルできることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- rust
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100