<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/153173>153173</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missing Flang semantics check causes ICE
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:frontend,
            flang:openmp
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          skatrak
      </td>
    </tr>
</table>

<pre>
    The following invalid OpenMP program causes a compiler crash rather than emitting a semantics error:
```f90
! test.f90
program main
  implicit none
 integer, parameter :: n = 100
  integer, parameter :: expected = n+2
 integer :: i
  integer :: counter

  counter = 0
  !$omp target map(tofrom:counter)
    counter = counter+1
    !$omp teams distribute reduction(+:counter)
    do i=1, n
      counter = counter+1
    end do
 counter = counter+1
  !$omp end target

  print '("Result: "I0)', counter
end program
```
This is triggered both when offloading and when compiling exclusively for the host device.
```shell
flang -fopenmp --offload-arch=gfx1030 test.f90
flang -fopenmp test.f90
```
The specific OpenMP restriction being violated by the previous example is found in the specification version 6.0, page 395, lines 33 -- 35 (emphasis mine):
> [...] If a `teams` region is nested inside a `target` region, the corresponding `target` construct must not contain any statements, declarations or directives outside of the corresponding `teams` construct.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVN2O4ygTfRpyU0qEoZ2fC1-kOxNpLkbfp9W8AIayXdsYLApnut9-hZN0p1e7s1Ikh6rinFPUAcNMfUBsRP0s6tPKzHmIqeFXk5N5XbXRvTc_B4Queh9_UeiBwsV4cvC_CcOP_8OUYp_MCNbMjAwGbBwn8pjAJsMDJJMHTJAHEwBHyrlgGGAcTchkGTClmIQ-CnkUW3n9dQdZlqqCjJw31-WdaTQUhDwC0Dh5spQhxIAlQiFjj0moF5hMMiNmTFCg9RECCH2CSsrr1t9U4tuENqNbNgShntUD9r2IHmHuQRvnkDEV6SV7Wy44V1qhKqGe4jhBNqnHD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