flang-compiler / f18

F18 is a front-end for Fortran intended to replace the existing front-end in the Flang compiler
229 stars 48 forks source link

[OpenMP] Predetermined rule for sequential loop index #976

Closed ichoyjx closed 4 years ago

ichoyjx commented 4 years ago

This commit implements rule: A loop iteration variable for a sequential loop in a parallel or task generating construct is private in the innermost such construct that encloses the loop.

A Simple example:

  i = -1                    <== Scope 0
  j = -1
  !$omp parallel            <== Scope 1
  print *,i,j      <-- both are shared (Scope 0)
  !$omp parallel            <== Scope 2
  print *,i,j      <-- a) i is shared (Scope 0), j is private (Scope 2)
  !$omp do                  <== Scope 3
  do i=1, 10       <-- i is private (Scope 3)
     do j=1, 10    <-- b) j is private (Scope 2, not 3!)
     enddo
  enddo
  print *,i,j      <-- c) i is shared (Scope 0), j is private (Scope 2)
  !$omp end parallel
  print *,i,j      <-- both are shared (Scope 0)
  !$omp end parallel
  print *,i,j      <-- both are shared (Scope 0)
end

The basic idea is when visiting the DoConstruct node within an OpenMP construct, if the do-loop is not associated (like i loop is associated with !$omp do) AND the do-loop is in the parallel/task generating construct, resolve the loop index to be private to that innermost construct.

In the above example, j loop is not associated (then it is sequential) and the innermost parallel/task generating construct that encloses the j loop is the parallel construct marked with <== Scope 2, so j is private to that construct. To do that, I also need to change the prototype of those ResolveOmp* functions to allow specifiying the scope because the new symbol for j should be created in Scope 2 and all the symbol field of Name j in that parallel construct should be fixed, such as c).

ichoyjx commented 4 years ago

Everything builds, tests, and looks good to me. I hesitate to approve since I don't really understand the changes.

Thanks Pete!!

ichoyjx commented 4 years ago

Note that the latest commit added the second Walk of OmpAttributeVisitor if there is no error during the first Walk, this resolves the issue a).

jeanPerier commented 4 years ago

LGTM