Skip to main content

LEGO: Layout Expression Language for Code Generation

Project description

LEGO: Layout Expression Language for Code Generation

CI PyPI Python License: MIT

LEGO is an algebraic, compiler-agnostic framework for specifying and transforming memory layouts. It provides composable layout primitives that lower through a custom MLIR dialect to generate optimized code for CPU and GPU targets.

[LEGO: A Layout Expression Language for Code Generation of Hierarchical Mapping] [CGO 2026 Artifact]

Project Structure

LEGO/
├── python/                  # Python package (lego-layout)
│   ├── lego/
│   │   ├── core.py          # Layout primitives (Row, Col, RegP, GenP, OrderBy, GroupBy, TileByLayout)
│   │   ├── backend/         # MLIR compilation, JIT, SymPy lowering, PyTorch autograd
│   │   └── frontends/       # User-facing APIs (Triton @jit, NumPy/PyTorch tensor transforms)
│   ├── examples/            # Usage examples (triton, python_mlir, symbolic)
│   └── tests/               # Python tests
│
├── include/Lego/           # MLIR dialect headers (ODS definitions, passes)
├── lib/Lego/               # MLIR dialect implementation (lowering, verification, simplification)
├── tools/lego-opt/         # MLIR optimizer CLI
├── test/                   # MLIR lit tests
│
├── paper/                  # Paper benchmarks and evaluation scripts
├── docs/                   # Architecture and dialect documentation
├── scripts/                # Setup scripts
└── CMakeLists.txt          # Build system (monolithic and decoupled modes)

Architecture

All frontends lower through a unified MLIR-based backend:

 ┌─────────────┐ ┌────────────┐ ┌───────────┐
 │ Triton @jit │ │ Tensor API │ │ Symbolic  │
 └──────┬──────┘ └─────┬──────┘ └─────┬─────┘
        │              │              │
        └──────────────┼──────────────┘
                       │
                       v
        ┌──────────────────────────┐
        │       lego dialect       │
        │  ······················  │
        │  normalization           │
        │  simplification          │
        │  verification            │
        │  lowering                │
        └────────────┬─────────────┘
                     │
                     v
        ┌──────────────────────────┐
        │        LLVM / MLIR       │
        │  ······················  │
        │  X86 │ AArch64 │ NVPTX  │
        └──────────────────────────┘

Frontends

  • Triton JIT (lego.jit) -- @jit decorator that transforms Triton GPU kernels via AST rewriting (example)
  • Tensor API (lego.frontends.python_mlir) -- JIT-compiled layout transforms for NumPy and PyTorch tensors (example)
  • Symbolic (lego.core) -- SymPy-based algebraic layout expressions (example)

MLIR Backend

The lego MLIR dialect defines layout operations (gen_p, reg_p, row, col, order_by, group_by, tile_by, apply, apply_inverse) with types !lego.layout and !lego.view<T>. The dialect includes passes for:

  • Normalization -- desugar row/col/tile_by to primitive reg_p/order_by/group_by
  • Lowering -- lego ops to arith/scf/affine
  • Simplification -- optimize divui/remui patterns
  • Verification -- bijectivity, GPU bank conflicts, memory coalescing

Requirements

Dependency Version Notes
Python >= 3.12 Tested with 3.12, 3.13, 3.14
LLVM/MLIR commit a3d8e35 Included as a submodule
CMake >= 3.20
Ninja Recommended build generator
NumPy 2.1.2
SymPy 1.14.0

Installation

Quick install (Python package only)

pip install lego-layout

This installs the core layout algebra and frontends. The MLIR dialect native extensions are included in the wheel when available.

Development install

1. Clone and set up the environment

git clone https://github.com/tavakkoliamirmohammad/lego.git
cd lego
./scripts/setup.sh
source venv/bin/activate
pip install -e ./python

2. Build the MLIR dialect

Monolithic build (builds LLVM/MLIR + LEGO together):

cmake -S . -B build -DLEGO_MONOLITHIC_LLVM=ON
cmake --build build -j$(nproc) --target check-lego

Decoupled build (uses a prebuilt MLIR for fast iteration):

cmake -S . -B build -DMLIR_DIR=<mlir_build>/lib/cmake/mlir -DLEGO_MONOLITHIC_LLVM=OFF
cmake --build build -j$(nproc) --target check-lego

The build system automatically detects and uses fast linkers (mold/lld) and ccache.

To customize LLVM targets (default X86;NVPTX):

cmake -S . -B build -DLEGO_MONOLITHIC_LLVM=ON -DLEGO_LLVM_TARGETS="X86;NVPTX;AArch64"

Testing

# MLIR lit tests
cmake --build build --target check-lego

# Python tests
cmake --build build --target check-lego-python

# All tests
cmake --build build --target check-lego-all

Citation

If you use LEGO in your research, please cite:

Amir Mohammad Tavakkoli, Cosmin E. Oancea, and Mary Hall. "LEGO: A Layout Expression Language for Code Generation of Hierarchical Mapping." In 2026 IEEE/ACM International Symposium on Code Generation and Optimization (CGO), pp. 228-241, 2026.

@INPROCEEDINGS{tavakkoli2026lego,
  author={Tavakkoli, Amir Mohammad and Oancea, Cosmin E. and Hall, Mary},
  booktitle={2026 IEEE/ACM International Symposium on Code Generation and Optimization (CGO)},
  title={LEGO: A Layout Expression Language for Code Generation of Hierarchical Mapping},
  year={2026},
  pages={228-241},
  keywords={Codes;Algebra;Shape;Instruction sets;Layout;Graphics processing units;Organizations;Optimization;Indexing;Python;data layout;MLIR compiler;domain-specific optimization tools},
  doi={10.1109/CGO68049.2026.11394846}}

The paper artifact is available at: https://zenodo.org/records/17633994

License

MIT License. See LICENCE.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl (74.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ x86-64

lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl (26.0 MB view details)

Uploaded CPython 3.14macOS 15.0+ universal2 (ARM64, x86-64)

lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl (74.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl (26.0 MB view details)

Uploaded CPython 3.13macOS 15.0+ universal2 (ARM64, x86-64)

lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl (74.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl (26.0 MB view details)

Uploaded CPython 3.12macOS 15.0+ universal2 (ARM64, x86-64)

File details

Details for the file lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 ac5a0b72a7ee54ccdde39f27536e787298a79fa485f8fc538d20287675aa8309
MD5 18b1f043abf897caed96b52bbc9b7067
BLAKE2b-256 043914fa1f4f4def021ee61c8f24781702d8d7157ce8b2d279e0a0ba1e328510

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e85dc5f765283970279c1452dd5f2a835b67933bb307e54ebd7c26726e6c54fd
MD5 47f2a06bc676ab1ef3e999feb71a8de9
BLAKE2b-256 80352a99737c26ccc9aaf830fd22ed6b21885f6e374024570465db7b4cdd816d

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 14ee8dea1c34a6d7047af137b315c9d29aa7efc4ae5ee4f5d70a8c2fa8731399
MD5 0b167e3c7fffef504934ea50a44a210e
BLAKE2b-256 1bee2e2c6826f0fb2bad38dbf32cceb936df2b3a84abdc3ab5fca1db06c42a6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7e73c98a7ddbe24f34e5ecb35640e3fea17ca1b85fa36e82c55922c5daf83544
MD5 3241ee570bc6aaf0cccc58f332aca802
BLAKE2b-256 237c96862f9ecfb6670c509ba48f080a9faeed52962596a128ce9ce6e1b143f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 ba130bfbec077449752605651e506dea161788c7085403b0399772ce06276292
MD5 dac134986bc68ea18dbba606c49cdf1a
BLAKE2b-256 adafb14005a80252f209a83f29be17534ee879fcc893d531d01a3d9e17d52875

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 065b1a01b51b728cbf5f0977df5de41301f7fb9b2f0308ec0b90381823b6ddfa
MD5 951e706e576ed0f2b9c2ccb6b90458b7
BLAKE2b-256 4c42a24c5c2025b03574483480fd8819d62081a53e288dbe75cc48875f4c3957

See more details on using hashes here.

Provenance

The following attestation bundles were made for lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl:

Publisher: ci.yml on tavakkoliamirmohammad/LEGO

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page