Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Optimizer is confused - probably by cshift

Jaromir_K_
Beginner
347 Views

Hello,

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
  0 -10
  10 0
  0 10
 -10 0

whereas with /O2, I get
  0  0
 -10 10
  10 -10
  0   0

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.

Thanks!

0 Kudos
4 Replies
CTOptimizer
Beginner
347 Views

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!).

Mark

0 Kudos
Jaromir_K_
Beginner
347 Views

Oh, thanks, I didn't know about supporttickets.intel.com. Wilco.

0 Kudos
Jaromir_K_
Beginner
347 Views

I created a support ticket for this with the ID 03450304.
Just posting that here for future reference...

 

0 Kudos
CTOptimizer
Beginner
347 Views

Excellent!

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.

Mark

0 Kudos
Reply