A Python DSL and a MLIR stack for authoring high‑performance GPU kernels with explicit layouts and tiling.
FlyDSL is the Python front‑end of the project: a Flexible Layout Python DSL for expressing
tiling, partitioning, data movement, and kernel structure at a high level.
FlyDSL: FlyDSL is powered by the Fly dialect:
an end‑to‑end, MLIR‑native compiler stack for GPU kernels. Its core is the fly dialect—a first‑class
layout IR with explicit algebra and coordinate mapping, plus a composable lowering pipeline to GPU/ROCDL.
Overview
FlyDSL (Python DSL): author kernels in Python, compile them through the Fly dialect,
and export compiled specializations as linkable C objects
Primary package: python/flydsl/
Kernel examples: kernels/ (importable as kernels.* from a source checkout; not installed in the wheel)
Fly dialect: the layout IR and compiler foundation
Python deps: nanobind, numpy, pybind11 (installed by scripts/build_llvm.sh; install them manually if you skip that step)
ROCm device bitcode: scripts/build.sh bundles amdgcn/bitcode/*.bc from the ROCm found via
ROCM_PATH/ROCM_ROOT/ROCM_HOME or /opt/rocm, so kernels calling __ocml_* compile wherever the
package is later installed. Override with -DFLYDSL_ROCM_BITCODE_DIR=<dir>; skipping it only means
those kernels need FLYDSL_COMPILE_ROCM_PATH or ROCM_PATH set at compile time.
# Clone ROCm LLVM and build MLIR (takes ~30min with -j64)
bash scripts/build_llvm.sh -j64
# Build FlyDSL C++ dialects, compiler passes, and Python bindings
bash scripts/build.sh -j64
# Install in development mode
python -m pip install -e .
If you already have an MLIR build with Python bindings enabled, point to it instead:
python -m pip install nanobind numpy pybind11 # build.sh does not install these
export MLIR_PATH=/path/to/llvm-project/build-flydsl/mlir_install
MLIR_PATH=$MLIR_PATH bash scripts/build.sh -j64
python -m pip install -e .
Note: If MLIR_PATH is set in your environment pointing to a wrong LLVM build, unset MLIR_PATH first.
The build scripts use python3 on PATH by default. Set PYTHON_EXECUTABLE
to select another Python 3.10+ interpreter (including a virtual environment):
LLVM's Python dependencies are installed into that interpreter's environment.
Both CMake Python detection passes are pinned to it. Builds launched by
python -m pip or python setup.py use that Python interpreter instead of
PATH or an inherited PYTHON_EXECUTABLE, so newly built bindings match the wheel ABI.
Use a separate FLY_BUILD_DIR for each Python version to avoid reusing old bindings.
Build on Windows
Prerequisites:
Visual Studio 2022 with the C++ tools and the C++ Clang tools, or pass
-ClangCl <path to clang-cl.exe> to both scripts.
CMake 3.20+, Ninja and Git on PATH.
64-bit Python 3.10+ in a venv with setuptools and wheel.
In that venv, TheRock wheels for your GPU family
(--index-url https://rocm.nightlies.amd.com/v2/gfx120X-all/): torch first, then
the matching rocm[libraries,devel]==<rocm_sdk.__version__>, then rocm-sdk init.
Without a torch that sees the GPU, set FLYDSL_GPU_ARCH (for example gfx1201).
AOT export (export_to_c) writes a COFF .obj; link it with amdhip64.lib
(see docs/api/compiler.rst).
Run tests
Tests and examples require pytest, pandas, and a ROCm build of torch (not installed by pip install -e .):
python -m pip install pytest pandas
# torch must be a ROCm build matching your ROCm version (rocm7.2 shown):
python -m pip install torch --index-url https://download.pytorch.org/whl/rocm7.2
# Run GEMM correctness tests (fast, ~15s)
python -m pytest tests/kernels/test_preshuffle_gemm.py -m "not large_shape"
# Run performance benchmarks
bash scripts/run_benchmark.sh
Test layout, pytest markers, and environment variables used by the suite are documented in tests/README.md .
Quick reference
# Install from PyPI:
python -m pip install flydsl
# Full source build from scratch:
bash scripts/build_llvm.sh -j64 # one-time: build LLVM/MLIR
bash scripts/build.sh -j64 # build FlyDSL
python -m pip install -e . # install in dev mode
bash scripts/run_tests.sh # verify
# Rebuild after code changes (C++ only):
bash scripts/build.sh -j64
# Rebuild after Python-only changes:
# No rebuild needed — editable install picks up changes automatically.
Troubleshooting
Wrong LLVM picked up (std::gcd not found, redeclaration errors)
unset MLIR_PATH and let build.sh auto-detect, or set it to the correct path.
No module named flydsl
Run python -m pip install flydsl. For source checkouts, run python -m pip install -e . after building.
The skills under .claude/skills/ for authoring, debugging, profiling and optimizing FlyDSL kernels are also published as a Claude Code plugin, so you can use them outside this checkout:
/plugin install flydsl --marketplace ROCm/FlyDSL
Or from the shell: claude plugin marketplace add ROCm/FlyDSL && claude plugin install flydsl@flydsl. Skills are invoked as /flydsl:<skill> (e.g. /flydsl:debug-flydsl-kernel); update with claude plugin update flydsl@flydsl.
divide(A, B): Partition layout A by B (Logical, Tiled, etc.).
🐍 Python API (flydsl)
@flyc.kernel / @flyc.jit API
import flydsl.compiler as flyc
import flydsl.expr as fx
from flydsl.expr import arith, gpu
@flyc.kernel
def my_kernel(arg_a: fx.Tensor, arg_b: fx.Tensor, n: fx.Constexpr[int]):
tid = gpu.thread_idx.x
bid = gpu.block_idx.x
# ... kernel body using layout ops ...
@flyc.jit
def launch(arg_a: fx.Tensor, arg_b: fx.Tensor, n: fx.Constexpr[int],
stream: fx.Stream = fx.Stream(None)):
grid_x = (n + 255) // 256
my_kernel(arg_a, arg_b, n).launch(
grid=(grid_x, 1, 1),
block=(256, 1, 1),
stream=stream,
)
Compilation Pipeline
On first call, @flyc.jit traces the Python function into an MLIR module, then compiles it through MlirCompiler. The pass list is built by RocmBackend._pipeline_parts() in three stages — see docs/architecture_guide.md for the per-pass table.
Compiled kernels are cached to disk (~/.flydsl/cache/) and reused on subsequent calls with the same type signature.
⚙️ Hierarchical Kernel Control
FlyDSL keeps the tiling hierarchy explicit across block, warp, thread, and instruction scopes using layout algebra:
import flydsl.expr as fx
# Define thread and value layouts for tiled copy
thr_layout = fx.make_layout((THR_M, THR_N), (1, THR_M))
val_layout = fx.make_layout((VAL_M, VAL_N), (1, VAL_M))
# Create tiled copy with vectorized atoms
copy_atom = fx.make_copy_atom(fx.UniversalCopy32b(), fx.Float32)
layout_thr_val = fx.raked_product(thr_layout, val_layout)
tile_mn = fx.make_tile(fx.make_layout(THR_M, 1), fx.make_layout(VAL_M, 1))
tiled_copy = fx.make_tiled_copy(copy_atom, layout_thr_val, tile_mn)
# Partition tensor across blocks and threads
thr_copy = tiled_copy.get_slice(tid)
partition_src = thr_copy.partition_S(block_tile_A)
partition_dst = thr_copy.partition_D(register_fragment)
# Execute copy
fx.copy(copy_atom, partition_src, partition_dst)
With per-level partitions, you can allocate register fragments, emit predicate masks, and schedule MFMA/vector instructions while retaining full knowledge of the execution hierarchy.
🧮 Minimal VecAdd Example
This condensed snippet mirrors examples/01-vectorAdd.py, showing how to define GPU kernels with layout algebra and tiled copies: