Skip to content

flang: update support and add flang-rt - #452306

Merged
alyssais merged 4 commits into
NixOS:stagingfrom
Acture:flang-update
May 11, 2026
Merged

flang: update support and add flang-rt#452306
alyssais merged 4 commits into
NixOS:stagingfrom
Acture:flang-update

Conversation

@Acture

@Acture Acture commented Oct 15, 2025

Copy link
Copy Markdown
Contributor

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:

  • Update the flang package with standalone build, passthru metadata (langFortran, isFlang, isClang, hardeningUnsupportedFlags)
  • Add flang-rt runtime library (libFortranRuntime, libFortranDecimal)

cc-wrapper integration:

  • Recognize -fc1 as frontend invocation (analogous to clang's -cc1)
  • Strip -frandom-seed=* when wrapping flang (injected by reproducible-build hook; flang doesn't accept it)
  • Use existing hardeningUnsupportedFlags to disable unsupported hardening flags

Driver patches (backported from upstream):

  • warn-on-fbuiltin-and-fno-builtin — accept -fbuiltin/-fno-builtin with warning instead of hard error
  • accept-and-ignore-some-gfortran-optimization-flags — handle -fexpensive-optimizations etc.
  • use-xflang-in-diagnostics — suggest -Xflang instead of -Xclang in error messages

MLIR: fix MLIRConfig.cmake for standalone builds

MLIRConfig.cmake unconditionally overwrites MLIR_TABLEGEN_EXE, breaking standalone
builds 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-tblgen target for downstream consumers. This replaces the previous dummy target
workaround 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_DIR
cmake flags, build inputs, and the empty dev output.

Tests:

Design

No user-supplied flags are silently dropped. Flag incompatibility is handled at three layers:

  1. cc-wrapper — flags that should never reach flang are not injected in the first place (hardeningUnsupportedFlags, -frandom-seed filtering)
  2. Upstream patches — flags commonly passed by Fortran build systems produce warnings, not hard errors
  3. Passthru metadata — downstream tooling can detect flang and adapt accordingly

Test plan

  • nix-build -A llvmPackages_21.flang
  • nix-build -A llvmPackages_21.flang-rt
  • nix-build -A llvmPackages_21.flang.passthru.tests.compile-smoke
  • nix-build -A llvmPackages_21.flang.passthru.tests.driver-flags
  • @VincentVanlaer's reproducer (basic Fortran compilation via mkDerivation)

Checklist

  • I have read the contributing guidelines.
  • The PR is formatted using nix fmt.

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related labels Oct 15, 2025
@Acture

Acture commented Oct 15, 2025

Copy link
Copy Markdown
Contributor Author

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.

@Acture

Acture commented Oct 15, 2025

Copy link
Copy Markdown
Contributor Author

Build took an extremely long time. wasn't expecting this though

@Acture
Acture marked this pull request as ready for review October 15, 2025 18:00
@Acture Acture self-assigned this Oct 15, 2025
@philiptaron
philiptaron requested a review from a team October 15, 2025 18:05
@RossComputerGuy

Copy link
Copy Markdown
Member

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 452306 --package llvmPackages_21.flang --package llvmPackages_21.flang-rt --package llvmPackages_21.clang --package llvmPackages_21.mlir
Commit: 17f5d754b59666c9558db102c824b481b0578047


aarch64-linux

❌ 6 packages failed to build:
  • llvmPackages_21.clang
  • llvmPackages_21.flang
  • llvmPackages_21.flang-rt
  • llvmPackages_21.flang-rt.dev (llvmPackages_21.flang-rt.dev.dev)
  • llvmPackages_21.mlir
  • llvmPackages_21.mlir.dev (llvmPackages_21.mlir.dev.dev)

@RossComputerGuy

Copy link
Copy Markdown
Member

I think I just ran into a bug with nixpkgs-review, the build is still running.

@RossComputerGuy

Copy link
Copy Markdown
Member

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 452306 --package llvmPackages_21.flang-rt --package llvmPackages_21.flang --package llvmPackages_21.mlir --package llvmPackages_21.clang
Commit: 17f5d754b59666c9558db102c824b481b0578047


aarch64-linux

✅ 6 packages built:
  • llvmPackages_21.clang
  • llvmPackages_21.flang
  • llvmPackages_21.flang-rt
  • llvmPackages_21.flang-rt.dev (llvmPackages_21.flang-rt.dev.dev)
  • llvmPackages_21.mlir
  • llvmPackages_21.mlir.dev (llvmPackages_21.mlir.dev.dev)

@alyssais alyssais left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be split into multiple commits. There's a lot going on here.

Comment thread pkgs/build-support/cc-wrapper/default.nix Outdated
Comment thread pkgs/development/compilers/llvm/common/default.nix Outdated
@Acture
Acture force-pushed the flang-update branch 2 times, most recently from ff9c766 to 1c38124 Compare October 17, 2025 00:57
@nix-owners
nix-owners Bot requested review from Synthetica9 and ethindp October 17, 2025 01:04
Copilot AI review requested due to automatic review settings March 2, 2026 08:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 flang packaging to include flang-rt sources and export language capability passthru flags for cc-wrapper.
  • Add a new flang-rt derivation (runtime library) and wire it into the LLVM toolchain set.
  • Extend cc-wrapper to recognize -fc1 as cc1-like and add initial Fortran wrapper support for flang.

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.

Comment thread pkgs/build-support/cc-wrapper/default.nix
Comment thread pkgs/development/compilers/llvm/common/default.nix Outdated
Comment thread pkgs/development/compilers/llvm/common/default.nix Outdated
Comment thread pkgs/development/compilers/llvm/common/default.nix Outdated
@Acture

Acture commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

@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:

  1. cc-wrapper — natively recognizes flang (isFlang), strips only -frandom-seed=* (which the reproducible-build hook injects and flang can't accept), and uses the existing hardeningUnsupportedFlags mechanism for the rest
  2. Driver patches backported from upstream — flang now warns on common gfortran flags (-fbuiltin, -fexpensive-optimizations) instead of hard-erroring, and suggests -Xflang instead of -Xclang in diagnostics
  3. Testsdriver-flags test verifies all of the above behavior, including that no flags are silently swallowed

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.

@Acture
Acture requested a review from alyssais March 31, 2026 10:49
@Acture

Acture commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

@emilazy Take a look when you have time. It seems requested changes somehow stucked.

@nixpkgs-ci
nixpkgs-ci Bot requested review from a team, balsoft, infinisil and pyrox0 March 31, 2026 10:52
@balsoft balsoft moved this from Needs Review to In Review in Nixpkgs security review Apr 1, 2026
Comment thread pkgs/development/compilers/llvm/common/mlir/default.nix
@balsoft balsoft moved this from In Review to Reviewed in Nixpkgs security review Apr 1, 2026
@emilazy
emilazy dismissed their stale review April 4, 2026 18:53

No time to re‐review

@emilazy

emilazy commented Apr 4, 2026

Copy link
Copy Markdown
Member

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 alyssais left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks so much better. Thanks a lot for reworking it. I don't think it's far off now.

Comment thread pkgs/build-support/cc-wrapper/cc-wrapper.sh Outdated
Comment thread pkgs/development/compilers/llvm/common/default.nix
Comment thread pkgs/development/compilers/llvm/common/mlir/default.nix
Acture and others added 4 commits May 4, 2026 18:05
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>
@Acture

Acture commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

Also reorganized into 4 atomic commits:

  • clang, cc-wrapper: make wrapper flang-aware
  • flang-rt: add runtime package (wired with buildFlang from the start)
  • flang: package standalone flang with LLVM 20+ driver fixes — patches now apply to LLVM 20+; the two LLVM 22 already merged upstream are skipped there
  • llvm/mlir: fix MLIRConfig.cmake to support external tablegen overrides — also rolls in the dummy-target removal it obsoletes

@alyssais Take another look when you have time.

@alyssais alyssais left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not expect it could turn out so nicely. Thank you!

@alyssais

alyssais commented May 4, 2026

Copy link
Copy Markdown
Member

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. :)

@Acture Acture mentioned this pull request May 5, 2026
3 tasks
@Acture

Acture commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. 12.approvals: 2 This PR was reviewed and approved by two persons. 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in any of the changed packages.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

9 participants