Skip to content

Commit cbd99c5

Browse files
authored
[TableGen] Add mapping from processor ID to resource index for packetizer (#158182)
Tablegen would generate code to access TargetResourceIndices with processor ID. The TargetProcResourceIndexStart[] array is generated for each processor which has itineraries. The processor which doesn't has itineraries is excluded from the array. When a target has mixed processors, the processor ID may exceed the array size and cause the error. This patch is to generate a table mapping processor with itineraries to resource index, so that scheduler can get the correct resource index with processor ID.
1 parent 799b80d commit cbd99c5

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

llvm/lib/Target/AMDGPU/R600Packetizer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
319319

320320
MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
321321

322+
const InstrItineraryData *II = ST.getInstrItineraryData();
323+
// If there is no itineraries information, abandon.
324+
if (II->Itineraries == nullptr)
325+
return false;
326+
322327
// Instantiate the packetizer.
323328
R600PacketizerList Packetizer(Fn, ST, MLI);
324329

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: llvm-tblgen -gen-dfa-packetizer -I %p/../../include %s | FileCheck %s
2+
3+
include "llvm/Target/Target.td"
4+
5+
def TestTarget : Target;
6+
7+
def TestSchedModel : SchedMachineModel {
8+
let CompleteModel = 0;
9+
}
10+
11+
def TestProcessor1 : ProcessorModel<"testprocessor1", TestSchedModel, []>;
12+
13+
def FU0 : FuncUnit;
14+
def FU1 : FuncUnit;
15+
16+
def OP0 : InstrItinClass;
17+
def OP1 : InstrItinClass;
18+
19+
def Itin {
20+
list<InstrItinData> ItinList = [
21+
InstrItinData<OP0, [InstrStage<1, [FU0]>]>,
22+
InstrItinData<OP1, [InstrStage<1, [FU1]>]>,
23+
];
24+
}
25+
26+
// CHECK: int TestTargetGetResourceIndex(unsigned ProcID) {
27+
// CHECK-NEXT: static const unsigned TestTargetProcIdToProcResourceIdxTable[][2] = {
28+
// CHECK-NEXT: { 2, 1 }, // TestItinerariesModel
29+
// CHECK-NEXT: };
30+
// CHECK-NEXT: auto It = llvm::lower_bound(TestTargetProcIdToProcResourceIdxTable, ProcID,
31+
// CHECK-NEXT: [](const unsigned LHS[], unsigned Val) { return LHS[0] < Val; });
32+
// CHECK-NEXT: assert(*It[0] == ProcID);
33+
// CHECK-NEXT: return (*It)[1];
34+
// CHECK-NEXT: }
35+
36+
// CHECK: unsigned Index = TestTargetGetResourceIndex(IID->SchedModel.ProcID);
37+
38+
def TestItineraries: ProcessorItineraries<[], [], Itin.ItinList>;
39+
def TestProcessor2 : Processor<"testprocessor2", TestItineraries, []>;

llvm/utils/TableGen/DFAPacketizerEmitter.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ void DFAPacketizerEmitter::emitForItineraries(
266266
}
267267
OS << " " << ScheduleClasses.size() << "\n};\n\n";
268268

269+
// Output the mapping from proc ID to ResourceIndexStart
270+
Idx = 1;
271+
OS << "int " << TargetName << DFAName
272+
<< "GetResourceIndex(unsigned ProcID) { \n"
273+
<< " static const unsigned " << TargetName << DFAName
274+
<< "ProcIdToProcResourceIdxTable[][2] = {\n";
275+
for (const CodeGenProcModel *Model : ProcModels) {
276+
OS << " { " << Model->Index << ", " << Idx++ << " }, // "
277+
<< Model->ModelName << "\n";
278+
}
279+
OS << " };\n"
280+
<< " auto It = llvm::lower_bound(" << TargetName << DFAName
281+
<< "ProcIdToProcResourceIdxTable, ProcID,\n"
282+
<< " [](const unsigned LHS[], unsigned Val) { return LHS[0] < Val; "
283+
"});\n"
284+
<< " assert(*It[0] == ProcID);\n"
285+
<< " return (*It)[1];\n"
286+
<< "}\n\n";
287+
269288
// The type of a state in the nondeterministic automaton we're defining.
270289
using NfaStateTy = uint64_t;
271290

@@ -339,16 +358,17 @@ void DFAPacketizerEmitter::emitForItineraries(
339358

340359
std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
341360
OS << "namespace llvm {\n";
342-
OS << "DFAPacketizer *" << SubTargetClassName << "::"
343-
<< "create" << DFAName
361+
OS << "DFAPacketizer *" << SubTargetClassName << "::" << "create" << DFAName
344362
<< "DFAPacketizer(const InstrItineraryData *IID) const {\n"
345363
<< " static Automaton<uint64_t> A(ArrayRef<" << TargetAndDFAName
346364
<< "Transition>(" << TargetAndDFAName << "Transitions), "
347365
<< TargetAndDFAName << "TransitionInfo);\n"
366+
<< " unsigned Index = " << TargetName << DFAName
367+
<< "GetResourceIndex(IID->SchedModel.ProcID);\n"
348368
<< " unsigned ProcResIdxStart = " << TargetAndDFAName
349-
<< "ProcResourceIndexStart[IID->SchedModel.ProcID];\n"
369+
<< "ProcResourceIndexStart[Index];\n"
350370
<< " unsigned ProcResIdxNum = " << TargetAndDFAName
351-
<< "ProcResourceIndexStart[IID->SchedModel.ProcID + 1] - "
371+
<< "ProcResourceIndexStart[Index + 1] - "
352372
"ProcResIdxStart;\n"
353373
<< " return new DFAPacketizer(IID, A, {&" << TargetAndDFAName
354374
<< "ResourceIndices[ProcResIdxStart], ProcResIdxNum});\n"

0 commit comments

Comments
 (0)