This README provides an overview of the Loop Unrolling project, detailing its purpose, how to set up the environment, and how to run and use the project. The project utilizes LLVM passes to extract features from loops, apply loop unrolling with multiple factors, and use machine learning to predict optimal unrolling factors.
Loop unrolling is a compiler optimization technique used to reduce the overhead of loop control. This project introduces a machine learning-based approach to predict the optimal unroll factor for loops in C source files. The system extracts features from loops, generates unrolled versions of loops for various unroll factors, and uses machine learning models to predict the most optimal unroll factor.
The project is organized into the following main components:
- llvm_passes/: Contains the LLVM passes to extract loop features and unroll loops.
- scripts/: Contains scripts to extract features, run the unroll passes, and generate labels for training the machine learning model.
- model.py: Python script to train and run the machine learning model to predict the optimal unroll factor.
git clone https://github.com/Aarya-Kulshrestha/LoopUnrollOptimizer_ML.git
cd LoopUnrollOptimizer_MLYou have two options:
- Use the self-curated dataset provided in the repository.
- Use your own dataset of C source files.
Place your dataset in an appropriate directory for use in the later steps.
cd llvm_passes
mkdir build
cd build
cmake ..
makeThis will compile and build all the LLVM passes required for the project.
Run the script to extract loop features from your dataset.
This option extracts all available features for each loop in every file of your dataset.
cd ../..
./scripts/extract_features.shNote: Make sure to modify the
OUTPUT_JSONvariable inextract_features.shto specify the path for the output JSON file that will store the extracted features.
This option extracts only instruction count features for loops, which can result in lower accuracy.
./scripts/extract_instruction_features.shNote: Modify the
OUTPUT_JSONvariable inextract_instruction_features.shto specify the path for the output JSON file.
Run the following command to unroll loops for each file in the dataset for each unroll factor in [1, 2, 4, 6, 8]. This will generate .ll files containing LLVM IR for each combination of loop and unroll factor.
./scripts/run_unroll_pass.sh <input_dataset_dir> <output_dir>- <input_dataset_dir>: Path to the directory containing the C source files.
- <output_dir>: Path to the output directory where the .ll files will be stored.
This step produces LLVM Intermediate Representation (.ll) files for each loop and unroll factor.
Run the following script to generate labels for optimal loop unroll factors. You will need to modify specific variables in the script:
- EXECUTABLE_DIR: The output directory from the
run_unroll_pass.shscript. - INPUT_FILES_DIR: The directory containing the original .C files.
- OUTPUT_JSON: The path to the output JSON file that will store the optimal unroll factor labels for each loop.
./scripts/generate_labels.shThis step generates the optimal unroll factor labels for each loop in the dataset.
Run the following Python script to train the model using the extracted loop features and optimal labels. The model will then predict the optimal unroll factor for any new set of loop features.
python3 models/model.pyThis step trains the model and makes predictions for optimal unroll factors for loops in your dataset.
-
Clone Repository
git clone <repository-url> cd <repository-name>
-
Build LLVM Passes
cd llvm_passes mkdir build cd build cmake .. make
-
Extract Features
./scripts/extract_features.sh
-
Run Unroll Passes
./scripts/run_unroll_pass.sh <input_dataset_dir> <output_dir>
-
Generate Labels
./scripts/generate_labels.sh
-
Run the Machine Learning Model
python3 model.py
- LLVM Passes: These passes extract features from loops and unroll loops with different factors.
- Dataset: You can use the self-curated dataset or your own. Ensure it is in a format that the extraction scripts can parse correctly.
- Model: The machine learning model is an MLP (Multilayer Perceptron) that predicts optimal unroll factors for loops based on the extracted features.
- CMake Errors: If you encounter errors during
cmake .., ensure you have the correct version of LLVM installed and that your environment variables are properly set. - File Paths: Double-check that you have set paths for OUTPUT_JSON, EXECUTABLE_DIR, and INPUT_FILES_DIR correctly in the shell scripts.
- Python Errors: If
model.pyfails, ensure you have all necessary Python packages installed. You may need to run:pip install -r requirements.txt
- Rishi Baronia
- Akanksha Girdhar
- Aarya Kulshrestha
- Aidan Szuch
- Omkar Vodela
For questions, issues, or contributions, please raise an issue on the repository.
We hope this README helps you understand the structure, purpose, and steps required to run the Loop Unrolling project. If you have any questions, please reach out to the contributors or open an issue in the repository.