Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions MPIReduction/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
cmake_minimum_required(VERSION 3.10)
project(MPIReductionAnalyzer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find LLVM
find_package(LLVM 17 REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Add LLVM headers and libraries
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Set compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-openmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

# Add debug flags for development
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")

# Source files
set(SOURCES
src/main.cpp
src/Utils.cpp
src/ReductionDetector.cpp
src/MPIReductionAnalyzer.cpp
src/MPIReductionPass.cpp
)

# Header files (for IDE support)
set(HEADERS
include/Utils.h
include/ReductionDetector.h
include/MPIReductionAnalyzer.h
include/MPIReductionPass.h
)

# Add the analyzer executable
add_executable(mpi_reduction_analyzer ${SOURCES} ${HEADERS})

# Link against specific LLVM libraries
target_link_libraries(mpi_reduction_analyzer
LLVMCore
LLVMSupport
LLVMAnalysis
LLVMTransformUtils
LLVMIRReader
LLVMAsmParser
LLVMBitReader
LLVMBitWriter
LLVMTarget
LLVMPasses
)

# Optional: Create a shared library for the pass
option(BUILD_SHARED_PASS "Build shared library for LLVM pass" OFF)

if(BUILD_SHARED_PASS)
add_library(MPIReductionPass SHARED
src/MPIReductionPass.cpp
src/MPIReductionAnalyzer.cpp
src/ReductionDetector.cpp
src/Utils.cpp
)

target_link_libraries(MPIReductionPass
LLVMCore
LLVMSupport
LLVMAnalysis
LLVMTransformUtils
)

# Don't prefix the library with 'lib'
set_target_properties(MPIReductionPass PROPERTIES PREFIX "")
endif()

# Install targets
install(TARGETS mpi_reduction_analyzer
RUNTIME DESTINATION bin
)

if(BUILD_SHARED_PASS)
install(TARGETS MPIReductionPass
LIBRARY DESTINATION lib
)
endif()

# Add custom target for testing
add_custom_target(test_analyzer
COMMAND ${CMAKE_BINARY_DIR}/mpi_reduction_analyzer --help
DEPENDS mpi_reduction_analyzer
COMMENT "Testing MPI Reduction Analyzer"
)

# Print configuration summary
message(STATUS "")
message(STATUS "=== MPI Reduction Analyzer Configuration ===")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "LLVM version: ${LLVM_PACKAGE_VERSION}")
message(STATUS "Build shared pass: ${BUILD_SHARED_PASS}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "==========================================")
message(STATUS "")
82 changes: 82 additions & 0 deletions MPIReduction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 🧮 MPI Reduction Analyzer

This tool statically analyzes LLVM IR code to identify **opportunities for MPI reduction operations** like `MPI_Reduce`, `MPI_Allreduce`, etc. It detects manual reduction patterns in C/C++ programs and suggests optimization potential for collective communication.

## 📁 Project Structure

MPIReduction/
├── build/ # Build directory (CMake output + binary)
│ └── mpi_reduction_analyzer
├── include/ # Header files
│ ├── MPIReductionAnalyzer.h
│ ├── MPIReductionPass.h
│ ├── ReductionDetector.h
│ └── Utils.h
├── src/ # Source files (main logic)
│ ├── main.cpp
│ ├── MPIReductionAnalyzer.cpp
│ ├── MPIReductionPass.cpp
│ ├── ReductionDetector.cpp
│ └── Utils.cpp
├── test/ # Test files
│ ├── mpi.h
│ ├── test_reduction.ll # LLVM IR for testing
│ ├── testit.c # Optional C file to generate IR
│ └── testit.ll
├── CMakeLists.txt # CMake configuration
├── build.md # (Optional) Build documentation
└── README.md # 📄 You're reading it!


## ⚙️ Prerequisites

- LLVM (v10 or higher) with `clang`, `opt`, and `llvm-config`
- CMake ≥ 3.10
- C++ compiler (`g++` or `clang++`)
- Unix-like environment (Linux, WSL, macOS)

## 🛠️ LLVM + Clang + Build Tools Setup on Ubuntu

# Update package list
sudo apt update

# Install compiler toolchain, CMake, and Ninja
sudo apt install -y build-essential cmake ninja-build

# Install LLVM core tools and Clang
sudo apt install -y clang llvm lld

# Install development libraries for writing LLVM passes
sudo apt install -y llvm-dev libclang-dev clang-tools

# Install OpenMPI and mpicc
sudo apt install -y openmpi-bin libopenmpi-dev

# Check Installed Versions
clang --version
llvm-as --version
cmake --version
ninja --version

## 🔨 Build Instructions

# 1. Clone the repo or navigate into the folder
cd MPIReduction

# 2. Create a build directory
mkdir build && cd build

# 3. Generate Makefiles using CMake
cmake ..

# 4. Compile the analyzer
make

This produces the binary: ./build/mpi_reduction_analyzer

## 🔨 Generate Intermediate Code
clang -S -emit-llvm test/testit.c -o test/testit.ll

## 🚀 Usage
/path/to/mpi_reduction_analyzer -analyze-mpi-reduction=<type> /path/to/input.ll

184 changes: 184 additions & 0 deletions MPIReduction/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# MPI Reduction Analyzer - Build Instructions

## Project Structure

```
mpi-reduction-analyzer/
├── include/
│ ├── MPIReductionPass.h # LLVM pass wrapper
│ ├── MPIReductionAnalyzer.h # Main analyzer class
│ ├── ReductionDetector.h # Pattern detection logic
│ └── Utils.h # Utility functions and constants
├── src/
│ ├── MPIReductionPass.cpp # LLVM pass implementation
│ ├── MPIReductionAnalyzer.cpp # Main analyzer implementation
│ ├── ReductionDetector.cpp # Pattern detection implementation
│ ├── Utils.cpp # Utility functions
│ └── main.cpp # Command-line interface
├── CMakeLists.txt # Build configuration
├── BUILD.md # This file
└── README.md # Usage documentation
```

## Prerequisites

- **LLVM 17**: The analyzer is built against LLVM 17
- **CMake 3.10+**: For building the project
- **C++17 compliant compiler**: GCC 7+ or Clang 5+

## Building

### 1. Clone and prepare the project

```bash
git clone <repository-url>
cd mpi-reduction-analyzer
mkdir build
cd build
```

### 2. Configure with CMake

```bash
# Basic configuration
cmake ..

# Or with specific LLVM installation
cmake -DLLVM_DIR=/path/to/llvm/lib/cmake/llvm ..

# Debug build
cmake -DCMAKE_BUILD_TYPE=Debug ..

# Release build with optimizations
cmake -DCMAKE_BUILD_TYPE=Release ..

# Build with shared pass library
cmake -DBUILD_SHARED_PASS=ON ..
```

### 3. Build the project

```bash
make -j$(nproc)
```

### 4. Install (optional)

```bash
make install
```

## Build Options

| Option | Description | Default |
|--------|-------------|---------|
| `CMAKE_BUILD_TYPE` | Build type (Debug/Release) | Debug |
| `BUILD_SHARED_PASS` | Build shared library for LLVM pass | OFF |
| `CMAKE_INSTALL_PREFIX` | Installation directory | /usr/local |

## Usage Examples

### Basic Analysis

```bash
# Analyze all reduction types
./mpi_reduction_analyzer program.bc

# Analyze only sum reductions
./mpi_reduction_analyzer -analyze-mpi-reduction=sum program.bc

# Verbose output
./mpi_reduction_analyzer -verbose program.bc
```

### Advanced Options

```bash
# Detailed report
./mpi_reduction_analyzer -detailed program.bc

# Summary only
./mpi_reduction_analyzer -summary program.bc

# Export results to file
./mpi_reduction_analyzer -output=analysis_report.txt program.bc

# Use LLVM pass manager
./mpi_reduction_analyzer -use-pass program.bc
```

### Generating LLVM Bitcode

To analyze your MPI programs, you first need to generate LLVM bitcode:

```bash
# For C programs
clang -emit-llvm -c your_mpi_program.c -o your_mpi_program.bc

# For C++ programs
clang++ -emit-llvm -c your_mpi_program.cpp -o your_mpi_program.bc

# With debug information (recommended)
clang -emit-llvm -g -c your_mpi_program.c -o your_mpi_program.bc
```

## Troubleshooting

### LLVM Not Found

If CMake cannot find LLVM:

```bash
# Set LLVM_DIR explicitly
export LLVM_DIR=/usr/lib/llvm-17/lib/cmake/llvm
cmake ..

# Or use find_package hint
cmake -DLLVM_DIR=/usr/lib/llvm-17/lib/cmake/llvm ..
```

### Compilation Errors

1. **C++ Standard Issues**: Ensure your compiler supports C++17
2. **Missing Headers**: Make sure LLVM development headers are installed
3. **Linking Errors**: Verify all required LLVM libraries are available

### Runtime Issues

1. **Cannot load bitcode**: Ensure the input file is valid LLVM bitcode
2. **No debug information**: Use `-g` flag when generating bitcode for better location reporting
3. **Permission errors**: Check file permissions for input and output files

## Development

### Adding New Reduction Patterns

1. Extend `ReductionType` enum in `Utils.h`
2. Add pattern detection logic in `ReductionDetector.cpp`
3. Update string conversion functions in `Utils.cpp`
4. Add test cases for the new pattern

### Modifying Analysis Logic

The main analysis flow:
1. `main.cpp` - Command line parsing and module loading
2. `MPIReductionAnalyzer` - Orchestrates the analysis
3. `ReductionDetector` - Performs pattern matching
4. `Utils` - Provides helper functions

### Testing

```bash
# Test the analyzer
make test_analyzer

# Run with sample programs
./mpi_reduction_analyzer samples/simple_reduction.bc
```

## Performance Considerations

- The analyzer uses a depth-limited search to avoid infinite loops
- Large modules may take significant time to analyze
- Use `-summary` flag for quick overview of large codebases
- Consider using the pass manager (`-use-pass`) for integration with LLVM optimization pipelines
Loading