Skip to content

Commit

Permalink
rough draft of handling JMP
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon540 committed Dec 1, 2024
1 parent 984dad9 commit f559f2c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
6 changes: 6 additions & 0 deletions core/BPTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ namespace olympia
uint64_t correctedPC;
};

enum class branchType {
CONDITIONAL_BRANCH,
JMP,
RET
};

}
13 changes: 12 additions & 1 deletion core/BPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace olympia
{
BPU::name = "bpu";
BPU::BPU(sparta::TreeNode* node, BPUParameterSet* p) :
sparta::Unit(node)
sparta::Unit(node),
ghr_size_(p->ghr_size)

{
in_fetch_prediction_credits_.createConsumerHandler(
Expand All @@ -24,6 +25,16 @@ namespace olympia
void BPU::recievePredictionInput(PredictionInput) {
std::cout << "hello" << std::endl;
}

void BPU::updateGHRTaken() {
ghr_ = ghr_ << 1;
ghr_ = ghr_ | 0b1;
}

void BPU::updateGHRNotTaken() {
ghr_ = ghr_ << 1;
ghr_ = ghr_ & 0b0;
}
}


15 changes: 12 additions & 3 deletions core/BPU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <stack>

namespace olympia
{
namespace BPU
{
class BPU : public sparta::Unit
{
Expand All @@ -24,6 +22,8 @@ namespace BPU
BPUParameterSet(sparta::TreeNode* n) : sparta::ParameterSet(n) {}

// Parameters for the Branch Prediction Unit
PARAMETER(uint32_t, ghr_size, 1024, "Number of history bits in GHR");


};
static const char name[];
Expand All @@ -33,6 +33,16 @@ namespace BPU
void receivePredictionCredits_(uint32_t);
void recievePredictionInput(PredictionInput);

// update GHR when last branch is taken
void updateGHRTaken();
// update GHR when last branch is not taken
void updateGHRNotTaken();

// Number of history bits in GHR
uint32_t ghr_size_;

// Global History Register(GHR)
uint32_t ghr_ = 0;;

// input ports
// verify cycle delay required
Expand Down Expand Up @@ -65,5 +75,4 @@ namespace BPU
"misprediction/total_prediction", getStatisticSet(),
"pred_req_num/mispred_num"};
};
}
}
70 changes: 40 additions & 30 deletions core/BasePredictor.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
//
// Created by shobhit on 12/1/24.
//

#include "BasePredictor.hpp"

namespace olympia
{
// PHT
// Pattern History Table
void PatternHistoryTable::incrementCounter(uint32_t idx) {
if(pattern_history_table_[idx] == ctr_bits_val_) {
if(pattern_history_table_.at(idx) == ctr_bits_val_) {
return;
}
else {
pattern_history_table_[idx]++;
pattern_history_table_.at(idx)++;
}
}

void PatternHistoryTable::decrementCounter(uint32_t idx) {
if(pattern_history_table_[idx] == 0) {
if(pattern_history_table_.at(idx) == 0) {
return;
}
else {
pattern_history_table_[idx]--;
pattern_history_table_.at(idx)--;
}
}

uint8_t PatternHistoryTable::getPrediction(uint32_t idx) {
return pattern_history_table_[idx];
return pattern_history_table_.at(idx);
}

// Branch Target Buffer
bool BranchTargetBuffer::isHit(uint64_t PC) {
if(branch_target_buffer_.count(PC)) {
return true;
}
else {
return false;
}
}

// BTB

uint64_t BranchTargetBuffer::getPredictedPC(uint64_t PC) {
if(branch_target_buffer_.count(PC)) {
return branch_target_buffer_[PC];
Expand Down Expand Up @@ -60,7 +66,7 @@ bool BranchTargetBuffer::removeEntry(uint64_t PC) {
}
}

// RAS
// Return Address Stack
bool ReturnAddressStack::pushAddress(uint64_t address) {
if(return_address_stack_.size() == ras_size_) {
return false;
Expand All @@ -83,28 +89,32 @@ uint64_t ReturnAddressStack::popAddress() {
}

// BasePredictor
void BasePredictor::handlePredictionReq() {
return;
void BasePredictor::handlePredictionReq(PredictionInput predIn) {
if(predIn.instType == branchType.JMP) {
handleJMP(predIn);
}
else if(predIn.instType == branchType.RET ||
predIn.instType == branchType.CONDITIONAL_BRANCH) {
// make prediction
// event to send prediction to fetch
}
}

PredictionOutput BasePredictor::makePrediction(PredictionInput predInput) {

PredictionOutput predOutput;
void BasePredictor::handleJMP(PredictionInput predInput) {
// push PC to RAS
returnAddressStack.pushAddress(predInput.PC);

// branch instruction
if(predInput.instType == 1) {
predOutput.predDirection = false;
predOutput.predPC = 5;
// PC hit on BTB?
if(branchTargetBuffer.isHit(predInput.PC)) {
// event to send PC
}
// call instructions
//else if(predInput.instType == 2) {
// in this case no prediction is made, only data is stored for future reference
//}
// ret instruction
else if(predInput.instType == 3) {
predOutput.predDirection = true;
predOutput.predPC = returnAddressStack.popAddress();
else {
// allocate entry in BTB
}
return predOutput;

}

void BasePredictor::handleRET(PredictionInput predInput);
void BasePredictor::handleBRANCH(PredictionInput predInput);

}
26 changes: 18 additions & 8 deletions core/BasePredictor.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <map>
#include "BPTypes.hpp"

namespace olympia
{
class PatternHistoryTable
Expand All @@ -21,7 +24,7 @@ namespace olympia
const uint8_t ctr_bits_;
const uint8_t ctr_bits_val_;

uint8_t pattern_history_table_[pht_size_];
std::map<uint64_t, uint8_t> pattern_history_table_;

};

Expand All @@ -30,13 +33,14 @@ namespace olympia
public:
BranchTargetBuffer(uint32_t btb_size) : btb_size_(btb_size) {}

bool isHit(uint64_t PC);
uint64_t getPredictedPC(uint64_t PC);
bool addEntry(uint64_t PC, uint64_t targetPC);
bool removeEntry(uint64_t PC);

private:
const uint32_t btb_size_;
std::map <uint64_t, uint64_t> branch_target_buffer_; // define size of this map too
std::map <uint64_t, uint64_t> branch_target_buffer_;
};

class ReturnAddressStack
Expand All @@ -58,15 +62,21 @@ namespace olympia
{
public:
BasePredictor(uint32_t pht_size, uint8_t ctr_bits,
uint32_t btb_size, uint32_t ras_size)
{
PatternHistoryTable patternHistoryTable(pht_size, ctr_bits);
BranchTargetBuffer branchTargetBuffer(btb_size);
ReturnAddressStack returnAddressStack(ras_size);
}
uint32_t btb_size, uint32_t ras_size) :
patternHistoryTable(pht_size, ctr_bits),
branchTargetBuffer(bht_size),
returnAddressStack(ras_size);
{}

PatternHistoryTable patternHistoryTable;
BranchTargetBuffer branchTargetBuffer;
ReturnAddressStack returnAddressStack;

void receivedPredictionReq();
PredictionOutput makePrediction(PredictionInput predInput);
void handleJMP(PredictionInput predInput);
void handleRET(PredictionInput predInput);
void handleBRANCH(PredictionInput predInput);

};
}
Expand Down

0 comments on commit f559f2c

Please sign in to comment.