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

    <tr>
        <th>Summary</th>
        <td>
            [Flang][OpenMP] crashes with OpenMP target teams followed by distribute parallel do
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

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

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

<pre>
    Reproducer:
https://godbolt.org/z/615K3sdcG
```f90
!$omp target teams
call foo()

!$omp distribute parallel do
do i=1,2
end do
!$omp end distribute parallel do
!$omp end target teams
end
```

Backtrace:
```console
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang -fc1 -triple x86_64-unknown-linux-gnu -S -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -mllvm -x86-asm-syntax=intel -fopenmp -resource-dir /opt/compiler-explorer/clang-llvmflang-trunk-20250726/lib/clang/22 -mframe-pointer=all -o /app/output.s -x f95 /app/example.f90
 #0 0x00000000041554f8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x41554f8)
 #1 0x00000000041526c4 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000077da15042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000000008644ad7 llvm::OpenMPIRBuilder::calculateCanonicalLoopTripCount(llvm::OpenMPIRBuilder::LocationDescription const&, llvm::Value*, llvm::Value*, llvm::Value*, bool, bool, llvm::Twine const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x8644ad7)
 #4 0x0000000005ee42c1 convertOmpTarget(mlir::Operation&, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) OpenMPToLLVMIRTranslation.cpp:0:0
 #5 0x0000000005ef960a convertHostOrTargetOperation(mlir::Operation*, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) OpenMPToLLVMIRTranslation.cpp:0:0
 #6 0x0000000005efab95 (anonymous namespace)::OpenMPDialectLLVMIRTranslationInterface::convertOperation(mlir::Operation*, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) const OpenMPToLLVMIRTranslation.cpp:0:0
 #7 0x00000000079acef6 mlir::LLVM::ModuleTranslation::convertOperation(mlir::Operation&, llvm::IRBuilderBase&, bool) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x79acef6)
 #8 0x00000000079ad30b mlir::LLVM::ModuleTranslation::convertBlockImpl(mlir::Block&, bool, llvm::IRBuilderBase&, bool) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x79ad30b)
 #9 0x00000000079b6ef3 mlir::LLVM::ModuleTranslation::convertOneFunction(mlir::LLVM::LLVMFuncOp) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x79b6ef3)
#10 0x00000000079b8308 mlir::LLVM::ModuleTranslation::convertFunctions() (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x79b8308)
#11 0x00000000079bd0b6 mlir::translateModuleToLLVMIR(mlir::Operation*, llvm::LLVMContext&, llvm::StringRef, bool) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x79bd0b6)
#12 0x0000000004459e9e Fortran::frontend::CodeGenAction::generateLLVMIR() (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x4459e9e)
#13 0x000000000445f6a8 Fortran::frontend::CodeGenAction::executeAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x445f6a8)
#14 0x00000000041aebcd Fortran::frontend::FrontendAction::execute() (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x41aebcd)
#15 0x000000000419cc3f Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x419cc3f)
#16 0x00000000041b7b4b Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x41b7b4b)
#17 0x00000000024b4c4a fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x24b4c4a)
#18 0x0000000002359c06 main (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x2359c06)
#19 0x000077da15029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#20 0x000077da15029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#21 0x00000000024b3605 _start (/opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin/flang+0x24b3605)
flang-22: error: unable to execute command: Segmentation fault (core dumped)
flang-22: error: flang frontend command failed due to signal (use -v to see invocation)
flang version 22.0.0git (https://github.com/llvm/llvm-project.git 799f28d7a5f155ae5456c87a511fbd18498f6396)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-llvmflang-trunk-20250726/bin
flang-22: note: diagnostic msg: 
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEWF1v2zrS_jX0DSFDoj4sXfhCietzgjdFiyY4twFFjmS-oUiBpBJnf_2CkhzZTptNusd7igamKM7M88wHRQ61VjQKYI3SK5RuFrR3O23WjwE1Rj_bRaX5y_oHdEbznoFBcYnCcudcZ_2QbBHZNppXWrqlNg0i238hss2i9P9iy9kfKCxRFo7_6yL0jyRCJNFthx01DTjsgLYWhSWjUuJaa0RyRAq_8ngxF9YZUfUOcEcNlRIk5hqFJddYoHgTIXJNUFiC4uP8LDpM_Ur8dNkZJFD8mMCI6YqyR2cog9EVr2-ZVlZLQGH5_fZLefcF275qhc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