{"id":13711534,"url":"https://github.com/lennyerik/cutransform","last_synced_at":"2025-05-06T21:31:35.739Z","repository":{"id":163265813,"uuid":"638663126","full_name":"lennyerik/cutransform","owner":"lennyerik","description":"CUDA kernels in any language supported by LLVM","archived":false,"fork":false,"pushed_at":"2023-09-06T13:27:31.000Z","size":53,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-13T22:34:55.552Z","etag":null,"topics":["c","cuda","gpgpu","gpu-compute","llvm","llvm-ir","nvidia","ptx","rust","zig"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lennyerik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-05-09T20:46:15.000Z","updated_at":"2024-11-08T11:22:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"af54c013-1ea6-477d-8694-4c88a798392b","html_url":"https://github.com/lennyerik/cutransform","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lennyerik%2Fcutransform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lennyerik%2Fcutransform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lennyerik%2Fcutransform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lennyerik%2Fcutransform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lennyerik","download_url":"https://codeload.github.com/lennyerik/cutransform/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772014,"owners_count":21801830,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cuda","gpgpu","gpu-compute","llvm","llvm-ir","nvidia","ptx","rust","zig"],"created_at":"2024-08-02T23:01:09.166Z","updated_at":"2025-05-06T21:31:35.421Z","avatar_url":"https://github.com/lennyerik.png","language":"Rust","funding_links":[],"categories":["Frameworks","GPU Programming","Multimedia \u0026 Graphics"],"sub_categories":["GPU Computing"],"readme":"# cutransform\nAre you tired of having to write your CUDA kernel code in C++?\nThis project aims to make it possible to compile CUDA kernels written in any language supported by LLVM without much hassle.\nSpecifically, this is basically a transpiler from LLVM-IR to NVVM-IR.\n\nImportantly, languages like plain C, Rust and Zig are all supported.\nExpecially CUDA in Rust is not yet very good and [Rust-CUDA](https://github.com/Rust-GPU/Rust-CUDA) has been stale since July 2022.\nMaybe we can fix that by using a different approach to the problem of CUDA codegen.\n\n**This is not a CUDA runtime API wrapper! You cannot run the kernels with this project alone!**\nIf you're just looking for a simple way to write CUDA in Rust though, you're in luck.\n[cust](https://crates.io/crates/cust) is a really good wrapper around the CUDA API.\n\n\n## How it works\nIn order to compile a kernel in any language with an LLVM frontend, we\n\n* Invoke the standard compiler for the language and tell it to output LLVM bitcode for the nvptx64-nvidia-cuda target\n* Pass the generated bitcode to the code transformer (cutransform)\n  * The transformer will parse the bitcode and add required attributes and functions and structs\n  * It will output this modified version of the bitcode\n* Finally the bitcode can simply be passed through the llvm-bitcode compiler, llc to generate the PTX assembly\n* (Optional) Additionally can now choose to assemble the PTX to a SASS (cubin) program for your specific graphics card using Nvidia's proprietary ptxas assembler\n\n\n## Setup\nYou should already have\n\n* clang\n* llvm\n* cuda\n\nThen compile the cutransform binary:\n\n    cd cutransform\n    cargo build --release\n\nIf the build fails with an error message from the `llvm-sys` crate, you likely have a build of LLVM without the static libraries.\nThis is the default for newer LLVM binary distributions.\nTo build with a dynamically linked LLVM, run:\n\n    cargo build --release --features dynamic-llvm\n\ninstead.\n\n## Rust example usage\nFirst, make sure you have the nvptx Rust target installed:\n\n    rustup target add nvptx64-nvidia-cuda\n\nHere is an example Rust kernel:\n```rust\n#![no_std]\n\nextern \"C\" {\n    fn threadIdxX() -\u003e u32;\n}\n\n#[no_mangle]\npub extern \"C\" fn kernel(arr: *mut u32) {\n    unsafe {\n        let idx = threadIdxX() as usize;\n        *arr.add(idx) = 123;\n    }\n}\n```\n\n**Please note that all kernel functions should have a name starting with the word \"kernel\". Otherwise they won't be exported.**\n\nTo compile the Rust kernel to LLVM bitcode, run:\n\n    rustc -O -C opt-level=3 -o kernel.bc --emit llvm-bc --target nvptx64-nvidia-cuda -C target-cpu=sm_86 -C target-feature=+ptx75 --crate-type lib kernel.rs\n\nYou can change `sm_86` flag to the minimum supported compute capability of your kernel (8.6 is the newest supported in clang and it's mostly for 30-series cards and onwards).\nRefer to [this Wikipedia page](https://en.wikipedia.org/wiki/CUDA#GPUs_supported) for a list of cards and their supported compute capabilities.\n\nNow, run cutransform on the llvm bitcode\n\n    cutransform/target/release/cutransform kernel.bc\n\nFinally, compile the new bitcode to PTX:\n\n    llc -O3 -mcpu=sm_86 -mattr=+ptx75 kernel.bc\n\nNow you can also choose to assemble the PTX for your card:\n\n    ptxas --allow-expensive-optimizations true -o kernel.cubin --gpu-name sm_89 kernel.s\n\nWhere you can again change `sm_89` to the compute capability of your card.\nCompute capability 8.9 is for 40-series cards.\n\nFor a complete and integrated example, see the `rust-example` crate included in this repo.\n\n\n## C example usage\nHere is an example C kernel:\n```c\nextern int threadIdxX(void);\n\nvoid kernel(int *arr) {\n    arr[threadIdxX()] = 123;\n}\n```\n\n**Please note that all kernel functions should have a name starting with the word \"kernel\". Otherwise they won't be exported.**\n\nTo compile the C kernel to LLVM bitcode, run:\n\n    clang -cc1 -O3 -triple=nvptx64-nvidia-cuda -target-cpu sm_86 -target-feature +ptx75 -emit-llvm-bc -o kernel.bc kernel.c\n\nNow, run cutransform on the llvm bitcode\n\n    cutransform/target/release/cutransform kernel.bc\n\nFinally, compile the new bitcode to PTX:\n\n    llc -O3 -mcpu=sm_86 -mattr=+ptx75 kernel.bc\n\nNow you can also choose to assemble the PTX for your card:\n\n    ptxas --allow-expensive-optimizations true -o kernel.cubin --gpu-name sm_89 kernel.s\n\nWhere you can again change `sm_89` to the compute capability of your card.\nCompute capability 8.9 is for 40-series cards.\n\nFor a complete and integrated example, see the `c-example` folder included in this repo.\n\n## Zig example usage\nHere is an example Zig kernel:\n```zig\nextern fn threadIdxX() i32;\n\nexport fn kernel(arr: [*]u32) callconv(.C) void {\n    arr[@intCast(usize, threadIdxX())] = 123;\n}\n\n// Override the default entrypoint\npub fn _start() callconv(.Naked) void {}\n```\n\n**Please note that all kernel functions should have a name starting with the word \"kernel\". Otherwise they won't be exported.**\n\nTo compile the Zig kernel to LLVM bitcode, run:\n\n    zig build-obj -O ReleaseSmall -target nvptx64-cuda -mcpu sm_86+ptx75 -fno-emit-asm -femit-llvm-bc=kernel.bc kernel.zig\n\nNow, run cutransform on the llvm bitcode\n\n    cutransform/target/release/cutransform kernel.bc\n\nFinally, compile the new bitcode to PTX:\n\n    llc -O3 -mcpu=sm_86 -mattr=+ptx75 kernel.bc\n\nNow you can also choose to assemble the PTX for your card:\n\n    ptxas --allow-expensive-optimizations true -o kernel.cubin --gpu-name sm_89 kernel.s\n\nWhere you can again change `sm_89` to the compute capability of your card.\nCompute capability 8.9 is for 40-series cards.\n\nFor a complete and integrated example, see the `zig-example` folder included in this repo.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flennyerik%2Fcutransform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flennyerik%2Fcutransform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flennyerik%2Fcutransform/lists"}