Flang fails to build when a non-host LLVM_DEFAULT_TARGET_TRIPLE is provided
After https://github.com/llvm/llvm-project/commit/2f8b64d327a27a73b57a54eb5d1f28c41e650aee / https://github.com/llvm/llvm-project/pull/95388 builds including flang seem to fail if a non-host LLVM_DEFAULT_TARGET_TRIPLE triple is provided.
For example, building llvm at cb5ec3796ab08b34d9b0c42f654ca3ca026e50d3 using the following cmake invocation (on an x64 Linux machine) fails with errors for me:
$ cmake -G Ninja -DLLVM_ENABLE_PROJECTS="clang;lld;mlir;flang;openmp" -DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabi -DCMAKE_INSTALL_PREFIX=$PWD/../install -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=0n ../llvm-project/llvm
The errors I see are as follows:
[7829/7904] Generating ../../../../include/flang/iso_fortran_env_impl.mod, iso_fortran_env_impl.o
FAILED: cd /path/to/build/tools/flang/tools/f18 && /path/to/cmake/3.23.1/bin/cmake -E make_directory /path/to/build/include/flang && /path/to/build/bin/flang-new -cpp -c -o /path/to/build/tools/flang/tools/f18/iso_fortran_env_impl.o -module-dir /path/to/build/include/flang /path/to/llvm-project/flang/module/iso_fortran_env_impl.f90
error: /path/to/llvm-project/flang/lib/Optimizer/CodeGen/Target.cpp:1114: not yet implemented: target not implemented
LLVM ERROR: aborting
The specific error is presumably due to 32bit arm not being a supported target in Flang, but it seems like the larger issue is inconsistent targets being used--even supported targets (expectedly, I think) have problems. Ex: on an arm64 MacOS machine with the default target triple set to riscv64, libFortranRuntime.a fails to link with the below error:
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: tools/flang/tools/f18/iso_fortran_env_impl.o is not an object file (not allowed in a library)
file shows tools/flang/tools/f18/iso_fortran_env_impl.o is a ELF 64bit RISC-V object file versus the rest which are Mach-O arm64 objects.
Presumably a consistent --target (or equivalent) should be enforced when building iso_fortran_env_impl.o (and the runtime more generally) but I'm not sure if this is an expected/supported use case at the moment or how best to handle this offhand given that the current runtime build setup seems to be intended as a temporary measure.
(Also CC'ing @mjklemm in case you have any thoughts here on how/if this should be resolved)
I think the assessment is right in that this seems to be related to the issues that we are seeing because the Fortran runtime is not really built a true runtime build. The build process for iso_fortran_env_impl.o inherited that to some extend. I think the only true and sustainable solution will be to rework the build process for the Fortran runtime. CC @Meinersbur
I think we cannot reasonable support this and cross-compiling in general, until we build the Fortran runtime using the LLVM_ENABLE_RUNTIMES mechanism. Currently, the Fortran runtime will be built for, and only for, the host platform. Hence you will not be able to run any programs since the runtime is missing.
One could do some manual work and compile the Fortran runtime for the target using a separate LLVM build directory. So if one is blocked only by the build of Flang itself is failing, we maybe should explicitly add -target with the host triple to flang-new when compiling iso_fortran_env_impl such that LLVM_DEFAULT_TARGET_TRIPLE is ignored.
Thanks @mjklemm and @Meinersbur for your input!
One could do some manual work and compile the Fortran runtime for the target using a separate LLVM build directory. So if one is blocked only by the build of Flang itself is failing, we maybe should explicitly add
-targetwith the host triple to flang-new when compiling iso_fortran_env_impl such thatLLVM_DEFAULT_TARGET_TRIPLEis ignored.
FWIW, in the past we would cross-compile the Runtime and Decimal libraries by invoking cmake on the runtime/Decimal folders with appropriate flags for cross-compiling and this seemed to work well enough for what we needed. However, I think that will have the same issue re: mixing targets as above, so I'm personally not bothered if that won't be possible anymore (or if it will only be possible with some manual work, as you mentioned) but I'm not sure if anyone else is/was doing something similar.
I'm not involved enough with Flang to comment on the approach more generally, but what you suggested re: -target with the host triple would work for me at least!