Skip to content

DarkStarStrix/PyC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

54 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PyC: A Python-like Compiler Toolchain

Project CodeName: darkstarstrix-pyc

Mission Statement:

Build a lightweight, high-performance Python compiler and optimization toolchain that speeds up AI workflows through graph optimizations, efficient tensor memory planning, and seamless custom kernel APIs β€” unified into a CLI-first architecture.

PyC is an experimental compiler that transforms a subset of Python-like syntax into executable machine code using the LLVM infrastructure. It serves as both an educational tool and a foundation for a lightweight compiler targeting Python-like code, with a focus on AI and scientific computing workloads.

Note: This project is under active development. Some features, like the CLI interface, are now implemented, while others (e.g., full Python syntax, functional CUDA) are planned. Contributions are welcomeβ€”check Doc.md for an understanding of the project, then create a feature branch and open a pull request.

Features

  • Frontend:
    • Lexer and parser for Python-like syntax with indentation support.
    • Abstract Syntax Tree (AST) construction for expressions, assignments, and if statements.
  • Symbol Table:
    • Manages variable scopes and integrates with LLVM IR generation (supports variables but not functions or complex types).
  • IR Generation:
    • Converts AST to LLVM IR for expressions, assignments, and conditionals.
  • Backend:
    • JIT compilation for immediate execution.
    • Object file generation with multithreaded compilation.
  • Optimization:
    • Applies LLVM passes (e.g., instruction combining, GVN).
  • AI-Specific Modules:
    • Graph compiler for tensor operations.
    • Memory planner for dynamic tensor memory allocation.
    • Optimizer for AI model runtime strategies.
    • Visualizer for computational graphs.
  • CUDA Integration:
    • Experimental tokenization and matrix multiplication kernels (non-operational).
  • CLI Interface:
    • Commands for building, optimizing, visualizing, and running Python-like scripts.
  • Cross-Platform:
    • Primarily tested on Windows, designed for portability.

Project Structure

darkstarstrix-pyc/
β”œβ”€β”€ README.md
β”œβ”€β”€ Architecture.md
β”œβ”€β”€ CODE_OF_CONDUCT.md
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ Doc.md
β”œβ”€β”€ Hello.py
β”œβ”€β”€ hello.spec
β”œβ”€β”€ LICENSE
β”œβ”€β”€ Result.md
β”œβ”€β”€ third_party/                # External libraries (e.g., CLI11.hpp)
β”œβ”€β”€ AI/
β”‚   β”œβ”€β”€ graph_compiler.c
β”‚   β”œβ”€β”€ memory_planner.c
β”‚   β”œβ”€β”€ optimizer.c
β”‚   └── visualizer.c
β”œβ”€β”€ Core/
β”‚   β”œβ”€β”€ C_Files/
β”‚   β”‚   β”œβ”€β”€ backend.c
β”‚   β”‚   β”œβ”€β”€ codegen.c
β”‚   β”‚   β”œβ”€β”€ Core.cpp
β”‚   β”‚   β”œβ”€β”€ error_handler.c
β”‚   β”‚   β”œβ”€β”€ frontend.c
β”‚   β”‚   β”œβ”€β”€ IR.c
β”‚   β”‚   β”œβ”€β”€ ir_generator.c
β”‚   β”‚   β”œβ”€β”€ lexer.c
β”‚   β”‚   β”œβ”€β”€ main.cpp           # Updated to C++ for CLI11 integration
β”‚   β”‚   β”œβ”€β”€ parser.c
β”‚   β”‚   β”œβ”€β”€ stack.c
β”‚   β”‚   β”œβ”€β”€ symbol_table.c
β”‚   β”‚   └── test_parser.c
β”‚   └── Header_Files/
β”‚       β”œβ”€β”€ backend.h
β”‚       β”œβ”€β”€ Core.h
β”‚       β”œβ”€β”€ error_handler.h
β”‚       β”œβ”€β”€ frontend.h
β”‚       β”œβ”€β”€ graph.h
β”‚       β”œβ”€β”€ ir_generator.h
β”‚       β”œβ”€β”€ lexer.h
β”‚       β”œβ”€β”€ memory_planner.h
β”‚       β”œβ”€β”€ parser.h
β”‚       β”œβ”€β”€ stack.h
β”‚       └── symbol_table.h
β”œβ”€β”€ Examples/
β”‚   β”œβ”€β”€ matrix_mult.py
β”‚   └── simple_graph.py
β”œβ”€β”€ hello/
β”‚   β”œβ”€β”€ Analysis-00.toc
β”‚   β”œβ”€β”€ base_library.zip
β”‚   β”œβ”€β”€ EXE-00.toc
β”‚   β”œβ”€β”€ hello.pkg
β”‚   β”œβ”€β”€ PKG-00.toc
β”‚   β”œβ”€β”€ PYZ-00.pyz
β”‚   β”œβ”€β”€ PYZ-00.toc
β”‚   β”œβ”€β”€ warn-hello.txt
β”‚   β”œβ”€β”€ xref-hello.html
β”‚   └── localpycs/
└── Kernel/
    β”œβ”€β”€ kernel.cu
    └── matrix_mult.cu

Installation

Prerequisites

  • CMake: 3.29.6 or later
  • LLVM: Configured with a path set in CMakeLists.txt
  • C/C++ Compiler: C11 and C++14 compatible (e.g., GCC, MSVC)
  • Python 3.x: For testing and PyInstaller
  • CUDA Toolkit: Optional for experimental CUDA features
  • CLI11: Download CLI11.hpp from GitHub and place it in third_party/.

Build Steps

  1. Clone the repository:

    git clone https://github.com/DarkStarStrix/PyC.git
    cd PyC
  2. Configure with CMake:

    mkdir build
    cd build
    cmake ..
  3. Build the project:

    cmake --build . --config Release

The executable MyCompiler will be in build/bin/.

Usage

Run the compiler using the CLI interface:

./build/bin/MyCompiler [command] [options] input_file.pc

CLI Commands

  • build file.pc: Compiles and optimizes the Python-like script.
  • optimize file.pc --graph: Applies graph/tensor optimizations.
  • visualize file.pc: Outputs a visual computational graph diagram.
  • run file.pc: Executes the optimized pipeline.
  • kernel register kernel.cu: Registers a custom CUDA kernel into the runtime.

Example

Compile a file with an if statement:

./build/bin/MyCompiler build test.pc -o test

test.pc

x = 5
if x:
    y = x + 3

This generates LLVM IR, applies optimizations, and produces an executable.

How it works

PyC transforms Python code into optimized machine code through several stages:

1. Compilation Pipeline

Python Code β†’ IR β†’ Computational Graph β†’ Optimized CUDA/Machine Code
     ↓            ↓           ↓                    ↓
   Parser     LLVM IR    Graph Decomp      Memory Planning

2. Key Components

Frontend Processing

  • Parses Python syntax into an Abstract Syntax Tree (AST)
  • Performs type inference and validation
  • Generates intermediate representation (IR)

Graph Compilation

Input β†’ Graph Construction β†’ Decomposition β†’ Optimization
  ↓            ↓                 ↓              ↓
Code     Tensor Operations    Sub-graphs     Memory Layout

Memory Management

  • Dynamic tensor allocation
  • Memory pool management
  • Smart buffer reuse
  • Cache-friendly data layouts

CUDA Integration

Python Code β†’ Optimized Graph β†’ CUDA Kernels β†’ Execution
                    ↑                ↑
            Custom Kernels β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3. Usage Workflow

  1. Installation:

    # Install via package manager
    pip install pyc-compiler
    
    # Or build from source
    cmake --build .
  2. Write Python Code:

    # model.py
    def matrix_multiply(a, b):
        return a @ b
    
    def neural_network(x):
        return matrix_multiply(x, weights)
  3. Compile & Optimize:

    # Basic compilation
    pyc build model.py
    
    # With graph optimization
    pyc optimize model.py --graph
    
    # Register custom CUDA kernel
    pyc kernel register custom_matmul.cu
  4. Execution:

    # Run optimized code
    pyc run model.py

4. Optimization Techniques

Graph Level

  • Operation fusion
  • Dead code elimination
  • Memory access pattern optimization
  • Kernel fusion
  • Layout transformations

Memory Level

  • Buffer reuse
  • Smart allocation
  • Minimal data movement
  • Cache optimization

CUDA Integration

  • Custom kernel support
  • Automatic kernel selection
  • Memory transfer optimization
  • Stream management

5. Performance Benefits

  • Reduced Memory Usage: Smart tensor management
  • Faster Execution: Optimized computational graphs
  • GPU Acceleration: Efficient CUDA kernels
  • Lower Latency: Minimized data transfers
  • Better Cache Usage: Optimized memory patterns

6. Monitoring & Debug

# Generate optimization visualization
pyc visualize model.py

# View memory allocation patterns
pyc analyze model.py --memory

# Profile execution
pyc profile model.py

7. API Integration

from pyc import Compiler, Optimizer

# Programmatic usage
compiler = Compiler()
optimizer = Optimizer(enable_cuda=True)

# Compile and optimize
graph = compiler.compile("model.py")
optimized = optimizer.optimize(graph)

# Execute
result = optimized.run()

This architecture provides a seamless workflow from Python code to optimized execution, with full visibility into the optimization process and easy integration of custom components.

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines:

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature/your-feature.
  3. Commit changes: git commit -m "Add feature".
  4. Push and open a pull request.

Adhere to C11/C++14 standards and include comments.

License

Licensed under the Apache License 2.0. See LICENSE for details.

Acknowledgments

Developed by DarkStarStrix. Feedback is welcome via GitHub Issues.