A compiler middle-end optimization framework that parses C programs into Abstract Syntax Trees (AST), constructs Control Flow Graphs (CFG), performs classical compiler optimizations, and visualizes the optimized execution flow through an interactive web dashboard.
This project implements a simplified compiler middle-end in Python, inspired by modern compiler optimization pipelines.
Starting from raw C source code, it:
- Parses the code into an Abstract Syntax Tree (AST) using
pycparser - Constructs a mathematical Control Flow Graph (CFG)
- Performs multiple static analysis passes
- Applies classical compiler optimizations
- Visualizes the optimized CFG using Graphviz
- Benchmarks optimization performance on large datasets such as CodeNet and SV-COMP
The project also includes an interactive Streamlit dashboard for visual exploration of CFG transformations.
- Parses ANSI C programs using pycparser
- Converts source code into an Abstract Syntax Tree
- Builds CFG nodes and directed edges
- Models execution paths using graph theory
Evaluates constant expressions during compile time.
Example:
int x = 5 * 4;β
int x = 20;Propagates known constant values through subsequent statements.
Computes:
live_inlive_out
sets for every CFG node.
Removes assignments whose values never contribute to future computation.
Example:
int x = 10;
x = 20;
return x;β
x = 20;
return x;Uses CFG traversal to identify and eliminate execution blocks that cannot be reached from the function entry point.
The Streamlit application allows users to:
- Paste custom C programs
- Generate AST
- Visualize the CFG
- Observe optimization passes
- Inspect optimized execution flow
CFG_OPTIMIZER/
β
βββ datasets/ # CodeNet & SV-COMP benchmark datasets
β
βββ app.py # Streamlit dashboard
βββ batch_runner.py # Batch benchmarking framework
βββ main.py # CFG builder & optimization engine
β
βββ codenet_report.json # Generated benchmark report
βββ sv_comp_report.json # Generated benchmark report
β
βββ cfg_demo.png # Dashboard preview
βββ README.md
- Python
- pycparser
- NetworkX
- Graphviz
- pydot
- Streamlit
git clone https://github.com/adi-2254/cfg_optimizer.git
cd cfg_optimizerLinux / macOS
python -m venv env
source env/bin/activateWindows
python -m venv env
.\env\Scripts\activatepip install streamlit networkx pycparser pydotGraphviz is required for CFG rendering.
Download from:
https://graphviz.org/download/
During installation, enable "Add Graphviz to PATH".
brew install graphvizsudo apt-get install graphvizstreamlit run app.pyOpen your browser and paste any C program to visualize:
- AST
- CFG
- Optimized CFG
Evaluate the optimizer on large benchmark datasets.
python batch_runner.pyThe generated reports include:
- Optimization statistics
- Number of optimized CFG nodes
- Removed dead code
- Reachability metrics
Reports are stored as:
codenet_report.json
sv_comp_report.json
| Optimization | Implemented |
|---|---|
| AST Generation | β |
| CFG Construction | β |
| Constant Folding | β |
| Constant Propagation | β |
| Live Variable Analysis | β |
| Dead Code Elimination | β |
| Unreachable Code Removal | β |
| CFG Visualization | β |
| Dataset Benchmarking | β |
- Anurag Raj (2024CSB1101)
- Aditya Raj (2024CSB1003)
- Aakash Jaisinghani (2024CSB1092)
If you found this project useful, consider β starring the repository.
