-
Notifications
You must be signed in to change notification settings - Fork 14k
Enforce the compiler-builtins partitioning scheme #135395
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion
5
tests/run-make/compiler-builtins/rmake.rs → ...n-make/compiler-builtins-linkage/rmake.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [package] | ||
| name = "scratch" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| path = "lib.rs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #![no_std] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //! The compiler_builtins library is special. It exists to export a number of intrinsics which may | ||
| //! also be provided by libgcc or compiler-rt, and when an intrinsic is provided by another | ||
| //! library, we want that definition to override the one in compiler_builtins because we expect | ||
| //! that those implementations are more optimized than compiler_builtins. To make sure that an | ||
| //! attempt to override a compiler_builtins intrinsic does not result in a multiple definitions | ||
| //! linker error, the compiler has special CGU partitioning logic for compiler_builtins that | ||
| //! ensures every intrinsic gets its own CGU. | ||
| //! | ||
| //! This test is slightly overfit to the current compiler_builtins CGU naming strategy; it doesn't | ||
| //! distinguish between "multiple intrinsics are in one object file!" which would be very bad, and | ||
| //! "This object file has an intrinsic and also some of its helper functions!" which would be okay. | ||
| //! | ||
| //! This test ensures that the compiler_builtins rlib has only one intrinsic in each object file. | ||
|
|
||
| // wasm and nvptx targets don't produce rlib files that object can parse. | ||
| //@ ignore-wasm | ||
| //@ ignore-nvptx64 | ||
|
|
||
| #![deny(warnings)] | ||
|
|
||
| use std::str; | ||
|
|
||
| use run_make_support::object::read::Object; | ||
| use run_make_support::object::read::archive::ArchiveFile; | ||
| use run_make_support::object::{ObjectSymbol, SymbolKind}; | ||
| use run_make_support::rfs::{read, read_dir}; | ||
| use run_make_support::{cargo, object, path, target}; | ||
|
|
||
| fn main() { | ||
| println!("Testing compiler_builtins CGU partitioning for {}", target()); | ||
|
|
||
| // CGU partitioning has some special cases for codegen-units=1, so we also test 2 CGUs. | ||
| for cgus in [1, 2] { | ||
| for profile in ["debug", "release"] { | ||
| run_test(profile, cgus); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn run_test(profile: &str, cgus: usize) { | ||
| println!("Testing with profile {profile} and -Ccodegen-units={cgus}"); | ||
|
|
||
| let target_dir = path("target"); | ||
|
|
||
| let mut cmd = cargo(); | ||
| cmd.args(&[ | ||
| "build", | ||
| "--manifest-path", | ||
| "Cargo.toml", | ||
| "-Zbuild-std=core", | ||
| "--target", | ||
| &target(), | ||
| ]) | ||
| .env("RUSTFLAGS", &format!("-Ccodegen-units={cgus}")) | ||
| .env("CARGO_TARGET_DIR", &target_dir) | ||
| .env("RUSTC_BOOTSTRAP", "1") | ||
| // Visual Studio 2022 requires that the LIB env var be set so it can | ||
| // find the Windows SDK. | ||
| .env("LIB", std::env::var("LIB").unwrap_or_default()); | ||
| if profile == "release" { | ||
| cmd.arg("--release"); | ||
| } | ||
| cmd.run(); | ||
|
|
||
| let rlibs_path = target_dir.join(target()).join(profile).join("deps"); | ||
| let compiler_builtins_rlib = read_dir(rlibs_path) | ||
| .find_map(|e| { | ||
| let path = e.unwrap().path(); | ||
| let file_name = path.file_name().unwrap().to_str().unwrap(); | ||
| if file_name.starts_with("libcompiler_builtins") && file_name.ends_with(".rlib") { | ||
| Some(path) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| // rlib files are archives, where the archive members are our CGUs, and we also have one called | ||
| // lib.rmeta which is the encoded metadata. Each of the CGUs is an object file. | ||
| let data = read(compiler_builtins_rlib); | ||
|
|
||
| let archive = ArchiveFile::parse(&*data).unwrap(); | ||
| for member in archive.members() { | ||
| let member = member.unwrap(); | ||
| if member.name() == b"lib.rmeta" { | ||
| continue; | ||
| } | ||
| let data = member.data(&*data).unwrap(); | ||
| let object = object::File::parse(&*data).unwrap(); | ||
|
|
||
| let mut global_text_symbols = 0; | ||
| println!("Inspecting object {}", str::from_utf8(&member.name()).unwrap()); | ||
| for symbol in object | ||
| .symbols() | ||
| .filter(|symbol| matches!(symbol.kind(), SymbolKind::Text)) | ||
| .filter(|symbol| symbol.is_definition() && symbol.is_global()) | ||
| { | ||
| println!("symbol: {:?}", symbol.name().unwrap()); | ||
| global_text_symbols += 1; | ||
| } | ||
| // Assert that this object/CGU does not define multiple global text symbols. | ||
| // We permit the 0 case because some CGUs may only be assigned a static. | ||
| assert!(global_text_symbols <= 1); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe filter out all symbols starting with
_ZNor_Ras those are necessarily helpers?