LEGO: Layout Expression Language for Code Generation
Project description
LEGO: Layout Expression Language for Code Generation
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) --@jitdecorator 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_byto primitivereg_p/order_by/group_by - Lowering --
legoops toarith/scf/affine - Simplification -- optimize
divui/remuipatterns - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl
- Upload date:
- Size: 74.7 MB
- Tags: CPython 3.14, manylinux: glibc 2.35+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac5a0b72a7ee54ccdde39f27536e787298a79fa485f8fc538d20287675aa8309
|
|
| MD5 |
18b1f043abf897caed96b52bbc9b7067
|
|
| BLAKE2b-256 |
043914fa1f4f4def021ee61c8f24781702d8d7157ce8b2d279e0a0ba1e328510
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp314-cp314-manylinux_2_35_x86_64.whl -
Subject digest:
ac5a0b72a7ee54ccdde39f27536e787298a79fa485f8fc538d20287675aa8309 - Sigstore transparency entry: 1114460513
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl
- Upload date:
- Size: 26.0 MB
- Tags: CPython 3.14, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e85dc5f765283970279c1452dd5f2a835b67933bb307e54ebd7c26726e6c54fd
|
|
| MD5 |
47f2a06bc676ab1ef3e999feb71a8de9
|
|
| BLAKE2b-256 |
80352a99737c26ccc9aaf830fd22ed6b21885f6e374024570465db7b4cdd816d
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp314-cp314-macosx_15_0_universal2.whl -
Subject digest:
e85dc5f765283970279c1452dd5f2a835b67933bb307e54ebd7c26726e6c54fd - Sigstore transparency entry: 1114460503
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl
- Upload date:
- Size: 74.7 MB
- Tags: CPython 3.13, manylinux: glibc 2.35+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14ee8dea1c34a6d7047af137b315c9d29aa7efc4ae5ee4f5d70a8c2fa8731399
|
|
| MD5 |
0b167e3c7fffef504934ea50a44a210e
|
|
| BLAKE2b-256 |
1bee2e2c6826f0fb2bad38dbf32cceb936df2b3a84abdc3ab5fca1db06c42a6e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp313-cp313-manylinux_2_35_x86_64.whl -
Subject digest:
14ee8dea1c34a6d7047af137b315c9d29aa7efc4ae5ee4f5d70a8c2fa8731399 - Sigstore transparency entry: 1114460539
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl
- Upload date:
- Size: 26.0 MB
- Tags: CPython 3.13, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e73c98a7ddbe24f34e5ecb35640e3fea17ca1b85fa36e82c55922c5daf83544
|
|
| MD5 |
3241ee570bc6aaf0cccc58f332aca802
|
|
| BLAKE2b-256 |
237c96862f9ecfb6670c509ba48f080a9faeed52962596a128ce9ce6e1b143f2
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp313-cp313-macosx_15_0_universal2.whl -
Subject digest:
7e73c98a7ddbe24f34e5ecb35640e3fea17ca1b85fa36e82c55922c5daf83544 - Sigstore transparency entry: 1114460527
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl
- Upload date:
- Size: 74.7 MB
- Tags: CPython 3.12, manylinux: glibc 2.35+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba130bfbec077449752605651e506dea161788c7085403b0399772ce06276292
|
|
| MD5 |
dac134986bc68ea18dbba606c49cdf1a
|
|
| BLAKE2b-256 |
adafb14005a80252f209a83f29be17534ee879fcc893d531d01a3d9e17d52875
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp312-cp312-manylinux_2_35_x86_64.whl -
Subject digest:
ba130bfbec077449752605651e506dea161788c7085403b0399772ce06276292 - Sigstore transparency entry: 1114460495
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl.
File metadata
- Download URL: lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl
- Upload date:
- Size: 26.0 MB
- Tags: CPython 3.12, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
065b1a01b51b728cbf5f0977df5de41301f7fb9b2f0308ec0b90381823b6ddfa
|
|
| MD5 |
951e706e576ed0f2b9c2ccb6b90458b7
|
|
| BLAKE2b-256 |
4c42a24c5c2025b03574483480fd8819d62081a53e288dbe75cc48875f4c3957
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lego_layout-0.1.4-cp312-cp312-macosx_15_0_universal2.whl -
Subject digest:
065b1a01b51b728cbf5f0977df5de41301f7fb9b2f0308ec0b90381823b6ddfa - Sigstore transparency entry: 1114460519
- Sigstore integration time:
-
Permalink:
tavakkoliamirmohammad/LEGO@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/tavakkoliamirmohammad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f4e689f5240f811e316b15cc13323c4b7bcdd254 -
Trigger Event:
push
-
Statement type: