# hardvan/mpi-analysis-using-llvm architecture

> Architecture diagram of the GitHub repository hardvan/mpi-analysis-using-llvm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hardvan/mpi-analysis-using-llvm
- Repository: https://github.com/hardvan/mpi-analysis-using-llvm

## Overview

##### Main components
- MPI C/C++ Source Code: The source files (e.g., mpi_example.c, mpi_example2.c, original.c) that contain MPI calls.
- LLVM IR Generation: The process where clang is used to compile the MPI C code into LLVM IR (input.ll). This is a key preprocessing step.
- LLVM Analysis Pass (MPIAnalysisPass): The core analysis component implemented in MPIAnalysisPass.cpp which detects MPI_Send and MPI_Recv calls and extracts communication patterns.
- Shell Script Automation (run_mpi_analysis.sh): A tool that automates the compilation of the LLVM pass and the execution of the analysis, streamlining the workflow.
- Build and Execution Environment: The use of LLVM, Clang, MPI libraries (e.g., mpicc/mpirun), and associated build flags. These external dependencies are vital to correctly set up and run the analysis.
- Output Reporting: The generation of detailed output logs that display the detected MPI communication patterns, including data like communicator, tag, and rank.

##### Relationships and data flow
- Data Flow: Start from the MPI program source code that is compiled into LLVM IR using clang. The LLVM IR file then serves as the input for the analysis pass.
- Control Flow: The run_mpi_analysis.sh script manages the sequential process: IR generation → LLVM pass compilation → analysis execution.
- Internal Interactions: The LLVM Analysis Pass (MPIAnalysisPass.cpp) is loaded into the LLVM ‘opt’ tool as a plugin. It processes the IR and reports on MPI calls.
- External Dependencies: LLVM/Clang and MPI libraries are foundational – they support both the IR generation and the execution of MPI programs.

##### Architectural patterns and design principles
- Modular Design: The project separates various concerns clearly – input generation, analysis logic, and automation script are distinct components.
- Plugin Architecture: The LLVM analysis pass is built as a shared object and dynamically loaded by the LLVM ‘opt’ tool, illustrating extensible plugin use.
- Automated Workflow: Use of a shell script to encapsulate the multiple steps shows emphasis on automation and repeatability.
- Integration with External Tools: Clear integration with compilation tools and MPI libraries indicates that the design leverages standard components.

- Block is labeled clearly with its purpose (e.g., “MPI Source Code Files: mpi_example.c, mpi_example2.c, original.c”).
- Next to the LLVM IR generation step, note the command used (e.g., “clang -emit-llvm –S …”) and mention the dependency on MPI headers.
- For the LLVM Analysis Pass, note that it is implemented in C++ (MPIAnalysisPass.cpp) and compiled to a shared object (MPIAnalysisPass.so) using LLVM’s build flags.
- For the automation script, highlight its role in sequencing the operations.
- External dependencies (LLVM/Clang, mpicc/ MPI libraries) either as separate blocks or annotate them near the components that use them.

##### Detail and clarity
- Given the project’s multiple components, use a layered approach: show a high-level overview (workflow from source to analysis output) and then, if needed, expand individual components (especially the LLVM pass) to show internal steps like detecting MPI calls and reporting uniform participation.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Source Code Subgraph
    subgraph "MPI Source Code Files"
        source1["mpi_example.c"]:::source
        source2["mpi_example2.c"]:::source
        source3["original.c"]:::source
    end

    %% Main Workflow
    shell["Shell Script Automation"]:::automation
    irgen["LLVM IR Generation (clang -emit-llvm -S)"]:::process
    llvmir["LLVM IR (input.ll)"]:::artifact
    analysis["LLVM Analysis Pass (MPIAnalysisPass.cpp)"]:::process
    output["Output Report Logs"]:::artifact

    %% External Dependencies
    extern1["LLVM/Clang"]:::external
    extern2["MPI Libraries"]:::external

    %% Data Flow from Source to IR Generation
    source1 -->|"compiles"| irgen
    source2 -->|"compiles"| irgen
    source3 -->|"compiles"| irgen

    %% Automation controlling steps
    shell -->|"triggers"| irgen
    irgen -->|"produces"| llvmir
    llvmir -->|"analyzes"| analysis
    shell -->|"invokes"| analysis
    analysis -->|"reports"| output

    %% External Dependencies influences
    extern1 -->|"supports"| irgen
    extern1 -->|"supports"| analysis
    extern2 ---|"required by"| source1
    extern2 ---|"required by"| source2
    extern2 ---|"required by"| source3

    %% Click Events for Component Mappings
    click source1 "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/mpi_example.c"
    click source2 "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/mpi_example2.c"
    click source3 "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/original.c"
    click llvmir "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/input.ll"
    click analysis "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/MPIAnalysisPass.cpp"
    click shell "https://github.com/hardvan/mpi-analysis-using-llvm/blob/main/final/run_mpi_analysis.sh"

    %% Styles
    classDef source fill:#f9e79f,stroke:#d35400,stroke-width:2px;
    classDef process fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef artifact fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef automation fill:#f5cba7,stroke:#d35400,stroke-width:2px;
    classDef external fill:#d6eaf8,stroke:#5dade2,stroke-width:2px;
```
