Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#9,246 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp, wasm
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
WebAssembly
Estrellas
8.7k
Forks
895
Merge medio
1 d 23 h
PR fusionados (30 d)
105

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de WebAssembly/binaryen

Todos los issues de WebAssembly/binaryen

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.