OptimizeInstructions: `--tnh` makes `ref.cast` non-nullable before a possibly infinite loop
维护者通常 1 天内回复
评估
调研方向
The check is in src/passes/OptimizeInstructions.cpp around lines 1843-1853, where a ref.cast is made non-nullable when a later sibling transfersControlFlow(). Read the effects code for mayNotReturn and how loops are marked after #5711. Run the reproducer with --traps-never-happen to see the cast turn into ref.cast (ref $S). Done when a sibling that may not return keeps the nullable cast, and the existing tests still pass.
由索引模型根据 Issue 内容生成。
描述
Reproducer
(module
(type $S (struct (field (mut i32))))
(func (export "f") (param $a anyref) (param $n i32)
(struct.set $S 0
(ref.cast (ref null $S) (local.get $a))
(loop $l (result i32) (br_if $l (local.get $n)) (i32.const 0)))))
wasm-opt -all in.wat --optimize-instructions --traps-never-happen (also -O3) turns the cast into (ref.cast (ref $S) ...). In pseudocode:
// input
x = cast_or_null<S>(a)
while (n != 0) {}
x.field0 = 0 // traps if x is null
// output
x = cast_nonnull<S>(a) // traps if a is null
while (n != 0) {}
x.field0 = 0
With a = null and n = 1, the input loops forever without reaching the struct.set, so nothing traps; the output traps before the loop.
Cause
This check excludes a later sibling that transfersControlFlow(), since then "we might not reach the parent". A sibling that may not return (an infinite loop) also means we might not reach the parent, but transfersControlFlow() does not include mayNotReturn, so the check misses it. The same applies to array.get, array.set and array.copy.
This check predates #5711. Before #5711 a loop had the effect "may trap (timeout)", which traps-never-happen ignores, so assuming the loop terminates was consistent. #5711 changed loops to mayNotReturn so that traps-never-happen keeps infinite loops, but this check was not updated.
Possible fix
Add || effects(child).mayNotReturn to that check.
If traps-never-happen is meant to allow this, please let us know.
- 主要语言
- WebAssembly
- 星标
- 8.7k
- 派生
- 895
- 平均合并
- 1 天 21 小时
- 30 天内合并 PR
- 95
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 天内回复
-
Memory64Lowering: table.get/table.set keep i64 index on lowered table64, output fails validation未关闭
难度 3/5 半天 新手友好度 66/100
WebAssembly/binaryen#9245 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 65/100
WebAssembly/binaryen#9244 ·
维护者通常 1 天内回复
查看 WebAssembly/binaryen 的全部 Issue
相似的 Issue
-
Invalid model / false unsat: to_fp of real * 2^int with a symbolic rounding mode treats RTN as RTZ未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
KhronosGroup/SPIRV-LLVM-Translator#4082 ·
维护者通常 2 天内回复
-
bug diagnostics
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
bradcypert/plum#227 ·
维护者通常 1 天内回复
-
crash mlir
难度 2/5 1-3 小时 新手友好度 68/100
llvm/llvm-project#231201 · 1 条评论 ·
维护者通常 1 天内回复