LoopInvariantCodeMotion: `struct.new` is hoisted out of a loop, so all iterations share one object
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 68/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- wasm
- 領域
- compilers
調査の方向性
Start with the LoopInvariantCodeMotion pass and its unsafeToMove logic, then reproduce the issue with the supplied WAT module using wasm-opt --enable-gc --enable-reference-types --licm --fuzz-exec. Check that struct.new remains per-iteration and that the optimized and unoptimized executions both return 1.
索引モデルが issue の本文から書いたものです。
説明
Summary
unsafeToMove does not treat an allocation as generative, so local.set $x (struct.new ...) is moved out of the loop and every iteration mutates the same object.
Root cause
struct.new is considered movable because it has no side effect on existing state, but it creates a new identity on each evaluation.
Affected passes
Only LoopInvariantCodeMotion. Allocations in other loop-hoisting code were not examined.
Reproducer
In C-like pseudo code, the function below is:
struct T { int f; };
int f(void) {
struct T *x;
int i = 0;
do {
x = new_T(0); // a new object on every iteration
x->f = x->f + 1;
i = i + 1;
} while (i < 2);
return x->f; // 1
}
After --licm, new_T(0) is moved above the loop, so both iterations update the same object and f returns 2:
x = new_T(0);
do {
x->f = x->f + 1;
i = i + 1;
} while (i < 2);
return x->f; // 2
(module
(type $T (struct (field (mut i32))))
(func (export "f") (result i32)
(local $x (ref null $T)) (local $i i32)
(loop $l
(local.set $x (struct.new $T (i32.const 0)))
(struct.set $T 0 (local.get $x)
(i32.add (struct.get $T 0 (local.get $x)) (i32.const 1)))
(br_if $l (i32.lt_u (local.tee $i (i32.add (local.get $i) (i32.const 1))) (i32.const 2))))
(struct.get $T 0 (local.get $x))))
$ wasm-opt in.wat --enable-gc --enable-reference-types --licm --fuzz-exec -o /dev/null
[fuzz-exec] export f
[fuzz-exec] note result: f => 1
[fuzz-exec] export f
[fuzz-exec] note result: f => 2
[fuzz-exec] comparing f
values not identical! 2 != 1
[fuzz-exec] optimization passes changed results
Expected vs actual
The original returns 1 (each iteration starts from a fresh object). After the pass the object is shared across the two iterations and the function returns 2.
Version
Reproduced on upstream main at 4d8ac549e2ab9b283246ea95e79ebe139ca579ac (wasm-opt version 133).
AI was used as part of the process of finding this issue. I have manually checked and reproduced it.
- 主要言語
- WebAssembly
- スター
- 8.7k
- フォーク
- 895
- 平均マージ
- 1日 22時間
- マージ済み PR(30日)
- 97
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
WebAssembly/binaryen のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
WebAssembly/binaryen#9135 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 半日 初心者へのやさしさ 76/100
WebAssembly/binaryen#9018 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
WebAssembly/binaryen#9249 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
WebAssembly/binaryen#9246 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Memory64Lowering: table.get/table.set keep i64 index on lowered table64, output fails validationオープン
難易度 3/5 半日 初心者へのやさしさ 66/100
WebAssembly/binaryen#9245 ·
メンテナーはふだん 1 日以内に返信
WebAssembly/binaryen の issue をすべて見る
似ている issue
-
flang:semantics
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
llvm/llvm-project#231051 ·
メンテナーはふだん 1 日以内に返信
-
bug derived types format I/O medium priority semantics
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
webgpu-tools/wesl-rs#329 ·
メンテナーはふだん 1 日以内に返信
-
compiler/runtime
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信