Skip to content

Support LLVM flang #12306

@h-vetinari

Description

@h-vetinari

Update, now that #13323 has been merged, I'm turning this into a list of further improvements, as opening a follow-up issue was deemed not necessary.

Blocked on upstream

Improvements

  • currently meson says that flang cannot handle fortran_std=legacy (workaround is using fortran_std=none)
  • automatically link to compiler-rt on windows (and other potential configuration).
  • remove default -Wall, which is not supported upstream yet, but currently set as the default in meson
    default_warn_args = ['-Wall']

For reference, in SciPy I currently use the following:

:: default flags for flang in conda-forge (in compiler activation)
set "FC=flang-new"
set "LD=lld-link.exe"
set "FFLAGS=-D_CRT_SECURE_NO_WARNINGS -fms-runtime-lib=dll -fuse-ld=lld -I%LIBRARY_INC%"
set "LDFLAGS=-Wl,-defaultlib:%CONDA_PREFIX:\=/%/lib/clang/@MAJOR_VER@/lib/windows/clang_rt.builtins-x86_64.lib"

:: in SciPy recipe
set "CC=clang-cl"
set "CXX=clang-cl"
:: https://github.com/conda-forge/scipy-feedstock/pull/253#issuecomment-1732578945
set "MESON_RSP_THRESHOLD=320000"

python -m build -w -n -x ^
    [...]
    -Csetup-args=-Dfortran_std=none

Note that -fms-runtime-lib is only supported since flang 18 (llvm/llvm-project@cf1e342)

Correctly handle non-functional versions

  • flang <17 requires a -flang-experimental-exec flag to compile things, and before flang 15/16, it cannot actually do any code generation itself, but relies on an external compiler, c.f. here. Realistically, not much before flang 17 is going to be functional (though apparently some people managed to build SciPy with flang 16).
  • flang 18 is broken for anything that wants to combine -shared with builds that require linking to Fortran_main (SciPy falls into this category); if it's possible to diagnose this and error out that would be great, or perhaps there are work-arounds to suppress/modify the linkage somehow.

Expand testing

  • test on more platforms
    • windows: pretty OK (what I've been testing mainly)
    • linux: should work, but haven't tested it
    • etc.

Previously:

This is a continuation of #10839, which got closed without any changes.

Flang has a long history, the relevant bits of which are that there's nowadays a "classic" flang, and a new LLVM-based flang (called f18 before it got merged into LLVM). Though they share a name, and developed largely by the same people, they're essentially completely different compilers.

As of LLVM 17, flang is starting to become useable (no more experimental flag necessary to generate executable), however, the naming still has not fully settled, and llvm-flang is currently still using flang-new as the name of the binary until some planned refactoring work is done.

One of the main differences that appeared pretty much immediately when testing this in conda-forge, is that llvm-flang does not use the PGI-style -Minform=inform and will throw an error upon that.

Furthermore, it doesn't support -module, but requires -module-dir. Finally, the runtime libs that need to be linked are different:

-        return search_dirs + ['-lflang', '-lpgmath']
+        return search_dirs + ['-lFortranRuntime', '-lFortranDecimal']

The runtime libs are due for a relatively large overhaul, so in addition with the binary name, supporting flang probably means:

  • having a separate class in fortran.py
  • doing some version-based logic that will switch runtime libs and binary name for LLVM 18/19 (or whenever these things land)

Finally, using llvm-flang also runs into #10778, and into some rsp-limitations resp. path mangling issues (for now worked around by raising MESON_RSP_THRESHOLD to ridiculous levels).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions