253 points · by todsacerdoti · 1y ago · 255 comments
Originally, the rustc_codegen_gcc project made this promise but never fulfilled it.
Do you actually use all four of those platforms, or is this an arbitrary threshold for what you consider a complete set of platform support?
Though, the trend I'm seeing a lot of is greenfield projects just migrating their MCUs to ARM.
That’s what I would expect, too.
The Venn diagram of projects using an old architecture like alpha but also wanting to adopt a new programming language is nearly two separate circles.
The parent comment even included HPPA (PA-RISC) which almost makes me think they’re into either retro computing or they have some arbitrary completionist goal of covering all platforms.
Plus, there is a very vibrant community around the Motorola 68000 and SuperH CPUs thanks to the Amiga, Sega Dreamcast and many other classical computer systems and consoles.
But it turns out that rustc_codegen_gcc still hasn't delivered and it seems the project has fallen asleep.
Currently, `cg_gcc` is within spitting distance of being able to bootstrap the compiler. There really are only 3(!) bugs that currently stop a stage 2 build.
I know for sure, because I found workarounds for them, and have a working stage 2 build. A stage 3 build requires a bit more RAM than I have, but, besides that, it is definitely possible.
Those 3 bugs are: 1. Lack of support for 128 bit SwitchInt terminators(Rust IR equivalent of switch). This is likely caused by an issue on the GCC side, since libgccjit rejects 128 bit labels provided by `cg_gcc`. 2. A semantic difference between Rust's `#[inline(always)]` and `__attribute__((always_inline)) `. In Rust `#[inline(always)]` is a hint and works on recursive functions, but the GCC equivalent is not a hint, but a gurante, and does not work on recursive function. 3. `cg_gcc` miscompiles the Rust compiler's interner code if level 3 optimzations are enabled. The Rust compiler interner is quite complex, and does a lot of fiddly unsafe things, so it is the most likely to break. The exact cause of this issue is hard to pin down, but it can be worked around(by setting a lower opt level).
If you work around those issues, `cg_gcc` is able to successfully build the Rust compiler, at least on `x86_64 Linux`. Going from that to other architectures will still take time, but it is not as far away as some might think.
.NET
Core tests 1662 39 12 97.02%
C
Core tests 1419 294 82.83%
Missing from HN title: The "95%" pass rate only applies to .NET. For GCC/Clang it is only "80%".
https://github.com/dotnet/coreclr/pull/4504/files
Any reason why it would not work.
This hack is a Rust compiler back-end. Backends get platform-specific instructions as an input, so non-trivial generated C code won't be portable. Users will need to either get pre-generated platform-specific source, or install the Rust compiler and this back-end to generate one themselves.
> When targeting the MSVC ABI, Rust additionally requires an installation of Visual Studio so rustc can use its linker and libraries.
> When targeting the GNU ABI, no additional software is strictly required for basic use. However, many library crates will not be able to compile until the full MSYS2 with MinGW has been installed.
...
> Since the MSVC ABI provides the best interoperation with other Windows software it is recommended for most purposes. The GNU toolchain is always available, even if you don’t use it by default.
> My representation of .NETs IR maps nicely to C, which means that I was able to add support for compiling Rust to C in 2-3K LOC. Almost all of the codebase is reused, with the C and .NET specific code only present in the very last stage of compilation
It started as a .NET backend but they found that their approach could easily support C code generation as well so they added that. They do this by turning what rustc gives them into their own IR.
Arguably it’s not the compiled binary that’s “safe”. It’s the code.
Then the machine code generated by LLVM is not run directly by modern CPUs and is translated into internal representation first. And the future CPUs will behave like JIT-compilers with even more complex transformations.
The intermediate C code generated by this project just adds yet another transformation not fundamentally different from any of the above.
For example, signed overflow is UB in C, but defined in Rust. Generated code can't simply use the + operator.
C has type-based alias analysis that makes some type casts illegal. Rust handles alias analysis through borrowing, so it's more forgiving about type casts.
Rust has an UnsafeCell wrapper type for hacks that break the safe memory model and would be UB otherwise. C doesn't have such thing, so only uses of UnsafeCell that are already allowed by C are safe.
There are also escape hatches for strict aliasing in the C standard - mainly using memcpy for all memory operations.
> For example, signed overflow is UB in C, but defined in Rust. Generated code can't simply use the + operator.
Obviously, yes, but it could generate overflow checks.
This would have to go into one big unsafe block for any nontrivial program. C doesn’t convey all of the explicit things you need to know about the code to make it even compile in Rust.
If that feels uncomfortable, consider that x64 machine code has no approximation to rust safety checks, and you trust rust binaries running on x64.
"Correct" is doing some heavy lifting here but generally people seem willing to believe that their toolchain is bug free.
With the help of powerful enough AI we might be able to get a tool like this, but as AI still very much sucks at actually doing what it's supposed to do, I don't think we're quite ready yet. I imagine you'd also need enough memory to keep the entire C and Rust code base inside of your context window, which would quickly require very expensive hardware once your code grows beyond a certain threshold. If you don't, you end up like many code assisting LLMs, generating code independently that's incompatible with itself.
Still, if you're looking to take a C project and extend it in Rust, or perhaps slowly rewrite it piece by piece, https://c2rust.com/ is ready for action.
https://www.darpa.mil/news/2024/memory-safety-vulnerabilitie...
2. You can do LTO between Rust and the C->Rust code so in theory you could get a smaller & faster executable.
3. In most cases it is the first step to a gradual rewrite in idiomatic Rust.
Rust is a modern language, with package management, streamlined integrated build/testing tools, much less cruft, and lots of high-level features and syntax that people actually like. C is neat but complex codebases benefit from modern languages that help in building robust abstractions while still maintaining the speed of C. Not to mention, of course, the borrow checker and memory safety.
What is the point of compiling rust to C?
Just a guess.
“ The project aims to provide a way to easily use Rust libraries in .NET. It comes with a Rust/.NET interop layer, which allows you to easily interact with .NET code from Rust
[…]
While .NET is the main focus of my work, this project can also be used to compile Rust to C, by setting the C_MODE enviroment flag to 1.
This may seem like a strange and unrelated feature, but the project was written in such a way that this is not only possible, but relatively easy.”
It also doesn’t mention for which version of C it produces code. That may or may not hinder attempts to use this to run rust on obscure platforms.
Truth be told, the support for C was at first added as a proff-of-concept that a Rust to C compiler is possible. But it worked surprisingly well, so I just decided to roll with it, and see where it takes me.
My policy in regards to C version is: I want to be as close to ANSI C as possible. So, I avoid modern C features as much as I can. I don't know if full compatibility is achievable, but I certainly hope so. Only time will tell.
Some simpler pieces of Rust work just fine with ANSI C compilers, but more complex code breaks(eg. due to unsupported intrinsics). If I will be able to solve that(+ some potential soundness issues) then I'll be able to use ANSI C.
[1] https://en.wikipedia.org/wiki/Vala_(programming_language)
To address platforms that don't support Rust. TFA mentions NonStop, whatever it is.
https://en.m.wikipedia.org/wiki/Tandem_Computers
I think it would be useful in open-source, fault tolerance to copy one of their designs with SiFive's RISC-V cores. They could use a 20 year old approach to dodge patent issues. Despite its age, the design would probably be competitive, maybe better, than FOSS clusters on modern hardware in fault tolerance.
One might also combine the architecture with one of the strong-consistency DR'S, like FoundationDB or CochroachDB, with modifications to take advantage of its custom hardware. At the local site, the result would be easy scaling of a system whose nodes appeared to never fail. The administrator still has to do regular maintenance, though, as the system reports component failures which it works around.
Hell, you might finally be able to get Rust into the Linux kernel. Just don't tell them the code was originally written in Rust to calm their nerves.
Now we can quickly re-rustify projects by converting them to C.
Also, if a * b overflows within the result type, it is an undefined behavior according to the C standard, so this overflow check is at least not properly portable, either, and the shown code for that is actually buggy because the last A1 has to be A0.
No idea why all that gets me so grumpy today ...
https://www.gnu.org/software/c-intro-and-ref/manual/html_nod...
Since signed multiplication is bitwise-equivalent to unsigned multiplication, I use unsigned multiplication to emulate UB-free signed multiplication. The signed variant of this overflow check is a bit harder to read because of that, but it still works just fine.
bool i128_mul_ovf_check(__int128 A0 ,__int128 A1 ){
bb0:
if((A1) != (0)) goto bb1;
return false;
bb1:
return (((__int128)((__uint128_t)(A0) * (__uint128_t)(A1))) / (A1)) == (A1);
}
As for using `__builtin_popcountll` instead - you are right, my mistake. Thanks for pointing that out :).
I did not use the word "unsigned" before long long for the sake of readability - I know that repeating a word so many times can make it harder to parse for some folk. The project itself uses the correct types in the code, I was just kind of loose with the language in the article itself. My bad, I'll fix that and be a bit more accurate.
Once again, thanks for the feedback!
> This is an update on the progress I have made on my Rust to C compiler.
> There still are about 65 tests that need fixing, but they all seem to have pretty similar causes. So, fixing them should not be too difficult.
[1]: https://stackoverflow.com/questions/39004513/how-to-emit-llv...
It is a very impressive piece of work.
I do have a whole bunch of articles about the project on my website, going trough the entire journey of the project, from its start as a Rust to .NET compiler, to the current state.
https://fractalfir.github.io/generated_html/home.html
I should have probably linked the previous articles in this one - I'll add that to my website. I'll also look into adding some more context to the articles next time.
Thanks for the feedback :)
That's how GCC became so dominant - there were people already using all sorts of Unixen and they wanted a compiler, so they made it work.
In practice you can't really draw a line between those two things.
I don't know the end result, but I remember a discussion of how implementing parts of Git in Rust would be a problem because NonStop, a currently supported platform, has no Rust support. Of course the sane response would be "screw NonStop then", but having accepted contributions to make NonStop a supported platform that isn't an easy path to take.
But anyway this is kind of off-topic. I think OutOfHere was imagining that this somehow skips the type checking and borrow checking steps which of course it doesn't.
https://stackoverflow.com/questions/755005/how-does-bytecode...