Skip to content

Commit 6f7ecae

Browse files
committed
[nfc][pgo] const-ify some APIs in InstrProfSymtab
1 parent 76de28d commit 6f7ecae

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -526,24 +526,27 @@ 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+
547+
// "dirty" flag for the rest of the mutable state. lookup APIs (like
548+
// getFunction) need the mutable state to be sorted.
549+
mutable bool Sorted = false;
547550

548551
static StringRef getExternalSymbol() { return "** External Symbol **"; }
549552

@@ -565,8 +568,10 @@ class InstrProfSymtab {
565568
// If the symtab is created by a series of calls to \c addFuncName, \c
566569
// finalizeSymtab needs to be called before looking up function names.
567570
// This is required because the underlying map is a vector (for space
568-
// efficiency) which needs to be sorted.
569-
inline void finalizeSymtab();
571+
// efficiency) which needs to be sorted. The API is `const` because it's part
572+
// of the implementation detail of `const` APIs that need to first ensure this
573+
// property of ordering on the other mutable state.
574+
inline void finalizeSymtab() const;
570575

571576
public:
572577
InstrProfSymtab() : VTableAddrMap(VTableAddrMapAllocator) {}
@@ -676,36 +681,37 @@ class InstrProfSymtab {
676681
}
677682

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

681686
/// 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);
687+
LLVM_ABI uint64_t getVTableHashFromAddress(uint64_t Address) const;
683688

684689
/// Return function's PGO name from the function name's symbol
685690
/// address in the object file. If an error occurs, return
686691
/// an empty string.
687-
LLVM_ABI StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
692+
LLVM_ABI StringRef getFuncName(uint64_t FuncNameAddress,
693+
size_t NameSize) const;
688694

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

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

698704
/// True if Symbol is the value used to represent external symbols.
699705
static bool isExternalSymbol(const StringRef &Symbol) {
700706
return Symbol == InstrProfSymtab::getExternalSymbol();
701707
}
702708

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

706712
/// Return the global variable corresponding to md5 hash. Return nullptr if
707713
/// not found.
708-
inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash);
714+
inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash) const;
709715

710716
/// Return the name section data.
711717
inline StringRef getNameData() const { return Data; }
@@ -748,7 +754,7 @@ Error InstrProfSymtab::create(const FuncNameIterRange &FuncIterRange,
748754
return Error::success();
749755
}
750756

751-
void InstrProfSymtab::finalizeSymtab() {
757+
void InstrProfSymtab::finalizeSymtab() const {
752758
if (Sorted)
753759
return;
754760
llvm::sort(MD5NameMap, less_first());
@@ -758,14 +764,14 @@ void InstrProfSymtab::finalizeSymtab() {
758764
Sorted = true;
759765
}
760766

761-
StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
762-
StringRef ret = getFuncOrVarName(MD5Hash);
763-
if (ret.empty())
767+
StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) const {
768+
StringRef Ret = getFuncOrVarName(MD5Hash);
769+
if (Ret.empty())
764770
return InstrProfSymtab::getExternalSymbol();
765-
return ret;
771+
return Ret;
766772
}
767773

768-
StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
774+
StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) const {
769775
finalizeSymtab();
770776
auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
771777
[](const std::pair<uint64_t, StringRef> &LHS,
@@ -775,7 +781,7 @@ StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
775781
return StringRef();
776782
}
777783

778-
Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
784+
Function *InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) const {
779785
finalizeSymtab();
780786
auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
781787
[](const std::pair<uint64_t, Function *> &LHS,
@@ -785,7 +791,7 @@ Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
785791
return nullptr;
786792
}
787793

788-
GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) {
794+
GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) const {
789795
return MD5VTableMap.lookup(MD5Hash);
790796
}
791797

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)