# khronosgroup/glslang architecture

> Architecture diagram of the GitHub repository khronosgroup/glslang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/khronosgroup/glslang
- Repository: https://github.com/khronosgroup/glslang

## Overview

##### Main Components
a. Front Ends
- GLSL/ESSL Parser (MachineIndependent/Preprocessor, Scan.cpp, glslang.y)
- HLSL Parser (glslang/HLSL)
b. Intermediate Representation (AST)
- SymbolTable & AST (MachineIndependent/*.cpp, Intermediate.cpp)
c. Back End
- SPIR-V Generator (SPIRV/*)
d. Reflection API
- Reflection queries on AST (MachineIndependent/reflection.cpp/h)
e. Library Interfaces
- C++ Class API (ShaderLang.h, TShader/TProgram)
- C Functional API (Sh* functions in ShaderLang.h)
- C Interface (glslang_c_interface.h)
f. Standalone Wrapper
- CLI tool (StandAlone/StandAlone.cpp)
g. External Projects & Tools
- SPIRV-Tools scripts (External/)
- Python scripts (update_glslang_sources.py, gen_extension_headers.py)
h. Build System & CI/CD
- CMakeLists.txt, BUILD.gn, GN support, GK layouts (.github/workflows)
i. Testing Harnesses
- GoogleTest (gtests/)
- “runtests” script (Test/runtests, Test/baseResults)

##### Relationships & Data Flows
- Shader source → Preprocessor → Scanner → Parser → AST
- AST → Back End → SPIR-V binary
- AST → Reflection API → Reflection data
- CLI wrapper or Library API → Front Ends & Back End → Outputs (errors, AST, SPIR-V)
- Build system invokes CMake/GN, drives compilation of all components
- CI/CD pipelines trigger builds, tests, deployment

##### Architectural Patterns
- Compiler pipeline (Lexical → Syntax → Semantic → IR → CodeGen)
- Layered modular libraries (MachineIndependent, SPIRV, OSDependent, GenericCodeGen)
- Plugin-like External modules for SPIRV-Tools integration
- Singleton initialization/finalization in APIs (InitializeProcess/FinalizeProcess)

##### Technologies & Frameworks
- C++17, bison (glslang.y), Python 3 for scripts
- CMake & Ninja, GN & depot_tools
- GoogleTest for unit & integration tests
- SPIR-V specification libraries (SPIRV-Headers)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Front Ends
    subgraph "Front Ends" 
        direction TB
        subgraph "GLSL/ESSL Front End" 
            direction TB
            ScanCPP["Scan.cpp"]:::frontend
            YACC["glslang.y"]:::frontend
            PP["Pp.cpp"]:::frontend
        end
        subgraph "HLSL Front End"
            direction TB
            HLSLGrammar["hlslGrammar.cpp/.h"]:::frontend
            HLSLHelper["hlslParseHelper.cpp/.h"]:::frontend
            HLSLScan["hlslScanContext.cpp/.h"]:::frontend
            HLSLToken["hlslTokenStream.cpp/.h"]:::frontend
        end
    end

    %% Intermediate Representation
    subgraph "Intermediate Representation (AST & Symbol Table)"
        direction TB
        IntermediateCPP["Intermediate.cpp"]:::ir
        SymbolTable["SymbolTable.cpp/.h"]:::ir
        IntermTraverse["IntermTraverse.cpp"]:::ir
    end

    %% Back End
    subgraph "SPIR-V Back End"
        direction TB
        ToSpv["GlslangToSpv.cpp/.h"]:::backend
        SpvBuilder["SpvBuilder.cpp/.h"]:::backend
        SpvPost["SpvPostProcess.cpp"]:::backend
        SpvTools["SpvTools.cpp/.h"]:::backend
        SpirvCInterface["spirv_c_interface.cpp"]:::backend
    end

    %% Reflection
    Reflection["Reflection API"]:::reflection

    %% Library Interfaces
    subgraph "Library Interfaces"
        direction TB
        CPPAPI["ShaderLang.h/ShaderLang.cpp"]:::api
        CAPI["glslang_c_interface.h/.cpp"]:::api
    end

    %% CLI Wrapper
    CLI["StandAlone.cpp"]:::api

    %% External Tools & Scripts
    subgraph "External Tools & Scripts"
        direction TB
        ExtCMake["External/CMakeLists.txt"]:::tools
        UpdatePy["update_glslang_sources.py"]:::tools
        GenExt["gen_extension_headers.py"]:::tools
    end

    %% Build System & CI/CD
    Build["Build System & CI/CD"]:::build

    %% Tests
    subgraph "Tests"
        direction TB
        GTests["gtests/"]:::test
        Runtests["Test/runtests"]:::test
    end

    %% Data Flows
    Source["Shader Source"]:::source
    Source -->|"lexical"| ScanCPP
    Source -->|"preprocess"| PP
    ScanCPP -->|"token stream"| YACC
    YACC -->|"AST"| IntermediateCPP
    HLSLGrammar --> IntermediateCPP
    HLSLHelper --> IntermediateCPP
    HLSLScan --> IntermediateCPP
    HLSLToken --> IntermediateCPP

    IntermediateCPP --> SymbolTable
    SymbolTable --> IntermTraverse

    IntermTraverse --> ToSpv
    ToSpv --> SpvBuilder
    SpvBuilder --> SpvPost
    SpvPost --> SpvTools
    SpvTools --> SpirvCInterface
    IntermediateCPP --> Reflection
    Reflection -->|metadata| CPPAPI

    %% Consumers
    ToSpv --> CLI
    SpirvCInterface --> CAPI
    IntermTraverse --> CPPAPI

    %% Build/Test relationships
    Build --> ScanCPP
    Build --> IntermediateCPP
    Build --> ToSpv
    Build --> CLI
    Build --> GTests
    Build --> Runtests

    ExtCMake --> Build
    UpdatePy --> Build
    GenExt --> Build
    GTests -->|tests| IntermediateCPP
    Runtests -->|harness| CLI

    %% Click events
    click ScanCPP "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/Scan.cpp"
    click YACC "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/glslang.y"
    click PP "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/preprocessor/Pp.cpp"
    click HLSLGrammar "https://github.com/khronosgroup/glslang/blob/main/glslang/HLSL/hlslGrammar.cpp/.h"
    click HLSLHelper "https://github.com/khronosgroup/glslang/blob/main/glslang/HLSL/hlslParseHelper.cpp/.h"
    click HLSLScan "https://github.com/khronosgroup/glslang/blob/main/glslang/HLSL/hlslScanContext.cpp/.h"
    click HLSLToken "https://github.com/khronosgroup/glslang/blob/main/glslang/HLSL/hlslTokenStream.cpp/.h"
    click IntermediateCPP "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/Intermediate.cpp"
    click SymbolTable "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/SymbolTable.cpp"
    click IntermTraverse "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/IntermTraverse.cpp"
    click Reflection "https://github.com/khronosgroup/glslang/blob/main/glslang/MachineIndependent/reflection.cpp"
    click ToSpv "https://github.com/khronosgroup/glslang/blob/main/SPIRV/GlslangToSpv.cpp"
    click SpvBuilder "https://github.com/khronosgroup/glslang/blob/main/SPIRV/SpvBuilder.cpp"
    click SpvPost "https://github.com/khronosgroup/glslang/blob/main/SPIRV/SpvPostProcess.cpp"
    click SpvTools "https://github.com/khronosgroup/glslang/blob/main/SPIRV/SpvTools.cpp"
    click SpirvCInterface "https://github.com/khronosgroup/glslang/blob/main/SPIRV/CInterface/spirv_c_interface.cpp"
    click CPPAPI "https://github.com/khronosgroup/glslang/blob/main/glslang/Public/ShaderLang.h"
    click CAPI "https://github.com/khronosgroup/glslang/blob/main/glslang/Include/glslang_c_interface.h"
    click CLI "https://github.com/khronosgroup/glslang/blob/main/StandAlone/StandAlone.cpp"
    click ExtCMake "https://github.com/khronosgroup/glslang/blob/main/External/CMakeLists.txt"
    click UpdatePy "https://github.com/khronosgroup/glslang/blob/main/update_glslang_sources.py"
    click GenExt "https://github.com/khronosgroup/glslang/blob/main/gen_extension_headers.py"
    click GTests "https://github.com/khronosgroup/glslang/tree/main/gtests/"
    click Runtests "https://github.com/khronosgroup/glslang/tree/main/Test/runtests"

    %% Styles
    classDef frontend fill:#ccffcc,stroke:#2d7f2d,stroke-width:2px
    classDef ir fill:#ffffcc,stroke:#e0b000,stroke-width:2px
    classDef backend fill:#ffdb99,stroke:#e06619,stroke-width:2px
    classDef api fill:#99ccff,stroke:#3366cc,stroke-width:2px
    classDef reflection fill:#ffcccc,stroke:#cc0000,stroke-width:2px
    classDef build fill:#dddddd,stroke:#888888,stroke-width:2px
    classDef tools fill:#f2f2f2,stroke:#999999,stroke-width:2px
    classDef test fill:#e6e6ff,stroke:#6666cc,stroke-width:2px
    classDef source fill:#ffffff,stroke:#000000,stroke-width:1px shape:diamond
```
