[Flang] `.mod` files built into `include/flang/` do not support multiple target architectures
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
When Flang is built with multiple target architectures specified for `-DLLVM_TARGETS_TO_BUILD`, `.mod` files are created only for the default target architecture and not for other architectures.
This becomes a problem, for example, when building on an X86_64 machine targeting both X86_64 and AArch64.
- CMake configuration:
```
cmake \
-G Ninja \
-DCMAKE_INSTALL_PREFIX=/ \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_PROJECTS="clang;mlir;flang" \
-DLLVM_TARGETS_TO_BUILD="AArch64;X86" \
-DLLVM_DEFAULT_TARGET_TRIPLE="x86_64-unknown-linux-gnu" \
//llvm-project/llvm
```
In this case, `.mod` files are likely built by [this command](https://github.com/llvm/llvm-project/blob/66ba9dc62ba647d5dcefdcab06f21d38f6b2dd5f/flang/tools/f18/CMakeLists.txt#L115-L116), but since no target architecture is specified, they seem to be built only for the default X86_64. This leads to cross-compilation failures like the following:
- foo.f90:
```fortran
program main
use, intrinsic :: IEEE_ARITHMETIC
implicit none
end program
```
- Compilation error:
```
$ flang --target=aarch64-unknown-linux-gnu --sysroot=/libc --gcc-toolchain= foo.f90 -c -o x86_to_aarch.o
error: Semantic errors in foo.f90
//bin/../include/flang/ieee_arithmetic.mod:143:1: error: REAL(KIND=10) is not an enabled type for this target
real(10),intent(in)::x
^^^^^^^^^^^^^^^^^^^^^^
//bin/../include/flang/ieee_arithmetic.mod:183:1: error: REAL(KIND=10) is not an enabled type for this target
real(10),intent(in)::y
^^^^^^^^^^^^^^^^^^^^^^
//bin/../include/flang/ieee_arithmetic.mod:223:1: error: REAL(KIND=10) is not an enabled type for this target
real(10),intent(in)::y
^^^^^^^^^^^^^^^^^^^^^^
...
```
This error seems to occur because `ieee_arithmetic.mod` is built for X86_64 with `REAL(KIND=10)` included, and then compiled for AArch64. A similar issue was also reported in [#146876](https://github.com/llvm/llvm-project/issues/146876).
Other modules also contain values that differ from the target architecture because they are built for X86_64. We have confirmed the following:
- `iso_c_binding.mod`
- Built natively for AArch64
- `integer(4),parameter::c_long_double=16_4`
- Built on X86_64 with `-DLLVM_TARGETS_TO_BUILD="AArch64;X86"`
- `integer(4),parameter::c_long_double=10_4`
- `iso_fortran_env_impl.mod`
- Built natively for AArch64
- `integer(4),parameter::__builtin_real_kinds(1_8:*)=[INTEGER(4)::2_4,3_4,4_4,8_4,16_4]`
- Built on X86_64 with `-DLLVM_TARGETS_TO_BUILD="AArch64;X86"`
- `integer(4),parameter::__builtin_real_kinds(1_8:*)=[INTEGER(4)::2_4,3_4,4_4,8_4,10_4]`
To resolve this, it might be necessary to change the build process to create all `.mod` files for each target architecture and then select the appropriate `.mod` file during compilation.
Contributor guide
Research direction
Start with the `.mod` generation command in `flang/tools/f18/CMakeLists.txt` at lines 115-116, then reproduce the issue using the multi-target CMake configuration and the AArch64 cross-compilation command. Trace how target architecture is selected for the generated modules and determine how architecture-specific modules could be built and selected. Done means the demonstrated intrinsic modules compile correctly for each target architecture.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake
- Domain
- build-system, compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100