Factory.ai

Open-Source Wikis

/

LLVM

/

Subprojects

/

flang

llvm/llvm-project

flang

Flang is the LLVM project's Fortran front end — a from-scratch implementation of Fortran 2018 (and most of the way to Fortran 2023) written in modern C++. It produces MLIR (in the fir/hlfir dialects) which lowers through MLIR's llvm dialect to LLVM IR, then to machine code via LLVM codegen.

Purpose

Flang exists to give the LLVM toolchain a real Fortran compiler. The original "flang" was an unmaintained PGI-derived front end; the current Flang grew out of the f18 rewrite and was accepted into LLVM under the new name. Per the flang/README.md, it "started off as the f18 project ... with an aim to replace the previous flang project ... and address its various deficiencies." Note that the README also warns Flang "is not ready yet for production usage" — the project is rapidly maturing but is not yet a drop-in replacement for gfortran or ifort for all workloads.

Directory layout

flang/
├── include/flang/    # Public headers
├── lib/              # Implementation
│   ├── Common/       # Shared utilities (intervals, format buffer, etc.)
│   ├── Decimal/      # IEEE-754 decimal formatting
│   ├── Evaluate/     # Constant folding, expression evaluation, type inference
│   ├── Frontend/     # Driver-side compiler-instance plumbing
│   ├── FrontendTool/
│   ├── Lower/        # AST → MLIR (FIR / HLFIR)
│   ├── Optimizer/    # FIR-specific optimization passes
│   ├── Parser/       # Fortran tokenizer and parser
│   ├── Semantics/    # Semantic analysis
│   ├── Tools/        # CLI and driver helpers
│   └── Support/, Testing/, ...
├── tools/            # flang-new (the user-facing driver), flang-aot, fir-opt, tco, bbc
├── test/             # lit regression tests
├── unittests/
├── docs/             # Markdown documentation
├── examples/
├── runtime/          # (Most runtime now lives in the flang-rt subproject)
├── README.md
└── CMakeLists.txt

Key abstractions

Type File Role
Fortran::parser::Parsing flang/include/flang/Parser/parsing.h Drives the parser
Fortran::parser::ParseTree flang/include/flang/Parser/parse-tree.h Parse-tree node hierarchy
Fortran::semantics::Semantics flang/include/flang/Semantics/semantics.h Semantic analyzer
Fortran::evaluate::Expr<> flang/include/flang/Evaluate/expression.h Type-parameterized expression representation
fir::FIROpsDialect flang/include/flang/Optimizer/Dialect/ The Flang IR dialect (FIR)
hlfir::* flang/include/flang/Optimizer/HLFIR/ High-Level FIR (a higher-abstraction layer)
Fortran::lower::Bridge flang/include/flang/Lower/Bridge.h Parse-tree → MLIR lowering driver

How it works

graph LR
    src[Fortran source] --> tokens[Lex]
    tokens --> parse[Parse]
    parse --> tree[Parse tree]
    tree --> sema[Semantics]
    sema --> tree2[Annotated parse tree]
    tree2 --> lower["Lower (Bridge)"]
    lower --> hlfir[HLFIR]
    hlfir --> fir[FIR]
    fir --> firopt[FIR opt passes]
    firopt --> mlir[Standard MLIR dialects]
    mlir --> llvm_dialect[llvm dialect]
    llvm_dialect --> ir[LLVM IR]
    ir --> codegen[LLVM CodeGen]
    codegen --> obj[Object]
    obj --> link[Link with flang-rt]
  • Parser is hand-written, predictive, and produces a strongly-typed parse tree (no untyped AST generic node).
  • Semantics rewrites the parse tree in place: scope construction, name resolution, expression typing, USE/IMPLICIT handling, OpenMP/OpenACC directives, IO statement legality.
  • Lower / Bridge walks the tree and emits MLIR using the FIR and HLFIR dialects.
  • FIR is Flang's IR; HLFIR is a thin layer above it that preserves Fortran-specific concepts (assumed-shape arrays, allocatables, intrinsic semantics).
  • Lowering pipelines (flang/lib/Optimizer/) progressively reduce HLFIR → FIR → standard MLIR → llvm dialect.
  • The runtime calls (e.g., for IO, allocatable management, derived-type assignment) target flang-rt.

Tools

  • flang-new — the user-facing driver (in transition from "flang"; flang-new is the modern name).
  • bbc — "Binary Bridge Compiler", a tester driver that runs parse → semantics → lower without invoking the rest of the toolchain.
  • fir-optmlir-opt with FIR/HLFIR registered, for ad hoc pass testing.
  • tco — "Test Compile Output", another internal driver for FIR-level testing.

Integration points

  • MLIR (mlir) is a hard dependency. Flang ships its own dialects but reuses every standard MLIR dialect from arith to llvm.
  • LLVM core (llvm) is the codegen target.
  • flang-rt (flang-rt) is the runtime invoked by lowered code.
  • OpenMP / OpenACC support routes through openmp and offload.

Entry points for modification

  • Adding a Fortran intrinsic: declare in the relevant flang/include/flang/Evaluate/intrinsics*.h, implement evaluation in flang/lib/Evaluate/, implement lowering in flang/lib/Lower/, and runtime support (if required) in flang-rt/.
  • Adding an FIR/HLFIR op: extend the dialect's *.td and the C++ implementation under flang/lib/Optimizer/.
  • Adding a semantic check: emit a diagnostic from the appropriate flang/lib/Semantics/ file. Diagnostics in Flang are not TableGen-driven; they're string-formatted.

Reference

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

flang – LLVM wiki | Factory