Release-only wrong result in integer `for` loop: local bound inside loop range expression clobbers a local slot
I maintainer di solito rispondono entro 3 giorni
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- compilers
Direzione di ricerca
Start by locating GenIntegerForLoop in the F# compiler and inspect how it allocates local slots while evaluating loop bounds; the issue identifies that as the source of the clobbering. Use the three repro scripts in the report with optimization both disabled and enabled, especially the case that loops forever. Done when optimized execution matches the unoptimized results without clobbering locals.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Release-only wrong result in integer for loop: local bound inside loop range expression clobbers a local slot
Repro steps
//// repro.fsx ////
// dotnet fsi --optimize- repro.fsx prints: 100
// dotnet fsi --optimize+ repro.fsx prints: 50
let f k =
k + (for _ in max 0 (k - 50) .. 0 do stdout.Write ""
0)
printfn "%d" (f 100)
Expected behavior
The behavior without optimization is correct. f k is equivalent to k+0, so f 100 should be 100.
Actual behavior
With optimization on, the generated IL spills k off the stack into a local variable slot, but then when evaluating the loop start expression, max needs a local to store k - 50.
GenIntegerForLoop puts that local in the same slot. That clobbers k.
In my first example the start expression is the culprit. But the end expression has the same problem when the optimizer rewrites a loop like for i in 0 .. arr.Length - 1 C#-style. The local can take the loop variable's own slot, and the loop never terminates. Example:
//// repro2.fsx ////
// dotnet fsi --optimize- repro2.fsx prints: 0 1 2
// dotnet fsi --optimize+ repro2.fsx prints: 0 0 0 0 0 ... forever
let f (xs: int[]) n =
// this could be any side-effect-free expr that binds a local
for i in 0 .. (if max 0 (n - 1) > 0 then xs else xs).Length - 1 do
printf "%d " i
f [| 1; 2; 3 |] 1
Known workarounds
Bind the loop's start and/or end to locals with let. Use for x in startVar .. endVar do instead of putting complex expressions directly inside the in _ .. _ part of the loop.
Related information
Windows 10, SDK 10.0.401, and reproduced on main at 3917a24af
This bug repros going back at least to SDK 6.0.428. But going from 10.0.202 to 10.0.401, it became more visible as it can affect some usages of Array.init as well. This is how I ran into it.
//// repro3.fsx ////
// dotnet fsi --optimize- repro3.fsx prints: [|0; 1; 2|]
// dotnet fsi --optimize+ repro3.fsx prints: [|2; 1; 0|]
let xs =
Array.init 3 (fun k ->
for _ in max 0 (2 - k) .. 0 do stdout.Write ""
k)
printfn "%A" xs
I will follow up with a small PR to fix this.
Possibly related: dotnet/fsharp#20564 (different symptom, also stack saving around a branch)
- Lingua principale
- F#
- Stelle
- 4.3k
- Fork
- 880
- Merge medio
- 4g 7h
- PR unite (30g)
- 112
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/fsharp
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Forse già presa @xperiandri l’ha presa 36 giorni fa. ApertaNeeds-Triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
I maintainer di solito rispondono entro 3 giorni
-
Needs-Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/fsharp#20265 · 1 commento ·
I maintainer di solito rispondono entro 3 giorni
-
Bug Needs-Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 3 giorni
-
Bug Needs-Triage
Difficoltà 4/5 Più di una settimana Idoneità per principianti 18/100
I maintainer di solito rispondono entro 3 giorni
-
Caches.Cache: its finalizer keeps a dropped check alive for one more full GC, and MailboxProcessor-mode caches are never collectedForse già presa @majocha l’ha presa oggi. ApertaNeeds-Triage
Difficoltà 4/5 3-5 giorni Idoneità per principianti 18/100
dotnet/fsharp#20756 · 2 reazioni ·
I maintainer di solito rispondono entro 3 giorni
Tutte le issue di dotnet/fsharp
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
jaseci-labs/jac#9934 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
rescript-lang/rescript#8763 ·
I maintainer di solito rispondono entro 1 giorno
-
Booleans are brokenAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
flagos-ai/FlagTree#1344 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno