Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

OptimizeInstructions: `--tnh` makes `ref.cast` non-nullable before a possibly infinite loop

未关闭
#9,246 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@tamaroning 已经在做这个了。

开始于 2026年10月13日。

  • #9256 来自 @tamaroning —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, wasm
领域
compilers

调研方向

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

https://github.com/WebAssembly/binaryen/blob/1d7748aa4ef48ac93f90712c2437ef88c6441389/src/passes/OptimizeInstructions.cpp#L1843-L1853

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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

WebAssembly/binaryen 的其他 Issue

查看 WebAssembly/binaryen 的全部 Issue

相似的 Issue

更多 Compilers Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。