Skip to content

Commit 61aa4f4

Browse files
committed
[AsmParser] Supprt non-consecutive global value numbers
#78171 added support for local value numbers to have gaps. This extends the support for global value numbers (for globals and functions). This means that it is now possible to delete an unnamed global definition/declaration without breaking the IR. This is a lot less common than unnamed local values, but it seems like something we should support for consistency. (Unnamed globals are also common in rustc IR).
1 parent 4c2422e commit 61aa4f4

File tree

8 files changed

+167
-68
lines changed

8 files changed

+167
-68
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "LLLexer.h"
1717
#include "llvm/ADT/StringMap.h"
18+
#include "llvm/AsmParser/NumberedValues.h"
1819
#include "llvm/AsmParser/Parser.h"
1920
#include "llvm/IR/Attributes.h"
2021
#include "llvm/IR/FMF.h"
@@ -133,7 +134,7 @@ namespace llvm {
133134
// Global Value reference information.
134135
std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
135136
std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
136-
std::vector<GlobalValue*> NumberedVals;
137+
NumberedValues<GlobalValue *> NumberedVals;
137138

138139
// Comdat forward reference information.
139140
std::map<std::string, LocTy> ForwardRefComdats;
@@ -346,14 +347,15 @@ namespace llvm {
346347
bool parseGlobalType(bool &IsConstant);
347348
bool parseUnnamedGlobal();
348349
bool parseNamedGlobal();
349-
bool parseGlobal(const std::string &Name, LocTy NameLoc, unsigned Linkage,
350-
bool HasLinkage, unsigned Visibility,
350+
bool parseGlobal(const std::string &Name, unsigned NameID, LocTy NameLoc,
351+
unsigned Linkage, bool HasLinkage, unsigned Visibility,
351352
unsigned DLLStorageClass, bool DSOLocal,
352353
GlobalVariable::ThreadLocalMode TLM,
353354
GlobalVariable::UnnamedAddr UnnamedAddr);
354-
bool parseAliasOrIFunc(const std::string &Name, LocTy NameLoc, unsigned L,
355-
unsigned Visibility, unsigned DLLStorageClass,
356-
bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
355+
bool parseAliasOrIFunc(const std::string &Name, unsigned NameID,
356+
LocTy NameLoc, unsigned L, unsigned Visibility,
357+
unsigned DLLStorageClass, bool DSOLocal,
358+
GlobalVariable::ThreadLocalMode TLM,
357359
GlobalVariable::UnnamedAddr UnnamedAddr);
358360
bool parseComdat();
359361
bool parseStandaloneMetadata();
@@ -452,27 +454,13 @@ namespace llvm {
452454
bool parseFunctionType(Type *&Result);
453455
bool parseTargetExtType(Type *&Result);
454456

455-
class NumberedValues {
456-
DenseMap<unsigned, Value *> Vals;
457-
unsigned NextUnusedID = 0;
458-
459-
public:
460-
unsigned getNext() const { return NextUnusedID; }
461-
Value *get(unsigned ID) const { return Vals.lookup(ID); }
462-
void add(unsigned ID, Value *V) {
463-
assert(ID >= NextUnusedID && "Invalid value ID");
464-
Vals.insert({ID, V});
465-
NextUnusedID = ID + 1;
466-
}
467-
};
468-
469457
// Function Semantic Analysis.
470458
class PerFunctionState {
471459
LLParser &P;
472460
Function &F;
473461
std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
474462
std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
475-
NumberedValues NumberedVals;
463+
NumberedValues<Value *> NumberedVals;
476464

477465
/// FunctionNumber - If this is an unnamed function, this is the slot
478466
/// number of it, otherwise it is -1.
@@ -614,8 +602,10 @@ namespace llvm {
614602
SmallVectorImpl<unsigned> &UnnamedArgNums,
615603
bool &IsVarArg);
616604
bool parseFunctionHeader(Function *&Fn, bool IsDefine,
605+
unsigned &FunctionNumber,
617606
SmallVectorImpl<unsigned> &UnnamedArgNums);
618-
bool parseFunctionBody(Function &Fn, ArrayRef<unsigned> UnnamedArgNums);
607+
bool parseFunctionBody(Function &Fn, unsigned FunctionNumber,
608+
ArrayRef<unsigned> UnnamedArgNums);
619609
bool parseBasicBlock(PerFunctionState &PFS);
620610

621611
enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- NumberedValues.h - --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_ASMPARSER_NUMBEREDVALUES_H
10+
#define LLVM_ASMPARSER_NUMBEREDVALUES_H
11+
12+
#include "llvm/ADT/DenseMap.h"
13+
14+
namespace llvm {
15+
16+
/// Mapping from value ID to value, which also remembers what the next unused
17+
/// ID is.
18+
template <class T> class NumberedValues {
19+
DenseMap<unsigned, T> Vals;
20+
unsigned NextUnusedID = 0;
21+
22+
public:
23+
unsigned getNext() const { return NextUnusedID; }
24+
T get(unsigned ID) const { return Vals.lookup(ID); }
25+
void add(unsigned ID, T V) {
26+
assert(ID >= NextUnusedID && "Invalid value ID");
27+
Vals.insert({ID, V});
28+
NextUnusedID = ID + 1;
29+
}
30+
};
31+
32+
} // end namespace llvm
33+
34+
#endif

llvm/include/llvm/AsmParser/SlotMapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_ASMPARSER_SLOTMAPPING_H
1515

1616
#include "llvm/ADT/StringMap.h"
17+
#include "llvm/AsmParser/NumberedValues.h"
1718
#include "llvm/IR/TrackingMDRef.h"
1819
#include <map>
1920
#include <vector>
@@ -30,7 +31,7 @@ class Type;
3031
/// textual references to the values in the module can be parsed outside of the
3132
/// module's source.
3233
struct SlotMapping {
33-
std::vector<GlobalValue *> GlobalValues;
34+
NumberedValues<GlobalValue *> GlobalValues;
3435
std::map<unsigned, TrackingMDNodeRef> MetadataNodes;
3536
StringMap<Type *> NamedTypes;
3637
std::map<unsigned, Type *> Types;

0 commit comments

Comments
 (0)