From 4495cbaa932518d134ea6bf14fb8a6fc218b1fbd Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Thu, 25 Aug 2022 19:43:48 +0100 Subject: [PATCH 1/4] Remove depdencies of `profiler_builtins` --- Cargo.lock | 2 -- compiler/rustc_metadata/src/creader.rs | 9 +++------ library/profiler_builtins/Cargo.toml | 4 ---- library/profiler_builtins/src/lib.rs | 6 ++---- 4 files changed, 5 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e1c904e337b9..b71f9deb7486b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2741,8 +2741,6 @@ name = "profiler_builtins" version = "0.0.0" dependencies = [ "cc", - "compiler_builtins", - "core", ] [[package]] diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index cfcceecbef40e..9e2f86d25980b 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -2,7 +2,7 @@ use crate::errors::{ ConflictingGlobalAlloc, CrateNotPanicRuntime, GlobalAllocRequired, NoMultipleGlobalAlloc, - NoPanicStrategy, NoTransitiveNeedsDep, NotProfilerRuntime, ProfilerBuiltinsNeedsCore, + NoPanicStrategy, NoTransitiveNeedsDep, NotProfilerRuntime, }; use crate::locator::{CrateError, CrateLocator, CratePaths}; use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob}; @@ -759,7 +759,7 @@ impl<'a> CrateLoader<'a> { self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime()); } - fn inject_profiler_runtime(&mut self, krate: &ast::Crate) { + fn inject_profiler_runtime(&mut self) { if self.sess.opts.unstable_opts.no_profiler_runtime || !(self.sess.instrument_coverage() || self.sess.opts.unstable_opts.profile @@ -771,9 +771,6 @@ impl<'a> CrateLoader<'a> { info!("loading profiler"); let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime); - if name == sym::profiler_builtins && self.sess.contains_name(&krate.attrs, sym::no_core) { - self.sess.emit_err(ProfilerBuiltinsNeedsCore); - } let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; let data = self.cstore.get_crate_data(cnum); @@ -927,7 +924,7 @@ impl<'a> CrateLoader<'a> { } pub fn postprocess(&mut self, krate: &ast::Crate) { - self.inject_profiler_runtime(krate); + self.inject_profiler_runtime(); self.inject_allocator_crate(krate); self.inject_panic_runtime(krate); diff --git a/library/profiler_builtins/Cargo.toml b/library/profiler_builtins/Cargo.toml index 3371dfa124253..a396b9c988e64 100644 --- a/library/profiler_builtins/Cargo.toml +++ b/library/profiler_builtins/Cargo.toml @@ -8,9 +8,5 @@ test = false bench = false doc = false -[dependencies] -core = { path = "../core" } -compiler_builtins = { version = "0.1.0", features = ['rustc-dep-of-std'] } - [build-dependencies] cc = "1.0.69" diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs index 0c83bcee06ff4..3f5c17fb76db9 100644 --- a/library/profiler_builtins/src/lib.rs +++ b/library/profiler_builtins/src/lib.rs @@ -1,10 +1,8 @@ -#![no_std] -#![feature(profiler_runtime)] +#![feature(no_core, profiler_runtime, staged_api)] +#![no_core] #![profiler_runtime] #![unstable( feature = "profiler_runtime_lib", reason = "internal implementation detail of rustc right now", issue = "none" )] -#![allow(unused_features)] -#![feature(staged_api)] From e74143ef8087aeeb13efe8bafb3575d6383c0c8d Mon Sep 17 00:00:00 2001 From: bogon-right Date: Sat, 3 Sep 2022 21:24:14 +0100 Subject: [PATCH 2/4] Add LLVM compiler-rt/profile in bootstrap --- compiler/rustc_codegen_ssa/src/back/link.rs | 70 ++++++++--- src/bootstrap/builder.rs | 1 + src/bootstrap/compile.rs | 26 ++++ src/bootstrap/native.rs | 125 ++++++++++++++++++++ 4 files changed, 205 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index a0b5e3b6daf44..511430ffade5a 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1144,23 +1144,46 @@ fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut d } } -fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) { - fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf { - let session_tlib = - filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple()); - let path = session_tlib.join(filename); - if path.exists() { - return session_tlib; - } else { - let default_sysroot = filesearch::get_or_default_sysroot(); - let default_tlib = filesearch::make_target_lib_path( - &default_sysroot, - sess.opts.target_triple.triple(), - ); - return default_tlib; - } +fn add_profiler_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) { + let needs_runtime = match crate_type { + CrateType::Executable => true, + CrateType::Dylib + | CrateType::Cdylib + | CrateType::ProcMacro + | CrateType::Rlib + | CrateType::Staticlib => false, + }; + + if !needs_runtime { + return; + } + + if !sess.opts.unstable_opts.no_profiler_runtime + && (sess.instrument_coverage() + || sess.opts.unstable_opts.profile + || sess.opts.cg.profile_generate.enabled()) + // If user doesn't provide custom profiler runtime, link default llvm profiler. + && sess.opts.unstable_opts.profiler_runtime == "profiler_builtins" + { + link_profiler_runtime(sess, linker); + } +} + +fn find_compiler_rt(sess: &Session, filename: &str) -> PathBuf { + let session_tlib = + filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple()); + let path = session_tlib.join(filename); + if path.exists() { + return session_tlib; + } else { + let default_sysroot = filesearch::get_or_default_sysroot(); + let default_tlib = + filesearch::make_target_lib_path(&default_sysroot, sess.opts.target_triple.triple()); + return default_tlib; } +} +fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) { let channel = option_env!("CFG_RELEASE_CHANNEL") .map(|channel| format!("-{}", channel)) .unwrap_or_default(); @@ -1171,17 +1194,27 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) { // rpath to the library as well (the rpath should be absolute, see // PR #41352 for details). let filename = format!("rustc{}_rt.{}", channel, name); - let path = find_sanitizer_runtime(&sess, &filename); + let path = find_compiler_rt(&sess, &filename); let rpath = path.to_str().expect("non-utf8 component in path"); linker.args(&["-Wl,-rpath", "-Xlinker", rpath]); linker.link_dylib(&filename, false, true); } else { let filename = format!("librustc{}_rt.{}.a", channel, name); - let path = find_sanitizer_runtime(&sess, &filename).join(&filename); + let path = find_compiler_rt(&sess, &filename).join(&filename); linker.link_whole_rlib(&path); } } +fn link_profiler_runtime(sess: &Session, linker: &mut dyn Linker) { + let channel = option_env!("CFG_RELEASE_CHANNEL") + .map(|channel| format!("-{}", channel)) + .unwrap_or_default(); + + let filename = format!("librustc{}_rt.profile.a", channel); + let path = find_compiler_rt(&sess, &filename).join(&filename); + linker.link_whole_rlib(&path); +} + /// Returns a boolean indicating whether the specified crate should be ignored /// during LTO. /// @@ -1997,6 +2030,9 @@ fn linker_with_args<'a>( // Sanitizer libraries. add_sanitizer_libraries(sess, crate_type, cmd); + // Profiler libraries. + add_profiler_libraries(sess, crate_type, cmd); + // Object code from the current crate. // Take careful note of the ordering of the arguments we pass to the linker // here. Linkers will assume that things on the left depend on things to the diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 8b144f1463579..199987852f098 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -608,6 +608,7 @@ impl<'a> Builder<'a> { tool::CargoClippy, native::Llvm, native::Sanitizers, + native::Profiler, tool::Rustfmt, tool::Miri, tool::CargoMiri, diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 9cc119d3301ac..d1eab1645ccc3 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -205,6 +205,14 @@ fn copy_third_party_objects( ); } + if builder.config.profiler_enabled(target) && compiler.stage != 0 { + // The profiler are only copied in stage1 or above, + // to avoid creating dependency on LLVM. + if let Some(profiler_path) = copy_profiler(builder, &compiler, target) { + target_deps.push((profiler_path, DependencyType::Target)); + } + } + if target == "x86_64-fortanix-unknown-sgx" || target.contains("pc-windows-gnullvm") || builder.config.llvm_libunwind(target) == LlvmLibunwind::InTree @@ -444,6 +452,24 @@ impl Step for StdLink { } } +/// Copies profiler runtime library into target libdir. +fn copy_profiler( + builder: &Builder<'_>, + compiler: &Compiler, + target: TargetSelection, +) -> Option { + let runtime: native::ProfilerRuntime = builder.ensure(native::Profiler { target })?; + + if builder.config.dry_run { + return None; + } + + let destination = builder.sysroot_libdir(*compiler, target).join(&runtime.name); + builder.copy(&runtime.path, &destination); + + Some(destination) +} + /// Copies sanitizer runtime libraries into target libdir. fn copy_sanitizers( builder: &Builder<'_>, diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 2f856c2761cf9..2faf8a5a99a8f 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -1182,6 +1182,131 @@ fn supported_sanitizers( } } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct Profiler { + pub target: TargetSelection, +} + +impl Step for Profiler { + type Output = Option; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.alias("profiler") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(Profiler { target: run.target }); + } + + /// Builds sanitizer runtime libraries. + fn run(self, builder: &Builder<'_>) -> Self::Output { + let compiler_rt_dir = builder.src.join("src/llvm-project/compiler-rt"); + if !compiler_rt_dir.exists() { + return None; + } + + let out_dir = builder.native_dir(self.target).join("profiler"); + let runtime = supported_profilers(&out_dir, self.target, &builder.config.channel)?; + + let llvm_config = builder.ensure(Llvm { target: builder.config.build }); + if builder.config.dry_run { + return Some(runtime); + } + + let stamp = out_dir.join("profiler-finished-building"); + let stamp = HashStamp::new(stamp, builder.in_tree_llvm_info.sha()); + + if stamp.is_done() { + if stamp.hash.is_none() { + builder.info(&format!( + "Rebuild profiler by removing the file `{}`", + stamp.path.display() + )); + } + return Some(runtime); + } + + builder.info(&format!("Building profiler for {}", self.target)); + t!(stamp.remove()); + let _time = util::timeit(&builder); + + let mut cfg = cmake::Config::new(&compiler_rt_dir); + cfg.profile("Release"); + cfg.define("CMAKE_C_COMPILER_TARGET", self.target.triple); + cfg.define("COMPILER_RT_BUILD_BUILTINS", "OFF"); + cfg.define("COMPILER_RT_BUILD_CRT", "OFF"); + cfg.define("COMPILER_RT_BUILD_LIBFUZZER", "OFF"); + cfg.define("COMPILER_RT_BUILD_PROFILE", "ON"); + cfg.define("COMPILER_RT_BUILD_SANITIZERS", "OFF"); + cfg.define("COMPILER_RT_BUILD_XRAY", "OFF"); + cfg.define("COMPILER_RT_DEFAULT_TARGET_ONLY", "ON"); + cfg.define("COMPILER_RT_USE_LIBCXX", "OFF"); + cfg.define("LLVM_CONFIG_PATH", &llvm_config); + + configure_cmake(builder, self.target, &mut cfg, true, LdFlags::default()); + + t!(fs::create_dir_all(&out_dir)); + cfg.out_dir(out_dir); + cfg.build_target(&runtime.cmake_target); + cfg.build(); + t!(stamp.write()); + + Some(runtime) + } +} + +#[derive(Clone, Debug)] +pub struct ProfilerRuntime { + /// CMake target used to build the runtime. + pub cmake_target: String, + /// Path to the built runtime library. + pub path: PathBuf, + /// Library filename that will be used rustc. + pub name: String, +} + +/// Returns sanitizers available on a given target. +fn supported_profilers( + out_dir: &Path, + target: TargetSelection, + channel: &str, +) -> Option { + // LLVM's compile-rt has a special taste on target naming. :D + // + // compiler runtime's name seems to be a chaos: + // + let darwin_libs = || -> Option { + Some(ProfilerRuntime { + cmake_target: format!("clang_rt.profile_osx"), + path: out_dir.join(&format!("build/lib/darwin/libclang_rt.profile_osx.a")), + name: format!("librustc-{}_rt.profile.a", channel), + }) + }; + + let common_libs = |os: &str, arch: &str| -> Option { + Some(ProfilerRuntime { + cmake_target: format!("clang_rt.profile-{}", arch), + path: out_dir.join(&format!("build/lib/{}/libclang_rt.profile-{}.a", os, arch)), + name: format!("librustc-{}_rt.profile.a", channel), + }) + }; + + match &*target.triple { + "aarch64-apple-darwin" => darwin_libs(), + "aarch64-fuchsia" => common_libs("fuchsia", "aarch64"), + "aarch64-unknown-linux-gnu" => common_libs("linux", "aarch64"), + "x86_64-apple-darwin" => darwin_libs(), + "x86_64-fuchsia" => common_libs("fuchsia", "x86_64"), + "x86_64-unknown-freebsd" => common_libs("freebsd", "x86_64"), + "x86_64-unknown-netbsd" => common_libs("netbsd", "x86_64"), + "x86_64-unknown-illumos" => common_libs("illumos", "x86_64"), + "x86_64-pc-solaris" => common_libs("solaris", "x86_64"), + "x86_64-unknown-linux-gnu" => common_libs("linux", "x86_64"), + "x86_64-unknown-linux-musl" => common_libs("linux", "x86_64"), + _ => None, + } +} + struct HashStamp { path: PathBuf, hash: Option>, From 36068ca0797944a6ffd613c898f174090ac1c072 Mon Sep 17 00:00:00 2001 From: bogon-right Date: Sat, 3 Sep 2022 21:45:45 +0100 Subject: [PATCH 3/4] Remove `profiler_builtins` crate --- Cargo.lock | 8 -- compiler/rustc_feature/src/builtin_attrs.rs | 2 +- compiler/rustc_metadata/src/creader.rs | 1 + compiler/rustc_metadata/src/rmeta/decoder.rs | 1 - compiler/rustc_session/src/options.rs | 2 +- library/profiler_builtins/Cargo.toml | 12 --- library/profiler_builtins/build.rs | 89 ------------------- library/profiler_builtins/src/lib.rs | 8 -- library/std/Cargo.toml | 3 +- src/bootstrap/builder.rs | 4 +- src/bootstrap/check.rs | 4 +- src/bootstrap/compile.rs | 4 +- src/bootstrap/doc.rs | 6 +- src/bootstrap/lib.rs | 6 +- .../library-features/profiler-runtime-lib.md | 5 -- src/test/rustdoc-ui/z-help.stdout | 4 +- .../feature-gate-profiler-runtime.stderr | 2 +- .../crates/ide-db/src/generated/lints.rs | 9 -- 18 files changed, 16 insertions(+), 154 deletions(-) delete mode 100644 library/profiler_builtins/Cargo.toml delete mode 100644 library/profiler_builtins/build.rs delete mode 100644 library/profiler_builtins/src/lib.rs delete mode 100644 src/doc/unstable-book/src/library-features/profiler-runtime-lib.md diff --git a/Cargo.lock b/Cargo.lock index b71f9deb7486b..be53c90197b72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2736,13 +2736,6 @@ dependencies = [ "std", ] -[[package]] -name = "profiler_builtins" -version = "0.0.0" -dependencies = [ - "cc", -] - [[package]] name = "psm" version = "0.1.16" @@ -4682,7 +4675,6 @@ dependencies = [ "object 0.26.2", "panic_abort", "panic_unwind", - "profiler_builtins", "rand 0.7.3", "rustc-demangle", "std_detect", diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index afa0f6fcb80f5..64facb8f53583 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -578,7 +578,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ ), gated!( profiler_runtime, Normal, template!(Word), WarnFollowing, - "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \ + "the `#[profiler_runtime]` attribute is used to identify the crate \ which contains the profiler runtime and will never be stable", ), diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 9e2f86d25980b..4a20cdf083618 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -764,6 +764,7 @@ impl<'a> CrateLoader<'a> { || !(self.sess.instrument_coverage() || self.sess.opts.unstable_opts.profile || self.sess.opts.cg.profile_generate.enabled()) + || self.sess.opts.unstable_opts.profiler_runtime == "profiler_builtins" { return; } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 830417eea1a0a..bf6071d698ea1 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1509,7 +1509,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { "proc_macro", "panic_abort", "panic_unwind", - "profiler_builtins", "rtstartup", "rustc-std-workspace-core", "rustc-std-workspace-alloc", diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 3f234a47a3d86..713480d4f8e8c 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1423,7 +1423,7 @@ options! { no_parallel_llvm: bool = (false, parse_no_flag, [UNTRACKED], "run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"), no_profiler_runtime: bool = (false, parse_no_flag, [TRACKED], - "prevent automatic injection of the profiler_builtins crate"), + "prevent automatic injection of the profiler runtime"), no_unique_section_names: bool = (false, parse_bool, [TRACKED], "do not use unique names for text and data sections when -Z function-sections is used"), normalize_docs: bool = (false, parse_bool, [TRACKED], diff --git a/library/profiler_builtins/Cargo.toml b/library/profiler_builtins/Cargo.toml deleted file mode 100644 index a396b9c988e64..0000000000000 --- a/library/profiler_builtins/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "profiler_builtins" -version = "0.0.0" -edition = "2021" - -[lib] -test = false -bench = false -doc = false - -[build-dependencies] -cc = "1.0.69" diff --git a/library/profiler_builtins/build.rs b/library/profiler_builtins/build.rs deleted file mode 100644 index 1b1f11798d74d..0000000000000 --- a/library/profiler_builtins/build.rs +++ /dev/null @@ -1,89 +0,0 @@ -//! Compiles the profiler part of the `compiler-rt` library. -//! -//! See the build.rs for libcompiler_builtins crate for details. - -use std::env; -use std::path::Path; - -fn main() { - let target = env::var("TARGET").expect("TARGET was not set"); - let cfg = &mut cc::Build::new(); - - // FIXME: `rerun-if-changed` directives are not currently emitted and the build script - // will not rerun on changes in these source files or headers included into them. - let mut profile_sources = vec![ - "GCDAProfiling.c", - "InstrProfiling.c", - "InstrProfilingBuffer.c", - "InstrProfilingFile.c", - "InstrProfilingMerge.c", - "InstrProfilingMergeFile.c", - "InstrProfilingNameVar.c", - "InstrProfilingPlatformDarwin.c", - "InstrProfilingPlatformFuchsia.c", - "InstrProfilingPlatformLinux.c", - "InstrProfilingPlatformOther.c", - "InstrProfilingPlatformWindows.c", - "InstrProfilingRuntime.cpp", - "InstrProfilingUtil.c", - "InstrProfilingValue.c", - "InstrProfilingVersionVar.c", - "InstrProfilingWriter.c", - // These files were added in LLVM 11. - "InstrProfilingInternal.c", - "InstrProfilingBiasVar.c", - ]; - - if target.contains("msvc") { - // Don't pull in extra libraries on MSVC - cfg.flag("/Zl"); - profile_sources.push("WindowsMMap.c"); - cfg.define("strdup", Some("_strdup")); - cfg.define("open", Some("_open")); - cfg.define("fdopen", Some("_fdopen")); - cfg.define("getpid", Some("_getpid")); - cfg.define("fileno", Some("_fileno")); - } else { - // Turn off various features of gcc and such, mostly copying - // compiler-rt's build system already - cfg.flag("-fno-builtin"); - cfg.flag("-fomit-frame-pointer"); - cfg.define("VISIBILITY_HIDDEN", None); - if !target.contains("windows") { - cfg.flag("-fvisibility=hidden"); - cfg.define("COMPILER_RT_HAS_UNAME", Some("1")); - } else { - profile_sources.push("WindowsMMap.c"); - } - } - - // Assume that the Unixes we are building this for have fnctl() available - if env::var_os("CARGO_CFG_UNIX").is_some() { - cfg.define("COMPILER_RT_HAS_FCNTL_LCK", Some("1")); - } - - // This should be a pretty good heuristic for when to set - // COMPILER_RT_HAS_ATOMICS - if env::var_os("CARGO_CFG_TARGET_HAS_ATOMIC") - .map(|features| features.to_string_lossy().to_lowercase().contains("ptr")) - .unwrap_or(false) - { - cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1")); - } - - // Note that this should exist if we're going to run (otherwise we just - // don't build profiler builtins at all). - let root = Path::new("../../src/llvm-project/compiler-rt"); - - let src_root = root.join("lib").join("profile"); - for src in profile_sources { - let path = src_root.join(src); - if path.exists() { - cfg.file(path); - } - } - - cfg.include(root.join("include")); - cfg.warnings(false); - cfg.compile("profiler-rt"); -} diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs deleted file mode 100644 index 3f5c17fb76db9..0000000000000 --- a/library/profiler_builtins/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![feature(no_core, profiler_runtime, staged_api)] -#![no_core] -#![profiler_runtime] -#![unstable( - feature = "profiler_runtime_lib", - reason = "internal implementation detail of rustc right now", - issue = "none" -)] diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index b8c35ecd34933..c1ddb098a1a14 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -17,7 +17,6 @@ panic_abort = { path = "../panic_abort" } core = { path = "../core" } libc = { version = "0.2.126", default-features = false, features = ['rustc-dep-of-std'] } compiler_builtins = { version = "0.1.73" } -profiler_builtins = { path = "../profiler_builtins", optional = true } unwind = { path = "../unwind" } hashbrown = { version = "0.12", default-features = false, features = ['rustc-dep-of-std'] } std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = ['rustc-dep-of-std'] } @@ -57,7 +56,7 @@ backtrace = [ gimli-symbolize = [] panic-unwind = ["panic_unwind"] -profiler = ["profiler_builtins"] +profiler = [] compiler-builtins-c = ["alloc/compiler-builtins-c"] compiler-builtins-mem = ["alloc/compiler-builtins-mem"] compiler-builtins-no-asm = ["alloc/compiler-builtins-no-asm"] diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 199987852f098..1c73b400a8fa8 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -406,7 +406,7 @@ impl<'a> ShouldRun<'a> { /// `all_krates` should probably be removed at some point. pub fn all_krates(mut self, name: &str) -> Self { let mut set = BTreeSet::new(); - for krate in self.builder.in_tree_crates(name, None) { + for krate in self.builder.in_tree_crates(name) { let path = krate.local_path(self.builder); set.insert(TaskPath { path, kind: Some(self.kind) }); } @@ -419,7 +419,7 @@ impl<'a> ShouldRun<'a> { /// /// `make_run` will be called a single time with all matching command-line paths. pub fn crate_or_deps(self, name: &str) -> Self { - let crates = self.builder.in_tree_crates(name, None); + let crates = self.builder.in_tree_crates(name); self.crates(crates) } diff --git a/src/bootstrap/check.rs b/src/bootstrap/check.rs index 229851238f1d0..b8b0c6a2b5ba9 100644 --- a/src/bootstrap/check.rs +++ b/src/bootstrap/check.rs @@ -152,7 +152,7 @@ impl Step for Std { // Explicitly pass -p for all dependencies krates -- this will force cargo // to also check the tests/benches/examples for these crates, rather // than just the leaf crate. - for krate in builder.in_tree_crates("test", Some(target)) { + for krate in builder.in_tree_crates("test") { cargo.arg("-p").arg(krate.name); } @@ -228,7 +228,7 @@ impl Step for Rustc { // Explicitly pass -p for all compiler krates -- this will force cargo // to also check the tests/benches/examples for these crates, rather // than just the leaf crate. - for krate in builder.in_tree_crates("rustc-main", Some(target)) { + for krate in builder.in_tree_crates("rustc-main") { cargo.arg("-p").arg(krate.name); } diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index d1eab1645ccc3..7bfa20f3cbade 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -327,8 +327,6 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car builder.update_submodule(&Path::new("src").join("llvm-project")); let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt"); - // Note that `libprofiler_builtins/build.rs` also computes this so if - // you're changing something here please also change that. cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root); " compiler-builtins-c" } else { @@ -617,7 +615,7 @@ impl Step for Rustc { const DEFAULT: bool = false; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - let mut crates = run.builder.in_tree_crates("rustc-main", None); + let mut crates = run.builder.in_tree_crates("rustc-main"); for (i, krate) in crates.iter().enumerate() { if krate.name == "rustc-main" { crates.swap_remove(i); diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 7bdd226cb692e..8b7664b797b96 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -709,9 +709,9 @@ impl Step for Rustc { paths.into_iter().map(|p| builder.crate_paths[p]).collect() }; // Find dependencies for top level crates. - let compiler_crates = root_crates.iter().flat_map(|krate| { - builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name) - }); + let compiler_crates = root_crates + .iter() + .flat_map(|krate| builder.in_tree_crates(krate).into_iter().map(|krate| krate.name)); let mut to_open = None; for krate in compiler_crates { diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 7e70e99bb8ca2..da397ddb40c72 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -1368,7 +1368,7 @@ impl Build { /// Returns a Vec of all the dependencies of the given root crate, /// including transitive dependencies and the root itself. Only includes /// "local" crates (those in the local source tree, not from a registry). - fn in_tree_crates(&self, root: &str, target: Option) -> Vec<&Crate> { + fn in_tree_crates(&self, root: &str) -> Vec<&Crate> { let mut ret = Vec::new(); let mut list = vec![INTERNER.intern_str(root)]; let mut visited = HashSet::new(); @@ -1386,10 +1386,6 @@ impl Build { // the future, we may want to consider just filtering all // build and dev dependencies in metadata::build. if visited.insert(dep) - && (dep != "profiler_builtins" - || target - .map(|t| self.config.profiler_enabled(t)) - .unwrap_or_else(|| self.config.any_profiler_enabled())) && (dep != "rustc_codegen_llvm" || self.config.llvm_enabled()) { list.push(*dep); diff --git a/src/doc/unstable-book/src/library-features/profiler-runtime-lib.md b/src/doc/unstable-book/src/library-features/profiler-runtime-lib.md deleted file mode 100644 index a01f1e73ab404..0000000000000 --- a/src/doc/unstable-book/src/library-features/profiler-runtime-lib.md +++ /dev/null @@ -1,5 +0,0 @@ -# `profiler_runtime_lib` - -This feature is internal to the Rust compiler and is not intended for general use. - ------------------------- diff --git a/src/test/rustdoc-ui/z-help.stdout b/src/test/rustdoc-ui/z-help.stdout index 46f11d2e5d1eb..4512d58b02068 100644 --- a/src/test/rustdoc-ui/z-help.stdout +++ b/src/test/rustdoc-ui/z-help.stdout @@ -94,7 +94,7 @@ -Z no-leak-check=val -- disable the 'leak check' for subtyping; unsound, but useful for tests -Z no-link=val -- compile without linking -Z no-parallel-llvm=val -- run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO) - -Z no-profiler-runtime=val -- prevent automatic injection of the profiler_builtins crate + -Z no-profiler-runtime=val -- prevent automatic injection of the profiler runtime -Z no-unique-section-names=val -- do not use unique names for text and data sections when -Z function-sections is used -Z normalize-docs=val -- normalize associated items in rustdoc when generating documentation -Z oom=val -- panic strategy for out-of-memory handling @@ -123,7 +123,7 @@ -Z profile-closures=val -- profile size of closures -Z profile-emit=val -- file path to emit profiling data at runtime when using 'profile' (default based on relative source path) -Z profile-sample-use=val -- use the given `.prof` file for sampled profile-guided optimization (also known as AutoFDO) - -Z profiler-runtime=val -- name of the profiler runtime crate to automatically inject (default: `profiler_builtins`) + -Z profiler-runtime=val -- name of the profiler runtime crate to automatically inject -Z query-dep-graph=val -- enable queries of the dependency graph for regression testing (default: no) -Z randomize-layout=val -- randomize the layout of types (default: no) -Z relax-elf-relocations=val -- whether ELF relocations can be relaxed diff --git a/src/test/ui/feature-gates/feature-gate-profiler-runtime.stderr b/src/test/ui/feature-gates/feature-gate-profiler-runtime.stderr index 18e6503c5524e..3544c691e2ebb 100644 --- a/src/test/ui/feature-gates/feature-gate-profiler-runtime.stderr +++ b/src/test/ui/feature-gates/feature-gate-profiler-runtime.stderr @@ -1,4 +1,4 @@ -error[E0658]: the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate which contains the profiler runtime and will never be stable +error[E0658]: the `#[profiler_runtime]` attribute is used to identify the crate which contains the profiler runtime and will never be stable --> $DIR/feature-gate-profiler-runtime.rs:1:1 | LL | #![profiler_runtime] diff --git a/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs b/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs index 64dd2bb5f2d14..302f477a476b0 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs @@ -3722,15 +3722,6 @@ This feature is internal to the Rust compiler and is not intended for general us The tracking issue for this feature is: [#42524](https://github.com/rust-lang/rust/issues/42524). ------------------------- -"##, - }, - Lint { - label: "profiler_runtime_lib", - description: r##"# `profiler_runtime_lib` - -This feature is internal to the Rust compiler and is not intended for general use. - ------------------------ "##, }, From 04963ab297683eb7cb5490311eee19c76edd28e9 Mon Sep 17 00:00:00 2001 From: bogon-right Date: Sat, 3 Sep 2022 22:11:07 +0100 Subject: [PATCH 4/4] Make `profiler_runtime` `Option` --- compiler/rustc_codegen_ssa/src/back/link.rs | 2 +- compiler/rustc_interface/src/tests.rs | 2 +- compiler/rustc_metadata/src/creader.rs | 18 ++++++++++-------- compiler/rustc_metadata/src/errors.rs | 4 ++-- compiler/rustc_metadata/src/locator.rs | 7 ++++++- compiler/rustc_session/src/options.rs | 4 ++-- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 511430ffade5a..a95b02559c7d1 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1163,7 +1163,7 @@ fn add_profiler_libraries(sess: &Session, crate_type: CrateType, linker: &mut dy || sess.opts.unstable_opts.profile || sess.opts.cg.profile_generate.enabled()) // If user doesn't provide custom profiler runtime, link default llvm profiler. - && sess.opts.unstable_opts.profiler_runtime == "profiler_builtins" + && sess.opts.unstable_opts.profiler_runtime.is_none() { link_profiler_runtime(sess, linker); } diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index eb8e65a6d59d3..7a442d031dacd 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -770,7 +770,7 @@ fn test_unstable_options_tracking_hash() { tracked!(profile, true); tracked!(profile_emit, Some(PathBuf::from("abc"))); tracked!(profile_sample_use, Some(PathBuf::from("abc"))); - tracked!(profiler_runtime, "abc".to_string()); + tracked!(profiler_runtime, Some("abc".to_string())); tracked!(relax_elf_relocations, Some(true)); tracked!(relro_level, Some(RelroLevel::Full)); tracked!(remap_cwd_prefix, Some(PathBuf::from("abc"))); diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 4a20cdf083618..dc34911a7bf41 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -764,21 +764,23 @@ impl<'a> CrateLoader<'a> { || !(self.sess.instrument_coverage() || self.sess.opts.unstable_opts.profile || self.sess.opts.cg.profile_generate.enabled()) - || self.sess.opts.unstable_opts.profiler_runtime == "profiler_builtins" { return; } - info!("loading profiler"); + // If user doesn't provide custom profiler runtime, skip injection. + if let Some(profiler_runtime) = &self.sess.opts.unstable_opts.profiler_runtime { + info!("loading profiler: {}", profiler_runtime); - let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime); + let name = Symbol::intern(profiler_runtime); - let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; - let data = self.cstore.get_crate_data(cnum); + let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; + let data = self.cstore.get_crate_data(cnum); - // Sanity check the loaded crate to ensure it is indeed a profiler runtime - if !data.is_profiler_runtime() { - self.sess.emit_err(NotProfilerRuntime { crate_name: name }); + // Sanity check the loaded crate to ensure it is indeed a profiler runtime + if !data.is_profiler_runtime() { + self.sess.emit_err(NotProfilerRuntime { crate_name: name }); + } } } diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 7c387b9a9ecde..633f4acb17add 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -601,7 +601,7 @@ pub struct CannotFindCrate { pub missing_core: bool, pub current_crate: String, pub is_nightly_build: bool, - pub profiler_runtime: Symbol, + pub profiler_runtime: Option, pub locator_triple: TargetTriple, } @@ -641,7 +641,7 @@ impl IntoDiagnostic<'_> for CannotFindCrate { if self.is_nightly_build { diag.help(rustc_errors::fluent::metadata_consider_building_std); } - } else if self.crate_name == self.profiler_runtime { + } else if Some(self.crate_name) == self.profiler_runtime { diag.note(rustc_errors::fluent::metadata_compiler_missing_profiler); } else if self.crate_name.as_str().starts_with("rustc_") { diag.help(rustc_errors::fluent::metadata_install_missing_components); diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 35f9ef92a1c42..befd39f72c105 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -1124,7 +1124,12 @@ impl CrateError { .clone() .unwrap_or("".to_string()), is_nightly_build: sess.is_nightly_build(), - profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime), + profiler_runtime: sess + .opts + .unstable_opts + .profiler_runtime + .as_ref() + .map(|x| Symbol::intern(x)), locator_triple: locator.triple, }); } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 713480d4f8e8c..357b7d5eaa3be 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1483,8 +1483,8 @@ options! { (default based on relative source path)"), profile_sample_use: Option = (None, parse_opt_pathbuf, [TRACKED], "use the given `.prof` file for sampled profile-guided optimization (also known as AutoFDO)"), - profiler_runtime: String = (String::from("profiler_builtins"), parse_string, [TRACKED], - "name of the profiler runtime crate to automatically inject (default: `profiler_builtins`)"), + profiler_runtime: Option = (None, parse_opt_string, [TRACKED], + "name of the profiler runtime crate to automatically inject"), query_dep_graph: bool = (false, parse_bool, [UNTRACKED], "enable queries of the dependency graph for regression testing (default: no)"), randomize_layout: bool = (false, parse_bool, [TRACKED],