Issue 244763
Summary [flang][OpenMP] Variant selection in loop bounds and leaf clauses of composite constructs follows MLIR nesting
Labels flang:openmp
Assignees chichunchen
Reporter chichunchen
    Flang lowers composite loop constructs into MLIR operations whose nesting differs from the source leaf nesting. It then emits loop bounds and clause expressions at whatever insertion point lowering has reached. Variant selection walks the enclosing MLIR operations (`collectEnclosingConstructTraits` in `flang/lib/Lower/OpenMP/Utils.cpp`), so a DECLARE VARIANT call or METADIRECTIVE in those expressions can see constructs that do not enclose it in the source and miss constructs that do.

Relevant spec rules:

- Loop bounds are "evaluated before the construct formed from that directive" (OpenMP 5.2 §4.4.1, 6.0 §6.4.1).
- A clause _expression_ on a compound construct "is evaluated immediately before the construct to which the clause has been split [...] (therefore inside of the outer leaf constructs)" (5.2 §17.2, 6.0 §19.2). The exceptions are NUM_TEAMS and THREAD_LIMIT, which are evaluated before the outermost leaf.
- The construct trait set contains all enclosing constructs, with compound constructs contributing their leaves in nesting order (5.2 §7.1, 6.0 §9.1). In a composite construct, the later leaves are applied inside the earlier ones: DISTRIBUTE encloses PARALLEL DO, and DO encloses SIMD (5.2 §17.3–§17.5, 6.0 §19.1–§19.3).

Observed problems:

1. **Loop bounds of DISTRIBUTE PARALLEL DO [SIMD] see PARALLEL.** `omp.parallel` is created outside `omp.distribute`, and the bounds are emitted inside it. The bounds are outside DISTRIBUTE, PARALLEL and DO in the source, so under any reading of §4.4.1/§6.4.1 they must not see PARALLEL.
2. **NUM_THREADS on TEAMS DISTRIBUTE PARALLEL DO does not see DISTRIBUTE.** NUM_THREADS applies only to the PARALLEL leaf, which DISTRIBUTE encloses. The _expression_ is therefore evaluated inside TEAMS and DISTRIBUTE, and its set is {TEAMS, DISTRIBUTE}. Flang evaluates it before `omp.parallel`, where only `omp.teams` encloses it, so `l` is 1 instead of 2. Counting DISTRIBUTE in the set is the reading used in #<issue-C>; the 6.0 glossary entry for "context-matching property" can be read the other way.
3. **IF(SIMD:) on DO SIMD does not see DO.** The `simd` modifier applies the clause to the SIMD leaf, which DO encloses. The _expression_'s set is therefore {DO}. Flang evaluates it before `omp.wsloop`. The same happens when DO SIMD is a METADIRECTIVE replacement.

For combined constructs such as `parallel do` and `teams distribute parallel do`, the spec does not say whether the loop bounds see the outer leaf constructs (PARALLEL, or TEAMS). Flang evaluates them inside those leaves today, and the fix keeps that behavior. This is an interpretation, not a spec requirement. In the composite case, PARALLEL and DISTRIBUTE are excluded under either reading.

### Reproducer

```fortran
module d
contains
  pure integer function teams_bound(n)
    integer, intent(in) :: n
    teams_bound = n
  end function
  pure integer function parallel_bound(n)
    integer, intent(in) :: n
    parallel_bound = n
  end function
  pure integer function bound(n)
    integer, intent(in) :: n
    !$omp declare variant(teams_bound) match(construct={teams})
    !$omp declare variant(parallel_bound) match(construct={parallel})
    bound = n
  end function

  pure integer function cpu_count(n)
    integer, intent(in) :: n
    cpu_count = n
  end function
  pure integer function scored_count(n)
    integer, intent(in) :: n
    scored_count = n + 1
  end function
  pure integer function thread_count(n)
    integer, intent(in) :: n
    !$omp declare variant(cpu_count) match(device={kind(cpu)})
    !$omp declare variant(scored_count) &
    !$omp& match(implementation={vendor(score(3): llvm)})
    thread_count = n
  end function

  pure integer function in_do(n)
    integer, intent(in) :: n
    in_do = n + 2
  end function
  pure integer function value(n)
    integer, intent(in) :: n
    !$omp declare variant(in_do) match(construct={do})
    value = n
  end function

  ! 1. The loop bounds are outside PARALLEL and DISTRIBUTE.
  subroutine composite_bounds(n, a)
    integer :: n, a(n), i
    !$omp teams distribute parallel do
    do i = 1, bound(n)
      a(i) = i
    end do
  end subroutine

  ! 2. NUM_THREADS sees {TEAMS, DISTRIBUTE}, l = 2: kind(cpu) = 5 > 4.
  subroutine composite_num_threads(n, a)
    integer :: n, a(n), i
    !$omp teams distribute parallel do num_threads(thread_count(n))
    do i = 1, n
      a(i) = i
    end do
  end subroutine

  ! 3. IF(SIMD:) sees DO.
  subroutine do_simd_if(n, a)
    integer :: n, a(n), i
    !$omp do simd if(simd: value(n) > 0)
    do i = 1, n
      a(i) = i
    end do
  end subroutine
end module
```

```console
$ flang -fc1 -fopenmp -fopenmp-version=52 -emit-hlfir repro.f90 -o -
```

| Procedure | Reference | Expected | Actual |
|---|---|---|---|
| `composite_bounds` | `bound(n)` in the loop bounds | Not `parallel_bound`. `teams_bound` if the bounds see the outer TEAMS leaf. | `parallel_bound`, inside `omp.parallel` |
| `composite_num_threads` | `thread_count(n)` | `cpu_count` | `scored_count`: DISTRIBUTE is missing, so `l = 1` |
| `do_simd_if` | `value(n)` | `in_do` | `value`, evaluated before `omp.wsloop` |

The DISTRIBUTE PARALLEL DO SIMD spellings behave the same way.

Assisted with Copilot.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to