blob: ae96568f1661f2cecc6a9adbddc10f0be72bf01a [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();
33 InstRef IR = S.peekNext();
Andrea Di Biagio49c85912018-05-04 13:10:10 +000034 std::unique_ptr<Instruction> Inst = IB.createInstruction(*IR.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.
Andrea Di Biagiod1569292018-03-26 12:04:53 +000073 HWInstructionIssuedEvent Event(IR.first, UsedResources);
Andrea Di Biagio5ffd2c32018-03-26 14:25:52 +000074 for (std::unique_ptr<View> &Listener : Views)
Andrea Di Biagiod1569292018-03-26 12:04:53 +000075 Listener->onInstructionEvent(Event);
76 S.updateNext();
77 }
78}
79
Andrea Di Biagio5ffd2c32018-03-26 14:25:52 +000080void InstructionTables::printReport(llvm::raw_ostream &OS) const {
81 for (const std::unique_ptr<View> &V : Views)
82 V->printView(OS);
83}
84
Andrea Di Biagiod1569292018-03-26 12:04:53 +000085} // namespace mca