Skip to content

Nikita-Kedari/CompileX--An-AI-Augmented-Explainable-Sustainable-Compiler-Sandbox

Repository files navigation

CompileX ⚡ - AI-Augmented Explainable & Sustainable Compiler Sandbox

CompileX transforms the compilation process into an explainable, interactive, and sustainable experience. It's an AI-enhanced compiler sandbox that provides transparency into compiler decisions, sustainability metrics, and interactive learning tools.

🌟 Features

Core Capabilities

  • Multi-Language Support: C, C++, Java, Python
  • AI-Powered Explanations: Gemini AI explains errors, optimizations, and IR code
  • Green Score Analysis: Performance vs sustainability trade-offs
  • Interactive Pipeline Visualization: See how your code flows through compilation stages
  • RAG Chatbot: Ask questions about compilers, optimizations, and your code
  • Real-time Error Fixing: AI suggests and applies fixes to your code

Unique Value Propositions

  • Explainable Compilation: Visual pipeline with step-by-step explanations
  • Sustainability Metrics: First compiler tool to show energy/carbon trade-offs
  • Educational Focus: Perfect for students learning compiler internals
  • Interactive Learning: Experiment with different optimization levels

🚀 Quick Start

Prerequisites

  • Python 3.8+
  • Clang/LLVM (for C/C++ support)
  • Java Development Kit (for Java support)
  • Google Gemini API key

Installation

  1. Clone the repository
git clone <repository-url>
cd compilex
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Set up environment variables Create a .env file in the project root:
GEMINI_API_KEY=your_gemini_api_key_here
  1. Create demo snippets directory
mkdir -p demo_snippets

Create the demo files:

  • demo_snippets/hello_world.c
  • demo_snippets/error_missing_semicolon.c
  • demo_snippets/optimization_constfold.c

Running the Application

streamlit run app.py

The application will be available at http://localhost:8501

📁 Project Structure

compilex/
├── app.py                    # Main Streamlit application
├── compiler_utils.py         # Core compilation engine
├── explainers.py            # AI explanation engine
├── visualizers.py           # Visualization components
├── rag_chatbot.py           # RAG-based chatbot
├── demo_snippets/           # Sample code snippets
│   ├── hello_world.c
│   ├── error_missing_semicolon.c
│   └── optimization_constfold.c
├── outputs/                 # Generated during runtime
├── chroma_db/              # Vector database (created automatically)
├── requirements.txt         # Python dependencies
├── .env                    # Environment variables
└── README.md               # This file

🎯 How to Use

1. Basic Compilation

  1. Select your programming language (C, C++, Java, Python)
  2. Choose optimization level (-O0, -O1, -O2, -O3)
  3. Write or load demo code
  4. Click "Compile & Analyze"

2. Understanding Results

  • Diagnostics Tab: View compilation errors/warnings with AI explanations
  • IR Analysis Tab: Explore intermediate representation with AI insights
  • Optimizations Tab: See applied optimizations and their impacts
  • Pipeline Tab: Visualize the compilation pipeline

3. Green Score Analysis

  • Performance Score: How fast your optimized code runs
  • Sustainability Score: Energy efficiency of compilation choices
  • Detailed Metrics: Breakdown of energy consumption factors

4. Using the AI Assistant

  • Ask questions about compiler errors
  • Get explanations of optimizations
  • Learn about performance vs sustainability trade-offs
  • Use quick question buttons for common queries

5. Apply AI Fixes

When compilation errors occur:

  1. Click "Apply AI Fix" button
  2. Review the suggested fix
  3. Apply it to your code automatically

🛠️ System Requirements

Required Software

  • For C/C++: Clang compiler (clang, clang++)
  • For Java: Java Development Kit (JDK 8+)
  • For Python: Python 3.x (usually pre-installed)

Installing Compilers

Ubuntu/Debian

sudo apt update
sudo apt install clang llvm default-jdk python3

macOS

# Install Xcode Command Line Tools
xcode-select --install

# Or using Homebrew
brew install llvm openjdk

Windows

  1. Install Visual Studio Build Tools or Visual Studio Community
  2. Download and install OpenJDK
  3. Ensure Python is installed from python.org

API Requirements

📊 Green Score Methodology

Our Green Score is based on research-backed proxies for energy consumption:

Metrics Used

  1. Compile Time (50%): CPU energy during compilation
  2. Instruction Count (30%): Runtime execution complexity
  3. IR/Binary Size (20%): Memory and storage efficiency

Research Basis

  • Green Software Foundation guidelines on measuring software energy consumption
  • Academic research on compiler optimization energy trade-offs
  • Industry best practices for sustainable software development

Calculation

energy_proxy = 0.5×normalize(compile_time) + 0.3×normalize(inst_count) + 0.2×normalize(ir_size)
green_score = 100 - energy_proxy×100

🤖 AI Integration

Gemini AI Features

  • Error Explanations: Plain English explanations of compilation errors
  • IR Analysis: Breaking down intermediate representations
  • Optimization Insights: Understanding compiler optimizations
  • Code Fixes: Automatic error correction suggestions

RAG Chatbot

  • Knowledge Base: Pre-loaded with compiler and optimization knowledge
  • Context Aware: Understands your current code and compilation results
  • Conversational: Maintains context across multiple questions
  • Extensible: Knowledge base can be expanded with new information

🎓 Educational Use Cases

For Students

  • Compiler Theory: Visualize compilation pipeline stages
  • Optimization Learning: See real optimization effects
  • Error Understanding: Learn from AI explanations
  • Interactive Experimentation: Try different code patterns

For Educators

  • Teaching Tool: Visual compiler pipeline demonstrations
  • Assignment Platform: Students can experiment and learn
  • Sustainability Education: Introduce green software concepts
  • Assessment: Compare student approaches to optimization

For Developers

  • Debugging Aid: Understand complex compilation errors
  • Optimization Analysis: Make informed performance decisions
  • Sustainability Awareness: Consider environmental impact
  • Learning Platform: Deepen compiler knowledge

🔧 Advanced Configuration

Custom Compiler Flags

Edit compiler_utils.py to add custom compiler configurations:

self.compilers['c']['compile_flags']['-Ofast'] = ['-Ofast', '-march=native']

Adding Languages

Extend the CompilerEngine class to support additional languages:

  1. Add compiler configuration
  2. Implement analysis methods
  3. Update language detection logic

Extending Knowledge Base

Add new compiler knowledge to the RAG chatbot:

chatbot.add_to_knowledge_base(
    content="Your compiler knowledge here",
    metadata={"topic": "optimization", "language": "rust"},
    doc_id="unique_identifier"
)

📈 Performance Tips

For Better Analysis

  • Use meaningful variable names for better IR readability
  • Include comments to help AI explanations
  • Try different optimization levels to see trade-offs
  • Experiment with code patterns to learn optimization effects

System Optimization

  • Ensure adequate RAM for large compilations
  • Use SSD storage for faster I/O operations
  • Close unnecessary applications during analysis
  • Consider compiler caching for repeated analyses

🐛 Troubleshooting

Common Issues

Compiler Not Found

# Check if compilers are installed
clang --version
javac -version
python3 --version

API Key Issues

  • Verify your Gemini API key is correct
  • Check API quota limits
  • Ensure .env file is in the project root

ChromaDB Issues

# Clear vector database if corrupted
rm -rf chroma_db/
# Restart the application to rebuild

Memory Issues

  • Reduce IR code size limits in explainers.py
  • Limit conversation history in rag_chatbot.py
  • Close browser tabs to free memory

Getting Help

  1. Check the error message in the application
  2. Review terminal output for detailed errors
  3. Verify all dependencies are installed
  4. Ensure API keys are properly configured

🚀 Development

Contributing

  1. Fork the repository
  2. Create feature branch
  3. Make changes with tests
  4. Submit pull request

Code Style

  • Follow PEP 8 for Python code
  • Use meaningful variable names
  • Add docstrings for public methods
  • Comment complex algorithms

Testing

# Install development dependencies
pip install pytest black isort

# Run tests
pytest

# Format code
black .
isort .

📄 License

🙏 Acknowledgments

  • Google Gemini AI for natural language processing
  • Streamlit for the web application framework
  • Plotly for interactive visualizations
  • ChromaDB for vector database capabilities
  • LLVM/Clang for compiler infrastructure
  • Green Software Foundation for sustainability guidelines

🌍 Environmental Impact

CompileX promotes sustainable software development by:

  • Making energy trade-offs visible to developers
  • Educating about green software practices
  • Providing tools to measure compilation sustainability
  • Encouraging responsible optimization choices

📞 Support

For support, questions, or feedback:

  • Create an issue in the repository
  • Check the troubleshooting section
  • Review documentation and examples

CompileX - Making compilers transparent, sustainable, and human-friendly! 🌱⚡

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages