Skip to content

Commit 68383aa

Browse files
committed
use map for numbered values, remove skip restriction
1 parent f21b4bd commit 68383aa

File tree

3 files changed

+24
-53
lines changed

3 files changed

+24
-53
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,27 @@ namespace llvm {
451451
bool parseFunctionType(Type *&Result);
452452
bool parseTargetExtType(Type *&Result);
453453

454+
class NumberedValues {
455+
DenseMap<unsigned, Value *> Vals;
456+
unsigned NextUnusedID = 0;
457+
458+
public:
459+
unsigned getNext() const { return NextUnusedID; }
460+
Value *get(unsigned ID) const { return Vals.lookup(ID); }
461+
void add(unsigned ID, Value *V) {
462+
assert(ID >= NextUnusedID && "Invalid value ID");
463+
Vals.insert({ID, V});
464+
NextUnusedID = ID + 1;
465+
}
466+
};
467+
454468
// Function Semantic Analysis.
455469
class PerFunctionState {
456470
LLParser &P;
457471
Function &F;
458472
std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
459473
std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
460-
std::vector<Value*> NumberedVals;
474+
NumberedValues NumberedVals;
461475

462476
/// FunctionNumber - If this is an unnamed function, this is the slot
463477
/// number of it, otherwise it is -1.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454

5555
using namespace llvm;
5656

57-
// Avoid large gaps in the NumberedVals vector.
58-
static const size_t MaxNumberedValSkip = 1000;
59-
6057
static std::string getTypeString(Type *T) {
6158
std::string Result;
6259
raw_string_ostream Tmp(Result);
@@ -2937,10 +2934,6 @@ bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix,
29372934
return error(Loc, Kind + " expected to be numbered '" + Prefix +
29382935
Twine(NextID) + "' or greater");
29392936

2940-
if (ID > NextID + MaxNumberedValSkip)
2941-
return error(Loc, "value numbers can skip at most " +
2942-
Twine(MaxNumberedValSkip) + " values");
2943-
29442937
return false;
29452938
}
29462939

@@ -3304,8 +3297,7 @@ LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
33043297
for (Argument &A : F.args()) {
33053298
if (!A.hasName()) {
33063299
unsigned ArgNum = *It++;
3307-
NumberedVals.resize(ArgNum);
3308-
NumberedVals.push_back(&A);
3300+
NumberedVals.add(ArgNum, &A);
33093301
}
33103302
}
33113303
}
@@ -3388,7 +3380,7 @@ Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
33883380

33893381
Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
33903382
// Look this name up in the normal function symbol table.
3391-
Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
3383+
Value *Val = NumberedVals.get(ID);
33923384

33933385
// If this is a forward reference for the value, see if we already created a
33943386
// forward ref record.
@@ -3436,9 +3428,9 @@ bool LLParser::PerFunctionState::setInstName(int NameID,
34363428
if (NameStr.empty()) {
34373429
// If neither a name nor an ID was specified, just use the next ID.
34383430
if (NameID == -1)
3439-
NameID = NumberedVals.size();
3431+
NameID = NumberedVals.getNext();
34403432

3441-
if (P.checkValueID(NameLoc, "instruction", "%", NumberedVals.size(),
3433+
if (P.checkValueID(NameLoc, "instruction", "%", NumberedVals.getNext(),
34423434
NameID))
34433435
return true;
34443436

@@ -3455,10 +3447,7 @@ bool LLParser::PerFunctionState::setInstName(int NameID,
34553447
ForwardRefValIDs.erase(FI);
34563448
}
34573449

3458-
// Fill in skipped IDs using nullptr.
3459-
NumberedVals.resize(unsigned(NameID));
3460-
3461-
NumberedVals.push_back(Inst);
3450+
NumberedVals.add(NameID, Inst);
34623451
return false;
34633452
}
34643453

@@ -3506,15 +3495,14 @@ BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
35063495
BasicBlock *BB;
35073496
if (Name.empty()) {
35083497
if (NameID != -1) {
3509-
if (P.checkValueID(Loc, "label", "", NumberedVals.size(), NameID))
3498+
if (P.checkValueID(Loc, "label", "", NumberedVals.getNext(), NameID))
35103499
return nullptr;
35113500
} else {
3512-
NameID = NumberedVals.size();
3501+
NameID = NumberedVals.getNext();
35133502
}
35143503
BB = getBB(NameID, Loc);
35153504
if (!BB) {
3516-
P.error(Loc, "unable to create block numbered '" +
3517-
Twine(NumberedVals.size()) + "'");
3505+
P.error(Loc, "unable to create block numbered '" + Twine(NameID) + "'");
35183506
return nullptr;
35193507
}
35203508
} else {
@@ -3532,8 +3520,7 @@ BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
35323520
// Remove the block from forward ref sets.
35333521
if (Name.empty()) {
35343522
ForwardRefValIDs.erase(NameID);
3535-
NumberedVals.resize(NameID);
3536-
NumberedVals.push_back(BB);
3523+
NumberedVals.add(NameID, BB);
35373524
} else {
35383525
// BB forward references are already in the function symbol table.
35393526
ForwardRefVals.erase(Name);
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
; RUN: split-file %s %t
22
; RUN: not llvm-as < %s %t/instr_smaller_id.ll 2>&1 | FileCheck %s --check-prefix=INSTR-SMALLER-ID
3-
; RUN: not llvm-as < %s %t/instr_too_large_skip.ll 2>&1 | FileCheck %s --check-prefix=INSTR-TOO-LARGE-SKIP
43
; RUN: not llvm-as < %s %t/arg_smaller_id.ll 2>&1 | FileCheck %s --check-prefix=ARG-SMALLER-ID
5-
; RUN: not llvm-as < %s %t/arg_too_large_skip.ll 2>&1 | FileCheck %s --check-prefix=ARG-TOO-LARGE-SKIP
64
; RUN: not llvm-as < %s %t/block_smaller_id.ll 2>&1 | FileCheck %s --check-prefix=BLOCK-SMALLER-ID
7-
; RUN: not llvm-as < %s %t/block_too_large_skip.ll 2>&1 | FileCheck %s --check-prefix=BLOCK-TOO-LARGE-SKIP
85

96
;--- instr_smaller_id.ll
107

@@ -15,29 +12,13 @@ define i32 @test() {
1512
ret i32 %5
1613
}
1714

18-
;--- instr_too_large_skip.ll
19-
20-
; INSTR-TOO-LARGE-SKIP: error: value numbers can skip at most 1000 values
21-
define i32 @test() {
22-
%10 = add i32 1, 2
23-
%1000000 = add i32 %10, 3
24-
ret i32 %1000000
25-
}
26-
2715
;--- arg_smaller_id.ll
2816

2917
; ARG-SMALLER-ID: error: argument expected to be numbered '%11' or greater
3018
define i32 @test(i32 %10, i32 %5) {
3119
ret i32 %5
3220
}
3321

34-
;--- arg_too_large_skip.ll
35-
36-
; ARG-TOO-LARGE-SKIP: error: value numbers can skip at most 1000 values
37-
define i32 @test(i32 %10, i32 %1000000) {
38-
ret i32 %1000000
39-
}
40-
4122
;--- block_smaller_id.ll
4223

4324
; BLOCK-SMALLER-ID: error: label expected to be numbered '11' or greater
@@ -48,14 +29,3 @@ define i32 @test() {
4829
5:
4930
ret i32 0
5031
}
51-
52-
;--- block_too_large_skip.ll
53-
54-
; BLOCK-TOO-LARGE-SKIP: error: value numbers can skip at most 1000 values
55-
define i32 @test() {
56-
10:
57-
br label %1000000
58-
59-
1000000:
60-
ret i32 0
61-
}

0 commit comments

Comments
 (0)