TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

RLSL: a Rust to SPIR-V Compiler

199 点作者 Impossible超过 5 年前

10 条评论

calebwin超过 5 年前
I&#x27;ve been working on a project with similar goal to RLSL, called Emu. It&#x27;s a procedural macro that automatically off-loads portions of code to run on a GPU. Here&#x27;s a quick example...<p><pre><code> #[gpu_use] fn main() { let mut a = vec![0.2; 1000]; let mut b = vec![0.3; 1000]; let c = vec![0.0; 1000]; gpu_do!(load(a)); gpu_do!(load(b)); gpu_do!(load(c)); gpu_do!(launch()); for i in 0..1000 { c[i] = a[i] + b[i]; } gpu_do!(read(c)); println!(&quot;{:?}&quot;, c); } </code></pre> Emu is currently open-sourced as a 100% stable Rust library and while it only supports a tiny subset of Rust, that subset is well-defined with useful compile-time errors and a comprehensive test suite.<p>So if you&#x27;re looking for a way to contribute to single-source GPGPU for Rust, please consider helping expand Emu&#x27;s supported subset of Rust. The repository is at <a href="https:&#x2F;&#x2F;www.github.com&#x2F;calebwin&#x2F;emu" rel="nofollow">https:&#x2F;&#x2F;www.github.com&#x2F;calebwin&#x2F;emu</a><p>I will say that since Emu works at the AST&#x2F;syntax level, RLSL is of great interest to me because it works instead at the MIR level which allows it to more easily support a large subset of Rust.
评论 #21317040 未加载
评论 #21320250 未加载
mitchmindtree超过 5 年前
Woah excellent to see Embark partnering with the RLSL project!<p>As someone who does a lot of creative-coding contracts with lots of video, graphics and real-time requirements, RLSL has long been one of the Rust projects that excites me the most. The idea of writing graphics and compute shaders in Rust, a modern language with a decent type system, standard package manager, module system, etc, is very exciting. It makes a lot of sense that Embark see the potential in this for game dev too.<p>The ability to share Rust code between the CPU and GPU alone will be so useful. The number of times I&#x27;ve had to port some pre-existing Rust function to GLSL or vice versa is silly.<p>Obviously the Rust that compiles to SPIR-V will be a subset of the Rust that compiles to LLVM or WASM, but this still opens up so many doors for improved ergonomics when interacting with the GPU from a Rust program.<p>I&#x27;ve long dreamed of an API that allows me to casually send a closure of almost arbitrary Rust code (with the necessary &#x27;lifetime + Send + Sync + SpirvCompatible compile-time checks) off to the GPU for execution and get back a GPU Future of the result. It looks like this may well become possible in the future :)
评论 #21316365 未加载
AndrewGaspar超过 5 年前
This is a great write-up. I think Rust could be a very nice language for Shader-like applications which are already very functional, and don&#x27;t involve alot of shared, mutable state across threads.<p>In HPC, we&#x27;re very much interested in GPU compute programming, rather than shader programming. In CUDA codes, you&#x27;re typically doing transformation from input buffers directly into output buffers from your CUDA kernels. This should immediately raise red flags for a Rust developer - you&#x27;ve got shared, mutable state across threads!<p>Consider this simple CUDA-ish Rust code with threads independently executing over 0..cuda.len() (ignore the bounds bugs at i = 0 and i = in.len()):<p><pre><code> fn stencil(i: usize, in: &amp;[f32], out: &amp;mut [f32]) { out[i] = (in[i - 1] + in[i] + in[i + 1]) &#x2F; 3.0; } </code></pre> (The `i` is a conceit around computing indexing from thread&#x2F;block IDs, but the input and output arrays are pretty similar to the style CUDA promotes).<p>It&#x27;s obvious to me, the programmer, that I don&#x27;t have any aliasing issue - each thread is only mutating at a single index in the output array. However, Rust is not smart enough to see this. If they allowed the definition of the kernel as is, you could easily write multi-threaded code that has shared mutable access to individual memory locations, violating Rust&#x27;s memory model. OK, you force the kernel to look more like this, then:<p><pre><code> &#x2F;&#x2F; `in` is the slice of [i-1,i+1] fn stencil(in: &amp;[f32], out: &amp;mut f32) { *out = (in[0] + in[1] + in[2]) &#x2F; 3.0; } </code></pre> And you&#x27;d enforce Rust&#x27;s invariants at the kernel launch site, computing the valid slices at some higher level in the library in some &quot;unsafe&quot; code. But this only solves the simple case where you have some array mapping to another array where the index relationship is obvious, and it&#x27;s easily provable that there are no aliasing issues. Start layering in things like unique indirect indexing, or perhaps non-unique indexing but with atomic reductions, and it becomes difficult to phrase your correct program in a way to safe(!) Rust that is compatible with the borrow checker, at least without having to build a bunch of abstractions to express each of your parallel patterns. Having to build a bunch of bespoke abstractions may not be scalable to the types of developers building big scientific codes.<p>Anyway, I&#x27;m curious if the folks at &quot;Embark&quot; have spent any time thinking about the issue of shared, mutable state in GPU programming with Rust. It seems like a deal breaker from where I stand.
评论 #21316915 未加载
评论 #21316122 未加载
mshockwave超过 5 年前
I understand Rust&#x27;s advantage in lots of use cases, but can someone elaborate the benefits of using Rust over other languages for shader?
评论 #21316826 未加载
评论 #21318364 未加载
kvark超过 5 年前
Exciting talk! And I&#x27;m happy to know that one of the leading shader devs from Rust community has successfully &quot;embarked&quot; :)<p>Technology-wise, the compiler goes from MIR to SPIR-V. This is specific to Rust and different from the other direction Khronos has been exploring: <a href="https:&#x2F;&#x2F;github.com&#x2F;KhronosGroup&#x2F;LLVM-SPIRV-Backend" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;KhronosGroup&#x2F;LLVM-SPIRV-Backend</a> . It&#x27;s a bit sad that we can&#x27;t all have nice things.
评论 #21315543 未加载
rough-sea超过 5 年前
Just to be clear, SPIR-V has no relation to RISC-V?
评论 #21315821 未加载
评论 #21315604 未加载
kbumsik超过 5 年前
Call me dumb but I&#x27;m always confused with SPIR-V. Does it have any relationship with Vulkan or OpenGL?
评论 #21319166 未加载
评论 #21318609 未加载
gravypod超过 5 年前
This is amazing and I hope to see things like this extended. There are so many gotchas with glsl that it&#x27;s hard to get going and something with a really strict compiler would make that a lot less of a burden on an engineer. It would be really cool to see whole program &amp; profiling guided optimization between GPU inputs, shader stages, and GPU outputs.
waiseristy超过 5 年前
This is a great candidate for the Vulkano rust library: <a href="https:&#x2F;&#x2F;github.com&#x2F;vulkano-rs&#x2F;vulkano" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vulkano-rs&#x2F;vulkano</a><p>It&#x27;s currently using shaderc to compile glsl -&gt; spir-v but it&#x27;s clunky and takes forever to compile.<p>Would be great to have a more Rust-centric way to send spir-v over to the GPU
Avi-D-coder超过 5 年前
Is the video of the talk posted anywhere?