Skip to content

Commit dfd36aa

Browse files
committed
[AsmParser] Gracefully handle non-existent GV summary reference
If the module summary references a global variable that does not exist, throw a nice error instead of asserting. Fixes #74726.
1 parent 04c4566 commit dfd36aa

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,10 @@ namespace llvm {
407407
std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
408408
&ResByArg);
409409
bool parseArgs(std::vector<uint64_t> &Args);
410-
void addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
410+
bool addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
411411
GlobalValue::LinkageTypes Linkage, unsigned ID,
412-
std::unique_ptr<GlobalValueSummary> Summary);
412+
std::unique_ptr<GlobalValueSummary> Summary,
413+
LocTy Loc);
413414
bool parseOptionalAllocs(std::vector<AllocInfo> &Allocs);
414415
bool parseMemProfs(std::vector<MIBInfo> &MIBs);
415416
bool parseAllocType(uint8_t &AllocType);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8622,9 +8622,9 @@ static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
86228622

86238623
/// Stores the given Name/GUID and associated summary into the Index.
86248624
/// Also updates any forward references to the associated entry ID.
8625-
void LLParser::addGlobalValueToIndex(
8625+
bool LLParser::addGlobalValueToIndex(
86268626
std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
8627-
unsigned ID, std::unique_ptr<GlobalValueSummary> Summary) {
8627+
unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
86288628
// First create the ValueInfo utilizing the Name or GUID.
86298629
ValueInfo VI;
86308630
if (GUID != 0) {
@@ -8634,7 +8634,9 @@ void LLParser::addGlobalValueToIndex(
86348634
assert(!Name.empty());
86358635
if (M) {
86368636
auto *GV = M->getNamedValue(Name);
8637-
assert(GV);
8637+
if (!GV)
8638+
return error(Loc, "Reference to undefined global \"" + Name + "\"");
8639+
86388640
VI = Index->getOrInsertValueInfo(GV);
86398641
} else {
86408642
assert(
@@ -8682,6 +8684,8 @@ void LLParser::addGlobalValueToIndex(
86828684
NumberedValueInfos.resize(ID + 1);
86838685
NumberedValueInfos[ID] = VI;
86848686
}
8687+
8688+
return false;
86858689
}
86868690

86878691
/// parseSummaryIndexFlags
@@ -8728,6 +8732,7 @@ bool LLParser::parseGVEntry(unsigned ID) {
87288732
parseToken(lltok::lparen, "expected '(' here"))
87298733
return true;
87308734

8735+
LocTy Loc = Lex.getLoc();
87318736
std::string Name;
87328737
GlobalValue::GUID GUID = 0;
87338738
switch (Lex.getKind()) {
@@ -8757,9 +8762,8 @@ bool LLParser::parseGVEntry(unsigned ID) {
87578762
// an external definition. We pass ExternalLinkage since that is only
87588763
// used when the GUID must be computed from Name, and in that case
87598764
// the symbol must have external linkage.
8760-
addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
8761-
nullptr);
8762-
return false;
8765+
return addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
8766+
nullptr, Loc);
87638767
}
87648768

87658769
// Have a list of summaries
@@ -8800,6 +8804,7 @@ bool LLParser::parseGVEntry(unsigned ID) {
88008804
/// [',' OptionalRefs]? ')'
88018805
bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
88028806
unsigned ID) {
8807+
LocTy Loc = Lex.getLoc();
88038808
assert(Lex.getKind() == lltok::kw_function);
88048809
Lex.Lex();
88058810

@@ -8876,17 +8881,17 @@ bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
88768881

88778882
FS->setModulePath(ModulePath);
88788883

8879-
addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8880-
ID, std::move(FS));
8881-
8882-
return false;
8884+
return addGlobalValueToIndex(Name, GUID,
8885+
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
8886+
std::move(FS), Loc);
88838887
}
88848888

88858889
/// VariableSummary
88868890
/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
88878891
/// [',' OptionalRefs]? ')'
88888892
bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
88898893
unsigned ID) {
8894+
LocTy Loc = Lex.getLoc();
88908895
assert(Lex.getKind() == lltok::kw_variable);
88918896
Lex.Lex();
88928897

@@ -8934,10 +8939,9 @@ bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
89348939
GS->setModulePath(ModulePath);
89358940
GS->setVTableFuncs(std::move(VTableFuncs));
89368941

8937-
addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8938-
ID, std::move(GS));
8939-
8940-
return false;
8942+
return addGlobalValueToIndex(Name, GUID,
8943+
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
8944+
std::move(GS), Loc);
89418945
}
89428946

89438947
/// AliasSummary
@@ -8984,10 +8988,9 @@ bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
89848988
AS->setAliasee(AliaseeVI, Summary);
89858989
}
89868990

8987-
addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8988-
ID, std::move(AS));
8989-
8990-
return false;
8991+
return addGlobalValueToIndex(Name, GUID,
8992+
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
8993+
std::move(AS), Loc);
89918994
}
89928995

89938996
/// Flag
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
3+
; CHECK: Reference to undefined global "does_not_exist"
4+
^0 = gv: (name: "does_not_exist")

0 commit comments

Comments
 (0)