Skip to content

Commit d3bac46

Browse files
committed
Address review comments and formatting issues
1 parent 5457365 commit d3bac46

15 files changed

Lines changed: 87 additions & 61 deletions

File tree

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,10 @@ class GlobalValue : public Constant {
615615
/// Return a 64-bit global unique ID for this value. It is based on the
616616
/// "original" name and linkage of this value (i.e. whenever its GUID was
617617
/// assigned). This might not match the current name and linkage.
618+
///
619+
/// The \c AssignGUIDPass must be run before this is called, otherwise
620+
/// GUIDs won't be available. This pass can be run multiple times as it does
621+
/// nothing if GUID metadata is already present.
618622
GUID getGUID() const;
619623

620624
/// Return the GUID for this value if it has been assigned, nullopt

llvm/include/llvm/IR/Module.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,21 +577,28 @@ class LLVM_ABI Module {
577577
// Use global_size() to get the total number of global variables.
578578
// Use globals() to get the range of all global variables.
579579

580-
std::optional<GlobalValue::GUID> getGUID(const Value* V) const {
580+
std::optional<GlobalValue::GUID> getGUID(const Value *V) const {
581581
const auto It = ValueToGUIDMap.find(V);
582582
if (It == ValueToGUIDMap.end()) {
583-
return {};
583+
return std::nullopt;
584584
}
585585

586586
return It->getSecond();
587587
}
588588

589-
void insertGUID(const Value* V, GlobalValue::GUID GUID) {
590-
ValueToGUIDMap[V] = GUID;
589+
void insertGUID(const Value *V, GlobalValue::GUID GUID) {
590+
const auto [It, WasInserted] = ValueToGUIDMap.insert({V, GUID});
591+
592+
(void)It, (void)WasInserted;
593+
#ifndef NDEBUG
594+
if (!WasInserted) {
595+
assert((It->second == GUID) && "insertGUID called with different value");
596+
}
597+
#endif
591598
}
592599

593600
private:
594-
DenseMap<const Value*, GlobalValue::GUID> ValueToGUIDMap;
601+
DenseMap<const Value *, GlobalValue::GUID> ValueToGUIDMap;
595602

596603
/// @}
597604
/// @name Direct access to the globals list, functions list, and symbol table

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,8 +1770,10 @@ class ModuleSummaryIndex {
17701770
return ValueInfo(HaveGVs, VP);
17711771
}
17721772

1773-
/// Return a ValueInfo for \p GV with GUID \p GUID and mark it as belonging to GV.
1774-
ValueInfo getOrInsertValueInfo(const GlobalValue *GV, GlobalValue::GUID GUID) {
1773+
/// Return a ValueInfo for \p GV with GUID \p GUID and mark it as belonging to
1774+
/// GV.
1775+
ValueInfo getOrInsertValueInfo(const GlobalValue *GV,
1776+
GlobalValue::GUID GUID) {
17751777
assert(HaveGVs);
17761778
auto VP = getOrInsertValuePtr(GUID);
17771779
VP->second.U.GV = GV;

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static cl::opt<bool> ForceIsInSpecializedModule(
5050
"ctx-profile-force-is-specialized", cl::init(false),
5151
cl::desc("Treat the given module as-if it were containing the "
5252
"post-thinlink module containing the root"));
53-
53+
5454
class ProfileAnnotatorImpl final {
5555
friend class ProfileAnnotator;
5656
class BBInfo;

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9851,7 +9851,9 @@ bool LLParser::addGlobalValueToIndex(
98519851
// Be a little lenient here, to accomodate older files without GUIDs
98529852
// already computed and assigned as metadata.
98539853
auto MaybeGUID = GV->getGUIDIfAssigned();
9854-
GUID = MaybeGUID ? *MaybeGUID : GlobalValue::getGUIDAssumingExternalLinkage(GV->getName());
9854+
GUID = MaybeGUID
9855+
? *MaybeGUID
9856+
: GlobalValue::getGUIDAssumingExternalLinkage(GV->getName());
98559857

98569858
VI = Index->getOrInsertValueInfo(GV, GUID);
98579859
} else {

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,8 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
978978
/// Callback to ask whether a symbol is the prevailing copy when invoked
979979
/// during combined index building.
980980
std::function<bool(StringRef)> IsPrevailing = nullptr;
981+
982+
/// Callback invoked whenever a new ValueInfo is generated.
981983
std::function<void(ValueInfo)> OnValueInfo = nullptr;
982984

983985
/// Saves the stack ids from the STACK_IDS record to consult when adding
@@ -993,9 +995,9 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
993995
/// list and used to avoid repeated hash lookups.
994996
std::vector<unsigned> StackIdToIndex;
995997

996-
/// A list of GUIDs defined by this module. Indexed by ValueID.
997-
std::vector<uint64_t> DefinedGUIDs;
998-
998+
/// A list of GUIDs defined by this module. Indexed by ValueID.
999+
std::vector<uint64_t> DefinedGUIDs;
1000+
9991001
public:
10001002
ModuleSummaryIndexBitcodeReader(
10011003
BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex,
@@ -4050,9 +4052,10 @@ Error BitcodeReader::globalCleanup() {
40504052

40514053
for (size_t ValueID = 0; ValueID < GUIDList.size(); ValueID++) {
40524054
const auto GUID = GUIDList[ValueID];
4053-
if (GUID == 0) continue;
4055+
if (GUID == 0)
4056+
continue;
40544057

4055-
const auto* Value = ValueList[ValueID];
4058+
const auto *Value = ValueList[ValueID];
40564059
TheModule->insertGUID(Value, GUID);
40574060
}
40584061

@@ -7282,13 +7285,14 @@ void ModuleSummaryIndexBitcodeReader::setValueGUID(
72827285
ValueGUID = DefinedGUIDs[ValueID];
72837286
if (ValueGUID == 0)
72847287
ValueGUID = GlobalValue::getGUIDAssumingExternalLinkage(
7285-
GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName));
7288+
GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName));
72867289

72877290
auto OriginalNameID = ValueGUID;
72887291
if (GlobalValue::isLocalLinkage(Linkage))
72897292
OriginalNameID = GlobalValue::getGUIDAssumingExternalLinkage(ValueName);
72907293
if (PrintSummaryGUIDs)
7291-
dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " << ValueName << "\n";
7294+
dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
7295+
<< ValueName << "\n";
72927296

72937297
// UseStrtab is false for legacy summary formats and value names are
72947298
// created on stack. In that case we save the name in a string saver in
@@ -8720,10 +8724,10 @@ BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,
87208724
// We don't use ModuleIdentifier here because the client may need to control the
87218725
// module path used in the combined summary (e.g. when reading summaries for
87228726
// regular LTO modules).
8723-
Error BitcodeModule::readSummary(
8724-
ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
8725-
std::function<bool(StringRef)> IsPrevailing,
8726-
std::function<void(ValueInfo)> OnValueInfo) {
8727+
Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex,
8728+
StringRef ModulePath,
8729+
std::function<bool(StringRef)> IsPrevailing,
8730+
std::function<void(ValueInfo)> OnValueInfo) {
87278731
BitstreamCursor Stream(Buffer);
87288732
if (Error JumpFailed = Stream.JumpToBit(ModuleBit))
87298733
return JumpFailed;

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,7 +4592,8 @@ void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
45924592

45934593
static GlobalValue::GUID getOrComputeGUID(const GlobalValue& V) {
45944594
auto MaybeGUID = V.getGUIDIfAssigned();
4595-
return MaybeGUID ? *MaybeGUID : GlobalValue::getGUIDAssumingExternalLinkage(V.getName());
4595+
return MaybeGUID ? *MaybeGUID
4596+
: GlobalValue::getGUIDAssumingExternalLinkage(V.getName());
45964597
}
45974598

45984599
// Collect the global value references in the given variable's initializer,
@@ -4919,7 +4920,8 @@ void ModuleBitcodeWriterBase::writeGUIDList() {
49194920
} else {
49204921
GUID = 0;
49214922
}
4922-
if (GUID == 0) continue;
4923+
if (GUID == 0)
4924+
continue;
49234925

49244926
const auto ValueID = VE.getValueID(&GV);
49254927
GUIDs[ValueID] = GUID;

llvm/lib/IR/Globals.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,23 @@ std::optional<GlobalValue::GUID> GlobalValue::getGUIDIfAssigned() const {
103103
auto *MD = getMetadata(LLVMContext::MD_unique_id);
104104
if (MD != nullptr)
105105
return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0))
106-
->getValue()
107-
->stripPointerCasts())
106+
->getValue()
107+
->stripPointerCasts())
108108
->getZExtValue();
109109

110110
// Handle a few special cases where we just want to compute it based on the
111111
// current properties.
112-
if (isDeclaration() || isa<GlobalAlias>(this) || getName().starts_with("llvm.")) {
112+
// TODO: Maybe we should use a more robust check for intrinsics than just
113+
// matching on the name?
114+
if (isDeclaration() || isa<GlobalAlias>(this) ||
115+
getName().starts_with("llvm.")) {
113116
return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier());
114117
}
115-
118+
116119
// Otherwise we try to look it up in the module, for cases where we've read
117120
// the GUID table but not the metadata.
118-
if (getParent() == nullptr) return {};
121+
if (getParent() == nullptr)
122+
return {};
119123
const Module &M = *getParent();
120124

121125
return M.getGUID(this);

llvm/lib/LTO/LTO.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,7 @@ LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
12091209
// Find the summary in the module for this very GV and record the new
12101210
// linkage so that we can switch it when we import the GV.
12111211
if (R.LinkerRedefined)
1212-
if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(
1213-
GUID, BMID))
1212+
if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID))
12141213
S->setLinkage(GlobalValue::WeakAnyLinkage);
12151214
}
12161215

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
609609

610610
for (auto &GV : Mod.global_values()) {
611611
auto GUID = Mod.getGUID(&GV);
612-
if (!GUID) continue;
612+
if (!GUID)
613+
continue;
613614

614615
if (GlobalValueSummary *GVS = DefinedGlobals.lookup(*GUID))
615616
if (!Index.isGlobalValueLive(GVS)) {

0 commit comments

Comments
 (0)