Skip to content

Commit a67c34d

Browse files
committed
[nfc][pgo] const-ify some APIs in InstrProfSymtab
1 parent 9487727 commit a67c34d

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -526,24 +526,24 @@ class InstrProfSymtab {
526526
// so it doesn't use a StringSet for function names.
527527
StringSet<> VTableNames;
528528
// A map from MD5 keys to function name strings.
529-
std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
529+
mutable std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
530530
// A map from MD5 keys to function define. We only populate this map
531531
// when build the Symtab from a Module.
532-
std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
532+
mutable std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
533533
// A map from MD5 to the global variable. This map is only populated when
534534
// building the symtab from a module. Use separate container instances for
535535
// `MD5FuncMap` and `MD5VTableMap`.
536536
// TODO: Unify the container type and the lambda function 'mapName' inside
537537
// add{Func,VTable}WithName.
538-
DenseMap<uint64_t, GlobalVariable *> MD5VTableMap;
538+
mutable DenseMap<uint64_t, GlobalVariable *> MD5VTableMap;
539539
// A map from function runtime address to function name MD5 hash.
540540
// This map is only populated and used by raw instr profile reader.
541-
AddrHashMap AddrToMD5Map;
541+
mutable AddrHashMap AddrToMD5Map;
542542

543543
AddrIntervalMap::Allocator VTableAddrMapAllocator;
544544
// This map is only populated and used by raw instr profile reader.
545545
AddrIntervalMap VTableAddrMap;
546-
bool Sorted = false;
546+
mutable bool Sorted = false;
547547

548548
static StringRef getExternalSymbol() { return "** External Symbol **"; }
549549

@@ -566,7 +566,7 @@ class InstrProfSymtab {
566566
// finalizeSymtab needs to be called before looking up function names.
567567
// This is required because the underlying map is a vector (for space
568568
// efficiency) which needs to be sorted.
569-
inline void finalizeSymtab();
569+
inline void finalizeSymtab() const;
570570

571571
public:
572572
InstrProfSymtab() : VTableAddrMap(VTableAddrMapAllocator) {}
@@ -676,36 +676,37 @@ class InstrProfSymtab {
676676
}
677677

678678
/// Return a function's hash, or 0, if the function isn't in this SymTab.
679-
LLVM_ABI uint64_t getFunctionHashFromAddress(uint64_t Address);
679+
LLVM_ABI uint64_t getFunctionHashFromAddress(uint64_t Address) const;
680680

681681
/// Return a vtable's hash, or 0 if the vtable doesn't exist in this SymTab.
682-
LLVM_ABI uint64_t getVTableHashFromAddress(uint64_t Address);
682+
LLVM_ABI uint64_t getVTableHashFromAddress(uint64_t Address) const;
683683

684684
/// Return function's PGO name from the function name's symbol
685685
/// address in the object file. If an error occurs, return
686686
/// an empty string.
687-
LLVM_ABI StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
687+
LLVM_ABI StringRef getFuncName(uint64_t FuncNameAddress,
688+
size_t NameSize) const;
688689

689690
/// Return name of functions or global variables from the name's md5 hash
690691
/// value. If not found, return an empty string.
691-
inline StringRef getFuncOrVarName(uint64_t ValMD5Hash);
692+
inline StringRef getFuncOrVarName(uint64_t ValMD5Hash) const;
692693

693694
/// Just like getFuncOrVarName, except that it will return literal string
694695
/// 'External Symbol' if the function or global variable is external to
695696
/// this symbol table.
696-
inline StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash);
697+
inline StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash) const;
697698

698699
/// True if Symbol is the value used to represent external symbols.
699700
static bool isExternalSymbol(const StringRef &Symbol) {
700701
return Symbol == InstrProfSymtab::getExternalSymbol();
701702
}
702703

703704
/// Return function from the name's md5 hash. Return nullptr if not found.
704-
inline Function *getFunction(uint64_t FuncMD5Hash);
705+
inline Function *getFunction(uint64_t FuncMD5Hash) const;
705706

706707
/// Return the global variable corresponding to md5 hash. Return nullptr if
707708
/// not found.
708-
inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash);
709+
inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash) const;
709710

710711
/// Return the name section data.
711712
inline StringRef getNameData() const { return Data; }
@@ -748,7 +749,7 @@ Error InstrProfSymtab::create(const FuncNameIterRange &FuncIterRange,
748749
return Error::success();
749750
}
750751

751-
void InstrProfSymtab::finalizeSymtab() {
752+
void InstrProfSymtab::finalizeSymtab() const {
752753
if (Sorted)
753754
return;
754755
llvm::sort(MD5NameMap, less_first());
@@ -758,14 +759,14 @@ void InstrProfSymtab::finalizeSymtab() {
758759
Sorted = true;
759760
}
760761

761-
StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
762-
StringRef ret = getFuncOrVarName(MD5Hash);
763-
if (ret.empty())
762+
StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) const {
763+
StringRef Ret = getFuncOrVarName(MD5Hash);
764+
if (Ret.empty())
764765
return InstrProfSymtab::getExternalSymbol();
765-
return ret;
766+
return Ret;
766767
}
767768

768-
StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
769+
StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) const {
769770
finalizeSymtab();
770771
auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
771772
[](const std::pair<uint64_t, StringRef> &LHS,
@@ -775,7 +776,7 @@ StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
775776
return StringRef();
776777
}
777778

778-
Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
779+
Function *InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) const {
779780
finalizeSymtab();
780781
auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
781782
[](const std::pair<uint64_t, Function *> &LHS,
@@ -785,7 +786,7 @@ Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
785786
return nullptr;
786787
}
787788

788-
GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) {
789+
GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) const {
789790
return MD5VTableMap.lookup(MD5Hash);
790791
}
791792

llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ Error InstrProfSymtab::create(SectionRef &Section) {
519519
return Error::success();
520520
}
521521

522-
StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
522+
StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) const {
523523
if (Pointer < Address)
524524
return StringRef();
525525
auto Offset = Pointer - Address;

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,13 +684,13 @@ Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName,
684684
return Error::success();
685685
}
686686

687-
uint64_t InstrProfSymtab::getVTableHashFromAddress(uint64_t Address) {
687+
uint64_t InstrProfSymtab::getVTableHashFromAddress(uint64_t Address) const {
688688
// Given a runtime address, look up the hash value in the interval map, and
689689
// fallback to value 0 if a hash value is not found.
690690
return VTableAddrMap.lookup(Address, 0);
691691
}
692692

693-
uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
693+
uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) const {
694694
finalizeSymtab();
695695
auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
696696
return A.first < Address;

0 commit comments

Comments
 (0)