From 93bdc01adf7091e473e36620d3f748ce0481f880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 3 Aug 2023 23:24:58 +0200 Subject: [PATCH 1/2] Add hotness data to LLVM remarks This makes sure that if PGO is used, remarks generated using `-Zremark-dir` will include the `Hotness` attribute. --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 3 +++ .../optimization-remarks-dir-pgo/Makefile | 16 ++++++++++++++++ .../run-make/optimization-remarks-dir-pgo/foo.rs | 6 ++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/run-make/optimization-remarks-dir-pgo/Makefile create mode 100644 tests/run-make/optimization-remarks-dir-pgo/foo.rs diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 8ef39a6c86627..8c04a4305734b 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1967,6 +1967,9 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler( std::unique_ptr LlvmRemarkStreamer; if (RemarkFilePath != nullptr) { + // Enable PGO hotness data for remarks, if available + unwrap(C)->setDiagnosticsHotnessRequested(true); + std::error_code EC; RemarkFile = std::make_unique( RemarkFilePath, diff --git a/tests/run-make/optimization-remarks-dir-pgo/Makefile b/tests/run-make/optimization-remarks-dir-pgo/Makefile new file mode 100644 index 0000000000000..93be1472037df --- /dev/null +++ b/tests/run-make/optimization-remarks-dir-pgo/Makefile @@ -0,0 +1,16 @@ +# needs-profiler-support + +include ../tools.mk + +PROFILE_DIR=$(TMPDIR)/profiles + +check_hotness: + $(RUSTC) -Cprofile-generate="$(TMPDIR)"/profdata -O foo.rs -o$(TMPDIR)/foo + $(TMPDIR)/foo + "$(LLVM_BIN_DIR)"/llvm-profdata merge \ + -o "$(TMPDIR)"/merged.profdata \ + "$(TMPDIR)"/profdata/*.profraw + $(RUSTC) -Cprofile-use=$(TMPDIR)/merged.profdata -O foo.rs -Cremark=all -Zremark-dir=$(PROFILE_DIR) + + # Check that PGO hotness is included in the remark files + cat $(PROFILE_DIR)/*.opt.yaml | $(CGREP) -e "Hotness" diff --git a/tests/run-make/optimization-remarks-dir-pgo/foo.rs b/tests/run-make/optimization-remarks-dir-pgo/foo.rs new file mode 100644 index 0000000000000..f7ca1826338ce --- /dev/null +++ b/tests/run-make/optimization-remarks-dir-pgo/foo.rs @@ -0,0 +1,6 @@ +#[inline(never)] +pub fn bar() {} + +fn main() { + bar(); +} From 9d417d7c86259498855fc50ba0e853edbb13320d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 7 Aug 2023 17:56:57 +0200 Subject: [PATCH 2/2] Only enable hotness information when PGO is available --- compiler/rustc_codegen_llvm/src/back/write.rs | 2 ++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 9 ++++++--- tests/run-make/optimization-remarks-dir-pgo/Makefile | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 46e6daed21f1c..6134d8fc1741c 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -320,6 +320,7 @@ impl<'a> DiagnosticHandlers<'a> { }) .and_then(|dir| dir.to_str().and_then(|p| CString::new(p).ok())); + let pgo_available = cgcx.opts.cg.profile_use.is_some(); let data = Box::into_raw(Box::new((cgcx, handler))); unsafe { let old_handler = llvm::LLVMRustContextGetDiagnosticHandler(llcx); @@ -333,6 +334,7 @@ impl<'a> DiagnosticHandlers<'a> { // The `as_ref()` is important here, otherwise the `CString` will be dropped // too soon! remark_file.as_ref().map(|dir| dir.as_ptr()).unwrap_or(std::ptr::null()), + pgo_available, ); DiagnosticHandlers { data, llcx, old_handler } } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index b167facfb02d0..7fb4d3227dba2 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2332,6 +2332,7 @@ extern "C" { remark_passes: *const *const c_char, remark_passes_len: usize, remark_file: *const c_char, + pgo_available: bool, ); #[allow(improper_ctypes)] diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 8c04a4305734b..a701827e057f4 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1869,7 +1869,8 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler( LLVMContextRef C, LLVMDiagnosticHandlerTy DiagnosticHandlerCallback, void *DiagnosticHandlerContext, bool RemarkAllPasses, const char * const * RemarkPasses, size_t RemarkPassesLen, - const char * RemarkFilePath + const char * RemarkFilePath, + bool PGOAvailable ) { class RustDiagnosticHandler final : public DiagnosticHandler { @@ -1967,8 +1968,10 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler( std::unique_ptr LlvmRemarkStreamer; if (RemarkFilePath != nullptr) { - // Enable PGO hotness data for remarks, if available - unwrap(C)->setDiagnosticsHotnessRequested(true); + if (PGOAvailable) { + // Enable PGO hotness data for remarks, if available + unwrap(C)->setDiagnosticsHotnessRequested(true); + } std::error_code EC; RemarkFile = std::make_unique( diff --git a/tests/run-make/optimization-remarks-dir-pgo/Makefile b/tests/run-make/optimization-remarks-dir-pgo/Makefile index 93be1472037df..c88ec1e6cb30c 100644 --- a/tests/run-make/optimization-remarks-dir-pgo/Makefile +++ b/tests/run-make/optimization-remarks-dir-pgo/Makefile @@ -1,4 +1,8 @@ # needs-profiler-support +# ignore-windows-gnu + +# FIXME(mati865): MinGW GCC miscompiles compiler-rt profiling library but with Clang it works +# properly. Since we only have GCC on the CI ignore the test for now. include ../tools.mk