[BOLT][NFC] Move getLTOCommonName to Utils

Reuse getLTOCommonName in components other than Profile (to be used in Core)

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D142259
This commit is contained in:
Amir Ayupov 2023-01-20 19:27:38 -08:00
parent 255c3f1159
commit dbb7316e02
5 changed files with 36 additions and 35 deletions

View File

@ -23,32 +23,6 @@ class BinaryContext;
class BinaryFunction;
class BoltAddressTranslation;
/// LTO-generated function names take a form:
///
/// <function_name>.lto_priv.<decimal_number>/...
/// or
/// <function_name>.constprop.<decimal_number>/...
///
/// they can also be:
///
/// <function_name>.lto_priv.<decimal_number1>.lto_priv.<decimal_number2>/...
///
/// The <decimal_number> is a global counter used for the whole program. As a
/// result, a tiny change in a program may affect the naming of many LTO
/// functions. For us this means that if we do a precise name matching, then
/// a large set of functions could be left without a profile.
///
/// To solve this issue, we try to match a function to any profile:
///
/// <function_name>.(lto_priv|consprop).*
///
/// The name before an asterisk above represents a common LTO name for a family
/// of functions. Later, out of all matching profiles we pick the one with the
/// best match.
///
/// Return a common part of LTO name for a given \p Name.
std::optional<StringRef> getLTOCommonName(const StringRef Name);
class ProfileReaderBase {
protected:
/// Name of the file with profile.

View File

@ -41,6 +41,32 @@ std::string getEscapedName(const StringRef &Name);
/// Return the unescaped name
std::string getUnescapedName(const StringRef &Name);
/// LTO-generated function names take a form:
///
/// <function_name>.lto_priv.<decimal_number>/...
/// or
/// <function_name>.constprop.<decimal_number>/...
///
/// they can also be:
///
/// <function_name>.lto_priv.<decimal_number1>.lto_priv.<decimal_number2>/...
///
/// The <decimal_number> is a global counter used for the whole program. As a
/// result, a tiny change in a program may affect the naming of many LTO
/// functions. For us this means that if we do a precise name matching, then
/// a large set of functions could be left without a profile.
///
/// To solve this issue, we try to match a function to any profile:
///
/// <function_name>.(lto_priv|consprop).*
///
/// The name before an asterisk above represents a common LTO name for a family
/// of functions. Later, out of all matching profiles we pick the one with the
/// best match.
///
/// Return a common part of LTO name for a given \p Name.
std::optional<StringRef> getLTOCommonName(const StringRef Name);
// Determines which register a given DWARF expression is being assigned to.
// If the expression is defining the CFA, return std::nullopt.
std::optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes);

View File

@ -41,15 +41,6 @@ DumpData("dump-data",
namespace llvm {
namespace bolt {
std::optional<StringRef> getLTOCommonName(const StringRef Name) {
for (StringRef Suffix : {".__uniq.", ".lto_priv.", ".constprop.", ".llvm."}) {
size_t LTOSuffixPos = Name.find(Suffix);
if (LTOSuffixPos != StringRef::npos)
return Name.substr(0, LTOSuffixPos + Suffix.size());
}
return std::nullopt;
}
namespace {
/// Return true if the function name can change across compilations.

View File

@ -14,6 +14,7 @@
#include "bolt/Passes/IdenticalCodeFolding.h"
#include "bolt/Profile/ProfileReaderBase.h"
#include "bolt/Rewrite/RewriteInstance.h"
#include "bolt/Utils/Utils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CommandLine.h"

View File

@ -66,6 +66,15 @@ std::string getUnescapedName(const StringRef &Name) {
return Output;
}
std::optional<StringRef> getLTOCommonName(const StringRef Name) {
for (StringRef Suffix : {".__uniq.", ".lto_priv.", ".constprop.", ".llvm."}) {
size_t LTOSuffixPos = Name.find(Suffix);
if (LTOSuffixPos != StringRef::npos)
return Name.substr(0, LTOSuffixPos + Suffix.size());
}
return std::nullopt;
}
std::optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes) {
uint8_t Opcode = ExprBytes[0];
if (Opcode == dwarf::DW_CFA_def_cfa_expression)