Index: include/llvm/IR/LLVMContext.h =================================================================== --- include/llvm/IR/LLVMContext.h +++ include/llvm/IR/LLVMContext.h @@ -193,6 +193,15 @@ /// diagnostics. void setDiagnosticsHotnessRequested(bool Requested); + /// \brief Return the minimum hotness value a diagnostic would need in order + /// to be included in optimization diagnostics. If there is no minimum, this + /// returns None. + Optional getDiagnosticsHotnessThreshold() const; + + /// \brief Set the minimum hotness value a diagnostic needs in order to be + /// included in optimization diagnostics. + void setDiagnosticsHotnessThreshold(uint64_t Threshold); + /// \brief Return the YAML file used by the backend to save optimization /// diagnostics. If null, diagnostics are not saved in a file but only /// emitted via the diagnostic handler. Index: lib/Analysis/OptimizationDiagnosticInfo.cpp =================================================================== --- lib/Analysis/OptimizationDiagnosticInfo.cpp +++ lib/Analysis/OptimizationDiagnosticInfo.cpp @@ -156,6 +156,14 @@ auto &OptDiag = cast(OptDiagBase); computeHotness(OptDiag); + // If a diagnostic has a hotness value, then only emit it if its hotness + // meets the threshold. + auto Threshold = F->getContext().getDiagnosticsHotnessThreshold(); + if (OptDiag.getHotness() && Threshold && + *OptDiag.getHotness() < *Threshold) { + return; + } + yaml::Output *Out = F->getContext().getDiagnosticsOutputFile(); if (Out) { auto *P = const_cast(&OptDiagBase); Index: lib/IR/LLVMContext.cpp =================================================================== --- lib/IR/LLVMContext.cpp +++ lib/IR/LLVMContext.cpp @@ -132,6 +132,13 @@ return pImpl->DiagnosticsHotnessRequested; } +void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) { + pImpl->DiagnosticsHotnessThreshold = Threshold; +} +Optional LLVMContext::getDiagnosticsHotnessThreshold() const { + return pImpl->DiagnosticsHotnessThreshold; +} + yaml::Output *LLVMContext::getDiagnosticsOutputFile() { return pImpl->DiagnosticsOutputFile.get(); } Index: lib/IR/LLVMContextImpl.h =================================================================== --- lib/IR/LLVMContextImpl.h +++ lib/IR/LLVMContextImpl.h @@ -1170,6 +1170,7 @@ void *DiagnosticContext = nullptr; bool RespectDiagnosticFilters = false; bool DiagnosticsHotnessRequested = false; + Optional DiagnosticsHotnessThreshold = None; std::unique_ptr DiagnosticsOutputFile; LLVMContext::YieldCallbackTy YieldCallback = nullptr; Index: test/Transforms/Inline/optimization-remarks-yaml.ll =================================================================== --- test/Transforms/Inline/optimization-remarks-yaml.ll +++ test/Transforms/Inline/optimization-remarks-yaml.ll @@ -1,8 +1,21 @@ -; RUN: opt < %s -S -inline -pass-remarks-missed=inline -pass-remarks-with-hotness \ +; RUN: opt < %s -S -inline -pass-remarks-missed=inline \ +; RUN: -pass-remarks-with-hotness -pass-remarks-hotness-threshold 15 \ ; RUN: -pass-remarks-output=%t 2>&1 | FileCheck %s ; RUN: cat %t | FileCheck -check-prefix=YAML %s ; RUN: opt < %s -S -inline -pass-remarks-with-hotness -pass-remarks-output=%t ; RUN: cat %t | FileCheck -check-prefix=YAML %s +; +; Verify that remarks that don't meet the hotness threshold are not output. +; RUN: opt < %s -S -inline -pass-remarks-missed=inline \ +; RUN: -pass-remarks-with-hotness -pass-remarks-hotness-threshold 100 \ +; RUN: -pass-remarks-output=%t.threshold 2>&1 | \ +; RUN: FileCheck -check-prefix=THRESHOLD %s +; RUN: test ! -s %t.threshold +; RUN: opt < %s -S -inline \ +; RUN: -pass-remarks-with-hotness -pass-remarks-hotness-threshold 100 \ +; RUN: -pass-remarks-output=%t.threshold +; The remarks output file should be empty. +; RUN: test ! -s %t.threshold ; Check the YAML file generated for inliner remarks for this program: ; @@ -43,6 +56,9 @@ ; YAML-NEXT: - String: ' because its definition is unavailable' ; YAML-NEXT: ... +; No remarks should be output, since none meet the threshold. +; THRESHOLD-NOT: remark + ; ModuleID = '/tmp/s.c' source_filename = "/tmp/s.c" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" Index: tools/llc/llc.cpp =================================================================== --- tools/llc/llc.cpp +++ tools/llc/llc.cpp @@ -149,6 +149,11 @@ cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden); +static cl::opt PassRemarksHotnessThreshold( + "pass-remarks-hotness-threshold", + cl::desc("Minimum profile count required for an optimization remark to be output"), + cl::Hidden); + static cl::opt RemarksFilename("pass-remarks-output", cl::desc("YAML output filename for pass remarks"), @@ -325,6 +330,9 @@ if (PassRemarksWithHotness) Context.setDiagnosticsHotnessRequested(true); + if (PassRemarksHotnessThreshold) + Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold); + std::unique_ptr YamlFile; if (RemarksFilename != "") { std::error_code EC; Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -242,6 +242,11 @@ cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden); +static cl::opt PassRemarksHotnessThreshold( + "pass-remarks-hotness-threshold", + cl::desc("Minimum profile count required for an optimization remark to be output"), + cl::Hidden); + static cl::opt RemarksFilename("pass-remarks-output", cl::desc("YAML output filename for pass remarks"), @@ -422,6 +427,9 @@ if (PassRemarksWithHotness) Context.setDiagnosticsHotnessRequested(true); + if (PassRemarksHotnessThreshold) + Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold); + std::unique_ptr YamlFile; if (RemarksFilename != "") { std::error_code EC;