Skip to content

[AsmParser] Support non-consecutive global value numbers #80013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions llvm/include/llvm/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "LLLexer.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/AsmParser/NumberedValues.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/FMF.h"
Expand Down Expand Up @@ -133,7 +134,7 @@ namespace llvm {
// Global Value reference information.
std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
std::vector<GlobalValue*> NumberedVals;
NumberedValues<GlobalValue *> NumberedVals;

// Comdat forward reference information.
std::map<std::string, LocTy> ForwardRefComdats;
Expand Down Expand Up @@ -346,14 +347,15 @@ namespace llvm {
bool parseGlobalType(bool &IsConstant);
bool parseUnnamedGlobal();
bool parseNamedGlobal();
bool parseGlobal(const std::string &Name, LocTy NameLoc, unsigned Linkage,
bool HasLinkage, unsigned Visibility,
bool parseGlobal(const std::string &Name, unsigned NameID, LocTy NameLoc,
unsigned Linkage, bool HasLinkage, unsigned Visibility,
unsigned DLLStorageClass, bool DSOLocal,
GlobalVariable::ThreadLocalMode TLM,
GlobalVariable::UnnamedAddr UnnamedAddr);
bool parseAliasOrIFunc(const std::string &Name, LocTy NameLoc, unsigned L,
unsigned Visibility, unsigned DLLStorageClass,
bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
bool parseAliasOrIFunc(const std::string &Name, unsigned NameID,
LocTy NameLoc, unsigned L, unsigned Visibility,
unsigned DLLStorageClass, bool DSOLocal,
GlobalVariable::ThreadLocalMode TLM,
GlobalVariable::UnnamedAddr UnnamedAddr);
bool parseComdat();
bool parseStandaloneMetadata();
Expand Down Expand Up @@ -452,27 +454,13 @@ namespace llvm {
bool parseFunctionType(Type *&Result);
bool parseTargetExtType(Type *&Result);

class NumberedValues {
DenseMap<unsigned, Value *> Vals;
unsigned NextUnusedID = 0;

public:
unsigned getNext() const { return NextUnusedID; }
Value *get(unsigned ID) const { return Vals.lookup(ID); }
void add(unsigned ID, Value *V) {
assert(ID >= NextUnusedID && "Invalid value ID");
Vals.insert({ID, V});
NextUnusedID = ID + 1;
}
};

// Function Semantic Analysis.
class PerFunctionState {
LLParser &P;
Function &F;
std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
NumberedValues NumberedVals;
NumberedValues<Value *> NumberedVals;

/// FunctionNumber - If this is an unnamed function, this is the slot
/// number of it, otherwise it is -1.
Expand Down Expand Up @@ -614,8 +602,10 @@ namespace llvm {
SmallVectorImpl<unsigned> &UnnamedArgNums,
bool &IsVarArg);
bool parseFunctionHeader(Function *&Fn, bool IsDefine,
unsigned &FunctionNumber,
SmallVectorImpl<unsigned> &UnnamedArgNums);
bool parseFunctionBody(Function &Fn, ArrayRef<unsigned> UnnamedArgNums);
bool parseFunctionBody(Function &Fn, unsigned FunctionNumber,
ArrayRef<unsigned> UnnamedArgNums);
bool parseBasicBlock(PerFunctionState &PFS);

enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
Expand Down
34 changes: 34 additions & 0 deletions llvm/include/llvm/AsmParser/NumberedValues.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- NumberedValues.h - --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ASMPARSER_NUMBEREDVALUES_H
#define LLVM_ASMPARSER_NUMBEREDVALUES_H

#include "llvm/ADT/DenseMap.h"

namespace llvm {

/// Mapping from value ID to value, which also remembers what the next unused
/// ID is.
template <class T> class NumberedValues {
DenseMap<unsigned, T> Vals;
unsigned NextUnusedID = 0;

public:
unsigned getNext() const { return NextUnusedID; }
T get(unsigned ID) const { return Vals.lookup(ID); }
void add(unsigned ID, T V) {
assert(ID >= NextUnusedID && "Invalid value ID");
Vals.insert({ID, V});
NextUnusedID = ID + 1;
}
};

} // end namespace llvm

#endif
3 changes: 2 additions & 1 deletion llvm/include/llvm/AsmParser/SlotMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define LLVM_ASMPARSER_SLOTMAPPING_H

#include "llvm/ADT/StringMap.h"
#include "llvm/AsmParser/NumberedValues.h"
#include "llvm/IR/TrackingMDRef.h"
#include <map>
#include <vector>
Expand All @@ -30,7 +31,7 @@ class Type;
/// textual references to the values in the module can be parsed outside of the
/// module's source.
struct SlotMapping {
std::vector<GlobalValue *> GlobalValues;
NumberedValues<GlobalValue *> GlobalValues;
std::map<unsigned, TrackingMDNodeRef> MetadataNodes;
StringMap<Type *> NamedTypes;
std::map<unsigned, Type *> Types;
Expand Down
Loading