I've come across a rather peculiar bug in the optimizer.
I've managed to isolate it in the following peace of code with /O2:
program intel_optimizer_bug implicit none integer(4),dimension(4) :: ind real(8),dimension(2) :: li real(8),dimension(4, 2) :: nli real(8),dimension(4, 2) :: xg integer :: i xg = reshape([0, 10, 10, 0, & 0, 0, 10, 10], [4,2]) nli = 0d0 ind=[1:4] do i = 1,4 li = xg(ind(1),:) - xg(ind(2),:) nli(i, :) = [-li(2), li(1)]; ind = cshift(ind, 1) end do print *, nli(:, 1) print *, nli(:, 2) end program intel_optimizer_bug
the optimizer seems to swap the instructions in the loop somehow, achieving an incorrect order.
In nli, should be getting the matrix
whereas with /O2, I get
Notice that the commands in the do loop depend on each other (they have to be executed in the defined order to get the correct result). I suspect the optimizer fails to detect this dependency for the cshift function.
I've managed to force it to behave it correctly by adding the !DIR$ NOUNROLL directive above the loop.
But as this is, in my view, definitely a bug, in addition one that is VERY hard to detect when buried in layers of code (no chance of having an exception thrown here), I thought you guys should know and fix it.
I've attached a complete Visual Studio solution with similar to above code, where this can be easily reproduced using the Release build configuration.
I'm using Intel® Parallel Studio XE 2018 Update 1 Composer Edition for Fortran Windows* Integration for Microsoft Visual Studio* 2017, Version 18.0.0037.15.
Hope this reaches the right person.
I tend to agree this is likely an optimizer bug. I was also able to get the correct results by adding the "volatile" attribute to the declaration of "ind". This effectively disables optimizations related to the given variable.
Since you have created this very nice reproducer, I recommend that you file a bug report at supporttickets.intel.com. This is the best way to ensure the problem gets addressed by the team (i.e. rather than "hoping" it reaches the right person!).
Your fellow users (myself included) thank you for creating this reproducer. I have learned over the last 15+ years that "one good (small) reproducer is worth 1000 words (posts)". Filing them with Intel is the best way to get the attention of their development team.