<div dir="auto">I think it's just going to get more messy and complicated for people who don't want to learn or use another language. Mesa already requires people to know C, Python, and now newly Gitlab CI scripts just to get stuff done and merged. Another language would only exacerbate the issue and steepen the learning curve.<div dir="auto"><br></div><div dir="auto">Marek</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu., Oct. 1, 2020, 21:36 Alyssa Rosenzweig, <<a href="mailto:alyssa.rosenzweig@collabora.com">alyssa.rosenzweig@collabora.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
Recently I've been thinking about the potential for the Rust programming<br>
language in Mesa. Rust bills itself a safe system programming language<br>
with comparable performance to C [0], which is a naturally fit for<br>
graphics driver development.<br>
<br>
Mesa today is written primarily in C, a notoriously low-level language,<br>
with some components in C++. To handle the impedance mismatch, we've<br>
built up a number of abstractions in-tree, including multiple ad hoc<br>
code generators (GenXML, NIR algebraic passes, Bifrost disassembler). A<br>
higher level language can help avoid the web of metaprogramming and<br>
effect code that is simpler and easier to reason about. Similarly, a<br>
better type system can aid static analysis.<br>
<br>
Beyond abstraction, Rust's differentiating feature is the borrow checker<br>
to guarantee memory safety. Historically, safety has not been a primary<br>
concern of graphics drivers, since drivers are implemented as regular<br>
userspace code running in the process of the application calling them.<br>
Unfortunately, now that OpenGL is being exposed to untrusted code via<br>
WebGL, the driver does become an attack vector.<br>
<br>
For the time being, Mesa attempts to minimize memory bugs with defensive<br>
programming, safe in-tree abstractions (including ralloc), and static<br>
analysis via Coverity. Nevertheless, these are all heuristic solutions.<br>
Static analysis is imperfect and in our case, proprietary software.<br>
Ideally, the bugs we've been fixing via Coverity could be caught at<br>
compile-time with a free and open source toolchain.<br>
<br>
As Rust would allow exactly this, I see the primary benefit of Rust in<br>
verifying correctness and robustness, rather than security concerns per<br>
se.  Indeed, safety guarantees do translate well beyond WebGL.<br>
<br>
Practically, how would Rust fit in with our existing C codebase?<br>
Obviously I'm not suggesting a rewrite of Mesa's more than 15 million<br>
lines of C. Instead, I see value in introducing Rust in targeted parts<br>
of the tree. In particular, I envision backend compilers written in part<br>
in Rust. While creating an idiomatic Rust wrapper for NIR or Gallium<br>
would be prohibitively costly for now, a backend compiler could be<br>
written in Rust with IR builders exported for use of the NIR -> backend<br>
IR translator written in C.<br>
<br>
This would have minimal impact on the tree. Users that are not building<br>
such a driver would be unaffected. For those who _are_ building Rust<br>
code, the Rust compiler would be added as a build-time dependency and<br>
the (statically linked) Rust standard library would be added as a<br>
runtime dependency. There is concern about the Rust compiler requiring<br>
LLVM as a dependency, but again this is build-time, and no worse than<br>
Mesa already requiring LLVM as a runtime dependency for llvmpipe and<br>
clover. As for the standard library, it is possible to eliminate the<br>
dependency as embedded Rust does, perhaps calling out to the C standard<br>
library via the FFI, but this is likely quixotic. I do regret the binary<br>
size increase, however.<br>
<br>
Implications for the build system vary. Rust prefers to be built by its<br>
own package manager, Cargo, which is tricky to integrate with other<br>
build systems. Actually, Meson has native support for Rust, invoking the<br>
compiler directly and skipping Cargo, as if it were C code. This support<br>
is not widely adopted as it prevents linking with external libraries<br>
("crates", in Rust parlance), with discussions between Rust and Meson<br>
developers ending in a stand-still [1]. For Mesa, this might be just<br>
fine. Our out-of-tree run-time dependencies are minimal for the C code,<br>
and Rust's standard library largely avoids the need for us to maintain a<br>
Rust version of util/ in-tree. If this proves impractical in the<br>
long-term, it is possible to integrate Cargo with Meson on our end [2].<br>
<br>
One outstanding concern is build-time, which has been a notorious<br>
growing pain for Rust due to both language design and LLVM itself [3],<br>
although there is active work to improve both fronts [4][5]. I build<br>
Mesa on my Arm laptop, so I suppose I'd be hurt more than many of us.<br>
There's also awkward bootstrapping questions, but there is work here too<br>
[6].<br>
<br>
If this is of interest, please discuss. It's clear to me Rust is not<br>
going away any time soon, and I see value in Mesa embracing the new<br>
technology. I'd like to hear other Mesa developers' thoughts.<br>
<br>
Thanks,<br>
<br>
Alyssa<br>
<br>
[0] <a href="https://www.rust-lang.org/" rel="noreferrer noreferrer" target="_blank">https://www.rust-lang.org/</a><br>
[1] <a href="https://github.com/mesonbuild/meson/issues/2173" rel="noreferrer noreferrer" target="_blank">https://github.com/mesonbuild/meson/issues/2173</a><br>
[2] <a href="https://gitlab.gnome.org/GNOME/fractal/-/blob/master/meson.build" rel="noreferrer noreferrer" target="_blank">https://gitlab.gnome.org/GNOME/fractal/-/blob/master/meson.build</a><br>
[3] <a href="https://pingcap.com/blog/rust-compilation-model-calamity/" rel="noreferrer noreferrer" target="_blank">https://pingcap.com/blog/rust-compilation-model-calamity/</a><br>
[4] <a href="https://blog.mozilla.org/nnethercote/2020/04/24/how-to-speed-up-the-rust-compiler-in-2020/" rel="noreferrer noreferrer" target="_blank">https://blog.mozilla.org/nnethercote/2020/04/24/how-to-speed-up-the-rust-compiler-in-2020/</a><br>
[5] <a href="https://github.com/bjorn3/rustc_codegen_cranelift" rel="noreferrer noreferrer" target="_blank">https://github.com/bjorn3/rustc_codegen_cranelift</a><br>
[6] <a href="https://github.com/thepowersgang/mrustc" rel="noreferrer noreferrer" target="_blank">https://github.com/thepowersgang/mrustc</a><br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank" rel="noreferrer">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div>