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

    <tr>
        <th>Summary</th>
        <td>
            [flang] compilation hangs in CheckScopeConstraints
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          k-arrows
      </td>
    </tr>
</table>

<pre>
    The following test program causes a hang, and it is documented in llvm-test-suite. (See https://github.com/llvm/llvm-test-suite/blob/5942ca45a1036ace15b70e117d636ecf89224957/Fortran/gfortran/regression/DisabledFiles.cmake#L1974)
https://github.com/llvm/llvm-test-suite/blob/main/Fortran/gfortran/regression/goto_5.f90

I will write here what I confirmed about this hang using the perf command.

perf record & perf report:
```console
$ timeout 10 perf record -g flang-new -c goto_5.f90
[ perf record: Woken up 10 times to write data ]
[ perf record: Captured and wrote 2.393 MB perf.data (39133 samples) ]
$ perf report | c++filt
```

Here is the output of the perf report command:
```console
    99.88%    99.88%  flang-new flang-new             [.] Fortran::semantics::CheckScopeConstraints(std::vector<Fortran::semantics::SourceStatementInfoTuplePOD, std::allocator<Fortran::semantics::SourceStatementInfoTuplePOD> > const&, std::map<unsigned long, Fortran::semantics::LabeledStatementInfoTuplePOD, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, Fortran::semantics::LabeledStatementInfoTuplePOD> > > const&, std::vector<Fortran::semantics::ScopeInfo, std::allocator<Fortran::semantics::ScopeInfo> > const&, Fortran::semantics::SemanticsContext&)
            |
 ---Fortran::semantics::CheckScopeConstraints(std::vector<Fortran::semantics::SourceStatementInfoTuplePOD, std::allocator<Fortran::semantics::SourceStatementInfoTuplePOD> > const&, std::map<unsigned long, Fortran::semantics::LabeledStatementInfoTuplePOD, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, Fortran::semantics::LabeledStatementInfoTuplePOD> > > const&, std::vector<Fortran::semantics::ScopeInfo, std::allocator<Fortran::semantics::ScopeInfo> > const&, Fortran::semantics::SemanticsContext&)
```

>From this output, we can see that almost all the time of compilation is spent with the symbol `Fortran::semantics::CheckScopeConstraints`. This symbol seems to come from a function called `CheckScopeConstraints` in the namespace `Fortran::semantics`.

https://github.com/llvm/llvm-project/blob/d923f4c2d824f42817d5a1d8fee9272ce3923f42/flang/lib/Semantics/resolve-labels.cpp#L989
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVk-P27YT_TT0ZWBBIvWPBx927Ri_APmhBTZAjwVFjix2KVIgqbj59gUlZ-1NspukORWIYVuixHnz5s1IMyIEfbKIO1Ldk-qwEXMcnN89boX37hw2nVMfd-8HhN4Z487aniBiiDB5d_JiBCnmgAEEDMKeCN2DsAp0BB1AOTmPaCMq0BaM-TBuk-k2zDpiBoS2D4gwxDgFwu4IPRJ6POk4zF0m3UjoMZlcDjeWhB474zpCjxUvqRRlJYqc1UJiUXVNjkXRqJrVKPuWU1ryqiH0eHQ-emGTh_7p1OPJYwjapcVBB9EZVEdtMGRyFI9IKHtX8KYklJP8QPK7n-A6Cm2_i8fJRfdnlfU8X32u_2_hrI2Bs9cRYUCPcB5EhLcgne21H1GB6NwcIQ46LLmAOSzJGhAm9D1IN47CquwWdbnhUTqvgNAaLuvJ-ZiiXHfW-fqVzgZn8HKVlhD1iMllkcMt0PYEvRH2tLV4hq2ELwKq7m_3E3YHf7hHtDBPCSqhBojuEqsSUQCpDi_a7sUUZ5_itwrO3kUEmjHO4P_r1mxFoC3jBWMQxDgZDITyG1Ra3oYOpNmDJPSe0Ptem_iZDrcC_i9lQodFZTfHaY7g-qvmF7yL9N-QFACA86xtCa2en1_lvJ7dfkh1n5HqAJ-Ki90RdhdwFDZqGdblfkD5-CDdhHtnQ_RC2xgIbUNU64YPKKPzhO1fQ3lws5f4EEXE9GS_tb17P08Gf__tkJ79JzBhjJPi5_DYG0i_pFEktH6GP4qJsP1sl3eXAuPWd89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