I encountered a gcc/clang -O1 (not -O2) optimization bug that really
shocked me a bit.
In the code below, if the following line is dropped,
val () = ptr_volatile (p_nseq)then the gcc/clang (with -O1) somehow thinks that the content of nseq is
never modified. As a consequence, the function always returns 0. If -O1
is dropped or changed to -O0, then the compiled code works fine. I also
tried tcc and noticed that it can compile the following code correctly.
This is a shock because the involved code is so simple!
To see what happens, you can try out the code at:
https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/SMALL/M-N-K-game/Part1implement
board_dirmax
(
board, pid, dir, i, j
) = let
//
var nseq: int = 0
val p_nseq = addr@nseq
//
fun loop
(
i: int, j: int
) : void = let
//
val pid2 = board[i, j]
//
in
if pid = pid2
then let
val n = $UN.ptr1_get<int> (p_nseq)
val () = $UN.ptr1_set<int> (p_nseq, n + 1)
val (i, j) = dirnext (dir, i, j)
in
loop (i, j)
end // end of [then]
// end of [if]
end // end of [loop]
//
val () = ptr_volatile (p_nseq) // -O1 bug if omitted
val () = try loop (i, j) with ~MatrixSubscriptExn () => ()
//
in
$UN.cast{intGte(0)}(nseq)
end // end of [board_dirmax]