gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
looppoint_analysis.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
30
31namespace gem5
32{
33
34LooppointAnalysis::LooppointAnalysis(const LooppointAnalysisParams &params)
36 lpaManager(params.looppoint_analysis_manager),
37 bbValidAddrRange(params.bb_valid_addr_range),
38 markerValidAddrRange(params.marker_valid_addr_range),
39 ifListening(params.if_listening),
41{
42 DPRINTF(LooppointAnalysis, "Start listening from the beginning of the "
43 "simulation? %s\n", ifListening ? "Yes" : "No");
44
45 for (int i = 0; i < params.bb_excluded_addr_ranges.size(); i++) {
46 bbExcludedAddrRanges.push_back(
48 params.bb_excluded_addr_ranges[i].start(),
49 params.bb_excluded_addr_ranges[i].end()
50 )
51 );
52 DPRINTF(LooppointAnalysis, "Excluding address range: (%li, %li)\n",
53 params.bb_excluded_addr_ranges[i].start(),
54 params.bb_excluded_addr_ranges[i].end()
55 );
56 }
57
58 DPRINTF(LooppointAnalysis, "Valid address range: (%li, %li)\n",
59 bbValidAddrRange.start(), bbValidAddrRange.end());
60 DPRINTF(LooppointAnalysis, "Valid marker address range: (%li, %li)\n",
62}
63
64void
66{
67 if (localBBV.find(pc) == localBBV.end()) {
68 localBBV.insert(std::make_pair(pc, 1));
69 } else {
70 localBBV.find(pc)->second++;
71 }
72}
73
74void
77)
78{
79 const StaticInstPtr &inst = inst_pair.second;
80
81 if (inst->isMicroop() && !inst->isLastMicroop()) {
82 // ignore this if it is a microop
83 return;
84 }
85
86 SimpleThread* thread = inst_pair.first;
87
88 auto &pcstate =
90 Addr pc = pcstate.pc();
91
92 if (lpaManager->ifEncountered(pc)) {
93 // if we have already encountered this pc, we should already
94 // categorized it
95 if (lpaManager->ifValidNotControl(pc)) {
96 // if it is categorized as a valid not control instruction
98 lpaManager->incrementGlobalInstCounter();
99 } else if (lpaManager->ifValidControl(pc)) {
100 // if it is categorized as a valid control instruction
101 bbInstCounter ++;
102 lpaManager->incrementGlobalInstCounter();
103 lpaManager->updateBBInstMap(pc, bbInstCounter);
105 lpaManager->updateGlobalBBV(pc);
106 bbInstCounter = 0;
107 if (lpaManager->ifBackwardBranch(pc)) {
108 // if it is categorized as a backward branch
109 lpaManager->countBackwardBranch(pc);
110 }
111 }
112 return;
113 }
114
115 lpaManager->updateEncountered(pc);
116 // if we have not encountered this pc before, we should now update it to
117 // the corresponding category
118
119 if (!thread->getIsaPtr()->inUserMode()) {
120 // ignore this if it is not in user mode
121 return;
122 }
123
124 if (bbValidAddrRange.end() > 0 && ! bbValidAddrRange.contains(pc)) {
125 // ignore this if it is not in the valid address range
126 return;
127 }
128
129 if (bbExcludedAddrRanges.size() > 0) {
130 for (int i = 0; i < bbExcludedAddrRanges.size(); i++) {
131 if (bbExcludedAddrRanges[i].contains(pc)) {
132 // ignore this if it is in the excluded address range
133 return;
134 }
135 }
136 }
137
139 lpaManager->incrementGlobalInstCounter();
140
141 if (inst->isControl()) {
142 // if it is a control instruction, we see it as the end of a basic
143 // block
144 lpaManager->updateValidControl(pc);
145 lpaManager->updateBBInstMap(pc, bbInstCounter);
147 lpaManager->updateGlobalBBV(pc);
148 bbInstCounter = 0;
149
150 if (markerValidAddrRange.end() > 0
151 && ! markerValidAddrRange.contains(pc)) {
152 // if it is not in the marker valid address range, we do not
153 // consider it as a possible marker used loop branch instruction
154 return;
155 }
156
157 if (inst->isDirectCtrl()) {
158 // We only consider direct control instructions as possible
159 // loop branch instructions because it is PC-relative and it
160 // excludes return instructions.
161 if (pcstate.npc() < pc) {
162 lpaManager->countBackwardBranch(pc);
163 }
164
165 }
166 } else {
167 lpaManager->updateValidNotControl(pc);
168 }
169}
170
171void
173{
174 if (ifListening) {
175 if (listeners.empty()) {
177 this, "Commit", &LooppointAnalysis::checkPc);
179 "Start listening to the RetiredInstsPC\n");
180 }
181 }
182
183}
184
185void
191
192void
194{
195 ifListening = false;
196 listeners.clear();
197 DPRINTF(LooppointAnalysis, "Stop listening to Commit\n");
198}
199
201 LooppointAnalysisManagerParams &params)
202 : SimObject(params),
203 regionLength(params.region_length),
206{
207 DPRINTF(LooppointAnalysis, "regionLength = %i\n", regionLength);
208}
209
210void
212{
214 backwardBranchCounter.insert(std::make_pair(pc, 1));
215 } else {
216 backwardBranchCounter.find(pc)->second++;
217 }
218
220
222 // note that we do not reset any counter here but only raise an
223 // exit event.
224 // we can reset the counters through the simulation script using
225 // the helper functions in the LooppointAnalysisManager class
226 DPRINTF(LooppointAnalysis, "simpoint starting point found\n");
227 DPRINTF(LooppointAnalysis, "globalInstCounter = %lu\n",
229 DPRINTF(LooppointAnalysis, "regionLength = %lu\n",
231 exitSimLoopNow("simpoint starting point found");
232 }
233}
234
235void
237{
238 if (globalBBV.find(pc) == globalBBV.end()) {
239 globalBBV.insert(std::make_pair(pc, 1));
240 } else {
241 globalBBV.find(pc)->second++;
242 }
243}
244
245}// namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition addr_range.hh:82
virtual bool inUserMode() const =0
void updateGlobalBBV(const Addr pc)
This function is called by the LooppointAnalysis probe listener when it reaches the branch instructio...
Addr mostRecentBackwardBranchPC
This variable stores the Program Counter address of the most recent valid backward branch that we con...
std::unordered_map< Addr, uint64_t > backwardBranchCounter
This counter is for the valid backward branches that we consider as candidates for marking the execut...
void countBackwardBranch(const Addr pc)
This function is called by the LooppointAnalysis probe listener when it finds a valid backward branch...
uint64_t globalInstCounter
This is a counter for the globally executed instructions.
std::unordered_map< Addr, uint64_t > globalBBV
This is the global basic block vector that contains the count of each basic block that is executed.
LooppointAnalysisManager(const LooppointAnalysisManagerParams &params)
uint64_t regionLength
This variable stores the number of instructions that we used to define a region.
std::vector< AddrRange > bbExcludedAddrRanges
Any basic block that is in this range will not be analyzed.
virtual void regProbeListeners()
Register probe listeners for this object.
void stopListening()
When this function is called, it sets the class variable ifListening to false, then removes the probe...
AddrRange markerValidAddrRange
We only consider the loops within this address range as candidates for marking the execution points.
void updateLocalBBV(const Addr pc)
This function updates the localBBV for the input PC's basic block.
uint64_t bbInstCounter
The counter for the number of instructions within the current basic block.
LooppointAnalysisManager * lpaManager
This is the pointer to the LooppointAnalysisManager SimObject that is managing all the LooppointAnaly...
LooppointAnalysis(const LooppointAnalysisParams &params)
std::unordered_map< Addr, uint64_t > localBBV
The basic block vector for the current core that the LooppointAnalysis is attached to.
bool ifListening
Only when this is set to true, the LooppointAnalysis will listen to the probe points.
AddrRange bbValidAddrRange
This is the valid address range for the basic block that the LooppointAnalysis considers analyzing.
void checkPc(const std::pair< SimpleThread *, StaticInstPtr > &inst_pair)
This function is called when a the probe point notifies the LoopPointAnalysis probe listener.
void startListening()
When this function is called, it sets the class variable ifListening to true, then calls the regProbe...
Target & as()
Definition pcstate.hh:73
std::vector< ProbeListenerPtr<> > listeners
ProbeListenerObject(const ProbeListenerObjectParams &params)
void connectListener(Args &&...args)
The SimpleThread object provides a combination of the ThreadState object and the ThreadContext interf...
ThreadContext * getTC()
Returns the pointer to this SimpleThread's ThreadContext.
BaseISA * getIsaPtr() const override
bool isDirectCtrl() const
bool isLastMicroop() const
bool isMicroop() const
bool isControl() const
virtual const PCStateBase & pcState() const =0
STL pair class.
Definition stl.hh:58
const Params & params() const
SimObject(const Params &p)
Definition sim_object.cc:58
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 4 > pc
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
RefCountingPtr< StaticInst > StaticInstPtr
void exitSimLoopNow(const std::string &message, int exit_code, Tick repeat, bool serialize)
Schedule an event as above, but make it high priority so it runs before any normal events which are s...

Generated on Mon May 26 2025 09:19:08 for gem5 by doxygen 1.13.2