Skip to content

Commit cd4deea

Browse files
committed
[llvm-mca] Simplify the logic in FetchStage. NFCI
Only method 'getNextInstruction()' needs to interact with the SourceMgr. llvm-svn: 345185
1 parent 5369168 commit cd4deea

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

llvm/tools/llvm-mca/include/Stages/FetchStage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace mca {
2525

2626
class FetchStage final : public Stage {
27-
std::unique_ptr<Instruction> CurrentInstruction;
27+
InstRef CurrentInstruction;
2828
using InstMap = std::map<unsigned, std::unique_ptr<Instruction>>;
2929
InstMap Instructions;
3030
InstrBuilder &IB;

llvm/tools/llvm-mca/lib/Pipeline.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ bool Pipeline::hasWorkToProcess() {
3939
Error Pipeline::run() {
4040
assert(!Stages.empty() && "Unexpected empty pipeline found!");
4141

42-
while (hasWorkToProcess()) {
42+
do {
4343
notifyCycleBegin();
4444
if (Error Err = runCycle())
4545
return Err;
4646
notifyCycleEnd();
4747
++Cycles;
48-
}
48+
} while (hasWorkToProcess());
49+
4950
return ErrorSuccess();
5051
}
5152

llvm/tools/llvm-mca/lib/Stages/FetchStage.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,44 @@
1818
namespace mca {
1919

2020
bool FetchStage::hasWorkToComplete() const {
21-
return CurrentInstruction.get() || SM.hasNext();
21+
return CurrentInstruction.isValid();
2222
}
2323

2424
bool FetchStage::isAvailable(const InstRef & /* unused */) const {
25-
if (!CurrentInstruction)
26-
return false;
27-
assert(SM.hasNext() && "Unexpected internal state!");
28-
const SourceRef SR = SM.peekNext();
29-
InstRef IR(SR.first, CurrentInstruction.get());
30-
return checkNextStage(IR);
25+
if (CurrentInstruction.isValid())
26+
return checkNextStage(CurrentInstruction);
27+
return false;
3128
}
3229

3330
llvm::Error FetchStage::getNextInstruction() {
34-
assert(!CurrentInstruction && "There is already an instruction to process!");
31+
assert(!CurrentInstruction.isValid() &&
32+
"There is already an instruction to process!");
3533
if (!SM.hasNext())
3634
return llvm::ErrorSuccess();
3735
const SourceRef SR = SM.peekNext();
3836
llvm::Expected<std::unique_ptr<Instruction>> InstOrErr =
3937
IB.createInstruction(SR.second);
4038
if (!InstOrErr)
4139
return InstOrErr.takeError();
42-
CurrentInstruction = std::move(InstOrErr.get());
40+
std::unique_ptr<Instruction> Inst = std::move(InstOrErr.get());
41+
CurrentInstruction = InstRef(SR.first, Inst.get());
42+
Instructions[SR.first] = std::move(Inst);
43+
SM.updateNext();
4344
return llvm::ErrorSuccess();
4445
}
4546

4647
llvm::Error FetchStage::execute(InstRef & /*unused */) {
47-
assert(CurrentInstruction && "There is no instruction to process!");
48-
const SourceRef SR = SM.peekNext();
49-
InstRef IR(SR.first, CurrentInstruction.get());
50-
assert(checkNextStage(IR) && "Invalid fetch!");
51-
52-
Instructions[IR.getSourceIndex()] = std::move(CurrentInstruction);
53-
if (llvm::Error Val = moveToTheNextStage(IR))
48+
assert(CurrentInstruction.isValid() && "There is no instruction to process!");
49+
if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
5450
return Val;
5551

56-
SM.updateNext();
57-
5852
// Move the program counter.
53+
CurrentInstruction.invalidate();
5954
return getNextInstruction();
6055
}
6156

6257
llvm::Error FetchStage::cycleStart() {
63-
if (!CurrentInstruction)
58+
if (!CurrentInstruction.isValid())
6459
return getNextInstruction();
6560
return llvm::ErrorSuccess();
6661
}

0 commit comments

Comments
 (0)