Skip to content

[TableGen] Migrate CodeGenHWModes to use const RecordKeeper #107851

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
Sep 9, 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
8 changes: 4 additions & 4 deletions llvm/utils/TableGen/CodeEmitterGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CodeEmitterGen {
int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
std::pair<std::string, std::string>
getInstructionCases(Record *R, CodeGenTarget &Target);
void addInstructionCasesForEncoding(Record *R, Record *EncodingDef,
void addInstructionCasesForEncoding(Record *R, const Record *EncodingDef,
CodeGenTarget &Target, std::string &Case,
std::string &BitOffsetCase);
bool addCodeToMergeInOperand(Record *R, BitsInit *BI,
Expand Down Expand Up @@ -342,8 +342,8 @@ CodeEmitterGen::getInstructionCases(Record *R, CodeGenTarget &Target) {
}

void CodeEmitterGen::addInstructionCasesForEncoding(
Record *R, Record *EncodingDef, CodeGenTarget &Target, std::string &Case,
std::string &BitOffsetCase) {
Record *R, const Record *EncodingDef, CodeGenTarget &Target,
std::string &Case, std::string &BitOffsetCase) {
BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst");

// Loop over all of the fields in the instruction, determining which are the
Expand Down Expand Up @@ -413,7 +413,7 @@ void CodeEmitterGen::emitInstructionBaseValues(
continue;
}

Record *EncodingDef = R;
const Record *EncodingDef = R;
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
Expand Down
17 changes: 8 additions & 9 deletions llvm/utils/TableGen/Common/CodeGenHwModes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ using namespace llvm;

StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";

HwMode::HwMode(Record *R) {
HwMode::HwMode(const Record *R) {
Name = R->getName();
Features = std::string(R->getValueAsString("Features"));

std::vector<Record *> PredicateRecs = R->getValueAsListOfDefs("Predicates");
SmallString<128> PredicateCheck;
raw_svector_ostream OS(PredicateCheck);
ListSeparator LS(" && ");
for (Record *Pred : PredicateRecs) {
for (const Record *Pred : R->getValueAsListOfDefs("Predicates")) {
StringRef CondString = Pred->getValueAsString("CondString");
if (CondString.empty())
continue;
Expand All @@ -39,7 +38,7 @@ HwMode::HwMode(Record *R) {
LLVM_DUMP_METHOD
void HwMode::dump() const { dbgs() << Name << ": " << Features << '\n'; }

HwModeSelect::HwModeSelect(Record *R, CodeGenHwModes &CGH) {
HwModeSelect::HwModeSelect(const Record *R, CodeGenHwModes &CGH) {
std::vector<Record *> Modes = R->getValueAsListOfDefs("Modes");
std::vector<Record *> Objects = R->getValueAsListOfDefs("Objects");
if (Modes.size() != Objects.size()) {
Expand All @@ -64,8 +63,8 @@ void HwModeSelect::dump() const {
dbgs() << " }\n";
}

CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {
for (Record *R : Records.getAllDerivedDefinitions("HwMode")) {
CodeGenHwModes::CodeGenHwModes(const RecordKeeper &RK) : Records(RK) {
for (const Record *R : Records.getAllDerivedDefinitions("HwMode")) {
// The default mode needs a definition in the .td sources for TableGen
// to accept references to it. We need to ignore the definition here.
if (R->getName() == DefaultModeName)
Expand All @@ -76,22 +75,22 @@ CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {

assert(Modes.size() <= 32 && "number of HwModes exceeds maximum of 32");

for (Record *R : Records.getAllDerivedDefinitions("HwModeSelect")) {
for (const Record *R : Records.getAllDerivedDefinitions("HwModeSelect")) {
auto P = ModeSelects.emplace(std::pair(R, HwModeSelect(R, *this)));
assert(P.second);
(void)P;
}
}

unsigned CodeGenHwModes::getHwModeId(Record *R) const {
unsigned CodeGenHwModes::getHwModeId(const Record *R) const {
if (R->getName() == DefaultModeName)
return DefaultMode;
auto F = ModeIds.find(R);
assert(F != ModeIds.end() && "Unknown mode name");
return F->second;
}

const HwModeSelect &CodeGenHwModes::getHwModeSelect(Record *R) const {
const HwModeSelect &CodeGenHwModes::getHwModeSelect(const Record *R) const {
auto F = ModeSelects.find(R);
assert(F != ModeSelects.end() && "Record is not a \"mode select\"");
return F->second;
Expand Down
20 changes: 10 additions & 10 deletions llvm/utils/TableGen/Common/CodeGenHwModes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ class RecordKeeper;
struct CodeGenHwModes;

struct HwMode {
HwMode(Record *R);
HwMode(const Record *R);
StringRef Name;
std::string Features;
std::string Predicates;
void dump() const;
};

struct HwModeSelect {
HwModeSelect(Record *R, CodeGenHwModes &CGH);
typedef std::pair<unsigned, Record *> PairType;
HwModeSelect(const Record *R, CodeGenHwModes &CGH);
typedef std::pair<unsigned, const Record *> PairType;
std::vector<PairType> Items;
void dump() const;
};
Expand All @@ -46,8 +46,8 @@ struct CodeGenHwModes {
enum : unsigned { DefaultMode = 0 };
static StringRef DefaultModeName;

CodeGenHwModes(RecordKeeper &R);
unsigned getHwModeId(Record *R) const;
CodeGenHwModes(const RecordKeeper &R);
unsigned getHwModeId(const Record *R) const;
const HwMode &getMode(unsigned Id) const {
assert(Id != 0 && "Mode id of 0 is reserved for the default mode");
return Modes[Id - 1];
Expand All @@ -57,18 +57,18 @@ struct CodeGenHwModes {
return DefaultModeName;
return getMode(Id).Name;
}
const HwModeSelect &getHwModeSelect(Record *R) const;
const std::map<Record *, HwModeSelect> &getHwModeSelects() const {
const HwModeSelect &getHwModeSelect(const Record *R) const;
const std::map<const Record *, HwModeSelect> &getHwModeSelects() const {
return ModeSelects;
}
unsigned getNumModeIds() const { return Modes.size() + 1; }
void dump() const;

private:
RecordKeeper &Records;
DenseMap<Record *, unsigned> ModeIds; // HwMode Record -> HwModeId
const RecordKeeper &Records;
DenseMap<const Record *, unsigned> ModeIds; // HwMode Record -> HwModeId
std::vector<HwMode> Modes;
std::map<Record *, HwModeSelect> ModeSelects;
std::map<const Record *, HwModeSelect> ModeSelects;
};
} // namespace llvm

Expand Down
12 changes: 7 additions & 5 deletions llvm/utils/TableGen/Common/InfoByHwMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,
return ValueTypeByHwMode(Rec, llvm::getValueType(Rec));
}

RegSizeInfo::RegSizeInfo(Record *R) {
RegSizeInfo::RegSizeInfo(const Record *R) {
RegSize = R->getValueAsInt("RegSize");
SpillSize = R->getValueAsInt("SpillSize");
SpillAlignment = R->getValueAsInt("SpillAlignment");
Expand All @@ -136,7 +136,8 @@ void RegSizeInfo::writeToStream(raw_ostream &OS) const {
<< ']';
}

RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH) {
RegSizeInfoByHwMode::RegSizeInfoByHwMode(const Record *R,
const CodeGenHwModes &CGH) {
const HwModeSelect &MS = CGH.getHwModeSelect(R);
for (const HwModeSelect::PairType &P : MS.Items) {
auto I = Map.insert({P.first, RegSizeInfo(P.second)});
Expand Down Expand Up @@ -183,12 +184,13 @@ void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
OS << '}';
}

SubRegRange::SubRegRange(Record *R) {
SubRegRange::SubRegRange(const Record *R) {
Size = R->getValueAsInt("Size");
Offset = R->getValueAsInt("Offset");
}

SubRegRangeByHwMode::SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH) {
SubRegRangeByHwMode::SubRegRangeByHwMode(const Record *R,
const CodeGenHwModes &CGH) {
const HwModeSelect &MS = CGH.getHwModeSelect(R);
for (const HwModeSelect::PairType &P : MS.Items) {
auto I = Map.insert({P.first, SubRegRange(P.second)});
Expand All @@ -197,7 +199,7 @@ SubRegRangeByHwMode::SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH) {
}
}

EncodingInfoByHwMode::EncodingInfoByHwMode(Record *R,
EncodingInfoByHwMode::EncodingInfoByHwMode(const Record *R,
const CodeGenHwModes &CGH) {
const HwModeSelect &MS = CGH.getHwModeSelect(R);
for (const HwModeSelect::PairType &P : MS.Items) {
Expand Down
12 changes: 6 additions & 6 deletions llvm/utils/TableGen/Common/InfoByHwMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct RegSizeInfo {
unsigned SpillSize;
unsigned SpillAlignment;

RegSizeInfo(Record *R);
RegSizeInfo(const Record *R);
RegSizeInfo() = default;
bool operator<(const RegSizeInfo &I) const;
bool operator==(const RegSizeInfo &I) const {
Expand All @@ -197,7 +197,7 @@ struct RegSizeInfo {
};

struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> {
RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
RegSizeInfoByHwMode(const Record *R, const CodeGenHwModes &CGH);
RegSizeInfoByHwMode() = default;
bool operator<(const RegSizeInfoByHwMode &VI) const;
bool operator==(const RegSizeInfoByHwMode &VI) const;
Expand All @@ -222,12 +222,12 @@ struct SubRegRange {
uint16_t Size;
uint16_t Offset;

SubRegRange(Record *R);
SubRegRange(const Record *R);
SubRegRange(uint16_t Size, uint16_t Offset) : Size(Size), Offset(Offset) {}
};

struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> {
SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH);
SubRegRangeByHwMode(const Record *R, const CodeGenHwModes &CGH);
SubRegRangeByHwMode(SubRegRange Range) { Map.insert({DefaultMode, Range}); }
SubRegRangeByHwMode() = default;

Expand All @@ -236,8 +236,8 @@ struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> {
}
};

struct EncodingInfoByHwMode : public InfoByHwMode<Record *> {
EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
struct EncodingInfoByHwMode : public InfoByHwMode<const Record *> {
EncodingInfoByHwMode(const Record *R, const CodeGenHwModes &CGH);
EncodingInfoByHwMode() = default;
};

Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
for (auto &KV : EBM) {
AltEncodingTy Mode = KV.first;
Modes.insert({Mode, "_" + HWM.getMode(Mode).Name.str()});
Record *EncodingDef = KV.second;
RecordVal *RV = EncodingDef->getValue("Inst");
const Record *EncodingDef = KV.second;
const RecordVal *RV = EncodingDef->getValue("Inst");
DagInit *DI = cast<DagInit>(RV->getValue());
VarLenInsts[R].insert({Mode, VarLenInst(DI, RV)});
}
Expand Down
Loading