In debug, I get 3. In release, I get 1.

At first, this looked like the case in C like languages where you access and set a value before a control point. However, the for loop seems to not be executed in release. So, I think this might be a problem with over eager loop optimization.

Here's a code sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let r = Array.create 8 1
let w = Array.create 5 2
printfn "rlength: %d" r.Length
printfn "wlength: %d" w.Length
for j = 0 to (min r.Length w.Length) - 1 do // loop not executed in release?!?
printfn "rj: %d" r.[j]
r.[j] printfn "rj: %d" r.[j]

printfn "r0: %d" r.[0] // 1 in release or 3 in debug
let mutable i = 0
printfn "i: %d" i // 0
i printfn "i: %d" i // 6
let j = ref 1 
printfn "j: %d" !j // 1 
j := !j + 1 
printfn "j: %d" !j // 2
By on 6/19/2009 7:09 AM ()

Yes, this is a bug. Thanks for reporting. When known we will update this thread with more information.

By on 6/19/2009 10:11 AM ()

Yes, this is an optimization bug, associated with constructs that cause non-trivial branching and binding inside the "end" expression of a for loop. To workaround, define the limit outside the loop:

1
2
3
4
5
6
  let r = Array.create 8 1
  let w = Array.create 5 2
  let lim = (min r.Length w.Length) - 1
  for j = 0 to lim do
      r.[j] <- r.[j] + w.[j]
  printf "%d\n" r.[0]

Thanks

By on 6/19/2009 12:08 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper