gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
ftq.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023 The University of Edinburgh
3 * Copyright (c) 2025 Arm Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "cpu/o3/ftq.hh"
40
42#include "base/logging.hh"
43#include "cpu/o3/cpu.hh"
44#include "debug/FTQ.hh"
45#include "params/BaseO3CPU.hh"
46
47namespace gem5
48{
49
50namespace o3
51{
52
54FetchTarget::FetchTarget(const ThreadID _tid, const PCStateBase &_start_pc,
55 InstSeqNum _seqNum)
56 : ftSeqNum(_seqNum),
57 tid(_tid),
58 is_branch(false),
59 taken(false),
60 bpuHistory(nullptr)
61{
62 set(startPC, _start_pc);
63}
64
65void
66FetchTarget::finalize(const PCStateBase &exit_pc, bool _is_branch,
67 bool pred_taken, const PCStateBase &pred_pc)
68{
69 set(endPC, exit_pc);
70 set(predPC, pred_pc);
71 taken = pred_taken;
72 is_branch = _is_branch;
73}
74
75std::string
77{
78 std::stringstream ss;
79 ss << "FT[" << ftSeqNum << "]: [0x" << std::hex << startPC->instAddr()
80 << "->0x" << endPC->instAddr() << "|B:" << is_branch << "]";
81 return ss.str();
82}
83
85
86FTQ::FTQ(CPU *_cpu, const BaseO3CPUParams &params)
87 : cpu(_cpu),
88 numEntries(params.numFTQEntries),
89 stats(_cpu, params.numFTQEntries)
90{
91 for (ThreadID tid = 0; tid < MaxThreads; tid++) {
92 resetState(tid);
93 }
94}
95
96void
98{
99 ftq[tid].clear();
100 ftqStatus[tid] = Valid;
101}
102
103std::string
105{
106 return cpu->name() + ".ftq";
107}
108
109void
111{
113 new ProbePointArg<FetchTargetPtr>(cpu->getProbeManager(), "FTQInsert");
115 new ProbePointArg<FetchTargetPtr>(cpu->getProbeManager(), "FTQRemove");
116}
117
118unsigned
120{
121 return numEntries - ftq[tid].size();
122}
123
124unsigned
126{
127 return ftq[tid].size();
128}
129
130bool
132{
133 return ftq[tid].size() >= numEntries;
134}
135
136bool
138{
139 return ftq[tid].empty();
140}
141
142void
144{
146 if (!ftq[tid].empty()) {
147 ftqStatus[tid] = Invalid;
148 }
149}
150
151bool
153{
154 return ftqStatus[tid] != Invalid;
155}
156
157void
159{
160 ftqStatus[tid] = Locked;
161}
162
163bool
165{
166 return ftqStatus[tid] == Locked;
167}
168
169void
170FTQ::forAllForward(ThreadID tid, std::function<void(FetchTargetPtr &)> f)
171{
172 for (auto it = ftq[tid].begin(); it != ftq[tid].end(); it++) {
173 f(*it);
174 }
175}
176
177void
178FTQ::forAllBackward(ThreadID tid, std::function<void(FetchTargetPtr &)> f)
179{
180 for (auto it = ftq[tid].rbegin(); it != ftq[tid].rend(); it++) {
181 f(*it);
182 }
183}
184
185void
187{
188 assert(ftq[tid].size() < numEntries);
189 ftq[tid].push_back(fetchTarget);
190 ppFTQInsert->notify(fetchTarget);
191 stats.inserts++;
192 stats.occupancy.sample(ftq[tid].size());
193
194 DPRINTF(FTQ, "Insert %s in FTQ[T:%i]. size FTQ:%i\n",
195 fetchTarget->toString(), tid, ftq[tid].size());
196}
197
198void
200{
201 for (auto ft : ftq[tid]) {
202 assert(ft->bpuHistory == nullptr);
203 ppFTQRemove->notify(ft);
204 }
205 ftq[tid].clear();
206 ftqStatus[tid] = Valid;
207 stats.squashes++;
208}
209
210void
212{
213 for (auto ft : ftq[tid]) {
214 assert(ft->bpuHistory == nullptr);
215 }
216}
217
218bool
220{
221 return (ftqStatus[tid] != Invalid) && (ftq[tid].size() > 0);
222}
223
226{
227 if (ftqStatus[tid] == Invalid) {
228 return nullptr;
229 }
230 if (ftq[tid].empty()) {
231 return nullptr;
232 }
233
234 return ftq[tid].front();
235}
236
237bool
239{
240 assert(!ftq[tid].empty());
241 if (ftq[tid].front()->bpuHistory != nullptr) {
242 DPRINTF(FTQ, "Pop FT:[fn%llu] failed. Still contains BP history.\n",
243 ftq[tid].front()->ftNum());
244 ftqStatus[tid] = Invalid;
245 return false;
246 }
247
248 bool ret_val = true;
249
250 // TODO make this more efficient
251 // Once the head of the FTQ gets updated and
252 // the FTQ got blocked by a complex instruction resteer
253 // we unblock by squashing
254 if (ftqStatus[tid] == Locked) {
255 DPRINTF(FTQ, "Pop FT:[fn%llu] unblocks FTQ. Require squash.\n",
256 ftq[tid].front()->ftNum());
257 ftqStatus[tid] = Invalid;
258 ret_val = false;
259 }
260
261 ppFTQRemove->notify(ftq[tid].front());
262 ftq[tid].pop_front();
263 stats.removals++;
264 return ret_val;
265}
266
267void
269{
270 int i = 0;
271 for (auto ft : ftq[tid]) {
272 DPRINTF(FTQ, "FTQ[tid:%i][%i]: %s.\n", tid, i, ft->toString());
273 i++;
274 }
275}
276
277FTQ::FTQStats::FTQStats(o3::CPU *cpu, unsigned numFTQEntries)
278 : statistics::Group(cpu, "ftq"),
279 ADD_STAT(inserts, statistics::units::Count::get(),
280 "The number of FTQ insertions"),
281 ADD_STAT(removals, statistics::units::Count::get(),
282 "The number of FTQ removals. Not including squashes"),
283 ADD_STAT(squashes, statistics::units::Count::get(),
284 "The number of FTQ squashes"),
285 ADD_STAT(locks, statistics::units::Count::get(),
286 "The number of times the FTQ got locked."),
287 ADD_STAT(occupancy, statistics::units::Count::get(),
288 "Distribution of the FTQ occupation.")
289{
291 .init(/* base value */ 0,
292 /* last value */ numFTQEntries,
293 /* bucket size */ 4)
294 .flags(statistics::pdf);
295}
296
297} // namespace o3
298} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
ProbePointArg generates a point for the class of Arg.
Definition probe.hh:273
O3CPU class, has each of the stages (fetch through commit) within it, as well as all of the time buff...
Definition cpu.hh:97
void insert(ThreadID tid, FetchTargetPtr fetchTarget)
Pushes a fetch target into the back/tail of the FTQ.
Definition ftq.cc:186
void forAllForward(ThreadID tid, std::function< void(FetchTargetPtr &)> f)
Iterates forward over all fetch targets in the FTQ from head/front to tail/back and applies a given f...
Definition ftq.cc:170
const unsigned numEntries
Number of fetch targets in the FTQ.
Definition ftq.hh:254
void resetState(ThreadID tid)
Reset the FTQ state.
Definition ftq.cc:97
ProbePointArg< FetchTargetPtr > * ppFTQInsert
Probe points to attach the FDP prefetcher.
Definition ftq.hh:257
void squash(ThreadID tid)
Squashes all fetch targets in the FTQ for a specific thread.
Definition ftq.cc:199
FTQ(CPU *_cpu, const BaseO3CPUParams &params)
FTQ constructor.
Definition ftq.cc:86
unsigned size(ThreadID tid)
Returns the size of the ftq for a specific partition.
Definition ftq.cc:125
ProbePointArg< FetchTargetPtr > * ppFTQRemove
Definition ftq.hh:258
void regProbePoints()
Registers probes.
Definition ftq.cc:110
FetchTargetPtr readHead(ThreadID tid)
Returns a pointer to the head fetch target of a specific thread within the FTQ.
Definition ftq.cc:225
bool isReady(ThreadID tid)
Returns if the FTQ is in a ready state and its safe to consmume fetch targets.
Definition ftq.cc:152
unsigned numFreeEntries(ThreadID tid)
Returns the number of free entries in a specific FTQ paritition.
Definition ftq.cc:119
bool isLocked(ThreadID tid)
Check if the FTQ is locked.
Definition ftq.cc:164
void invalidate(ThreadID tid)
Invalidates all fetch targets in the FTQ.
Definition ftq.cc:143
void squashSanityCheck(ThreadID tid)
Sanity check to verify that the fetch targets in the ftq do not have a valid bpu_history.
Definition ftq.cc:211
std::array< Status, MaxThreads > ftqStatus
Per-thread FTQ status.
Definition ftq.hh:248
bool isEmpty(ThreadID tid) const
Returns whether or not a specific thread's queue is empty.
Definition ftq.cc:137
bool isHeadReady(ThreadID tid)
Is the head entry ready for the fetch stage to be consumed.
Definition ftq.cc:219
std::string name() const
Definition ftq.cc:104
bool popHead(ThreadID tid)
Pops the head fetch target once its fully processed.
Definition ftq.cc:238
CPU * cpu
Pointer to the CPU.
Definition ftq.hh:251
bool isFull(ThreadID tid)
Returns whether or not a specific thread's queue is full.
Definition ftq.cc:131
std::array< std::list< FetchTargetPtr >, MaxThreads > ftq
FTQ List of Fetch targets.
Definition ftq.hh:261
gem5::o3::FTQ::FTQStats stats
void printFTQ(ThreadID tid)
Print the all fetch targets in the FTQ for debugging.
Definition ftq.cc:268
void forAllBackward(ThreadID tid, std::function< void(FetchTargetPtr &)> f)
Iterates backward over all fetch targets in the FTQ from tail/back to head/front and applies a given ...
Definition ftq.cc:178
void lock(ThreadID tid)
Locks the fetch target queue for a given thread.
Definition ftq.cc:158
std::unique_ptr< PCStateBase > predPC
Predicted target address of the fetch target.
Definition ftq.hh:97
bool taken
If the exit branch is predicted taken.
Definition ftq.hh:109
const ThreadID tid
The thread this FT belongs to.
Definition ftq.hh:103
std::unique_ptr< PCStateBase > startPC
Start address of the fetch target.
Definition ftq.hh:90
const FTSeqNum ftSeqNum
Definition ftq.hh:100
void finalize(const PCStateBase &exit_pc, bool _is_branch, bool pred_taken, const PCStateBase &pred_pc)
Complete a fetch target with the exit instruction.
Definition ftq.cc:66
FetchTarget(const ThreadID _tid, const PCStateBase &_start_pc, FTSeqNum _seqNum)
Fetch Target Methods -----------------------------—.
Definition ftq.cc:54
std::unique_ptr< PCStateBase > endPC
End address of the fetch target.
Definition ftq.hh:93
std::string toString()
Print the fetch target for debugging.
Definition ftq.cc:76
branch_prediction::BPredUnit::PredictorHistory * bpuHistory
Anchor point to attach a branch predictor history.
Definition ftq.hh:114
bool is_branch
Whether the exit instruction is a branch.
Definition ftq.hh:106
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition group.hh:75
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 6 > f
Definition misc_types.hh:68
Bitfield< 12, 11 > set
Bitfield< 21 > ss
Definition misc_types.hh:60
std::shared_ptr< FetchTarget > FetchTargetPtr
Definition bac.hh:62
static constexpr int MaxThreads
Definition limits.hh:38
Units for Stats.
Definition units.hh:113
const FlagsType pdf
Print the percent of the total that this entry represents.
Definition info.hh:61
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
int16_t ThreadID
Thread index/ID type.
Definition types.hh:235
uint64_t InstSeqNum
Definition inst_seq.hh:40
statistics::Distribution occupancy
Definition ftq.hh:349
FTQStats(CPU *cpu, unsigned numFTQEntries)
Definition ftq.cc:277
statistics::Scalar inserts
Definition ftq.hh:344
statistics::Scalar removals
Definition ftq.hh:345
statistics::Scalar squashes
Definition ftq.hh:346
statistics::Scalar locks
Definition ftq.hh:347

Generated on Mon Oct 27 2025 04:13:00 for gem5 by doxygen 1.14.0