Skip to content

Commit c2bef33

Browse files
committed
[X86][NFC] Auto-generate the function to check predicate for EVEX compression
1 parent dd6fec5 commit c2bef33

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

llvm/lib/Target/X86/X86CompressEVEX.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -121,44 +121,6 @@ static bool usesExtendedRegister(const MachineInstr &MI) {
121121
return false;
122122
}
123123

124-
static bool checkVEXInstPredicate(unsigned OldOpc, const X86Subtarget &ST) {
125-
switch (OldOpc) {
126-
default:
127-
return true;
128-
case X86::VCVTNEPS2BF16Z128rm:
129-
case X86::VCVTNEPS2BF16Z128rr:
130-
case X86::VCVTNEPS2BF16Z256rm:
131-
case X86::VCVTNEPS2BF16Z256rr:
132-
return ST.hasAVXNECONVERT();
133-
case X86::VPDPBUSDSZ128m:
134-
case X86::VPDPBUSDSZ128r:
135-
case X86::VPDPBUSDSZ256m:
136-
case X86::VPDPBUSDSZ256r:
137-
case X86::VPDPBUSDZ128m:
138-
case X86::VPDPBUSDZ128r:
139-
case X86::VPDPBUSDZ256m:
140-
case X86::VPDPBUSDZ256r:
141-
case X86::VPDPWSSDSZ128m:
142-
case X86::VPDPWSSDSZ128r:
143-
case X86::VPDPWSSDSZ256m:
144-
case X86::VPDPWSSDSZ256r:
145-
case X86::VPDPWSSDZ128m:
146-
case X86::VPDPWSSDZ128r:
147-
case X86::VPDPWSSDZ256m:
148-
case X86::VPDPWSSDZ256r:
149-
return ST.hasAVXVNNI();
150-
case X86::VPMADD52HUQZ128m:
151-
case X86::VPMADD52HUQZ128r:
152-
case X86::VPMADD52HUQZ256m:
153-
case X86::VPMADD52HUQZ256r:
154-
case X86::VPMADD52LUQZ128m:
155-
case X86::VPMADD52LUQZ128r:
156-
case X86::VPMADD52LUQZ256m:
157-
case X86::VPMADD52LUQZ256r:
158-
return ST.hasAVXIFMA();
159-
}
160-
}
161-
162124
// Do any custom cleanup needed to finalize the conversion.
163125
static bool performCustomAdjustments(MachineInstr &MI, unsigned NewOpc) {
164126
(void)NewOpc;
@@ -279,7 +241,7 @@ static bool CompressEVEXImpl(MachineInstr &MI, const X86Subtarget &ST) {
279241
}
280242

281243
if (!IsND) {
282-
if (usesExtendedRegister(MI) || !checkVEXInstPredicate(Opc, ST) ||
244+
if (usesExtendedRegister(MI) || !checkPredicate(I->NewOpc, &ST) ||
283245
!performCustomAdjustments(MI, I->NewOpc))
284246
return false;
285247
}

llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ class X86CompressEVEXTablesEmitter {
4646

4747
typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *>
4848
Entry;
49+
typedef std::map<const Record *, std::vector<const CodeGenInstruction *>>
50+
PredicateInstMap;
4951

5052
std::vector<Entry> Table;
53+
// Hold all compressed instructions that need to check predicate
54+
PredicateInstMap PredicateInsts;
5155

5256
public:
5357
X86CompressEVEXTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
@@ -58,12 +62,15 @@ class X86CompressEVEXTablesEmitter {
5862
private:
5963
// Prints the given table as a C++ array of type X86CompressEVEXTableEntry
6064
void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
65+
// Prints function which checks target feature for compressed instructions.
66+
void printCheckPredicate(const PredicateInstMap &PredicateInsts,
67+
raw_ostream &OS);
6168
};
6269

6370
void X86CompressEVEXTablesEmitter::printTable(const std::vector<Entry> &Table,
6471
raw_ostream &OS) {
6572

66-
OS << "static const X86CompressEVEXTableEntry X86CompressEVEXTable[] = { \n";
73+
OS << "static const X86CompressEVEXTableEntry X86CompressEVEXTable[] = {\n";
6774

6875
// Print all entries added to the table
6976
for (const auto &Pair : Table)
@@ -73,6 +80,22 @@ void X86CompressEVEXTablesEmitter::printTable(const std::vector<Entry> &Table,
7380
OS << "};\n\n";
7481
}
7582

83+
void X86CompressEVEXTablesEmitter::printCheckPredicate(
84+
const PredicateInstMap &PredicateInsts, raw_ostream &OS) {
85+
86+
OS << "static bool checkPredicate(unsigned Opc, const X86Subtarget *Subtarget) {\n"
87+
<< " switch (Opc) {\n"
88+
<< " default: return true;\n";
89+
for (const auto &[Key, Val] : PredicateInsts) {
90+
for (const auto &Inst : Val)
91+
OS << " case X86::" << Inst->TheDef->getName() << ":\n";
92+
OS << " return " << Key->getValueAsString("CondString") << ";\n";
93+
}
94+
95+
OS << " }\n";
96+
OS << "};\n\n";
97+
}
98+
7699
static uint8_t byteFromBitsInit(const BitsInit *B) {
77100
unsigned N = B->getNumBits();
78101
assert(N <= 8 && "Field is too large for uint8_t!");
@@ -196,9 +219,18 @@ void X86CompressEVEXTablesEmitter::run(raw_ostream &OS) {
196219
continue;
197220

198221
Table.push_back(std::make_pair(Inst, NewInst));
222+
auto Predicates = NewInst->TheDef->getValueAsListOfDefs("Predicates");
223+
auto It = llvm::find_if(Predicates, [](const Record *R) {
224+
StringRef Name = R->getName();
225+
return Name == "HasAVXNECONVERT" || Name == "HasAVXVNNI" ||
226+
Name == "HasAVXIFMA";
227+
});
228+
if(It!= Predicates.end())
229+
PredicateInsts[*It].push_back(NewInst);
199230
}
200231

201232
printTable(Table, OS);
233+
printCheckPredicate(PredicateInsts, OS);
202234
}
203235
} // namespace
204236

0 commit comments

Comments
 (0)