llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.33k stars 11.7k forks source link

[Flang][OpenMP] fatal internal error: SemanticsContext::FindScope(): invalid source location for 'end subroutine' #82913

Closed k-arrows closed 2 weeks ago

k-arrows commented 7 months ago

Compiling the following program with -fopenmp causes a crash.

$ cat test.f90
impure elemental module subroutine e()
end subroutine

Compile with: flang-new -fopenmp test.f90

Output:

fatal internal error: SemanticsContext::FindScope(): invalid source location for 'end subroutine'

Stack dump (see Godbolt: https://godbolt.org/z/bzY8xPz7Y)

 #0 0x000000000355b248 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x355b248)
 #1 0x0000000003558bbc SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f0e69e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007f0e69e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007f0e69e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007f0e69e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00000000049f4339 (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x49f4339)
 #7 0x0000000003e87dd9 (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3e87dd9)
 #8 0x00000000040b2b16 Fortran::semantics::ResolveOmpTopLevelParts(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x40b2b16)
 #9 0x0000000003e67c78 Fortran::semantics::ResolveNames(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&, Fortran::semantics::Scope&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3e67c78)
#10 0x0000000003ebe491 Fortran::semantics::Semantics::Perform() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3ebe491)
#11 0x000000000359e097 Fortran::frontend::FrontendAction::runSemanticChecks() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x359e097)
#12 0x00000000038bfe00 Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x38bfe00)
#13 0x000000000359d695 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x359d695)
#14 0x000000000358d437 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x358d437)
#15 0x00000000035a38fd Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x35a38fd)
#16 0x0000000001d6bfa8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1d6bfa8)
#17 0x0000000001c7124d main (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1c7124d)
#18 0x00007f0e69e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#19 0x00007f0e69e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#20 0x0000000001d6aeee _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1d6aeee)
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 19.0.0git (https://github.com/llvm/llvm-project.git 1e98d4883d78ac2c65b87e24694e8b2f1dc9f02d)
Target: x86_64-unknown-linux-gnu
Thread model: posix
llvmbot commented 7 months ago

@llvm/issue-subscribers-flang-frontend

Author: None (k-arrows)

Compiling the following program with `-fopenmp` causes a crash. ```console $ cat test.f90 impure elemental module subroutine e() end subroutine ``` Compile with: `flang-new -fopenmp test.f90` Output: ```console fatal internal error: SemanticsContext::FindScope(): invalid source location for 'end subroutine' ``` Stack dump (see Godbolt: https://godbolt.org/z/bzY8xPz7Y) ```console #0 0x000000000355b248 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x355b248) #1 0x0000000003558bbc SignalHandler(int) Signals.cpp:0:0 #2 0x00007f0e69e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #3 0x00007f0e69e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc) #4 0x00007f0e69e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476) #5 0x00007f0e69e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3) #6 0x00000000049f4339 (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x49f4339) #7 0x0000000003e87dd9 (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3e87dd9) #8 0x00000000040b2b16 Fortran::semantics::ResolveOmpTopLevelParts(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x40b2b16) #9 0x0000000003e67c78 Fortran::semantics::ResolveNames(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&, Fortran::semantics::Scope&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3e67c78) #10 0x0000000003ebe491 Fortran::semantics::Semantics::Perform() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x3ebe491) #11 0x000000000359e097 Fortran::frontend::FrontendAction::runSemanticChecks() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x359e097) #12 0x00000000038bfe00 Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x38bfe00) #13 0x000000000359d695 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x359d695) #14 0x000000000358d437 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x358d437) #15 0x00000000035a38fd Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x35a38fd) #16 0x0000000001d6bfa8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1d6bfa8) #17 0x0000000001c7124d main (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1c7124d) #18 0x00007f0e69e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90) #19 0x00007f0e69e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40) #20 0x0000000001d6aeee _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240225/bin/flang-new+0x1d6aeee) 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 19.0.0git (https://github.com/llvm/llvm-project.git 1e98d4883d78ac2c65b87e24694e8b2f1dc9f02d) Target: x86_64-unknown-linux-gnu Thread model: posix ```
tshaktiv commented 3 months ago

Shouldn't it be enclosed within the module or submodule?

I get error in other compiler as well:

$ gfortran x.f90 -fopenmp && ./a.out
x.f90:35:8:

   35 | module subroutine e()
      |        1
Error: MODULE prefix at (1) found outside of a module, submodule, or interface
x.f90:37:3:

   37 | end subroutine
      |   1
Error: Expecting END PROGRAM statement at (1)

But the following works:

module m

interface
    impure elemental module subroutine e()
    end subroutine
end interface
end module
k-arrows commented 3 months ago

The compiler should not crash even if the input is erroneous as a program.

Thirumalai-Shaktivel commented 3 months ago

Yes, that's right. I think this can be fixed with similar fix: https://github.com/llvm/llvm-project/pull/71944/files