[{"data":1,"prerenderedAt":20},["ShallowReactive",2],{"chapter:teenygrad\u002Fcompiler-internals\u002Fllvm-backend.json":3},{"project":4,"route":5,"title":6,"titleHtml":6,"navTitle":6,"part":7,"sourcePath":8,"editUrl":9,"html":10,"toc":11,"hasMermaid":12,"prev":13,"next":16},"teenygrad","\u002Fteenygrad\u002Fcompiler-internals\u002Fllvm-backend","The LLVM\u002FMLIR Backend","Compiler Internals","compiler-internals\u002Fllvm-backend.md","https:\u002F\u002Fgithub.com\u002Fteenygrad\u002Fteenygrad\u002Fedit\u002Fmain\u002Fbooks\u002Fteenygrad\u002Fsrc\u002Fcompiler-internals\u002Fllvm-backend.md","\u003Cp>\u003Ccode>teeny-compiler::compiler::backend::llvm\u003C\u002Fcode> compiles a kernel by shelling out to the custom\n\u003Ccode>teenyc\u003C\u002Fcode> compiler — a fork of \u003Ccode>rustc\u003C\u002Fcode> with an MLIR codegen backend — as a subprocess:\u003C\u002Fp>\n\u003Cpre class=\"code-panel\" data-lang=\"text\">\u003Ccode>teenyc &lt;kernel-source&gt;.rs \\\n  -Copt-level=3 \\\n  -Zcodegen-backend=mlir \\\n  --emit=obj \\\n  -o &lt;output&gt;.o \\\n  --target=nvptx64-nvidia-cuda \\\n  --crate-type=lib \\\n  -C overflow-checks=off \\\n  --frontend=triton\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>A few details worth knowing:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Locating \u003Ccode>teenyc\u003C\u002Fcode>\u003C\u002Fstrong>: \u003Ccode>compiler::find_teenyc\u003C\u002Fcode> uses \u003Ccode>TEENYC_PATH\u003C\u002Fcode> (env var) if set, otherwise\nauto-detects the sole \u003Ccode>rustup\u003C\u002Fcode>-linked toolchain whose name contains \u003Ccode>teenyc\u003C\u002Fcode> via\n\u003Ccode>rustup which --toolchain &lt;name&gt; teenyc\u003C\u002Fcode> — see\n\u003Ca href=\"\u002Fteenygrad\u002Fgetting-started\u002Finstallation\">Installation &amp; Toolchain\u003C\u002Fa>. This is a \u003Cem>runtime\u003C\u002Fem> dependency\nof \u003Ccode>teeny-compiler\u003C\u002Fcode>, not a build-time one — \u003Ccode>teeny-compiler\u003C\u002Fcode> itself builds with a plain\ntoolchain.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>\u003Ccode>-Zcodegen-backend\u003C\u002Fcode>\u003C\u002Fstrong> is an unstable rustc flag. \u003Ccode>teenyc\u003C\u002Fcode> is distributed on the stable channel\n(real version numbers, normal feature-gating) — \u003Ccode>RUSTC_BOOTSTRAP=1\u003C\u002Fcode> is set on the subprocess\ncall to permit this specific unstable flag against a stable-channel compiler, the same\nnarrowly-scoped mechanism rustc’s own bootstrap, \u003Ccode>bindgen\u003C\u002Fcode>, and \u003Ccode>miri\u003C\u002Fcode>’s installer rely on.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>\u003Ccode>--frontend=triton\u003C\u002Fcode>\u003C\u002Fstrong> tells \u003Ccode>teenyc\u003C\u002Fcode> to parse the input as \u003Ccode>teeny-triton\u003C\u002Fcode>’s DSL rather than\nplain Rust — the kernel source file written out for compilation is the DSL text\n(\u003Ccode>teeny_triton::triton_lang::TRITON\u003C\u002Fcode>) concatenated with your kernel’s own source.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Caching\u003C\u002Fstrong>: compiled kernels are cached by a hash of the kernel ID plus target\nCPU\u002FPTX-version overrides, so repeated compiles of the same kernel\u002Ftarget are skipped.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>\u003Ccode>TEENYC_PTX_VERSION\u003C\u002Fcode>\u003C\u002Fstrong>: overrides \u003Ccode>teenyc\u003C\u002Fcode>’s otherwise-conservative default PTX ISA floor for\na given \u003Ccode>-Ctarget-cpu\u003C\u002Fcode> — see \u003Ccode>teeny-compiler\u003C\u002Fcode>’s README\u002Frustdoc for when you need this (e.g.\ntargeting a GPU architecture newer than \u003Ccode>teenyc\u003C\u002Fcode>’s default assumption).\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cblockquote>\n\u003Cp>\u003Cstrong>TODO\u003C\u002Fstrong>: document the \u003Ccode>ndarray\u003C\u002Fcode> CPU backend as a parallel section once it has comparable\nsurface area documented here.\u003C\u002Fp>\n\u003C\u002Fblockquote>\n",[],false,{"title":14,"titleHtml":14,"route":15},"The egg\u002Fz3 Optimizer","\u002Fteenygrad\u002Fcompiler-internals\u002Foptimizer",{"title":17,"titleHtml":18,"route":19},"Using teeny-torch","Using \u003Ccode>teeny-torch\u003C\u002Fcode>","\u002Fteenygrad\u002Fpytorch-interop\u002Fteeny-torch",1786271829005]