Compiling with flang-new

I just compiled the flang project in LLVM 15.0.5 and tried to compile a simple hello world code with flang-new. It works, but only if I link against the C++ library:

flang-new -flang-experimental-exec hello.f90 -lc++

Is that the expected usage? I was a bit surprised by having to add -lc++, and I didn’t see this in the docs or get any hits when I looked for this in the (latest) CMake support for flang.

Maybe I missed some configuration flags when building?

For reference my configuration command looks like

cmake  -DCMAKE_INSTALL_PREFIX=/home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0 -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF  -DLLVM_ENABLE_PROJECTS="polly;lld;lldb;clang-tools-extra;flang;llvm;clang"  -DLLVM_ENABLE_RUNTIMES="libunwind;libcxx;libcxxabi;compiler-rt;openmp"  -DGCC_INSTALL_PREFIX='/project/boegelbot/Rocky8/haswell/software/GCCcore/11.3.0'  -DLLVM_REQUIRES_RTTI=ON  -DLLVM_ENABLE_RTTI=ON  -DLLVM_ENABLE_EH=ON  -DLLVM_ENABLE_ASSERTIONS=ON  -DLLVM_POLLY_LINK_INTO_TOOLS=ON  -DLLVM_ENABLE_Z3_SOLVER=ON  -DLLVM_Z3_INSTALL_DIR=/project/boegelbot/Rocky8/haswell/software/Z3/4.10.2-GCCcore-11.3.0  -DLLVM_TARGETS_TO_BUILD="X86"  -DLIBOMP_USE_HWLOC=ON  -DLIBOMP_HWLOC_INSTALL_DIR=/project/boegelbot/Rocky8/haswell/software/hwloc/2.7.1-GCCcore-11.3.0  /home/ocaisa/.local/easybuild/build/Clang/15.0.5/GCCcore-11.3.0/llvm-project-15.0.5.src/llvm

That shouldn’t be required. What is it complaining with when you don’t supply the C++ library?

[ocaisa@login1 ~]$ flang-new -flang-experimental-exec hello.f90 -lc++
[ocaisa@login1 ~]$ flang-new -flang-experimental-exec hello.f90
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o): in function `__clang_call_terminate':
io-api.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x2): undefined reference to `__cxa_begin_catch'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: io-api.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x7): undefined reference to `std::terminate()'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o): in function `std::enable_if<is_constructible_v<std::variant_alternative<3ul, std::variant<std::monostate, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)0, char>, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)1, char>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::InquireUnitState, Fortran::runtime::io::ErroneousIoStatementState> >::type, Fortran::runtime::io::ChildIo&, char const*&, int&>, std::variant_alternative<3ul, std::variant<std::monostate, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)0, char>, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)1, char>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::InquireUnitState, Fortran::runtime::io::ErroneousIoStatementState> >::type&>::type std::variant<std::monostate, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)0, char>, Fortran::runtime::io::ChildFormattedIoStatementState<(Fortran::runtime::io::Direction)1, char>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildListIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)0>, Fortran::runtime::io::ChildUnformattedIoStatementState<(Fortran::runtime::io::Direction)1>, Fortran::runtime::io::InquireUnitState, Fortran::runtime::io::ErroneousIoStatementState>::emplace<3ul, Fortran::runtime::io::ChildIo&, char const*&, int&>(Fortran::runtime::io::ChildIo&, char const*&, int&)':
io-api.cpp:(.text._ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_[_ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_]+0x7e): undefined reference to `__cxa_allocate_exception'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: io-api.cpp:(.text._ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_[_ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_]+0xa5): undefined reference to `std::exception::~exception()'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: io-api.cpp:(.text._ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_[_ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_]+0xad): undefined reference to `__cxa_throw'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: io-api.cpp:(.text._ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_[_ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_]+0xb2): undefined reference to `__cxa_allocate_exception'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: io-api.cpp:(.text._ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_[_ZNSt7variantIJSt9monostateN7Fortran7runtime2io30ChildFormattedIoStatementStateILNS3_9DirectionE0EcEENS4_ILS5_1EcEENS3_25ChildListIoStatementStateILS5_0EEENS8_ILS5_1EEENS3_32ChildUnformattedIoStatementStateILS5_0EEENSB_ILS5_1EEENS3_16InquireUnitStateENS3_25ErroneousIoStatementStateEEE7emplaceILm3EJRNS3_7ChildIoERPKcRiEEENSt9enable_ifIX18is_constructible_vINSt19variant_alternativeIXT_ESG_E4typeEDpT0_EERSR_E4typeEDpOSS_]+0xd1): undefined reference to `__cxa_begin_catch'

<...LOTS MORE CONTENT LIKE THIS...>

/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o):(.data.rel.ro._ZTISt18bad_variant_access[_ZTISt18bad_variant_access]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o):(.data.rel.ro._ZTISt18bad_variant_access[_ZTISt18bad_variant_access]+0x10): undefined reference to `typeinfo for std::exception'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o):(.data.rel.ro._ZTVSt18bad_variant_access[_ZTVSt18bad_variant_access]+0x10): undefined reference to `std::exception::~exception()'
/project/boegelbot/Rocky8/haswell/software/binutils/2.38-GCCcore-11.3.0/bin/ld: /home/ocaisa/.local/easybuild/software/Clang/15.0.5-GCCcore-11.3.0/lib/libFortranRuntime.a(io-api.cpp.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
flang-new: error: linker command failed with exit code 1 (use -v to see invocation)

I’m wondering if this is becasue you are using libcxx rather than libstdc++.

Not that it shouldn’t work to use that, but maybe there are bugs/features related to that combination.

There are no uses of exceptions etc in the flang/runtime/io-api.cpp itself, as you’d expect, they are called by the Fortran code, which doesn’t know anything about C++ exceptions. The same applies to all the other runtime code.

I’m not the right guy to debug this, I’m just explaining what I can see as errors and what I know about the runtime code (which is what is “missing symbols” - but shouldn’t be)

A surprise in your cmake line were:
-DLLVM_REQUIRES_RTTI=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_ENABLE_EH=ON
These are typically off for LLVM builds. Some of the error messages are related to exception handling.

For your Flang use-case this is probably overkill:

  • DLLVM_ENABLE_PROJECTS=“polly;lld;lldb;clang-tools-extra;flang;llvm;clang”
  • DLLVM_ENABLE_RUNTIMES=“libunwind;libcxx;libcxxabi;compiler-rt;openmp”

Those settings are all enabled by default with EasyBuild, I wanted to see what everything looks like together. The idea is start preparing EasyBuild for a complete LLVM toolchain (clang + flang).

I’m rebuilding without RTTI (which disables those 3 settings you mentioned) and will test it again tomorrow.

Confirmed, removing

-DLLVM_REQUIRES_RTTI=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_ENABLE_EH=ON

removes the need for linking to libc++. Is that requirement something that can be embedded in the CMake logic?

You did not remove them. They are off by default.

Sorry, I’m not being clear, I set enable_rtti = False within EasyBuild, which removes the configuration text -DLLVM_REQUIRES_RTTI=ON -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON from the CMake configure command used by EasyBuild (and switches us back to the default for these values).

What I was asking is if there is a way within the CMake packaging for flang to capture the fact that these are set and complain about it (especially if there is no plan to support them being enabled).

We are not the only ones to set these options, Spack also does this: spack/package.py at 6997991ad2ba58f2c70515a36e985a7c70fc1b2f · spack/spack · GitHub

cmake has support for the LLVM Flang:

Maybe cmake takes care of that problem.

I’m not overly familiar with CMake (nor the LLVM build system to know the implications of this), but what I meant was adding something along the lines of

if(LLVM_REQUIRES_RTTI)
  message(WARNING "RTTI support requires additional linking when using the flang-new compiler, you might be better off turning this off")  
endif()

to llvm-project/CMakeLists.txt at main · llvm/llvm-project · GitHub

Why are you setting LLVM_REQUIRES_RTTI? As far as I know that’s an undocumented internal flag for subdirectories to mark that they require RTTI (e.g. for tests). The other two are fine to set, though I suspect they’re erroneously leaking into the build of flang/runtimes if that still doesn’t work.

The LLVM CMake documentation:
https://llvm.org/docs/CMake.html
shows:

  • LLVM_ENABLE_RTTI
  • LLVM_ENABLE_EH
    I believe that LLVM_ENABLE_EH is the bigger issue because it needs library support.

Maybe the correct solution is to turn the exception handling off when compiling the Fortran runtime - as I stated above, there should be no exceptions coming out of the runtime - there is nothing that catches them… :slight_smile:

In the flang style guidelines flang/docs/C++style.md:

  1. Never throw or catch exceptions.
  2. Never use run-time type information or dynamic_cast<>.

Likely these prohibitions should be enforced in the CMakefile for the runtime.

@ocaisa File an issue in GitHub to track this?

1 Like

Sorry for the delay, issue opened at `flang` build not enforcing style guidelines · Issue #59353 · llvm/llvm-project · GitHub