| 1 |
#include "Views/SummaryView.h" |
| 2 |
#include "X86TestBase.h" |
| 3 |
#include "llvm/ADT/SmallPtrSet.h" |
| 4 |
#include "llvm/MCA/CustomBehaviour.h" |
| 5 |
#include "llvm/MCA/IncrementalSourceMgr.h" |
| 6 |
#include "llvm/MCA/InstrBuilder.h" |
| 7 |
#include "llvm/MCA/Pipeline.h" |
| 8 |
#include "llvm/Support/Format.h" |
| 9 |
#include "llvm/Support/JSON.h" |
| 10 |
#include "llvm/Support/raw_ostream.h" |
| 11 |
#include <unordered_map> |
| 12 |
|
| 13 |
using namespace llvm; |
| 14 |
using namespace mca; |
| 15 |
|
| 16 |
TEST_F(X86TestBase, TestResumablePipeline) { |
| 17 |
mca::Context MCA(*MRI, *STI); |
| 18 |
|
| 19 |
mca::IncrementalSourceMgr ISM; |
| 20 |
// Empty CustomBehaviour. |
| 21 |
auto CB = std::make_unique<mca::CustomBehaviour>(*STI, ISM, *MCII); |
| 22 |
|
| 23 |
auto PO = getDefaultPipelineOptions(); |
| 24 |
auto P = MCA.createDefaultPipeline(PO, ISM, *CB); |
| 25 |
ASSERT_TRUE(P); |
| 26 |
|
| 27 |
SmallVector<MCInst> MCIs; |
| 28 |
getSimpleInsts(MCIs, /*Repeats=*/100); |
| 29 |
|
| 30 |
// Add views. |
| 31 |
auto SV = std::make_unique<SummaryView>(STI->getSchedModel(), MCIs, |
| 32 |
PO.DispatchWidth); |
| 33 |
P->addEventListener(SV.get()); |
| 34 |
|
| 35 |
auto IM = std::make_unique<mca::InstrumentManager>(*STI, *MCII); |
| 36 |
mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get(), *IM); |
| 37 |
|
| 38 |
const SmallVector<mca::SharedInstrument> Instruments; |
| 39 |
// Tile size = 7 |
| 40 |
for (unsigned i = 0U, E = MCIs.size(); i < E;) { |
| 41 |
for (unsigned TE = i + 7; i < TE && i < E; ++i) { |
| 42 |
Expected<std::unique_ptr<mca::Instruction>> InstOrErr = |
| 43 |
IB.createInstruction(MCIs[i], Instruments); |
| 44 |
ASSERT_TRUE(bool(InstOrErr)); |
| 45 |
ISM.addInst(std::move(InstOrErr.get())); |
| 46 |
} |
| 47 |
|
| 48 |
// Run the pipeline. |
| 49 |
Expected<unsigned> Cycles = P->run(); |
| 50 |
if (!Cycles) { |
| 51 |
// Should be a stream pause error. |
| 52 |
ASSERT_TRUE(Cycles.errorIsA<mca::InstStreamPause>()); |
| 53 |
llvm::consumeError(Cycles.takeError()); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
ISM.endOfStream(); |
| 58 |
// Has to terminate properly. |
| 59 |
Expected<unsigned> Cycles = P->run(); |
| 60 |
ASSERT_TRUE(bool(Cycles)); |
| 61 |
|
| 62 |
json::Value Result = SV->toJSON(); |
| 63 |
auto *ResultObj = Result.getAsObject(); |
| 64 |
ASSERT_TRUE(ResultObj); |
| 65 |
|
| 66 |
// Run the baseline. |
| 67 |
json::Object BaselineResult; |
| 68 |
auto E = runBaselineMCA(BaselineResult, MCIs); |
| 69 |
ASSERT_FALSE(bool(E)) << "Failed to run baseline"; |
| 70 |
auto *BaselineObj = BaselineResult.getObject(SV->getNameAsString()); |
| 71 |
ASSERT_TRUE(BaselineObj) << "Does not contain SummaryView result"; |
| 72 |
|
| 73 |
// Compare the results. |
| 74 |
constexpr const char *Fields[] = {"Instructions", "TotalCycles", "TotaluOps", |
| 75 |
"BlockRThroughput"}; |
| 76 |
for (const auto *F : Fields) { |
| 77 |
auto V = ResultObj->getInteger(F); |
| 78 |
auto BV = BaselineObj->getInteger(F); |
| 79 |
ASSERT_TRUE(V && BV); |
| 80 |
ASSERT_EQ(*BV, *V) << "Value of '" << F << "' does not match"; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
TEST_F(X86TestBase, TestInstructionRecycling) { |
| 85 |
mca::Context MCA(*MRI, *STI); |
| 86 |
|
| 87 |
std::unordered_map<const mca::InstrDesc *, SmallPtrSet<mca::Instruction *, 2>> |
| 88 |
RecycledInsts; |
| 89 |
auto GetRecycledInst = [&](const mca::InstrDesc &Desc) -> mca::Instruction * { |
| 90 |
auto It = RecycledInsts.find(&Desc); |
| 91 |
if (It != RecycledInsts.end()) { |
| 92 |
auto &Insts = It->second; |
| 93 |
if (Insts.size()) { |
| 94 |
mca::Instruction *I = *Insts.begin(); |
| 95 |
Insts.erase(I); |
| 96 |
return I; |
| 97 |
} |
| 98 |
} |
| 99 |
return nullptr; |
| 100 |
}; |
| 101 |
auto AddRecycledInst = [&](mca::Instruction *I) { |
| 102 |
const mca::InstrDesc &D = I->getDesc(); |
| 103 |
RecycledInsts[&D].insert(I); |
| 104 |
}; |
| 105 |
|
| 106 |
mca::IncrementalSourceMgr ISM; |
| 107 |
ISM.setOnInstFreedCallback(AddRecycledInst); |
| 108 |
|
| 109 |
// Empty CustomBehaviour. |
| 110 |
auto CB = std::make_unique<mca::CustomBehaviour>(*STI, ISM, *MCII); |
| 111 |
|
| 112 |
auto PO = getDefaultPipelineOptions(); |
| 113 |
auto P = MCA.createDefaultPipeline(PO, ISM, *CB); |
| 114 |
ASSERT_TRUE(P); |
| 115 |
|
| 116 |
SmallVector<MCInst> MCIs; |
| 117 |
getSimpleInsts(MCIs, /*Repeats=*/100); |
| 118 |
|
| 119 |
// Add views. |
| 120 |
auto SV = std::make_unique<SummaryView>(STI->getSchedModel(), MCIs, |
| 121 |
PO.DispatchWidth); |
| 122 |
P->addEventListener(SV.get()); |
| 123 |
|
| 124 |
// Default InstrumentManager |
| 125 |
auto IM = std::make_unique<mca::InstrumentManager>(*STI, *MCII); |
| 126 |
|
| 127 |
mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get(), *IM); |
| 128 |
IB.setInstRecycleCallback(GetRecycledInst); |
| 129 |
|
| 130 |
const SmallVector<mca::SharedInstrument> Instruments; |
| 131 |
// Tile size = 7 |
| 132 |
for (unsigned i = 0U, E = MCIs.size(); i < E;) { |
| 133 |
for (unsigned TE = i + 7; i < TE && i < E; ++i) { |
| 134 |
Expected<std::unique_ptr<mca::Instruction>> InstOrErr = |
| 135 |
IB.createInstruction(MCIs[i], Instruments); |
| 136 |
|
| 137 |
if (!InstOrErr) { |
| 138 |
mca::Instruction *RecycledInst = nullptr; |
| 139 |
// Check if the returned instruction is a recycled |
| 140 |
// one. |
| 141 |
auto RemainingE = handleErrors(InstOrErr.takeError(), |
| 142 |
[&](const mca::RecycledInstErr &RC) { |
| 143 |
RecycledInst = RC.getInst(); |
| 144 |
}); |
| 145 |
ASSERT_FALSE(bool(RemainingE)); |
| 146 |
ASSERT_TRUE(RecycledInst); |
| 147 |
ISM.addRecycledInst(RecycledInst); |
| 148 |
} else { |
| 149 |
ISM.addInst(std::move(InstOrErr.get())); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Run the pipeline. |
| 154 |
Expected<unsigned> Cycles = P->run(); |
| 155 |
if (!Cycles) { |
| 156 |
// Should be a stream pause error. |
| 157 |
ASSERT_TRUE(Cycles.errorIsA<mca::InstStreamPause>()); |
| 158 |
llvm::consumeError(Cycles.takeError()); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
ISM.endOfStream(); |
| 163 |
// Has to terminate properly. |
| 164 |
Expected<unsigned> Cycles = P->run(); |
| 165 |
ASSERT_TRUE(bool(Cycles)); |
| 166 |
|
| 167 |
json::Value Result = SV->toJSON(); |
| 168 |
auto *ResultObj = Result.getAsObject(); |
| 169 |
ASSERT_TRUE(ResultObj); |
| 170 |
|
| 171 |
// Run the baseline. |
| 172 |
json::Object BaselineResult; |
| 173 |
auto E = runBaselineMCA(BaselineResult, MCIs); |
| 174 |
ASSERT_FALSE(bool(E)) << "Failed to run baseline"; |
| 175 |
auto *BaselineObj = BaselineResult.getObject(SV->getNameAsString()); |
| 176 |
ASSERT_TRUE(BaselineObj) << "Does not contain SummaryView result"; |
| 177 |
|
| 178 |
// Compare the results. |
| 179 |
constexpr const char *Fields[] = {"Instructions", "TotalCycles", "TotaluOps", |
| 180 |
"BlockRThroughput"}; |
| 181 |
for (const auto *F : Fields) { |
| 182 |
auto V = ResultObj->getInteger(F); |
| 183 |
auto BV = BaselineObj->getInteger(F); |
| 184 |
ASSERT_TRUE(V && BV); |
| 185 |
ASSERT_EQ(*BV, *V) << "Value of '" << F << "' does not match"; |
| 186 |
} |
| 187 |
} |