1 //===--------------------- InstructionTables.cpp ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file implements the method InstructionTables::execute().
11 /// Method execute() prints a theoretical resource pressure distribution based
12 /// on the information available in the scheduling model, and without running
13 /// the pipeline.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/MCA/Stages/InstructionTables.h"
18 
19 namespace llvm {
20 namespace mca {
21 
22 Error InstructionTables::execute(InstRef &IR) {
23   const InstrDesc &Desc = IR.getInstruction()->getDesc();
24   UsedResources.clear();
25 
26   // Identify the resources consumed by this instruction.
27   for (const std::pair<const uint64_t, ResourceUsage> Resource :
28        Desc.Resources) {
29     // Skip zero-cycle resources (i.e., unused resources).
30     if (!Resource.second.size())
31       continue;
32     unsigned Cycles = Resource.second.size();
33     unsigned Index = std::distance(
34         Masks.begin(), std::find(Masks.begin(), Masks.end(), Resource.first));
35     const MCProcResourceDesc &ProcResource = *SM.getProcResource(Index);
36     unsigned NumUnits = ProcResource.NumUnits;
37     if (!ProcResource.SubUnitsIdxBegin) {
38       // The number of cycles consumed by each unit.
39       for (unsigned I = 0, E = NumUnits; I < E; ++I) {
40         ResourceRef ResourceUnit = std::make_pair(Index, 1U << I);
41         UsedResources.emplace_back(
42             std::make_pair(ResourceUnit, ResourceCycles(Cycles, NumUnits)));
43       }
44       continue;
45     }
46 
47     // This is a group. Obtain the set of resources contained in this
48     // group. Some of these resources may implement multiple units.
49     // Uniformly distribute Cycles across all of the units.
50     for (unsigned I1 = 0; I1 < NumUnits; ++I1) {
51       unsigned SubUnitIdx = ProcResource.SubUnitsIdxBegin[I1];
52       const MCProcResourceDesc &SubUnit = *SM.getProcResource(SubUnitIdx);
53       // Compute the number of cycles consumed by each resource unit.
54       for (unsigned I2 = 0, E2 = SubUnit.NumUnits; I2 < E2; ++I2) {
55         ResourceRef ResourceUnit = std::make_pair(SubUnitIdx, 1U << I2);
56         UsedResources.emplace_back(std::make_pair(
57             ResourceUnit, ResourceCycles(Cycles, NumUnits * SubUnit.NumUnits)));
58       }
59     }
60   }
61 
62   // Send a fake instruction issued event to all the views.
63   HWInstructionIssuedEvent Event(IR, UsedResources);
64   notifyEvent<HWInstructionIssuedEvent>(Event);
65   return ErrorSuccess();
66 }
67 
68 } // namespace mca
69 } // namespace llvm
70