OptimizeInstructions: `--tnh` makes `ref.cast` non-nullable before a possibly infinite loop
Maintainer antworten meist innerhalb von 1 Tag
Ein zugehöriger Pull Request wurde bereits gemerged.
- #9256 von @tamaroning — gemerged
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 45/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- WebAssembly
- Sterne
- 8.7k
- Forks
- 897
- Ø Merge
- 2 T. 1 Std.
- Gemergte PRs (30 T.)
- 103
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus WebAssembly/binaryen
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
WebAssembly/binaryen#9135 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 76/100
WebAssembly/binaryen#9018 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 76/100
WebAssembly/binaryen#9291 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 15/100
WebAssembly/binaryen#9260 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 62/100
WebAssembly/binaryen#9257 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in WebAssembly/binaryen
Ähnliche Issues
-
bug priority: P2
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
confirmed generated by fuzzer llvm:optimizations miscompilation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
llvm/llvm-project#234325 ·
Maintainer antworten meist innerhalb von 1 Tag
-
grammar selfhost sev-3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
gren-lang/compiler-common#41 ·