Skip to content

flang: update support and add flang-rt#452306

Open
Acture wants to merge 7 commits intoNixOS:stagingfrom
Acture:flang-update
Open

flang: update support and add flang-rt#452306
Acture wants to merge 7 commits intoNixOS:stagingfrom
Acture:flang-update

Conversation

@Acture
Copy link
Copy Markdown
Contributor

@Acture Acture commented Oct 15, 2025

Supersedes #391942

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
Copy link
Copy Markdown
Contributor Author

Acture commented Oct 15, 2025

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
Copy link
Copy Markdown
Contributor Author

Acture commented Oct 15, 2025

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)

Copy link
Copy Markdown
Member

@alyssais alyssais left a comment

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
@Acture
Copy link
Copy Markdown
Contributor Author

Acture commented Feb 20, 2026

The adapter had two issues for me:

  • -nostdlibinc was missing in the arguments that needs to be removed
  • Arguments read in from files (the @filename thing) have all commas escaped (these can occur for example with -Wl. Either these need to be unescaped or written out again to a file.

I've updated the wrapper to explicitly filter -nostdlibinc, and rewrite @file arguments to a tmp file.
Seems to work fine with my local test. Could you take another look?

@VincentVanlaer
Copy link
Copy Markdown
Contributor

It works for me too now

@Acture
Copy link
Copy Markdown
Contributor Author

Acture commented Feb 23, 2026

It works for me too now

Great. Let's see if someone could help with merging.

Comment thread pkgs/development/compilers/llvm/common/flang-rt/default.nix Outdated
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
Copilot AI review requested due to automatic review settings March 2, 2026 08:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 and others added 4 commits March 30, 2026 17:47
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>
Build on the initial LLVM Flang runtime packaging work and carry the final runtime build settings needed by the current branch state.

Co-authored-by: stove <stove@rivosinc.com>
Co-authored-by: acture <acture@gmail.com>
Build on the earlier standalone flang work by wiring flang into llvmPackages, carrying the LLVM 21 driver compatibility patches, and adding focused wrapper/driver tests.

Co-authored-by: stove <stove@rivosinc.com>
Co-authored-by: acture <acture@gmail.com>
Co-authored-by: Alyssa Ross <hi@alyssa.is>
@Acture
Copy link
Copy Markdown
Contributor Author

Acture commented Mar 31, 2026

@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
Copy link
Copy Markdown
Contributor Author

Acture commented Mar 31, 2026

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

Acture added 2 commits April 1, 2026 16:51
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).

Add guards to respect caller-set values and create an imported
mlir-tblgen target automatically. Remove the now-unnecessary dummy
target workaround from the flang package.

See: llvm/llvm-project#150986
flang-rt's runtimes build does not consume CLANG_DIR or MLIR_DIR, and
does not link against MLIR or libclang. Remove the unused inputs and
the empty dev output.
@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
Co-authored-by: Alexander Bantyev <balsoft@balsoft.ru>
@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
Copy link
Copy Markdown
Member

emilazy commented Apr 4, 2026

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

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

Status: Reviewed

Development

Successfully merging this pull request may close these issues.

9 participants