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

    <tr>
        <th>Summary</th>
        <td>
            [Flang][AArch64] Compilation error of a internal subroutine subprogram that use variables defined in the host program
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          yus3710-fj
      </td>
    </tr>
</table>

<pre>
    This is an issue from Fujitsu testsuite.

Flang-new terminates abnormally when compiling a internal subroutine subprogram that use variables defined in the host program.
This issue isn't reproduced when the target is X86.

The following are the test program, Flang-new and gfortran compilation result.

```fortran
! test.f90
  integer i
  call sub(inn)
 contains
    subroutine inn
      i = 1
    end subroutine inn
end
```
```console
$ flang-new -v test.f90 -c
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 701e6f7630474b637e0bc45d009bf2ec47f2d3fd)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Selected GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o test.o -x f95-cpp-input test.f90
unimplemented operand
UNREACHABLE executed at /path/to/llvm-project/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:5872!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /path/to/install/bin/flang-new -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o test.o -x f95-cpp-input test.f90
1.      Running pass 'Function Pass Manager' on module 'FIRModule'.
2.      Running pass 'AArch64 Instruction Selection' on function '@_QQmain'
 #0 0x0000ffff81ee5f60 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/install/bin/../lib/libLLVMSupport.so.18git+0x1a5f60)
 #1 0x0000ffff81ee3b40 llvm::sys::RunSignalHandlers() (/path/to/install/bin/../lib/libLLVMSupport.so.18git+0x1a3b40)
 #2 0x0000ffff81ee3cb0 SignalHandler(int) Signals.cpp:0:0
 #3 0x0000ffff839c066c (linux-vdso.so.1+0x66c)
 #4 0x0000ffff818c2dbc raise (/lib64/libc.so.6+0x32dbc)
 #5 0x0000ffff818b0928 abort (/lib64/libc.so.6+0x20928)
 #6 0x0000ffff81e1895c (/path/to/install/bin/../lib/libLLVMSupport.so.18git+0xd895c)
 #7 0x0000ffff8350ffd0 llvm::AArch64TargetLowering::LowerOperation(llvm::SDValue, llvm::SelectionDAG&) const (/path/to/install/bin/../lib/libLLVMAArch64CodeGen.so.18git+0x26ffd0)
 #8 0x0000ffff8123d444 (anonymous namespace)::SelectionDAGLegalize::LegalizeOp(llvm::SDNode*) LegalizeDAG.cpp:0:0
 #9 0x0000ffff81248e94 llvm::SelectionDAG::Legalize() (/path/to/install/bin/../lib/../lib/libLLVMSelectionDAG.so.18git+0x148e94)
#10 0x0000ffff813e8ff4 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/path/to/install/bin/../lib/../lib/libLLVMSelectionDAG.so.18git+0x2e8ff4)
#11 0x0000ffff813ec4dc llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/path/to/install/bin/../lib/../lib/libLLVMSelectionDAG.so.18git+0x2ec4dc)
#12 0x0000ffff813ede08 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.0) SelectionDAGISel.cpp:0:0
#13 0x0000ffff80aae570 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#14 0x0000ffff7f4404a4 llvm::FPPassManager::runOnFunction(llvm::Function&) (/path/to/install/bin/../lib/../lib/libLLVMCore.so.18git+0x2904a4)
#15 0x0000ffff7f4406e0 llvm::FPPassManager::runOnModule(llvm::Module&) (/path/to/install/bin/../lib/../lib/libLLVMCore.so.18git+0x2906e0)
#16 0x0000ffff7f440fe8 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/path/to/install/bin/../lib/../lib/libLLVMCore.so.18git+0x290fe8)
#17 0x0000ffff82bd0c6c Fortran::frontend::CodeGenAction::executeAction() (/path/to/install/bin/../lib/libflangFrontend.so.18git+0x120c6c)
#18 0x0000ffff82b02b2c Fortran::frontend::FrontendAction::execute() (/path/to/install/bin/../lib/libflangFrontend.so.18git+0x52b2c)
#19 0x0000ffff82af43c4 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/path/to/install/bin/../lib/libflangFrontend.so.18git+0x443c4)
#20 0x0000ffff83053bbc Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/path/to/install/bin/../lib/libflangFrontendTool.so.18git+0x3bbc)
#21 0x00000000004048dc fc1_main(llvm::ArrayRef<char const*>, char const*) (/path/to/install/bin/flang-new+0x4048dc)
#22 0x0000000000403108 main (/path/to/install/bin/flang-new+0x403108)
#23 0x0000ffff818b0d64 __libc_start_main (/lib64/libc.so.6+0x20d64)
#24 0x00000000004035d0 _start (/path/to/install/bin/flang-new+0x4035d0)
flang-new: error: unable to execute command: Aborted (core dumped)
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 701e6f7630474b637e0bc45d009bf2ec47f2d3fd)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
flang-new: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-new: note: diagnostic msg: /tmp/test-398d2a
flang-new: note: diagnostic msg: /tmp/test-398d2a.sh
flang-new: note: diagnostic msg: 

********************
```
```console
$ gfortran -v test.f90 -c
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
 :
$
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWV9z26gW_zTkhZFHRrIsP-TBseO0M-kmm2R371sGwZFMi0AXUP7sp78DkmzLTdJu2t6He3eyaQTo8Pudv-hArRWVAjhFszM0W5_Q1m21OX1ubTKfxlH5-aTQ_Pn0bissFhZThYW1LeDS6Bpv2s_C2RY7sM62wsEExWsUL7vfG0lVFSl4xA5MLRR1YDEtlDY1lfIZP25BYabrRkihKkyxUA6MohLbtjC6dUKB_7MxujK0xm5LHW4t4AdqBC0kWMyhFAo4Fgq7LeCttg73y3soPXAPWViFyNxhA43RvGXAOwj-TUdNBc4z_FeejVjcbQGXWkr9GEAa6NbDfidEVnjPlSqOq1IbZ-jAjjqhFTZgW-lGslEWdz_9-n6UTIP8SbmIuxEcVFOBwWIYYFQGPSGSC6UQWfQTTCtHhbLDOnyoTL9yN46xwChZ4-l-CBR_cTkofgT46JFpZbWEAX-Ky50-oocdGRyxbsV-9gGM9cqZ5pN4EmNE8q1zjUXJEpENIptKuG1bTJiuEdlI-TD8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