Skip to content

[LLVM][TableGen] Change GlobalISelEmitter to use const RecordKeeper #110109

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 30, 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
12 changes: 6 additions & 6 deletions llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,9 @@ void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) {
RM.getGISelFlags());
}

void RuleMatcher::definePhysRegOperand(Record *Reg, OperandMatcher &OM) {
if (!PhysRegOperands.contains(Reg)) {
void RuleMatcher::definePhysRegOperand(const Record *Reg, OperandMatcher &OM) {
if (!PhysRegOperands.contains(Reg))
PhysRegOperands[Reg] = &OM;
return;
}
}

InstructionMatcher &
Expand All @@ -904,7 +902,8 @@ RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const {
("Failed to lookup instruction " + SymbolicName).str().c_str());
}

const OperandMatcher &RuleMatcher::getPhysRegOperandMatcher(Record *Reg) const {
const OperandMatcher &
RuleMatcher::getPhysRegOperandMatcher(const Record *Reg) const {
const auto &I = PhysRegOperands.find(Reg);

if (I == PhysRegOperands.end()) {
Expand Down Expand Up @@ -1717,7 +1716,8 @@ OperandMatcher &InstructionMatcher::getOperand(unsigned OpIdx) {
llvm_unreachable("Failed to lookup operand");
}

OperandMatcher &InstructionMatcher::addPhysRegInput(Record *Reg, unsigned OpIdx,
OperandMatcher &InstructionMatcher::addPhysRegInput(const Record *Reg,
unsigned OpIdx,
unsigned TempOpIdx) {
assert(SymbolicName.empty());
OperandMatcher *OM = new OperandMatcher(*this, OpIdx, "", TempOpIdx);
Expand Down
18 changes: 9 additions & 9 deletions llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class RuleMatcher : public Matcher {

/// A map of anonymous physical register operands defined by the matchers that
/// may be referenced by the renderers.
DenseMap<Record *, OperandMatcher *> PhysRegOperands;
DenseMap<const Record *, OperandMatcher *> PhysRegOperands;

/// ID for the next instruction variable defined with
/// implicitlyDefineInsnVar()
Expand Down Expand Up @@ -651,7 +651,7 @@ class RuleMatcher : public Matcher {

void defineOperand(StringRef SymbolicName, OperandMatcher &OM);

void definePhysRegOperand(Record *Reg, OperandMatcher &OM);
void definePhysRegOperand(const Record *Reg, OperandMatcher &OM);

Error defineComplexSubOperand(StringRef SymbolicName,
const Record *ComplexPattern,
Expand All @@ -669,7 +669,7 @@ class RuleMatcher : public Matcher {
InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const;
OperandMatcher &getOperandMatcher(StringRef Name);
const OperandMatcher &getOperandMatcher(StringRef Name) const;
const OperandMatcher &getPhysRegOperandMatcher(Record *) const;
const OperandMatcher &getPhysRegOperandMatcher(const Record *) const;

void optimize() override;
void emit(MatchTable &Table) override;
Expand Down Expand Up @@ -1759,7 +1759,7 @@ class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
/// PhysRegInputs - List list has an entry for each explicitly specified
/// physreg input to the pattern. The first elt is the Register node, the
/// second is the recorded slot number the input pattern match saved it in.
SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs;
SmallVector<std::pair<const Record *, unsigned>, 2> PhysRegInputs;

bool canAddNumOperandsCheck() const {
// Add if it's allowed, and:
Expand Down Expand Up @@ -1799,10 +1799,10 @@ class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
unsigned AllocatedTemporariesBaseID,
bool IsVariadic = false);
OperandMatcher &getOperand(unsigned OpIdx);
OperandMatcher &addPhysRegInput(Record *Reg, unsigned OpIdx,
OperandMatcher &addPhysRegInput(const Record *Reg, unsigned OpIdx,
unsigned TempOpIdx);

ArrayRef<std::pair<Record *, unsigned>> getPhysRegInputs() const {
ArrayRef<std::pair<const Record *, unsigned>> getPhysRegInputs() const {
return PhysRegInputs;
}

Expand Down Expand Up @@ -1969,10 +1969,10 @@ class CopyRenderer : public OperandRenderer {
class CopyPhysRegRenderer : public OperandRenderer {
protected:
unsigned NewInsnID;
Record *PhysReg;
const Record *PhysReg;

public:
CopyPhysRegRenderer(unsigned NewInsnID, Record *Reg)
CopyPhysRegRenderer(unsigned NewInsnID, const Record *Reg)
: OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID), PhysReg(Reg) {
assert(PhysReg);
}
Expand All @@ -1981,7 +1981,7 @@ class CopyPhysRegRenderer : public OperandRenderer {
return R->getKind() == OR_CopyPhysReg;
}

Record *getPhysReg() const { return PhysReg; }
const Record *getPhysReg() const { return PhysReg; }

void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void GlobalISelMatchTableExecutorEmitter::emitSubtargetFeatureBitsetImpl(
}

void GlobalISelMatchTableExecutorEmitter::emitComplexPredicates(
raw_ostream &OS, ArrayRef<Record *> ComplexOperandMatchers) {
raw_ostream &OS, ArrayRef<const Record *> ComplexOperandMatchers) {
// Emit complex predicate table and an enum to reference them with.
OS << "// ComplexPattern predicates.\n"
<< "enum {\n"
Expand Down Expand Up @@ -174,7 +174,8 @@ void GlobalISelMatchTableExecutorEmitter::emitMatchTable(

void GlobalISelMatchTableExecutorEmitter::emitExecutorImpl(
raw_ostream &OS, const MatchTable &Table, ArrayRef<LLTCodeGen> TypeObjects,
ArrayRef<RuleMatcher> Rules, ArrayRef<Record *> ComplexOperandMatchers,
ArrayRef<RuleMatcher> Rules,
ArrayRef<const Record *> ComplexOperandMatchers,
ArrayRef<StringRef> CustomOperandRenderers, StringRef IfDefName) {
OS << "#ifdef " << IfDefName << "\n";
emitTypeObjects(OS, TypeObjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GlobalISelMatchTableExecutorEmitter {
/// Emits an enum + an array that stores references to
/// \p ComplexOperandMatchers.
void emitComplexPredicates(raw_ostream &OS,
ArrayRef<Record *> ComplexOperandMatchers);
ArrayRef<const Record *> ComplexOperandMatchers);

/// Emits an enum + an array that stores references to
/// \p CustomOperandRenderers.
Expand Down Expand Up @@ -206,7 +206,7 @@ class GlobalISelMatchTableExecutorEmitter {
void emitExecutorImpl(raw_ostream &OS, const gi::MatchTable &Table,
ArrayRef<gi::LLTCodeGen> TypeObjects,
ArrayRef<gi::RuleMatcher> Rules,
ArrayRef<Record *> ComplexOperandMatchers,
ArrayRef<const Record *> ComplexOperandMatchers,
ArrayRef<StringRef> CustomOperandRenderers,
StringRef IfDefName);
void emitPredicateBitset(raw_ostream &OS, StringRef IfDefName);
Expand Down
86 changes: 45 additions & 41 deletions llvm/utils/TableGen/GlobalISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ static Expected<LLTCodeGen> getInstResultType(const TreePatternNode &Dst,

class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
public:
explicit GlobalISelEmitter(RecordKeeper &RK);
explicit GlobalISelEmitter(const RecordKeeper &RK);

void emitAdditionalImpl(raw_ostream &OS) override;

Expand All @@ -335,18 +335,18 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
private:
std::string ClassName;

RecordKeeper &RK;
const RecordKeeper &RK;
const CodeGenDAGPatterns CGP;
const CodeGenTarget &Target;
CodeGenRegBank &CGRegs;

std::vector<Record *> AllPatFrags;
ArrayRef<const Record *> AllPatFrags;

/// Keep track of the equivalence between SDNodes and Instruction by mapping
/// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to
/// check for attributes on the relation such as CheckMMOIsNonAtomic.
/// This is defined using 'GINodeEquiv' in the target description.
DenseMap<const Record *, Record *> NodeEquivs;
DenseMap<const Record *, const Record *> NodeEquivs;

/// Keep track of the equivalence between ComplexPattern's and
/// GIComplexOperandMatcher. Map entries are specified by subclassing
Expand Down Expand Up @@ -379,8 +379,8 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
void gatherTypeIDValues();
void gatherNodeEquivs();

Record *findNodeEquiv(const Record *N) const;
const CodeGenInstruction *getEquivNode(Record &Equiv,
const Record *findNodeEquiv(const Record *N) const;
const CodeGenInstruction *getEquivNode(const Record &Equiv,
const TreePatternNode &N) const;

Error importRulePredicates(RuleMatcher &M,
Expand Down Expand Up @@ -472,7 +472,7 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
InstructionMatcher &InsnMatcher, bool &HasAddedMatcher);
};

StringRef getPatFragPredicateEnumName(Record *R) { return R->getName(); }
StringRef getPatFragPredicateEnumName(const Record *R) { return R->getName(); }

void GlobalISelEmitter::gatherOpcodeValues() {
InstructionOpcodeMatcher::initOpcodeValuesMap(Target);
Expand All @@ -484,32 +484,35 @@ void GlobalISelEmitter::gatherTypeIDValues() {

void GlobalISelEmitter::gatherNodeEquivs() {
assert(NodeEquivs.empty());
for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
for (const Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv;

assert(ComplexPatternEquivs.empty());
for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
for (const Record *Equiv :
RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
const Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
if (!SelDAGEquiv)
continue;
ComplexPatternEquivs[SelDAGEquiv] = Equiv;
}

assert(SDNodeXFormEquivs.empty());
for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
for (const Record *Equiv :
RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
const Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
if (!SelDAGEquiv)
continue;
SDNodeXFormEquivs[SelDAGEquiv] = Equiv;
}
}

Record *GlobalISelEmitter::findNodeEquiv(const Record *N) const {
const Record *GlobalISelEmitter::findNodeEquiv(const Record *N) const {
return NodeEquivs.lookup(N);
}

const CodeGenInstruction *
GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode &N) const {
GlobalISelEmitter::getEquivNode(const Record &Equiv,
const TreePatternNode &N) const {
if (N.getNumChildren() >= 1) {
// setcc operation maps to two different G_* instructions based on the type.
if (!Equiv.isValueUnset("IfFloatingPoint") &&
Expand All @@ -536,7 +539,7 @@ GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode &N) const {
return &Target.getInstruction(Equiv.getValueAsDef("I"));
}

GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
GlobalISelEmitter::GlobalISelEmitter(const RecordKeeper &RK)
: GlobalISelMatchTableExecutorEmitter(), RK(RK), CGP(RK),
Target(CGP.getTargetInfo()), CGRegs(Target.getRegBank()) {
ClassName = Target.getName().str() + "InstructionSelector";
Expand Down Expand Up @@ -721,7 +724,7 @@ Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
const TreePatternNode &Src, unsigned &TempOpIdx) {
const auto SavedFlags = Rule.setGISelFlags(Src.getGISelFlagsRecord());

Record *SrcGIEquivOrNull = nullptr;
const Record *SrcGIEquivOrNull = nullptr;
const CodeGenInstruction *SrcGIOrNull = nullptr;

// Start with the defined operands (i.e., the results of the root operator).
Expand Down Expand Up @@ -942,7 +945,7 @@ Error GlobalISelEmitter::importComplexPatternOperandMatcher(
// Get the name to use for a pattern operand. For an anonymous physical register
// input, this should use the register name.
static StringRef getSrcChildName(const TreePatternNode &SrcChild,
Record *&PhysReg) {
const Record *&PhysReg) {
StringRef SrcChildName = SrcChild.getName();
if (SrcChildName.empty() && SrcChild.isLeaf()) {
if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild.getLeafValue())) {
Expand All @@ -962,7 +965,7 @@ Error GlobalISelEmitter::importChildMatcher(
const TreePatternNode &SrcChild, bool OperandIsAPointer,
bool OperandIsImmArg, unsigned OpIdx, unsigned &TempOpIdx) {

Record *PhysReg = nullptr;
const Record *PhysReg = nullptr;
std::string SrcChildName = std::string(getSrcChildName(SrcChild, PhysReg));
if (!SrcChild.isLeaf() &&
SrcChild.getOperator()->isSubClassOf("ComplexPattern")) {
Expand Down Expand Up @@ -1196,7 +1199,8 @@ Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer(
auto &Child = DstChild.getChild(0);
auto I = SDNodeXFormEquivs.find(DstChild.getOperator());
if (I != SDNodeXFormEquivs.end()) {
Record *XFormOpc = DstChild.getOperator()->getValueAsDef("Opcode");
const Record *XFormOpc =
DstChild.getOperator()->getValueAsDef("Opcode");
if (XFormOpc->getName() == "timm") {
// If this is a TargetConstant, there won't be a corresponding
// instruction to transform. Instead, this will refer directly to an
Expand Down Expand Up @@ -2290,65 +2294,65 @@ void GlobalISelEmitter::emitAdditionalImpl(raw_ostream &OS) {
}

void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) {
std::vector<Record *> MatchedRecords;
std::vector<const Record *> MatchedRecords;
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
std::back_inserter(MatchedRecords), [&](Record *R) {
std::back_inserter(MatchedRecords), [](const Record *R) {
return !R->getValueAsString("GISelPredicateCode").empty();
});
emitMIPredicateFnsImpl<Record *>(
emitMIPredicateFnsImpl<const Record *>(
OS,
" const MachineFunction &MF = *MI.getParent()->getParent();\n"
" const MachineRegisterInfo &MRI = MF.getRegInfo();\n"
" const auto &Operands = State.RecordedOperands;\n"
" (void)Operands;\n"
" (void)MRI;",
ArrayRef<Record *>(MatchedRecords), &getPatFragPredicateEnumName,
[&](Record *R) { return R->getValueAsString("GISelPredicateCode"); },
ArrayRef<const Record *>(MatchedRecords), &getPatFragPredicateEnumName,
[](const Record *R) { return R->getValueAsString("GISelPredicateCode"); },
"PatFrag predicates.");
}

void GlobalISelEmitter::emitI64ImmPredicateFns(raw_ostream &OS) {
std::vector<Record *> MatchedRecords;
std::vector<const Record *> MatchedRecords;
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
std::back_inserter(MatchedRecords), [&](Record *R) {
std::back_inserter(MatchedRecords), [](const Record *R) {
bool Unset;
return !R->getValueAsString("ImmediateCode").empty() &&
!R->getValueAsBitOrUnset("IsAPFloat", Unset) &&
!R->getValueAsBit("IsAPInt");
});
emitImmPredicateFnsImpl<Record *>(
OS, "I64", "int64_t", ArrayRef<Record *>(MatchedRecords),
emitImmPredicateFnsImpl<const Record *>(
OS, "I64", "int64_t", ArrayRef<const Record *>(MatchedRecords),
&getPatFragPredicateEnumName,
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
"PatFrag predicates.");
}

void GlobalISelEmitter::emitAPFloatImmPredicateFns(raw_ostream &OS) {
std::vector<Record *> MatchedRecords;
std::vector<const Record *> MatchedRecords;
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
std::back_inserter(MatchedRecords), [&](Record *R) {
std::back_inserter(MatchedRecords), [](const Record *R) {
bool Unset;
return !R->getValueAsString("ImmediateCode").empty() &&
R->getValueAsBitOrUnset("IsAPFloat", Unset);
});
emitImmPredicateFnsImpl<Record *>(
OS, "APFloat", "const APFloat &", ArrayRef<Record *>(MatchedRecords),
&getPatFragPredicateEnumName,
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
emitImmPredicateFnsImpl<const Record *>(
OS, "APFloat", "const APFloat &",
ArrayRef<const Record *>(MatchedRecords), &getPatFragPredicateEnumName,
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
"PatFrag predicates.");
}

void GlobalISelEmitter::emitAPIntImmPredicateFns(raw_ostream &OS) {
std::vector<Record *> MatchedRecords;
std::vector<const Record *> MatchedRecords;
std::copy_if(AllPatFrags.begin(), AllPatFrags.end(),
std::back_inserter(MatchedRecords), [&](Record *R) {
std::back_inserter(MatchedRecords), [](const Record *R) {
return !R->getValueAsString("ImmediateCode").empty() &&
R->getValueAsBit("IsAPInt");
});
emitImmPredicateFnsImpl<Record *>(
OS, "APInt", "const APInt &", ArrayRef<Record *>(MatchedRecords),
emitImmPredicateFnsImpl<const Record *>(
OS, "APInt", "const APInt &", ArrayRef<const Record *>(MatchedRecords),
&getPatFragPredicateEnumName,
[&](Record *R) { return R->getValueAsString("ImmediateCode"); },
[](const Record *R) { return R->getValueAsString("ImmediateCode"); },
"PatFrag predicates.");
}

Expand Down Expand Up @@ -2461,7 +2465,7 @@ void GlobalISelEmitter::run(raw_ostream &OS) {
return A->getName() < B->getName();
};

std::vector<Record *> ComplexPredicates =
std::vector<const Record *> ComplexPredicates =
RK.getAllDerivedDefinitions("GIComplexOperandMatcher");
llvm::sort(ComplexPredicates, OrderByName);

Expand Down
Loading