Skip to content

Commit f525bb4

Browse files
committed
Auto merge of #114439 - Kobzol:remark-pgo-hotness, r=tmiasko
Add hotness data to LLVM remarks Slight improvement of #113040. This makes sure that if PGO is used, remarks generated using `-Zremark-dir` will include the `Hotness` attribute. r? `@tmiasko`
2 parents bf62436 + 9d417d7 commit f525bb4

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ impl<'a> DiagnosticHandlers<'a> {
320320
})
321321
.and_then(|dir| dir.to_str().and_then(|p| CString::new(p).ok()));
322322

323+
let pgo_available = cgcx.opts.cg.profile_use.is_some();
323324
let data = Box::into_raw(Box::new((cgcx, handler)));
324325
unsafe {
325326
let old_handler = llvm::LLVMRustContextGetDiagnosticHandler(llcx);
@@ -333,6 +334,7 @@ impl<'a> DiagnosticHandlers<'a> {
333334
// The `as_ref()` is important here, otherwise the `CString` will be dropped
334335
// too soon!
335336
remark_file.as_ref().map(|dir| dir.as_ptr()).unwrap_or(std::ptr::null()),
337+
pgo_available,
336338
);
337339
DiagnosticHandlers { data, llcx, old_handler }
338340
}

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,7 @@ extern "C" {
23322332
remark_passes: *const *const c_char,
23332333
remark_passes_len: usize,
23342334
remark_file: *const c_char,
2335+
pgo_available: bool,
23352336
);
23362337

23372338
#[allow(improper_ctypes)]

Diff for: compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,8 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
18691869
LLVMContextRef C, LLVMDiagnosticHandlerTy DiagnosticHandlerCallback,
18701870
void *DiagnosticHandlerContext, bool RemarkAllPasses,
18711871
const char * const * RemarkPasses, size_t RemarkPassesLen,
1872-
const char * RemarkFilePath
1872+
const char * RemarkFilePath,
1873+
bool PGOAvailable
18731874
) {
18741875

18751876
class RustDiagnosticHandler final : public DiagnosticHandler {
@@ -1967,6 +1968,11 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
19671968
std::unique_ptr<LLVMRemarkStreamer> LlvmRemarkStreamer;
19681969

19691970
if (RemarkFilePath != nullptr) {
1971+
if (PGOAvailable) {
1972+
// Enable PGO hotness data for remarks, if available
1973+
unwrap(C)->setDiagnosticsHotnessRequested(true);
1974+
}
1975+
19701976
std::error_code EC;
19711977
RemarkFile = std::make_unique<ToolOutputFile>(
19721978
RemarkFilePath,

Diff for: tests/run-make/optimization-remarks-dir-pgo/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# needs-profiler-support
2+
# ignore-windows-gnu
3+
4+
# FIXME(mati865): MinGW GCC miscompiles compiler-rt profiling library but with Clang it works
5+
# properly. Since we only have GCC on the CI ignore the test for now.
6+
7+
include ../tools.mk
8+
9+
PROFILE_DIR=$(TMPDIR)/profiles
10+
11+
check_hotness:
12+
$(RUSTC) -Cprofile-generate="$(TMPDIR)"/profdata -O foo.rs -o$(TMPDIR)/foo
13+
$(TMPDIR)/foo
14+
"$(LLVM_BIN_DIR)"/llvm-profdata merge \
15+
-o "$(TMPDIR)"/merged.profdata \
16+
"$(TMPDIR)"/profdata/*.profraw
17+
$(RUSTC) -Cprofile-use=$(TMPDIR)/merged.profdata -O foo.rs -Cremark=all -Zremark-dir=$(PROFILE_DIR)
18+
19+
# Check that PGO hotness is included in the remark files
20+
cat $(PROFILE_DIR)/*.opt.yaml | $(CGREP) -e "Hotness"

Diff for: tests/run-make/optimization-remarks-dir-pgo/foo.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[inline(never)]
2+
pub fn bar() {}
3+
4+
fn main() {
5+
bar();
6+
}

0 commit comments

Comments
 (0)