Peter Zijlstra
2025-Dec-04 11:11 UTC
[PATCH 4/4] build: rust: provide an option to inline C helpers into Rust
On Thu, Dec 04, 2025 at 10:23:17AM +0000, Alice Ryhl wrote:> > The other day [*] I proposed extending Rust such that it would be able > > to consume a clang precompiled header directly, this would allow doing > > away with most of all this. No more helpers and no more bindgen. > > > > Would that not be a much saner approach to all this? > > > > [*] https://lkml.kernel.org/r/20251124163315.GL4068168 at noisy.programming.kicks-ass.net > > I have actually discussed similar ideas in the past with Josh Triplett, > so you are not the only one who thinks it is a good idea. Unfortunately, > the road to get there is long.Right. Earlier I also proposed using libclang to parse the C header and inject that. This might be a little simpler, in that..> Another option to get rid of the helpers is that bindgen is working on a > --wrap-static-fns flag, which would generate the helpers for us. > However, that route would still require this patch for them to be > inlined. > > One detail that makes the precompiled clang header really tricky is > that IMO we should not require RUSTC_CLANG_LLVM_COMPATIBLE for the > build. With bindgen, you just need bindgen and clang to match LLVMs. > That's easy since bindgen loads a dylib from your clang install. But if > you build this logic into rustc, then you need to be really careful to > get rustc and clang from the same source, and that same source must use > the same LLVM to build both.... if you build rustc against libclang they are necessarily from the same LLVM build. But that might be more tricky in that you might need ways to specify C specific build flags. Anyway, good to know people are in fact pondering this, because IMO the whole interoperability thing with C is quite terrible.
Miguel Ojeda
2025-Dec-04 11:57 UTC
[PATCH 4/4] build: rust: provide an option to inline C helpers into Rust
On Thu, Dec 4, 2025 at 12:11?PM Peter Zijlstra <peterz at infradead.org> wrote:> > Right. Earlier I also proposed using libclang to parse the C header and > inject that. This might be a little simpler, in that..Yeah, that would be closer to the `bindgen` route in that `libclang` gets already involved.> ... if you build rustc against libclang they are necessarily from the > same LLVM build.So currently there are 3 "LLVMs" that get involved: - The one Clang uses (in LLVM=1 builds). - The one `rustc` uses (the LLVM backend). - The one `bindgen` uses (via libclang). If that is all done within `rustc` (so no `bindgen`), then there may still be `rustc` vs. Clang mismatches, which are harder to resolve in the Rust side at least (it is easier to pick another Clang version to match). For those using builds from distros, that shouldn't be a problem. Others using external `rustc` builds, e.g. from `rustup` (e.g. for testing different Rust versions) it would be harder. But I mean, anything approach that gets us into a better position is welcome and I think requiring people to match LLVM everywhere should be easier now that distributions are starting to enable Rust (even Debian). We have been talking about this since the very beginning of the project -- e.g. I remember Wedson and I talking to Josh et al. about improving the situation here (in particular, talking about integrating a solution into `rustc` directly) long before Rust was merged into the kernel. Even on things like a `rustc cc` or `cImport` like Zig (but Zig moved on the other direction since then), which I recall Gary having opinions about too. There is also the question about GCC. A deeper integration into `rustc` would ideally need to have a way (perhaps depending on the backend picked?) to support GCC builds properly (to read the header and flags as expected, as you mention). And finally there is the question of what GCC Rust would do in such a case. Things have substantially changed on the GCC Rust in the last years, and they are now closer to build the kernel, thus I think their side of things is getting important to consider too. Cc'ing Emilio (`bindgen`), Antoni (GCC backend) and Arthur (GCC Rust) so that they are in the loop -- context at: https://lore.kernel.org/rust-for-linux/20251204111124.GJ2528459 at noisy.programming.kicks-ass.net/ Cheers, Miguel
Peter Zijlstra
2025-Dec-04 12:39 UTC
[PATCH 4/4] build: rust: provide an option to inline C helpers into Rust
On Thu, Dec 04, 2025 at 12:57:31PM +0100, Miguel Ojeda wrote:> On Thu, Dec 4, 2025 at 12:11?PM Peter Zijlstra <peterz at infradead.org> wrote: > > > > Right. Earlier I also proposed using libclang to parse the C header and > > inject that. This might be a little simpler, in that.. > > Yeah, that would be closer to the `bindgen` route in that `libclang` > gets already involved. > > > ... if you build rustc against libclang they are necessarily from the > > same LLVM build. > > So currently there are 3 "LLVMs" that get involved: > > - The one Clang uses (in LLVM=1 builds).Well, being on Debian, I'm more likely to be using LLVM=-22 (or whatever actual version is required, 22 just being the latest shipped by Debian at this point in time).> - The one `rustc` uses (the LLVM backend). > - The one `bindgen` uses (via libclang).These are not necessarily the same? That is, is not bindgen part of the rustc project and so would be built against the same LLVM?> If that is all done within `rustc` (so no `bindgen`), then there may > still be `rustc` vs. Clang mismatches, which are harder to resolve in > the Rust side at least (it is easier to pick another Clang version to > match). > > For those using builds from distros, that shouldn't be a problem. > Others using external `rustc` builds, e.g. from `rustup` (e.g. for > testing different Rust versions) it would be harder.Make rust part of LLVM and get them all built and distributed together... such that LLVM=-23 will get me a coherent set of tools. /me runs like crazeh ;-)> There is also the question about GCC. A deeper integration into > `rustc` would ideally need to have a way (perhaps depending on the > backend picked?) to support GCC builds properly (to read the header > and flags as expected, as you mention).Right, so the backend that spits out C could obviously just pass through any C headers. But otherwise, inlining C headers (and inline functions) would be something that is independent of the C files. At the end of the day all that really matters is the architecture C ABI. That is, if rustc inlines a C function from a header, it doesn't matter it used libclang to do so, even if the C files are then compiled with GCC.> And finally there is the question of what GCC Rust would do in such a > case. Things have substantially changed on the GCC Rust in the last > years, and they are now closer to build the kernel, thus I think their > side of things is getting important to consider too. > > Cc'ing Emilio (`bindgen`), Antoni (GCC backend) and Arthur (GCC Rust) > so that they are in the loop -- context at:Right, so clearly GCC has the capability to parse C headers :-) So I would imagine their Rust front-end would be able to hand off C headers and get back IR much like LLVM based projects can using libclang.