LLVM 20.0.0git
EntryStage.cpp
Go to the documentation of this file.
1//===---------------------- EntryStage.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 defines the Fetch stage of an instruction pipeline. Its sole
11/// purpose in life is to produce instructions for the rest of the pipeline.
12///
13//===----------------------------------------------------------------------===//
14
17
18namespace llvm {
19namespace mca {
20
22 return static_cast<bool>(CurrentInstruction) || !SM.isEnd();
23}
24
25bool EntryStage::isAvailable(const InstRef & /* unused */) const {
26 if (CurrentInstruction)
27 return checkNextStage(CurrentInstruction);
28 return false;
29}
30
31Error EntryStage::getNextInstruction() {
32 assert(!CurrentInstruction && "There is already an instruction to process!");
33 if (!SM.hasNext()) {
34 if (!SM.isEnd())
35 return llvm::make_error<InstStreamPause>();
36 else
37 return llvm::ErrorSuccess();
38 }
39 SourceRef SR = SM.peekNext();
40 std::unique_ptr<Instruction> Inst = std::make_unique<Instruction>(SR.second);
41 CurrentInstruction = InstRef(SR.first, Inst.get());
42 Instructions.emplace_back(std::move(Inst));
43 SM.updateNext();
44 return llvm::ErrorSuccess();
45}
46
48 assert(CurrentInstruction && "There is no instruction to process!");
49 if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
50 return Val;
51
52 // Move the program counter.
53 CurrentInstruction.invalidate();
54 return getNextInstruction();
55}
56
58 if (!CurrentInstruction)
59 return getNextInstruction();
60 return llvm::ErrorSuccess();
61}
62
64 assert(!CurrentInstruction);
65 return getNextInstruction();
66}
67
69 // Find the first instruction which hasn't been retired.
70 auto Range = drop_begin(Instructions, NumRetired);
71 auto It = find_if(Range, [](const std::unique_ptr<Instruction> &I) {
72 return !I->isRetired();
73 });
74
75 NumRetired = std::distance(Instructions.begin(), It);
76 // Erase instructions up to the first that hasn't been retired.
77 if ((NumRetired * 2) >= Instructions.size()) {
78 Instructions.erase(Instructions.begin(), It);
79 NumRetired = 0;
80 }
81
82 return llvm::ErrorSuccess();
83}
84
85} // namespace mca
86} // namespace llvm
This file defines the Entry stage of an instruction pipeline.
This file defines abstractions used by the Pipeline to model register reads, register writes and inst...
#define I(x, y, z)
Definition: MD5.cpp:58
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition: Error.h:335
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Error cycleStart() override
Called once at the start of each cycle.
Definition: EntryStage.cpp:57
bool hasWorkToComplete() const override
Returns true if some instructions are still executing this stage.
Definition: EntryStage.cpp:21
Error cycleEnd() override
Called once at the end of each cycle.
Definition: EntryStage.cpp:68
bool isAvailable(const InstRef &IR) const override
Returns true if it can execute IR during this cycle.
Definition: EntryStage.cpp:25
Error execute(InstRef &IR) override
The primary action that this stage performs on instruction IR.
Definition: EntryStage.cpp:47
Error cycleResume() override
Called after the pipeline is resumed from pausing state.
Definition: EntryStage.cpp:63
An InstRef contains both a SourceMgr index and Instruction pair.
Definition: Instruction.h:720
void invalidate()
Invalidate this reference.
Definition: Instruction.h:741
Error moveToTheNextStage(InstRef &IR)
Called when an instruction is ready to move the next pipeline stage.
Definition: Stage.h:73
bool checkNextStage(const InstRef &IR) const
Definition: Stage.h:65
std::pair< unsigned, const Instruction & > SourceRef
Definition: SourceMgr.h:25
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
virtual void updateNext()=0
Advance to the next SourceRef.
virtual bool isEnd() const =0
Whether the instruction stream has eneded.
virtual SourceRef peekNext() const =0
The next SourceRef.
virtual bool hasNext() const =0
Whether there is any SourceRef to inspect / peek next.