[flang] [OpenMP] Predetermined rule for sequential loop index (flang-compiler/f18...
authorJinxin (Brian) Yang <jinxiny@nvidia.com>
Wed, 19 Feb 2020 00:27:43 +0000 (16:27 -0800)
committerGitHub <noreply@github.com>
Wed, 19 Feb 2020 00:27:43 +0000 (16:27 -0800)
commitaa9fc5bddcbccfc7adc8f05dbd579cb0b94f7e20
treef9de9e481405d2ff066acefd4c0430c933ebc300
parent840e19eed8409de46bfaccf63301eb741ffa86ef
[flang] [OpenMP] Predetermined rule for sequential loop index (flang-compiler/f18#976)

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
```

Ideally the above rule solves a), b), and c) but a) is left as a TODO
because it is better to handle the data-sharing attribute conflicts
along with the rules for "Predetermined DSA on Clauses".

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

Original-commit: flang-compiler/f18@69a845283b058a3644053ec58b00d3361f4d4a59
Reviewed-on: https://github.com/flang-compiler/f18/pull/976
flang/include/flang/semantics/symbol.h
flang/lib/semantics/check-omp-structure.h
flang/lib/semantics/resolve-names.cpp
flang/test/semantics/omp-symbol01.f90
flang/test/semantics/omp-symbol04.f90
flang/test/semantics/omp-symbol06.f90
flang/test/semantics/omp-symbol08.f90