Release-only wrong result in integer `for` loop: local bound inside loop range expression clobbers a local slot
Los mantenedores suelen responder en 3 días
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Área
- compilers
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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)
- Lenguaje dominante
- F#
- Estrellas
- 4.3k
- Forks
- 880
- Merge medio
- 4 d 14 min
- PR fusionados (30 d)
- 99
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de dotnet/fsharp
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Posiblemente ocupada @xperiandri la tomó hace 37 días. AbiertoNeeds-Triage
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
Los mantenedores suelen responder en 3 días
-
Needs-Triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
dotnet/fsharp#20265 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
Bug Needs-Triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
Feature Request Needs-Triage
Dificultad 3/5 1-2 días Aptitud para principiantes 57/100
Los mantenedores suelen responder en 3 días
-
Feature Request Needs-Triage
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
Los mantenedores suelen responder en 3 días
Todos los issues de dotnet/fsharp
Issues similares
-
flang:semantics
Dificultad 2/5 1-3 horas Aptitud para principiantes 73/100
llvm/llvm-project#231051 ·
Los mantenedores suelen responder en 1 día
-
bug derived types format I/O medium priority semantics
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
lexer: a lone `"` at the end of a file panics instead of reporting an errorPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
bytecodealliance/wasm-tools#2768 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
antlr/grammars-v4#5035 ·
Los mantenedores suelen responder en 5 días