Rust 单态化:Instance 类型与惰性实例化 Rust 的单态化跟很多人想的不一样——它不在编译期预先生成每个泛型实例的独立 MIR,而是用 Instance(def + args) 这种'定义 + 参数'的轻量结构,在 codegen 时按需替换。本文讲清楚 Instance 类型、惰性单态化、is_fully_monomorphized 的两个检查,以及 cuda-oxide 怎么用它过滤泛型代码。 zTgx 0 0 0 专题compilerrust
cuda-oxide:hello-constant 拆解 04——codegen_crate 入口接管 拆 hello-constant 系列第四站。终于进入我们自己的代码——librustc_codegen_cuda.so 被 rustc dlopen 之后,__rustc_codegen_backend 返回一个包装了 LlvmCodegenBackend 的对象,后续 trait 调用大多转发,只在 codegen_crate 这一处拦截 device 路径。 zTgx 0 0 0 专题cuda-oxiderust
cuda-oxide:hello-constant 拆解 05——找 #[kernel] 函数 + BFS 调用图 + stable MIR 转换 拆 hello-constant 系列第五站。codegen_crate 检测到 device code 之后,先靠命名约定(KERNEL_PREFIX 加魔数 246e25db)识别 kernel,worklist BFS 遍历调用图收集所有可达的 device 函数,最后通过 rustc_public 把 internal Instance 转成 stable Instance 喂给 mir-importer。本文讲清楚整条 collector 流程,以及背后那串 246e25db 是怎么来的。 zTgx 0 0 0 专题cuda-oxiderust
cuda-oxide:用组合代替继承——一个真实例子 面向对象书里讲烂了的 Composition over Inheritance,在 cuda-oxide 的 CudaCodegenBackend 上是怎么落地的——为什么我们不去重写一个 LLVM 后端,而是把它包起来。 zTgx 0 0 0 专题cuda-oxiderust
rustc -Z codegen-backend:替换 Rust 编译器后端 rustc 的 -Z codegen-backend 是 nightly 阶段的插件机制,允许把默认 LLVM 后端换成自定义实现——Cranelift、GCC、甚至 CUDA。详细讲它的协议、加载流程、CodegenBackend trait,以及自己写一个要注意什么。 zTgx 1 0 0 专题compilerrustc
cuda-oxide:hello-constant 拆解 03——rustc 前端把源码变成 MIR 拆 hello-constant 系列第三站。进入子进程,看 rustc 的前端流程——五段流水线把 src/main.rs 变成 MIR。我们 backend 拿到的输入到底长什么样?为什么 MIR 是恰到好处的过渡点?这一篇讲清楚 backend 的'原料'。 zTgx 0 0 0 专题cuda-oxiderust
cuda-oxide:hello-constant 拆解 02——cargo-oxide driver 进程做了什么 拆 hello-constant 系列第二站。停在全景图最上面那个方框,看 cargo-oxide driver 进程在 spawn rustc 之前到底做了哪些事——找 backend .so、拼 RUSTFLAGS、然后挂起等子进程退出。它不做编译,只做配置。 zTgx 0 0 0 专题cuda-oxiderust
cuda-oxide:rustc backend dylib 是怎么编出来的 聚焦一个 hello-constant 拆解正篇略过的问题:librustc_codegen_cuda.so 到底是怎么编出来的?Cargo.toml 里 dylib、空 [workspace]、#![feature(rustc_private)] 三个不寻常的设计各自解决了什么问题。 zTgx 1 0 0 专题cuda-oxiderust