Skip to content

Commit e2301d6

Browse files
authored
[TableGen] Reduce size of MatchTableRecord (NFC) (#125221)
MatchTableRecord stores a 64-bit RawValue. However, this field is only needed by a small part of the code (jump table generation). Create a separate RecordAndValue structure that is used in just the necessary places. Based on massif, this reduces memory usage on RISCVGenGlobalISel.inc by about 100MB (to 2.15GB).
1 parent 272ce90 commit e2301d6

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp

+17-29
Original file line numberDiff line numberDiff line change
@@ -227,26 +227,12 @@ MatchTableRecord MatchTable::NamedValue(unsigned NumBytes,
227227
MatchTableRecord::MTRF_CommaFollows);
228228
}
229229

230-
MatchTableRecord MatchTable::NamedValue(unsigned NumBytes, StringRef NamedValue,
231-
int64_t RawValue) {
232-
return MatchTableRecord(std::nullopt, NamedValue, NumBytes,
233-
MatchTableRecord::MTRF_CommaFollows, RawValue);
234-
}
235-
236230
MatchTableRecord MatchTable::NamedValue(unsigned NumBytes, StringRef Namespace,
237231
StringRef NamedValue) {
238232
return MatchTableRecord(std::nullopt, (Namespace + "::" + NamedValue).str(),
239233
NumBytes, MatchTableRecord::MTRF_CommaFollows);
240234
}
241235

242-
MatchTableRecord MatchTable::NamedValue(unsigned NumBytes, StringRef Namespace,
243-
StringRef NamedValue,
244-
int64_t RawValue) {
245-
return MatchTableRecord(std::nullopt, (Namespace + "::" + NamedValue).str(),
246-
NumBytes, MatchTableRecord::MTRF_CommaFollows,
247-
RawValue);
248-
}
249-
250236
MatchTableRecord MatchTable::IntValue(unsigned NumBytes, int64_t IntValue) {
251237
assert(isUIntN(NumBytes * 8, IntValue) || isIntN(NumBytes * 8, IntValue));
252238
auto Str = llvm::to_string(IntValue);
@@ -651,8 +637,8 @@ void SwitchMatcher::emit(MatchTable &Table) {
651637
[&Table]() { return Table.allocateLabelID(); });
652638
const unsigned Default = Table.allocateLabelID();
653639

654-
const int64_t LowerBound = Values.begin()->getRawValue();
655-
const int64_t UpperBound = Values.rbegin()->getRawValue() + 1;
640+
const int64_t LowerBound = Values.begin()->RawValue;
641+
const int64_t UpperBound = Values.rbegin()->RawValue + 1;
656642

657643
emitPredicateSpecificOpcodes(*Condition, Table);
658644

@@ -664,10 +650,11 @@ void SwitchMatcher::emit(MatchTable &Table) {
664650
auto VI = Values.begin();
665651
for (unsigned I = 0, E = Values.size(); I < E; ++I) {
666652
auto V = *VI++;
667-
while (J++ < V.getRawValue())
653+
while (J++ < V.RawValue)
668654
Table << MatchTable::IntValue(4, 0);
669-
V.turnIntoComment();
670-
Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]);
655+
V.Record.turnIntoComment();
656+
Table << MatchTable::LineBreak << V.Record
657+
<< MatchTable::JumpTarget(LabelIDs[I]);
671658
}
672659
Table << MatchTable::LineBreak;
673660

@@ -1145,11 +1132,11 @@ void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table,
11451132

11461133
std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues;
11471134

1148-
MatchTableRecord LLTOperandMatcher::getValue() const {
1135+
RecordAndValue LLTOperandMatcher::getValue() const {
11491136
const auto VI = TypeIDValues.find(Ty);
11501137
if (VI == TypeIDValues.end())
11511138
return MatchTable::NamedValue(1, getTy().getCxxEnumValue());
1152-
return MatchTable::NamedValue(1, getTy().getCxxEnumValue(), VI->second);
1139+
return {MatchTable::NamedValue(1, getTy().getCxxEnumValue()), VI->second};
11531140
}
11541141

11551142
bool LLTOperandMatcher::hasValue() const {
@@ -1167,7 +1154,8 @@ void LLTOperandMatcher::emitPredicateOpcodes(MatchTable &Table,
11671154
<< MatchTable::ULEB128Value(InsnVarID);
11681155
}
11691156
Table << MatchTable::Comment("Op") << MatchTable::ULEB128Value(OpIdx)
1170-
<< MatchTable::Comment("Type") << getValue() << MatchTable::LineBreak;
1157+
<< MatchTable::Comment("Type") << getValue().Record
1158+
<< MatchTable::LineBreak;
11711159
}
11721160

11731161
//===- PointerToAnyOperandMatcher -----------------------------------------===//
@@ -1411,12 +1399,12 @@ Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy,
14111399
DenseMap<const CodeGenInstruction *, unsigned>
14121400
InstructionOpcodeMatcher::OpcodeValues;
14131401

1414-
MatchTableRecord
1402+
RecordAndValue
14151403
InstructionOpcodeMatcher::getInstValue(const CodeGenInstruction *I) const {
14161404
const auto VI = OpcodeValues.find(I);
14171405
if (VI != OpcodeValues.end())
1418-
return MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName(),
1419-
VI->second);
1406+
return {MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName()),
1407+
VI->second};
14201408
return MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName());
14211409
}
14221410

@@ -1428,14 +1416,14 @@ void InstructionOpcodeMatcher::initOpcodeValuesMap(
14281416
OpcodeValues[I] = Target.getInstrIntValue(I->TheDef);
14291417
}
14301418

1431-
MatchTableRecord InstructionOpcodeMatcher::getValue() const {
1419+
RecordAndValue InstructionOpcodeMatcher::getValue() const {
14321420
assert(Insts.size() == 1);
14331421

14341422
const CodeGenInstruction *I = Insts[0];
14351423
const auto VI = OpcodeValues.find(I);
14361424
if (VI != OpcodeValues.end())
1437-
return MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName(),
1438-
VI->second);
1425+
return {MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName()),
1426+
VI->second};
14391427
return MatchTable::NamedValue(2, I->Namespace, I->TheDef->getName());
14401428
}
14411429

@@ -1447,7 +1435,7 @@ void InstructionOpcodeMatcher::emitPredicateOpcodes(MatchTable &Table,
14471435
<< MatchTable::ULEB128Value(InsnVarID);
14481436

14491437
for (const CodeGenInstruction *I : Insts)
1450-
Table << getInstValue(I);
1438+
Table << getInstValue(I).Record;
14511439
Table << MatchTable::LineBreak;
14521440
}
14531441

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h

+21-21
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,10 @@ struct MatchTableRecord {
145145
/// A bitfield of RecordFlagsBits flags.
146146
unsigned Flags;
147147

148-
/// The actual run-time value, if known
149-
int64_t RawValue;
150-
151148
MatchTableRecord(std::optional<unsigned> LabelID_, StringRef EmitStr,
152-
unsigned NumElements, unsigned Flags,
153-
int64_t RawValue = std::numeric_limits<int64_t>::min())
149+
unsigned NumElements, unsigned Flags)
154150
: LabelID(LabelID_.value_or(~0u)), EmitStr(EmitStr),
155-
NumElements(NumElements), Flags(Flags), RawValue(RawValue) {
151+
NumElements(NumElements), Flags(Flags) {
156152
assert((!LabelID_ || LabelID != ~0u) &&
157153
"This value is reserved for non-labels");
158154
}
@@ -166,12 +162,6 @@ struct MatchTableRecord {
166162
NumElements = 0;
167163
}
168164

169-
/// For Jump Table generation purposes
170-
bool operator<(const MatchTableRecord &Other) const {
171-
return RawValue < Other.RawValue;
172-
}
173-
int64_t getRawValue() const { return RawValue; }
174-
175165
void emit(raw_ostream &OS, bool LineBreakNextAfterThis,
176166
const MatchTable &Table) const;
177167
unsigned size() const { return NumElements; }
@@ -202,12 +192,8 @@ class MatchTable {
202192
static MatchTableRecord Comment(StringRef Comment);
203193
static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0);
204194
static MatchTableRecord NamedValue(unsigned NumBytes, StringRef NamedValue);
205-
static MatchTableRecord NamedValue(unsigned NumBytes, StringRef NamedValue,
206-
int64_t RawValue);
207195
static MatchTableRecord NamedValue(unsigned NumBytes, StringRef Namespace,
208196
StringRef NamedValue);
209-
static MatchTableRecord NamedValue(unsigned NumBytes, StringRef Namespace,
210-
StringRef NamedValue, int64_t RawValue);
211197
static MatchTableRecord IntValue(unsigned NumBytes, int64_t IntValue);
212198
static MatchTableRecord ULEB128Value(uint64_t IntValue);
213199
static MatchTableRecord Label(unsigned LabelID);
@@ -400,6 +386,20 @@ class GroupMatcher final : public Matcher {
400386
bool candidateConditionMatches(const PredicateMatcher &Predicate) const;
401387
};
402388

389+
/// MatchTableRecord and associated value, for jump table generation.
390+
struct RecordAndValue {
391+
MatchTableRecord Record;
392+
int64_t RawValue;
393+
394+
RecordAndValue(MatchTableRecord Record,
395+
int64_t RawValue = std::numeric_limits<int64_t>::min())
396+
: Record(std::move(Record)), RawValue(RawValue) {}
397+
398+
bool operator<(const RecordAndValue &Other) const {
399+
return RawValue < Other.RawValue;
400+
}
401+
};
402+
403403
class SwitchMatcher : public Matcher {
404404
/// All the nested matchers, representing distinct switch-cases. The first
405405
/// conditions (as Matcher::getFirstCondition() reports) of all the nested
@@ -414,7 +414,7 @@ class SwitchMatcher : public Matcher {
414414

415415
/// Temporary set used to check that the case values don't repeat within the
416416
/// same switch.
417-
std::set<MatchTableRecord> Values;
417+
std::set<RecordAndValue> Values;
418418

419419
/// An owning collection for any auxiliary matchers created while optimizing
420420
/// nested matchers contained.
@@ -874,7 +874,7 @@ class PredicateMatcher {
874874
return hasValue() && PredicateMatcher::isIdentical(B);
875875
}
876876

877-
virtual MatchTableRecord getValue() const {
877+
virtual RecordAndValue getValue() const {
878878
assert(hasValue() && "Can not get a value of a value-less predicate!");
879879
llvm_unreachable("Not implemented yet");
880880
}
@@ -968,7 +968,7 @@ class LLTOperandMatcher : public OperandPredicateMatcher {
968968
Ty == cast<LLTOperandMatcher>(&B)->Ty;
969969
}
970970

971-
MatchTableRecord getValue() const override;
971+
RecordAndValue getValue() const override;
972972
bool hasValue() const override;
973973

974974
LLTCodeGen getTy() const { return Ty; }
@@ -1378,7 +1378,7 @@ class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
13781378

13791379
static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues;
13801380

1381-
MatchTableRecord getInstValue(const CodeGenInstruction *I) const;
1381+
RecordAndValue getInstValue(const CodeGenInstruction *I) const;
13821382

13831383
public:
13841384
static void initOpcodeValuesMap(const CodeGenTarget &Target);
@@ -1405,7 +1405,7 @@ class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
14051405

14061406
// TODO: This is used for the SwitchMatcher optimization. We should be able to
14071407
// return a list of the opcodes to match.
1408-
MatchTableRecord getValue() const override;
1408+
RecordAndValue getValue() const override;
14091409

14101410
void emitPredicateOpcodes(MatchTable &Table,
14111411
RuleMatcher &Rule) const override;

0 commit comments

Comments
 (0)