Issue 244761
Summary [flang][OpenMP] Variant selection ignores enclosing constructs other than TARGET, TEAMS, PARALLEL, DO and DISPATCH
Labels flang:openmp
Assignees chichunchen
Reporter chichunchen
    Flang builds the construct trait set for METADIRECTIVE and DECLARE VARIANT selection by walking the MLIR operations that enclose the current insertion point (`collectEnclosingConstructTraits` in `flang/lib/Lower/OpenMP/Utils.cpp`). The walk recognizes only `omp.target`, `omp.teams`, `omp.parallel`, `omp.wsloop` and `omp.dispatch`, and skips every other construct. Examples of skipped constructs:

- TASK and TASKLOOP
- DISTRIBUTE
- SECTIONS
- ATOMIC
- TARGET DATA
- loop transformations such as TILE, UNROLL and FUSE

OpenMP 5.2 §7.1 and 6.0 §9.1 define the construct trait set as all constructs that enclose the source location, up to and including the innermost TARGET. Compound constructs contribute their leaf constructs in nesting order. Only the context-matching constructs (TARGET, TEAMS, PARALLEL, DO/FOR, SIMD and DISPATCH) can be named in a `construct` selector, but every other enclosing construct is still a member of the set.

Each member therefore changes `p`, a construct trait's position, and `l`, the size of the set. Scoring uses both (5.2 §7.3, 6.0 §9.3):

- A `construct` trait scores 2^(p-1).
- `device` traits `kind`, `arch` and `isa` score 2^l, 2^(l+1) and 2^(l+2).

The OpenMP Examples document counts DISTRIBUTE and TASK in `l = 6` for the same reason (`selector_scoring.1`, *Context-Based Variants*). Skipping these constructs makes `l` too small, which changes which variant wins.

### Reproducer

```fortran
module c
contains
  subroutine cpu_variant
  end subroutine
  subroutine vendor_variant
  end subroutine
  subroutine weight_base
    !$omp declare variant(cpu_variant) match(device={kind(cpu)})
    !$omp declare variant(vendor_variant) &
    !$omp& match(implementation={vendor(score(3): llvm)})
  end subroutine

  ! Construct set {PARALLEL, TASK}, l = 2: kind(cpu) = 1 + 2**2 = 5 > 4.
  subroutine task_depth()
    !$omp parallel
    !$omp task
    call weight_base()
    !$omp end task
    !$omp end parallel
  end subroutine

  ! Construct set {TEAMS, DISTRIBUTE}, l = 2: expected cpu_variant.
  subroutine distribute_depth(n)
    integer :: n, i
    !$omp teams
    !$omp distribute
    do i = 1, n
      call weight_base()
    end do
    !$omp end teams
  end subroutine

  ! Construct set {PARALLEL, TILE}, l = 2: expected cpu_variant.
  subroutine tile_depth(n)
    integer :: n, i
    !$omp parallel
    !$omp tile sizes(4)
    do i = 1, n
      call weight_base()
    end do
    !$omp end parallel
  end subroutine
end module
```

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

| Procedure | Expected | Actual |
|---|---|---|
| `task_depth` | `cpu_variant`¹ | `vendor_variant`: TASK is not counted |
| `distribute_depth` | `cpu_variant` | `vendor_variant`: DISTRIBUTE is not counted |
| `tile_depth` | `cpu_variant`² | `vendor_variant`: TILE is not counted |

¹ With `l = 2`, `kind(cpu)` scores 1 + 2^2 = 5 and `vendor(score(3): llvm)` scores 1 + 3 = 4. Without TASK, `l = 1`, so `kind(cpu)` scores 3 and the vendor variant wins. This matches the Examples document. Clang does not count TASK either: it selects `vendor_variant` for the equivalent C code.

² Loop transformation constructs are not context-matching. Counting them follows the literal "all enclosing constructs" wording.

A METADIRECTIVE construct selector goes through the same path and is affected in 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