Release-only wrong result in integer `for` loop: local bound inside loop range expression clobbers a local slot
Maintainer thường phản hồi trong vòng 3 ngày
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- compilers
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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)
- Ngôn ngữ chính
- F#
- Star
- 4.3k
- Fork
- 880
- Merge trung bình
- 4 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 112
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/fsharp
-
Semantic classification cache for opened documents is never populated (written to the unopened-documents cache)Có thể đã có người làm @xperiandri đã nhận 37 ngày trước. Đang mởNeeds-Triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/fsharp#20265 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Bug Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Question about changed behavior of string interpolations in NativeAOT in the .NET 11 rc1 SDKĐang mởBug Needs-Triage
Độ khó 4/5 Hơn một tuần Mức phù hợp với người mới 18/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Caches.Cache: its finalizer keeps a dropped check alive for one more full GC, and MailboxProcessor-mode caches are never collectedCó thể đã có người làm @majocha đã nhận hôm nay. Đang mởNeeds-Triage
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 18/100
dotnet/fsharp#20756 · 2 reaction ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của dotnet/fsharp
Issue tương tự
-
bug derived types format I/O medium priority semantics
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
bytecodealliance/wasm-tools#2768 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
antlr/grammars-v4#5035 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày