flang: update support and add flang-rt - #452306
Conversation
|
Continuing the work from @picostove's seemly inactive PR #391942. Requesting reviews from original reviewers in case I miss something. Any feedback would be greatly appreciated. |
|
Build took an extremely long time. wasn't expecting this though |
|
|
I think I just ran into a bug with nixpkgs-review, the build is still running. |
|
alyssais
left a comment
There was a problem hiding this comment.
I think this should be split into multiple commits. There's a lot going on here.
ff9c766 to
1c38124
Compare
There was a problem hiding this comment.
Pull request overview
Updates the LLVM Flang Fortran toolchain packaging to include the new flang-rt runtime and adjusts cc-wrapper/toolchain plumbing so Flang can be used as a wrapped compiler in nixpkgs (including handling Flang’s -fc1 mode).
Changes:
- Update
flangpackaging to includeflang-rtsources and export language capability passthru flags forcc-wrapper. - Add a new
flang-rtderivation (runtime library) and wire it into the LLVM toolchain set. - Extend
cc-wrapperto recognize-fc1ascc1-like and add initial Fortran wrapper support forflang.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkgs/development/compilers/llvm/common/flang/default.nix | Adds flang-rt to the extracted monorepo source and exports passthru.lang* fields for wrapper behavior. |
| pkgs/development/compilers/llvm/common/flang-rt/default.nix | Introduces the flang-rt runtime build from LLVM runtimes. |
| pkgs/development/compilers/llvm/common/default.nix | Adds flang-unwrapped, flang-rt, and a Flang adapter wrapper; wires wrapped flang to include runtime libs. |
| pkgs/development/compilers/llvm/common/clang/default.nix | Explicitly marks clang as providing C and C++. |
| pkgs/build-support/cc-wrapper/default.nix | Updates Fortran wrapping logic to optionally wrap flang; adjusts a libc++ flags condition to use cc.langCC. |
| pkgs/build-support/cc-wrapper/cc-wrapper.sh | Treats -fc1 like -cc1 to avoid injecting wrapper flags in that mode. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@alyssais I've reworked the approach based on your feedback. The standalone wrapper script is gone. Instead of silently dropping flags, the new revision handles flag compatibility at three levels:
No user-supplied flags are dropped. Flags that flang doesn't support either produce a proper diagnostic (via backported patches) or are never injected in the first place (via cc-wrapper / hardeningUnsupportedFlags). Ready for another look when you have time. |
|
@emilazy Take a look when you have time. It seems |
|
I think I won’t be able to get around to re‐reviewing this PR in a timely manner – apologies! – so I’ve dismissed my blocking review to not hold it up. (Looks like @alyssais still has a blocking review, though.) |
alyssais
left a comment
There was a problem hiding this comment.
This looks so much better. Thanks a lot for reworking it. I don't think it's far off now.
Build on the initial LLVM Flang wrapper work by exposing the needed clang passthru and teaching cc-wrapper about flang-specific flag handling. Co-authored-by: stove <stove@rivosinc.com> Co-authored-by: acture <acture@gmail.com> Co-authored-by: Emily <hello@emily.moe> Co-authored-by: Philip Taron <philip.taron@gmail.com>
Add a standalone derivation for LLVM's flang-rt runtime libraries (libFortranRuntime, libFortranDecimal). flang-rt is a runtimes-style package: building it requires running a working flang binary at *build* time, but the produced libraries are linked into derivations targeting the *host*. The runtime is therefore wired up using `buildFlang`, sourced from `buildLlvmPackages.flang-unwrapped` so that the build-platform flang (spliced via the LLVM package set) compiles the runtime, while the resulting libraries are still produced for the host platform. This mirrors the existing `buildLlvmTools.tblgen` / `buildLlvmPackages.tblgen` pattern used elsewhere in the LLVM package set. `unwrapped` rather than the wrapped flang is used because flang-rt's CMake glue invokes the compiler directly and does not need (or want) Nix's cc-wrapper resource-dir and rpath plumbing during the runtime build. flang-rt only consumes LLVM's CMake helpers and the build-time flang; it does not link against MLIR or libclang, so neither MLIR_DIR nor CLANG_DIR is passed and the unused empty `dev` output is omitted. Co-authored-by: stove <stove@rivosinc.com> Co-authored-by: acture <acture@gmail.com>
Wire flang into llvmPackages as a first-class Fortran compiler. flang
is built standalone on top of the LLVM/clang package set rather than
bundled into the llvm derivation, with passthru metadata
(`langFortran`, `isFlang`, `isClang`, `hardeningUnsupportedFlags`) so
cc-wrapper and downstream tooling can detect and adapt to it.
Driver compatibility patches backported from upstream are applied
selectively per LLVM version:
* `use-xflang-in-diagnostics` is applied to LLVM 20 and newer; it
teaches the driver to suggest `-Xflang` instead of `-Xclang` in
error messages for options only available to `flang -fc1`.
* `warn-on-fbuiltin-and-fno-builtin` and
`accept-and-ignore-some-gfortran-optimization-flags` are applied
to LLVM 20 and 21 only. LLVM 22 has equivalent behaviour merged
upstream (`warn_drv_invalid_argument_for_flang` and
`clang_ignored_gcc_optimization_f_Group` handling in
clang/lib/Driver/ToolChains/Flang.cpp), so the patches are skipped
there.
Patches live under pkgs/development/compilers/llvm/21/flang/ and are
shared across versions via patches.nix. They are applied to a private
libclang variant rather than the flang source tree because standalone
flang resolves driver/option definitions through the installed libclang
package.
Two focused passthru tests are added:
* `compile-smoke` exercises basic compilation and `@response-file`
handling.
* `driver-flags` covers wrapper flag isolation
(`NIX_CFLAGS_COMPILE` must not leak into flang;
`NIX_FFLAGS_COMPILE` must reach it), the backported driver
diagnostics, and regression coverage for previously hard-erroring
flags.
Build on the earlier standalone flang work by @picostove.
Co-authored-by: stove <stove@rivosinc.com>
Co-authored-by: acture <acture@gmail.com>
Co-authored-by: Alyssa Ross <hi@alyssa.is>
MLIRConfig.cmake unconditionally overwrites MLIR_TABLEGEN_EXE and does not create an imported target, breaking standalone builds that provide their own mlir-tblgen binary (e.g. Nix sandboxed builds). The patch adds guards to respect caller-set MLIR_TABLEGEN_EXE, MLIR_PDLL_TABLEGEN_EXE, and MLIR_SRC_SHARDER_TABLEGEN_EXE values, and auto-creates an imported mlir-tblgen target for downstream consumers. This replaces the previous dummy-target workaround in flang's CMakeLists.txt. Upstream issue: llvm/llvm-project#150986 Co-authored-by: Alexander Bantyev <balsoft@balsoft.ru>
|
Also reorganized into 4 atomic commits:
@alyssais Take another look when you have time. |
alyssais
left a comment
There was a problem hiding this comment.
I did not expect it could turn out so nicely. Thank you!
|
I'll leave it a bit to give other people a chance for last comments. Ping me in a week if it looks ready for merge then. :) |
@alyssais Looks like it's ready. |
Supersedes #391942, and should close #516651.
Summary
Package LLVM Flang as a first-class Fortran compiler in nixpkgs, with runtime library, cc-wrapper integration, and driver compatibility patches backported from upstream.
Based on the excellent initial implementation by @picostove.
Key Changes
Packages:
cc-wrapper integration:
Driver patches (backported from upstream):
MLIR: fix MLIRConfig.cmake for standalone builds
MLIRConfig.cmake unconditionally overwrites
MLIR_TABLEGEN_EXE, breaking standalonebuilds that provide their own pre-built
mlir-tblgen(e.g. in Nix sandboxed builds).The patch adds guards to respect caller-set values and auto-creates an imported
mlir-tblgentarget for downstream consumers. This replaces the previous dummy targetworkaround in flang's CMakeLists.txt.
Upstream issue: llvm/llvm-project#150986
flang-rt: remove unused dependencies
flang-rt does not link against MLIR or libclang; removed unused
CLANG_DIR/MLIR_DIRcmake flags, build inputs, and the empty
devoutput.Tests:
Design
No user-supplied flags are silently dropped. Flag incompatibility is handled at three layers:
Test plan
nix-build -A llvmPackages_21.flangnix-build -A llvmPackages_21.flang-rtnix-build -A llvmPackages_21.flang.passthru.tests.compile-smokenix-build -A llvmPackages_21.flang.passthru.tests.driver-flagsChecklist
nix fmt.