Triton compiler for RISC-V platforms.
This repository is forked from triton-shared and provides a Triton compiler backend for RISC-V. The upstream triton-shared repo is no longer maintained, so this project is developed independently under the name triton-riscv.
Triton acts as a frontend (Python AST → TTIR); triton-riscv handles the rest of the pipeline (TTIR → Linalg → LLVM IR → native object). No NVIDIA or AMD toolchain is required.
Compilation pipeline:
Python @triton.jit
└─► TTIR (triton: ast_to_ttir + make_ttir passes)
└─► Linalg MLIR (triton-shared-opt --triton-to-linalg-experimental)
└─► LLVM MLIR (Buddy Compiler lowering passes)
└─► LLVM IR (mlir-translate --mlir-to-llvmir)
└─► native .o (llc -filetype=obj)
git clone https://github.com/RuyiAI-Stack/triton-riscv.git triton-riscv
TRITON_RISCV_DIR="$(pwd)/triton-riscv"
git clone https://github.com/triton-lang/triton.git triton
cd triton && git checkout "$(cat "${TRITON_RISCV_DIR}/triton-hash.txt")"Note: Ensure PyTorch is available in your virtual environment. For RISC-V, since PyTorch does not officially support RISC-V yet, you can build from source or use third-party builds: https://community-ci.openruyi.cn/pypi/riscv64/dev/+simple/torch . To use the third-party builds, python 3.12 or 3.13 are required.
Apply the build-system patches from triton-riscv/patches/ using the helper script. These patches make the NVIDIA/AMD LLVM codegen libraries conditional on their backends being enabled, so the build succeeds without any GPU toolchain.
"${TRITON_RISCV_DIR}/scripts/apply_patches.sh" "${TRITON_RISCV_DIR}/../triton"The script is idempotent: re-running it on an already-patched tree prints SKIPPED (patch already applied) per patch and exits cleanly.
Use either a Python venv or an existing conda environment:
python -m venv "${TRITON_RISCV_DIR}/.venv" --prompt triton-riscv
source "${TRITON_RISCV_DIR}/.venv/bin/activate"Or with conda:
conda activate ruyiaiWhen a conda environment is active, scripts/triton-riscv-env.sh automatically uses CONDA_PREFIX as TRITON_VENV.
-
Install the dependencies required by the Ruyi Buddy Compiler.
-
Install triton-riscv Python dependencies in the active environment:
pip install cmake ninja pytest-xdist pybind11 setuptools
cmake,ninja, andpybind11are needed because the rebuild script usespip install --no-build-isolationto reuse the active environment instead of creating an isolated build environment. -
Build the Buddy Compiler — Getting started
scripts/triton-riscv-env.sh centralizes the local environment variables needed by triton-riscv.
By default it assumes:
TRITON_RISCV_DIR=/path/to/triton-riscvTRITON_DIR=$TRITON_RISCV_DIR/../tritonTRITON_VENV=$CONDA_PREFIXwhen a conda environment is active, otherwise$TRITON_RISCV_DIR/.venvBUDDY_DIR=$TRITON_RISCV_DIR/../buddy-mlirBUILD_DIRis auto-detected from$TRITON_DIR/build/cmake.linux-*-cpython-*- runtime cache, dump, and JSON dependency directories default to
~/.triton
It exports:
TRITON_PLUGIN_DIRSLLVM_SYSPATHJSON_SYSPATHLLVM_BINARY_DIRBUDDY_MLIR_BINARY_DIRTRITON_SHARED_OPT_PATHTRITON_HOME,TRITON_CACHE_DIR,TRITON_DUMP_DIR,TRITON_OVERRIDE_DIR
If your local layout differs, override variables before sourcing:
export TRITON_DIR=/path/to/triton
export TRITON_VENV=/path/to/triton-venv # or rely on active conda env
export BUDDY_DIR=/path/to/buddy-mlir
source /path/to/triton-riscv/scripts/triton-riscv-env.shThis helper removes most of the repetitive local environment setup from the README, but it does not replace the external prerequisites themselves: Buddy/LLVM dependencies, a built buddy-mlir, a checked-out Triton tree, the virtual environment, and the patch step are still required.
For both the initial build and later source-only rebuilds:
conda activate ruyiai # or: source /path/to/triton-riscv/.venv/bin/activate
cd /path/to/triton-riscv
source scripts/triton-riscv-env.sh
scripts/rebuild-triton-riscv.shscripts/rebuild-triton-riscv.sh does the following:
- reuses the environment from
triton-riscv-env.sh - removes a stale Triton build directory when it was created under an old path or with a different Python version
- runs
python -m pip install --no-build-isolation -vvv .in$TRITON_DIR, which configures and builds Triton with the currentLLVM_SYSPATH,JSON_SYSPATH, and plugin settings - syncs
backend/compiler.pyandbackend/driver.pyinto the installedtriton_sharedbackend package - verifies the rebuilt install with Python import and
triton-shared-opt --version
Use the same command sequence after changing the backend, lowering pipeline, or C++ sources. If you change the buddy-opt pass pipeline or cached compilation behavior, clear Triton's cache after sourcing the environment and before rerunning tests:
rm -rf "$TRITON_CACHE_DIR"Build artifacts are placed under triton/build/{current_cmake_version}/third_party/triton_shared.
conda activate ruyiai # or: source /path/to/triton-riscv/.venv/bin/activate
cd /path/to/triton-riscv
source scripts/triton-riscv-env.sh
python -c "import triton; import triton.backends.triton_shared.compiler as c; print(triton.__version__); print(c.__file__)"
"$TRITON_SHARED_OPT_PATH" --versionpytest python/examples/ \
--ignore=python/examples/test_core.py \
--ignore=python/examples/test_annotations.py \
--ignore=python/examples/flaggems \
-vTo run a single test:
pytest python/examples/test_vec_add.py -vThe backend can compile a Python Triton kernel directly to a self-contained RISC-V Linux ELF, automatically generate its runner, execute it with QEMU, and verify outputs. No user-written C runner is required.
source scripts/triton-riscv-env.sh
python python/examples/rvv_vec_add_elf.pySee docs/06-RISCV-QEMU.md for toolchain setup, the end-to-end API, output verification, and troubleshooting.
See docs/07-Optimization.md for the current code-generation bottlenecks, correctness gaps, and optimization priorities identified from the RVV and bufferization analysis.
Building on RISC-V often runs into dependency issues; we're happy to help if you run into trouble.
Preload the correct Fortran library by adding the following to your virtualenv’s activate script (e.g. ~/triton/.venv/bin/activate):
export LD_PRELOAD=/usr/lib64/libgfortran.so.5:$LD_PRELOAD