Rust-GPU / Rust-GPU/rust-gpu

Rust and SPIR-V float rounding semantics are different

オープン
#229 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Rust
スター
3.4k
フォーク
126
PR マージ指標
30日以内にマージされた PR はありません

説明

There is a footgun when trying to share code between CPU and GPU. Rust and SPIR-V have different semantics when rounding floats:

Rust:

  • Rust’s built-in conversion using as truncates the fractional part, effectively rounding toward zero.
  • For example, 1743028479.999... becomes 1743028479.
    Rust Language Reference

SPIR-V:

fn main() {
    let x = 1743028480i32;
    let y = (x as f64) * (1.0 / (i32::MAX as f64));

    // Without explicit rounding, differs:
    let without = (y * (i32::MAX as f64)) as i32;
    // Using `trunc()` forces Rust’s truncation:
    let with = (y * (i32::MAX as f64)).trunc() as i32;

    println!("x = {}. Without rounding = {}. With trunc() = {}.", x, without, with);
}

I'm not sure which should be the default. If it is Rust's we'll want to polyfill on the spir-v side. Might be good to have this user controlled in any case.

Originally posted by @LegNeato in https://github.com/Rust-GPU/rust-gpu/discussions/228#discussioncomment-12784276

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

crates/rustc_codegen_spirv/src/builder/builder_methods.rs の、参照されている OpConvertFToS の生成箇所付近から始め、issue にリンクされている Rust リファレンスと SPIR-V 仕様を比較します。浮動小数点数から整数への変換について、意図されたデフォルトの動作またはユーザーが制御できる動作を特定し、提示された例に対して検証します。選択したセマンティクスについて明示的に合意され、CPU/GPU の動作が一致するか、意図的に設定可能になっていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rust
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。