@@ -526,24 +526,27 @@ class InstrProfSymtab {
526
526
// so it doesn't use a StringSet for function names.
527
527
StringSet<> VTableNames;
528
528
// 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;
530
530
// A map from MD5 keys to function define. We only populate this map
531
531
// 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;
533
533
// A map from MD5 to the global variable. This map is only populated when
534
534
// building the symtab from a module. Use separate container instances for
535
535
// `MD5FuncMap` and `MD5VTableMap`.
536
536
// TODO: Unify the container type and the lambda function 'mapName' inside
537
537
// add{Func,VTable}WithName.
538
- DenseMap<uint64_t , GlobalVariable *> MD5VTableMap;
538
+ mutable DenseMap<uint64_t , GlobalVariable *> MD5VTableMap;
539
539
// A map from function runtime address to function name MD5 hash.
540
540
// This map is only populated and used by raw instr profile reader.
541
- AddrHashMap AddrToMD5Map;
541
+ mutable AddrHashMap AddrToMD5Map;
542
542
543
543
AddrIntervalMap::Allocator VTableAddrMapAllocator;
544
544
// This map is only populated and used by raw instr profile reader.
545
545
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 ;
547
550
548
551
static StringRef getExternalSymbol () { return " ** External Symbol **" ; }
549
552
@@ -565,8 +568,10 @@ class InstrProfSymtab {
565
568
// If the symtab is created by a series of calls to \c addFuncName, \c
566
569
// finalizeSymtab needs to be called before looking up function names.
567
570
// 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 ;
570
575
571
576
public:
572
577
InstrProfSymtab () : VTableAddrMap(VTableAddrMapAllocator) {}
@@ -676,36 +681,37 @@ class InstrProfSymtab {
676
681
}
677
682
678
683
// / 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 ;
680
685
681
686
// / 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 ;
683
688
684
689
// / Return function's PGO name from the function name's symbol
685
690
// / address in the object file. If an error occurs, return
686
691
// / 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 ;
688
694
689
695
// / Return name of functions or global variables from the name's md5 hash
690
696
// / value. If not found, return an empty string.
691
- inline StringRef getFuncOrVarName (uint64_t ValMD5Hash);
697
+ inline StringRef getFuncOrVarName (uint64_t ValMD5Hash) const ;
692
698
693
699
// / Just like getFuncOrVarName, except that it will return literal string
694
700
// / 'External Symbol' if the function or global variable is external to
695
701
// / this symbol table.
696
- inline StringRef getFuncOrVarNameIfDefined (uint64_t ValMD5Hash);
702
+ inline StringRef getFuncOrVarNameIfDefined (uint64_t ValMD5Hash) const ;
697
703
698
704
// / True if Symbol is the value used to represent external symbols.
699
705
static bool isExternalSymbol (const StringRef &Symbol) {
700
706
return Symbol == InstrProfSymtab::getExternalSymbol ();
701
707
}
702
708
703
709
// / 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 ;
705
711
706
712
// / Return the global variable corresponding to md5 hash. Return nullptr if
707
713
// / not found.
708
- inline GlobalVariable *getGlobalVariable (uint64_t MD5Hash);
714
+ inline GlobalVariable *getGlobalVariable (uint64_t MD5Hash) const ;
709
715
710
716
// / Return the name section data.
711
717
inline StringRef getNameData () const { return Data; }
@@ -748,7 +754,7 @@ Error InstrProfSymtab::create(const FuncNameIterRange &FuncIterRange,
748
754
return Error::success ();
749
755
}
750
756
751
- void InstrProfSymtab::finalizeSymtab () {
757
+ void InstrProfSymtab::finalizeSymtab () const {
752
758
if (Sorted)
753
759
return ;
754
760
llvm::sort (MD5NameMap, less_first ());
@@ -758,14 +764,14 @@ void InstrProfSymtab::finalizeSymtab() {
758
764
Sorted = true ;
759
765
}
760
766
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 ())
764
770
return InstrProfSymtab::getExternalSymbol ();
765
- return ret ;
771
+ return Ret ;
766
772
}
767
773
768
- StringRef InstrProfSymtab::getFuncOrVarName (uint64_t MD5Hash) {
774
+ StringRef InstrProfSymtab::getFuncOrVarName (uint64_t MD5Hash) const {
769
775
finalizeSymtab ();
770
776
auto Result = llvm::lower_bound (MD5NameMap, MD5Hash,
771
777
[](const std::pair<uint64_t , StringRef> &LHS,
@@ -775,7 +781,7 @@ StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
775
781
return StringRef ();
776
782
}
777
783
778
- Function* InstrProfSymtab::getFunction (uint64_t FuncMD5Hash) {
784
+ Function * InstrProfSymtab::getFunction (uint64_t FuncMD5Hash) const {
779
785
finalizeSymtab ();
780
786
auto Result = llvm::lower_bound (MD5FuncMap, FuncMD5Hash,
781
787
[](const std::pair<uint64_t , Function *> &LHS,
@@ -785,7 +791,7 @@ Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
785
791
return nullptr ;
786
792
}
787
793
788
- GlobalVariable *InstrProfSymtab::getGlobalVariable (uint64_t MD5Hash) {
794
+ GlobalVariable *InstrProfSymtab::getGlobalVariable (uint64_t MD5Hash) const {
789
795
return MD5VTableMap.lookup (MD5Hash);
790
796
}
791
797
0 commit comments