[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.
This commit is contained in:
Luo, Yuanke 2025-09-17 09:12:37 +08:00 committed by GitHub
parent 799b80d47a
commit cbd99c55a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 68 additions and 4 deletions

View File

@ -319,6 +319,11 @@ bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
const InstrItineraryData *II = ST.getInstrItineraryData();
// If there is no itineraries information, abandon.
if (II->Itineraries == nullptr)
return false;
// Instantiate the packetizer.
R600PacketizerList Packetizer(Fn, ST, MLI);

View File

@ -0,0 +1,39 @@
// RUN: llvm-tblgen -gen-dfa-packetizer -I %p/../../include %s | FileCheck %s
include "llvm/Target/Target.td"
def TestTarget : Target;
def TestSchedModel : SchedMachineModel {
let CompleteModel = 0;
}
def TestProcessor1 : ProcessorModel<"testprocessor1", TestSchedModel, []>;
def FU0 : FuncUnit;
def FU1 : FuncUnit;
def OP0 : InstrItinClass;
def OP1 : InstrItinClass;
def Itin {
list<InstrItinData> ItinList = [
InstrItinData<OP0, [InstrStage<1, [FU0]>]>,
InstrItinData<OP1, [InstrStage<1, [FU1]>]>,
];
}
// CHECK: int TestTargetGetResourceIndex(unsigned ProcID) {
// CHECK-NEXT: static const unsigned TestTargetProcIdToProcResourceIdxTable[][2] = {
// CHECK-NEXT: { 2, 1 }, // TestItinerariesModel
// CHECK-NEXT: };
// CHECK-NEXT: auto It = llvm::lower_bound(TestTargetProcIdToProcResourceIdxTable, ProcID,
// CHECK-NEXT: [](const unsigned LHS[], unsigned Val) { return LHS[0] < Val; });
// CHECK-NEXT: assert(*It[0] == ProcID);
// CHECK-NEXT: return (*It)[1];
// CHECK-NEXT: }
// CHECK: unsigned Index = TestTargetGetResourceIndex(IID->SchedModel.ProcID);
def TestItineraries: ProcessorItineraries<[], [], Itin.ItinList>;
def TestProcessor2 : Processor<"testprocessor2", TestItineraries, []>;

View File

@ -266,6 +266,25 @@ void DFAPacketizerEmitter::emitForItineraries(
}
OS << " " << ScheduleClasses.size() << "\n};\n\n";
// Output the mapping from proc ID to ResourceIndexStart
Idx = 1;
OS << "int " << TargetName << DFAName
<< "GetResourceIndex(unsigned ProcID) { \n"
<< " static const unsigned " << TargetName << DFAName
<< "ProcIdToProcResourceIdxTable[][2] = {\n";
for (const CodeGenProcModel *Model : ProcModels) {
OS << " { " << Model->Index << ", " << Idx++ << " }, // "
<< Model->ModelName << "\n";
}
OS << " };\n"
<< " auto It = llvm::lower_bound(" << TargetName << DFAName
<< "ProcIdToProcResourceIdxTable, ProcID,\n"
<< " [](const unsigned LHS[], unsigned Val) { return LHS[0] < Val; "
"});\n"
<< " assert(*It[0] == ProcID);\n"
<< " return (*It)[1];\n"
<< "}\n\n";
// The type of a state in the nondeterministic automaton we're defining.
using NfaStateTy = uint64_t;
@ -339,16 +358,17 @@ void DFAPacketizerEmitter::emitForItineraries(
std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
OS << "namespace llvm {\n";
OS << "DFAPacketizer *" << SubTargetClassName << "::"
<< "create" << DFAName
OS << "DFAPacketizer *" << SubTargetClassName << "::" << "create" << DFAName
<< "DFAPacketizer(const InstrItineraryData *IID) const {\n"
<< " static Automaton<uint64_t> A(ArrayRef<" << TargetAndDFAName
<< "Transition>(" << TargetAndDFAName << "Transitions), "
<< TargetAndDFAName << "TransitionInfo);\n"
<< " unsigned Index = " << TargetName << DFAName
<< "GetResourceIndex(IID->SchedModel.ProcID);\n"
<< " unsigned ProcResIdxStart = " << TargetAndDFAName
<< "ProcResourceIndexStart[IID->SchedModel.ProcID];\n"
<< "ProcResourceIndexStart[Index];\n"
<< " unsigned ProcResIdxNum = " << TargetAndDFAName
<< "ProcResourceIndexStart[IID->SchedModel.ProcID + 1] - "
<< "ProcResourceIndexStart[Index + 1] - "
"ProcResIdxStart;\n"
<< " return new DFAPacketizer(IID, A, {&" << TargetAndDFAName
<< "ResourceIndices[ProcResIdxStart], ProcResIdxNum});\n"