Skip to content

Commit

Permalink
[AsmParser] Gracefully handle non-existent GV summary reference
Browse files Browse the repository at this point in the history
If the module summary references a global variable that does not
exist, throw a nice error instead of asserting.

Fixes llvm#74726.
  • Loading branch information
nikic committed Dec 7, 2023
1 parent 04c4566 commit dfd36aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
5 changes: 3 additions & 2 deletions llvm/include/llvm/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,10 @@ namespace llvm {
std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
&ResByArg);
bool parseArgs(std::vector<uint64_t> &Args);
void addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
bool addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
GlobalValue::LinkageTypes Linkage, unsigned ID,
std::unique_ptr<GlobalValueSummary> Summary);
std::unique_ptr<GlobalValueSummary> Summary,
LocTy Loc);
bool parseOptionalAllocs(std::vector<AllocInfo> &Allocs);
bool parseMemProfs(std::vector<MIBInfo> &MIBs);
bool parseAllocType(uint8_t &AllocType);
Expand Down
39 changes: 21 additions & 18 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8622,9 +8622,9 @@ static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {

/// Stores the given Name/GUID and associated summary into the Index.
/// Also updates any forward references to the associated entry ID.
void LLParser::addGlobalValueToIndex(
bool LLParser::addGlobalValueToIndex(
std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
unsigned ID, std::unique_ptr<GlobalValueSummary> Summary) {
unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
// First create the ValueInfo utilizing the Name or GUID.
ValueInfo VI;
if (GUID != 0) {
Expand All @@ -8634,7 +8634,9 @@ void LLParser::addGlobalValueToIndex(
assert(!Name.empty());
if (M) {
auto *GV = M->getNamedValue(Name);
assert(GV);
if (!GV)
return error(Loc, "Reference to undefined global \"" + Name + "\"");

VI = Index->getOrInsertValueInfo(GV);
} else {
assert(
Expand Down Expand Up @@ -8682,6 +8684,8 @@ void LLParser::addGlobalValueToIndex(
NumberedValueInfos.resize(ID + 1);
NumberedValueInfos[ID] = VI;
}

return false;
}

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

LocTy Loc = Lex.getLoc();
std::string Name;
GlobalValue::GUID GUID = 0;
switch (Lex.getKind()) {
Expand Down Expand Up @@ -8757,9 +8762,8 @@ bool LLParser::parseGVEntry(unsigned ID) {
// an external definition. We pass ExternalLinkage since that is only
// used when the GUID must be computed from Name, and in that case
// the symbol must have external linkage.
addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
nullptr);
return false;
return addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
nullptr, Loc);
}

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

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

FS->setModulePath(ModulePath);

addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
ID, std::move(FS));

return false;
return addGlobalValueToIndex(Name, GUID,
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
std::move(FS), Loc);
}

/// VariableSummary
/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
/// [',' OptionalRefs]? ')'
bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
unsigned ID) {
LocTy Loc = Lex.getLoc();
assert(Lex.getKind() == lltok::kw_variable);
Lex.Lex();

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

addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
ID, std::move(GS));

return false;
return addGlobalValueToIndex(Name, GUID,
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
std::move(GS), Loc);
}

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

addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
ID, std::move(AS));

return false;
return addGlobalValueToIndex(Name, GUID,
(GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
std::move(AS), Loc);
}

/// Flag
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/Assembler/summary-parsing-error.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; RUN: not llvm-as < %s 2>&1 | FileCheck %s

; CHECK: Reference to undefined global "does_not_exist"
^0 = gv: (name: "does_not_exist")

0 comments on commit dfd36aa

Please sign in to comment.