blob: 79ca87048af8e26fd6f3117512efc207391d1b65 [file] [log] [blame]
Andrea Di Biagiod1569292018-03-26 12:04:53 +00001//===--------------------- InstructionTables.cpp ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
11/// This file implements method InstructionTables::run().
12/// Method run() prints a theoretical resource pressure distribution based on
13/// the information available in the scheduling model, and without running
14/// the backend pipeline.
15///
16//===----------------------------------------------------------------------===//
17
18#include "InstructionTables.h"
19
20namespace mca {
21
22using namespace llvm;
23
24using ResourceRef = std::pair<uint64_t, uint64_t>;
25
26void InstructionTables::run() {
27 ArrayRef<uint64_t> Masks = IB.getProcResourceMasks();
28 SmallVector<std::pair<ResourceRef, double>, 4> UsedResources;
29
30 // Create an instruction descriptor for every instruction in the sequence.
31 while (S.hasNext()) {
32 UsedResources.clear();
Matt Davis21a8d322018-05-07 18:29:15 +000033 SourceRef SR = S.peekNext();
34 std::unique_ptr<Instruction> Inst = IB.createInstruction(*SR.second);
Andrea Di Biagiod1569292018-03-26 12:04:53 +000035 const InstrDesc &Desc = Inst->getDesc();
36 // Now identify the resources consumed by this instruction.
37 for (const std::pair<uint64_t, ResourceUsage> Resource : Desc.Resources) {
38 // Skip zero-cycle resources (i.e. unused resources).
39 if (!Resource.second.size())
40 continue;
41 double Cycles = static_cast<double>(Resource.second.size());
42 unsigned Index =
43 std::distance(Masks.begin(), std::find(Masks.begin(), Masks.end(),
44 Resource.first));
45 const MCProcResourceDesc &ProcResource = *SM.getProcResource(Index);
46 unsigned NumUnits = ProcResource.NumUnits;
47 if (!ProcResource.SubUnitsIdxBegin) {
48 // The number of cycles consumed by each unit.
49 Cycles /= NumUnits;
50 for (unsigned I = 0, E = NumUnits; I < E; ++I) {
51 ResourceRef ResourceUnit = std::make_pair(Index, 1U << I);
52 UsedResources.emplace_back(std::make_pair(ResourceUnit, Cycles));
53 }
54 continue;
55 }
56
57 // This is a group. Obtain the set of resources contained in this
58 // group. Some of these resources may implement multiple units.
59 // Uniformly distribute Cycles across all of the units.
60 for (unsigned I1 = 0; I1 < NumUnits; ++I1) {
61 unsigned SubUnitIdx = ProcResource.SubUnitsIdxBegin[I1];
62 const MCProcResourceDesc &SubUnit = *SM.getProcResource(SubUnitIdx);
63 // Compute the number of cycles consumed by each resource unit.
64 double RUCycles = Cycles / (NumUnits * SubUnit.NumUnits);
65 for (unsigned I2 = 0, E2 = SubUnit.NumUnits; I2 < E2; ++I2) {
66 ResourceRef ResourceUnit = std::make_pair(SubUnitIdx, 1U << I2);
67 UsedResources.emplace_back(std::make_pair(ResourceUnit, RUCycles));
68 }
69 }
70 }
71
Andrea Di Biagio5ffd2c32018-03-26 14:25:52 +000072 // Now send a fake instruction issued event to all the views.
Matt Davis21a8d322018-05-07 18:29:15 +000073 InstRef IR(SR.first, Inst.get());
74 HWInstructionIssuedEvent Event(IR, UsedResources);
Andrea Di Biagio5ffd2c32018-03-26 14:25:52 +000075 for (std::unique_ptr<View> &Listener : Views)
Andrea Di Biagiod1569292018-03-26 12:04:53 +000076 Listener->onInstructionEvent(Event);
77 S.updateNext();
78 }
79}
80
Andrea Di Biagio5ffd2c32018-03-26 14:25:52 +000081void InstructionTables::printReport(llvm::raw_ostream &OS) const {
82 for (const std::unique_ptr<View> &V : Views)
83 V->printView(OS);
84}
85
Andrea Di Biagiod1569292018-03-26 12:04:53 +000086} // namespace mca