-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce the compiler-builtins partitioning scheme #135395
base: master
Are you sure you want to change the base?
Conversation
…oboet Add #[inline] to copy_from_slice I'm doing cooked things to CGU partitioning for compiler-builtins (rust-lang#135395) and this was the lone symbol in my compiler-builtins rlib that wasn't an intrinsic. Adding `#[inline]` makes it go away. Perf report indicates a marginal but chaotic effect on compile time, marginal improvement in codegen. As expected.
7cf1a94
to
b371e7f
Compare
b371e7f
to
50dbf9c
Compare
r? bjorn3 |
This PR modifies cc @jieyouxu Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
// See https://github.com/rust-lang/rust/issues/73135 | ||
if tcx.is_compiler_builtins(rustc_span::def_id::LOCAL_CRATE) { | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to make inlining inside the crate more likely without causing MIR for all functions in compiler-builtins to get encoded in the crate metadata?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you're pointing out here is that these functions are not reachable as MIR, so we don't need to encode MIR for them. The problem as I see it is that our notion of reachable uses this worklist/visited algorithm that tracks items in a path-independent way:
rust/compiler/rustc_passes/src/reachable.rs
Lines 168 to 173 in 2ae9916
while let Some(search_item) = self.worklist.pop() { | |
if !scanned.insert(search_item) { | |
continue; | |
} | |
self.propagate_node(&self.tcx.hir_node_by_def_id(search_item), search_item); |
Also we already have an issue for the inverse inefficiency, emitting object code when we only need MIR: #119214
I put a hack in this place specifically because the compiler is designed around this function either true or false for whatever reason, past the first few checks. I'm not aware of anywhere else we could make a small localized change to get the behavior we want.
The only other place I could think of putting a hack is MonoItem::instantiation_mode
, but that doesn't work because then we get linker errors because instantiation mode needs to agree with exported_symbols
, and those disagree because because exported_symbols
is based on reachable_set
. I really think the inaccuracy of the reachable_set
analysis is the root problem here, and it's net better to implement this in a non-invasive way that will be fixed automatically if reachable_set
gets improved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if I back up to my merge-base, x build library
, then ar x
the stage1-std libcompiler_builtins.rlib and run du -sch *
I get:
808K lib.rmeta
5.7M total
Then with my changes:
968K lib.rmeta
4.1M total
So even though it's not perfect, this PR is still a net win.
@bors r+ rollup=never |
…jorn3 Enforce the compiler-builtins partitioning scheme compiler-builtins needs every intrinsic in its own CGU. Currently, the compiler-builtins crate puts every intrinsic in its own inline module then `library/Cargo.toml` uses a profile override so that when we build the sysroot, compiler-builtins is built with more `codegen-units` than we have intrinsics, and partitioning never merges two intrinsics together. This approach does not work with `-Zbuild-std` because the profile override gets ignored. And it's kludgey anyway, our own standard library should not be fighting with our own compiler in an attempt to override its behavior. We should change the compiler's behavior to do the right thing in the first place. So that's what this PR does. There's some light refactoring of the CGU partitioning code, then in 3 places I've added a check for `is_compiler_builtins`: * There's a special case now in `cross_crate_inlinable`; every function in compiler-builtins that is not `#[no_mangle]` is made cross-crate-inlinable, which ensures we do not run into problems inlining helpers into intrinsics such as rust-lang#73135 * When building compiler-builtins, the name of the CGU that a MonoItem is given is just the MonoItem's symbol name. This puts every GloballyShared item in its own CGU. * Then when building compiler-builtins, we skip CGU merging. That should ensure that we have one object file per intrinsic, and if optimizations are enabled, there should be no extra extra CGUs full of helper functions (which is what currently happens in the precompiled standard library we distribute, my nightly libcompiler_builtins.rlib for x86_64-unknown-linux-gnu has 174 CGUs and with this PR we have 150).
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
50dbf9c
to
b3841ed
Compare
Test failed because it was checking for exactly 1 text section in each object file, but on some platforms, we have object files that have no text sections because they are just a static. I made the test permit 0 or 1 text sections. @bors r=bjorn3 |
Thanks for updating the test @saethlin ! Unfortunately, the SGX platform with its LVI mitigations still causes issues:
|
@raoulstrackx I advise you try filing an issue. If you are complaining that the tests do not pass in a configuration that we do not test, then that configuration either needs to be added to the test suite, the test needs to be ignored on that target, or the test needs to be improved. I am not going to hold back this change because the tests do not pass on a configuration that we do not test. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@rustbot ping rfl It looks like RFL has been relying on compiler-builtins being compiled into a single object file, and with this change we will always compile compiler-builtins to multiple object files. I can't tell from the diagnostic: #135395 (comment) whether this needs a compiler change or not. Please advise? |
@rustbot ping rust-for-linux Silent error on unrecognized team name? (I don't know the answer to the question unfortunately) |
(It's been forever since I looked at this code, so hopefully someone will correct me if I got that wrong) I think that's correct that we expect a single However, we use our own compiler-builtins: https://github.com/torvalds/linux/blob/master/rust/compiler_builtins.rs |
RFL uses |
Which is marked with |
hm. based on the PR's motivation, it seems likely the way that RFL is doing this is increasing bloat for the kernel, making the engineering choices that RFL has made contrary to what RFL wants? do I understand everything correctly? |
It is entirely unclear to me what effect this PR has on RFL's compiler-builtins crate in practice, because I haven't used this PR to compile it and I certainly don't know what linkage options or other processing they are using with that object file. |
This PR is forcing multiple codegen units for compiler-builtins, but RFL needs all crates to use a single codegen unit each. |
Could these new settings be ignored if To be clear, the policy is that the RFL job can be disabled if it is getting in the way of things. Much nicer if we could figure out a workaround first though. |
I'd like to actually understand why RFL wants exactly the opposite CGU partitioning of compiler-builtins from every other user of Rust before we continue adding hacks for RFL. The diff in this PR is already kept deliberately small to minimize its impact on the compiler, both your suggestions would approximately double its size. |
If it helps, we currently do something like: RUSTC_BOOTSTRAP=1 rustc --edition=2021 -Cpanic=abort -Cembed-bitcode=n -Clto=n -Cforce-unwind-tables=n -Ccodegen-units=1 -Csymbol-mangling-version=v0 -Crelocation-model=static -Zfunction-sections=n --target=aarch64-unknown-none -Ctarget-feature="-neon" -Cforce-unwind-tables=n -Copt-level=2 -Cdebug-assertions=n -Coverflow-checks=y -Cforce-frame-pointers=y -Cdebuginfo=1 --cfg no_fp_fmt_parse --emit=obj=core.o --emit=metadata=libcore.rmeta --crate-type rlib -L. --crate-name core `rustc --print sysroot`/lib/rustlib/src/rust/library/core/src/lib.rs --sysroot=/dev/null
llvm-objcopy --redefine-sym __addsf3=__rust__addsf3 --redefine-sym __eqsf2=__rust__eqsf2 --redefine-sym __extendsfdf2=__rust__extendsfdf2 --redefine-sym __gesf2=__rust__gesf2 --redefine-sym __lesf2=__rust__lesf2 --redefine-sym __ltsf2=__rust__ltsf2 --redefine-sym __mulsf3=__rust__mulsf3 --redefine-sym __nesf2=__rust__nesf2 --redefine-sym __truncdfsf2=__rust__truncdfsf2 --redefine-sym __unordsf2=__rust__unordsf2 --redefine-sym __adddf3=__rust__adddf3 --redefine-sym __eqdf2=__rust__eqdf2 --redefine-sym __ledf2=__rust__ledf2 --redefine-sym __ltdf2=__rust__ltdf2 --redefine-sym __muldf3=__rust__muldf3 --redefine-sym __unorddf2=__rust__unorddf2 --redefine-sym __muloti4=__rust__muloti4 --redefine-sym __multi3=__rust__multi3 --redefine-sym __udivmodti4=__rust__udivmodti4 --redefine-sym __udivti3=__rust__udivti3 --redefine-sym __umodti3=__rust__umodti3 --redefine-sym __ashrti3=__rust__ashrti3 --redefine-sym __ashlti3=__rust__ashlti3 --redefine-sym __lshrti3=__rust__lshrti3 core.o
RUSTC_BOOTSTRAP=1 rustc --edition=2021 -Cpanic=abort -Cembed-bitcode=n -Clto=n -Cforce-unwind-tables=n -Ccodegen-units=1 -Csymbol-mangling-version=v0 -Crelocation-model=static -Zfunction-sections=n --target=aarch64-unknown-none -Ctarget-feature="-neon" -Cforce-unwind-tables=n -Copt-level=2 -Cdebug-assertions=n -Coverflow-checks=y -Cforce-frame-pointers=y -Cdebuginfo=1 --emit=obj=compiler_builtins.o --emit=metadata=libcompiler_builtins.rmeta --crate-type rlib -L. --crate-name compiler_builtins compiler_builtins.rs --sysroot=/dev/null
llvm-objcopy -w -W '__*' compiler_builtins.o Cc @nbdd0121
Something like that sounds nice, but if there is something that we should be doing to improve our builds, happy to adjust.
Yeah, if the breakage is unintentional and the PR not urgent, then it is best to figure out first. |
It's not clear to me why above, you need everything in one object file as opposed to operating on an archive file (if llvm-objcopy can do that) or having a loop over all the object files. |
RfL build system doesn't handle rlibs, it handles object files only. So we require CGU=1 and emit object files. The reason that compiler_builtins want as many CGUs as possible is that if an intrinsic is already present and linked in libgcc then we use it instead of linking in our compiler_builtins. I believe fundamental there's nothing in compiler_builtins that require it to have as many CGUs as possible. So in essence I see it as being a hack to always ignore CGU settings and always emit as many CGUs as possible and I disagree the statement that restoring the correct meaning of the options is a hack. RfL doesn't have libgcc linked so it has no reason to have multiple CGUs. Why dealing with archive files or multiple .o files if one suffices? I believe this is also not needed for embedded use cases. |
I don't get it. If anything a single CGU is a size reduction. |
Why does RFL need to use the |
Then it doesn't compile, as Rust injects compiler_builtins as a dependency and now self-dependency happens: rust/compiler/rustc_builtin_macros/src/standard_library_imports.rs Lines 22 to 32 in 6067b36
This patch would make RfL compile, although now we need diff --git a/rust/Makefile b/rust/Makefile
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -438,6 +438,7 @@ $(obj)/core.o: scripts/target.json
endif
$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
+$(obj)/compiler_builtins.o: private rustc_target_flags = --extern core
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
+$(call if_changed_rule,rustc_library)
diff --git a/rust/compiler_builtins.rs b/rust/compiler_builtins.rs
--- a/rust/compiler_builtins.rs
+++ b/rust/compiler_builtins.rs
@@ -19,11 +19,13 @@
//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
//! [`compiler-rt`]: https://compiler-rt.llvm.org/
-#![allow(internal_features)]
-#![feature(compiler_builtins)]
-#![compiler_builtins]
+#![feature(no_core)]
#![no_builtins]
-#![no_std]
+// We need `no_core` to avoid pulling in `compiler_builtins` (which creates a self-recursion)
+// since all crates get injected with `core` and `compiler_builtins` dependencies.
+#![no_core]
+
+use core::{concat, panic, stringify};
macro_rules! define_panicking_intrinsics(
($reason: tt, { $($ident: ident, )* }) => { |
Wouldn't that need to be applied to literally every crate rather than just compiler-builtins? |
By the way a while back I looked into getting RFL to link rlibs rather than raw object files (to among other things make incr comp possible and to increase parallelism during compilation). Unfortunately llvm-ar doesn't accept adding the contents of a regular archive to a thin archive (binutils ar does allow this by having the thin archive reference individual members of the regular archive, but neither llvm-ar nor lld support this) and Linux builds a thin archive for each directory and there may be multiple crates in a single directory which thus need to be merged into a single thin archive. |
No, there's still a crate named |
That seems like a bug. A user defined compiler-builtins crate shouldn't be able to take precedence. Because of this you can do |
Well, I don't see why the user shouldn't be allowed to override compiler_builtins if they wish to do so. |
compiler-builtins is an internal implementation detail. We don't provide any guarantees about which functions it needs to define for linking to succeed. |
Add #[inline] to copy_from_slice I'm doing cooked things to CGU partitioning for compiler-builtins (rust-lang/rust#135395) and this was the lone symbol in my compiler-builtins rlib that wasn't an intrinsic. Adding `#[inline]` makes it go away. Perf report indicates a marginal but chaotic effect on compile time, marginal improvement in codegen. As expected.
@saethlin sorry for not being more elaborate. I wanted to ask to ignore this test for the |
compiler-builtins needs every intrinsic in its own CGU. Currently, the compiler-builtins crate puts every intrinsic in its own inline module then
library/Cargo.toml
uses a profile override so that when we build the sysroot, compiler-builtins is built with morecodegen-units
than we have intrinsics, and partitioning never merges two intrinsics together. This approach does not work with-Zbuild-std
because the profile override gets ignored. And it's kludgey anyway, our own standard library should not be fighting with our own compiler in an attempt to override its behavior. We should change the compiler's behavior to do the right thing in the first place.So that's what this PR does. There's some light refactoring of the CGU partitioning code, then in 3 places I've added a check for
is_compiler_builtins
:cross_crate_inlinable
; every function in compiler-builtins that is not#[no_mangle]
is made cross-crate-inlinable, which ensures we do not run into problems inlining helpers into intrinsics such as compiler-builtins: Int trait functions are not inlined on wasm #73135That should ensure that we have one object file per intrinsic, and if optimizations are enabled, there should be no extra extra CGUs full of helper functions (which is what currently happens in the precompiled standard library we distribute, my nightly libcompiler_builtins.rlib for x86_64-unknown-linux-gnu has 174 CGUs and with this PR we have 150).