Skip to content

Commit

Permalink
clang format and finessing
Browse files Browse the repository at this point in the history
  • Loading branch information
ABenC377 committed Feb 7, 2024
1 parent 13e79e2 commit 4351315
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 57 deletions.
6 changes: 2 additions & 4 deletions configs/sst-cores/a64fx-sst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ Queue-Sizes:
Load: 40
Store: 24
Branch-Predictor:
Type: "Generic"
Type: "Perceptron"
BTB-Tag-Bits: 11
Saturating-Count-Bits: 2
Global-History-Length: 11
Global-History-Length: 19
RAS-entries: 8
Fallback-Static-Predictor: "Always-Taken"
L1-Data-Memory:
Interface-Type: External
L1-Instruction-Memory:
Expand Down
6 changes: 2 additions & 4 deletions configs/sst-cores/m1_firestorm-sst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ Queue-Sizes:
Load: 130
Store: 60
Branch-Predictor:
Type: "Generic"
Type: "Perceptron"
BTB-Tag-Bits: 11
Saturating-Count-Bits: 2
Global-History-Length: 11
Global-History-Length: 11
RAS-entries: 8
Fallback-Static-Predictor: "Always-Taken"
L1-Data-Memory:
Interface-Type: External
L1-Instruction-Memory:
Expand Down
4 changes: 1 addition & 3 deletions configs/sst-cores/tx2-sst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ Queue-Sizes:
Load: 64
Store: 36
Branch-Predictor:
Type: "Generic"
Type: "Perceptron"
BTB-Tag-Bits: 11
Saturating-Count-Bits: 2
Global-History-Length: 10
RAS-entries: 5
Fallback-Static-Predictor: "Always-Taken"
L1-Data-Memory:
Interface-Type: External
L1-Instruction-Memory:
Expand Down
15 changes: 3 additions & 12 deletions src/include/simeng/PerceptronPredictor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,9 @@ class PerceptronPredictor : public BranchPredictor {
void flush(uint64_t address) override;

private:
/** Returns the dot product of a perceptron and a history vector. Used to determine
* a direction prediction */
int64_t getDotProduct(std::vector<int8_t> perceptron, uint64_t history) {
int64_t Pout = perceptron[globalHistoryLength_];
for (int i = 0; i < globalHistoryLength_; i++) {
// Get branch direction for ith entry in the history
bool historyTaken =
((history & (1 << ((globalHistoryLength_ - 1) - i))) != 0);
Pout += historyTaken ? perceptron[i] : (0 - perceptron[i]);
}
return Pout;
}
/** Returns the dot product of a perceptron and a history vector. Used to
* determine a direction prediction */
int64_t getDotProduct(std::vector<int8_t> perceptron, uint64_t history);

/** The length in bits of the BTB index; BTB will have 2^bits entries. */
uint64_t btbBits_;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/CoreInstance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ void CoreInstance::createCore() {
arch_ = std::make_unique<arch::aarch64::Architecture>(kernel_);
}

std::string predictorType = config_["Branch-Predictor"]["Type"].as<std::string>();
std::string predictorType =
config_["Branch-Predictor"]["Type"].as<std::string>();
if (predictorType == "Generic") {
predictor_ = std::make_unique<GenericPredictor>();
} else if (predictorType == "Perceptron") {
Expand Down
40 changes: 28 additions & 12 deletions src/lib/PerceptronPredictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ PerceptronPredictor::PerceptronPredictor(ryml::ConstNodeRef config)
// Build BTB based on config options
uint32_t btbSize = (1 << btbBits_);
btb_.resize(btbSize);
// Initialise perceptron values with 0 for the global history weights, and 1 for the bias weight;
// and initialise the target with 0 (i.e., unknown)
// Initialise perceptron values with 0 for the global history weights, and 1
// for the bias weight; and initialise the target with 0 (i.e., unknown)
for (int i = 0; i < btbSize; i++) {
btb_[i].first.assign(globalHistoryLength_, 0);
btb_[i].first.push_back(1);
Expand All @@ -29,14 +29,15 @@ PerceptronPredictor::~PerceptronPredictor() {

BranchPrediction PerceptronPredictor::predict(uint64_t address, BranchType type,
int64_t knownOffset) {
// Get the hashed index for the prediction table. XOR the global history with the
// non-zero bits of the address, and then keep only the btbBits_ bits of the output
// to keep it in bounds of the prediction table.
uint64_t hashedIndex = ((address >> 2) ^ globalHistory_) & ((1 << btbBits_) - 1);
// Get the hashed index for the prediction table. XOR the global history with
// the non-zero bits of the address, and then keep only the btbBits_ bits of
// the output to keep it in bounds of the prediction table.
uint64_t hashedIndex =
((address >> 2) ^ globalHistory_) & ((1 << btbBits_) - 1);

// Store the global history for correct hashing in update() --
// needs to be global history and not the hashed index as hashing loses information at longer
// global history lengths
// needs to be global history and not the hashed index as hashing loses
// information at longer global history lengths
btbHistory_[address] = globalHistory_;

// Retrieve the perceptron from the BTB
Expand Down Expand Up @@ -93,17 +94,20 @@ void PerceptronPredictor::update(uint64_t address, bool taken,
int64_t Pout = getDotProduct(perceptron, prevGlobalHistory);
bool directionPrediction = (Pout >= 0);

// Update the perceptron if the prediction was wrong, or the dot product's magnitude
// was not greater than the training threshold
// Update the perceptron if the prediction was wrong, or the dot product's
// magnitude was not greater than the training threshold
if ((directionPrediction != taken) || (abs(Pout) < trainingThreshold_)) {
int8_t t = (taken) ? 1 : -1;

for (int i = 0; i < globalHistoryLength_; i++) {
int8_t xi =
((prevGlobalHistory & (1 << ((globalHistoryLength_ - 1) - i))) == 0) ? -1 : 1;
((prevGlobalHistory & (1 << ((globalHistoryLength_ - 1) - i))) == 0)
? -1
: 1;
int8_t product_xi_t = xi * t;
// Make sure no overflow (+-127)
if (!(perceptron[i] == 127 && product_xi_t == 1) && !(perceptron[i] == -127 && product_xi_t == -1)) {
if (!(perceptron[i] == 127 && product_xi_t == 1) &&
!(perceptron[i] == -127 && product_xi_t == -1)) {
perceptron[i] += product_xi_t;
}
}
Expand Down Expand Up @@ -141,4 +145,16 @@ void PerceptronPredictor::flush(uint64_t address) {
}
}

int64_t PerceptronPredictor::getDotProduct(std::vector<int8_t> perceptron,
uint64_t history) {
int64_t Pout = perceptron[globalHistoryLength_];
for (int i = 0; i < globalHistoryLength_; i++) {
// Get branch direction for ith entry in the history
bool historyTaken =
((history & (1 << ((globalHistoryLength_ - 1) - i))) != 0);
Pout += historyTaken ? perceptron[i] : (0 - perceptron[i]);
}
return Pout;
}

} // namespace simeng
23 changes: 10 additions & 13 deletions src/lib/config/ModelConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ void ModelConfig::setExpectations(bool isDefault) {
ExpectationNode::createExpectation("Branch-Predictor"));

expectations_["Branch-Predictor"].addChild(
ExpectationNode::createExpectation<std::string>(
"Perceptron", "Type"));
ExpectationNode::createExpectation<std::string>("Perceptron", "Type"));
expectations_["Branch-Predictor"]["Type"].setValueSet(
std::vector<std::string>{"Generic", "Perceptron"});

Expand All @@ -435,14 +434,13 @@ void ModelConfig::setExpectations(bool isDefault) {
expectations_["Branch-Predictor"]["BTB-Tag-Bits"].setValueBounds<uint8_t>(1,
64);
// Saturating counter bits are relevant to the GenericPredictor only
if (!isDefault) {
if (configTree_["Branch-Predictor"]["Type"].as<std::string>() == "Generic") {
expectations_["Branch-Predictor"].addChild(
ExpectationNode::createExpectation<uint8_t>(2,
"Saturating-Count-Bits"));
expectations_["Branch-Predictor"]["Saturating-Count-Bits"]
.setValueBounds<uint8_t>(1, 64);
}
if (!isDefault &&
configTree_["Branch-Predictor"]["Type"].as<std::string>() == "Generic") {
expectations_["Branch-Predictor"].addChild(
ExpectationNode::createExpectation<uint8_t>(2,
"Saturating-Count-Bits"));
expectations_["Branch-Predictor"]["Saturating-Count-Bits"]
.setValueBounds<uint8_t>(1, 64);
}

expectations_["Branch-Predictor"].addChild(
Expand All @@ -456,14 +454,13 @@ void ModelConfig::setExpectations(bool isDefault) {
1, UINT16_MAX);

// The fallback predictor is relevant to the GenericPredictor only
if (!isDefault) {
if (configTree_["Branch-Predictor"]["Type"].as<std::string>() == "Generic") {
if (!isDefault &&
configTree_["Branch-Predictor"]["Type"].as<std::string>() == "Generic") {
expectations_["Branch-Predictor"].addChild(
ExpectationNode::createExpectation<std::string>(
"Always-Taken", "Fallback-Static-Predictor"));
expectations_["Branch-Predictor"]["Fallback-Static-Predictor"].setValueSet(
std::vector<std::string>{"Always-Taken", "Always-Not-Taken"});
}
}

// L1-Data-Memory
Expand Down
9 changes: 5 additions & 4 deletions test/integration/ConfigTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ TEST(ConfigTest, Default) {
"'FloatingPoint/SVE-Count': 32\n 'Predicate-Count': 17\n "
"'Conditional-Count': 1\n 'Matrix-Count': 1\n'Pipeline-Widths':\n "
"Commit: 1\n FrontEnd: 1\n 'LSQ-Completion': 1\n'Queue-Sizes':\n ROB: "
"32\n Load: 16\n Store: 16\n'Branch-Predictor':\n Type: Perceptron\n 'BTB-Tag-Bits': 8\n "
"32\n Load: 16\n Store: 16\n'Branch-Predictor':\n Type: Perceptron\n "
"'BTB-Tag-Bits': 8\n "
" 'Global-History-Length': 8\n "
"'RAS-entries': 8\n'L1-Data-Memory':\n 'Interface-Type': "
"Flat\n'L1-Instruction-Memory':\n 'Interface-Type': "
Expand Down Expand Up @@ -103,9 +104,9 @@ TEST(ConfigTest, Default) {
"100000\n'Register-Set':\n 'GeneralPurpose-Count': 32\n "
"'FloatingPoint-Count': 32\n'Pipeline-Widths':\n Commit: 1\n FrontEnd: "
"1\n 'LSQ-Completion': 1\n'Queue-Sizes':\n ROB: 32\n Load: 16\n "
"Store: 16\n'Branch-Predictor':\n Type: Perceptron\n 'BTB-Tag-Bits': 8\n"
" 'Global-History-Length': 8\n "
"'RAS-entries': 8\n'L1-Data-Memory':\n 'Interface-Type': "
"Store: 16\n'Branch-Predictor':\n Type: Perceptron\n 'BTB-Tag-Bits': "
"8\n 'Global-History-Length': 8\n 'RAS-entries': "
"8\n'L1-Data-Memory':\n 'Interface-Type': "
"Flat\n'L1-Instruction-Memory':\n 'Interface-Type': "
"Flat\n'LSQ-L1-Interface':\n 'Access-Latency': 4\n Exclusive: 0\n "
"'Load-Bandwidth': 32\n 'Store-Bandwidth': 32\n "
Expand Down
5 changes: 3 additions & 2 deletions test/regression/RegressionTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ void RegressionTest::run(const char* source, const char* triple,
createPortAllocator();

// Create a branch predictor for a pipelined core
simeng::PerceptronPredictor predictor = simeng::PerceptronPredictor();
std::unique_ptr<simeng::BranchPredictor> predictor_ = nullptr;
std::string predictorType = simeng::config::SimInfo::getConfig()["Branch-Predictor"]["Type"].as<std::string>();
std::string predictorType =
simeng::config::SimInfo::getConfig()["Branch-Predictor"]["Type"]
.as<std::string>();
if (predictorType == "Generic") {
predictor_ = std::make_unique<simeng::GenericPredictor>();
} else if (predictorType == "Perceptron") {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/PerceptronPredictorTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ TEST_F(PerceptronPredictorTest, RAS) {
EXPECT_EQ(prediction.target, 12);
}

// Tests that the PerceptronPredictor will predict a previously encountered branch
// correctly, when no address aliasing has occurred
// Tests that the PerceptronPredictor will predict a previously encountered
// branch correctly, when no address aliasing has occurred
TEST_F(PerceptronPredictorTest, Hit) {
simeng::config::SimInfo::addToConfig(
"{Branch-Predictor: {Type: Perceptron, BTB-Tag-Bits: 11, "
Expand Down

0 comments on commit 4351315

Please sign in to comment.