Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

These are a set of features which extend the capablities of the branch predictor simulator #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/bct.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ void BCT_Initial(BCT* BranchChooserTable, uint32_t index_width)
BranchChooserTable->chooser[i] = weakly_bimodal;
}

Predictor BCT_Predict(BCT* BranchChooserTable, uint32_t addr)
Predictor BCT_Predict(BCT* BranchChooserTable, uint32_t addr, uint8_t two_byte_inst)
{
uint32_t index = Get_Index(addr, BranchChooserTable->attributes.index_width);
uint32_t index = Get_Index(addr, BranchChooserTable->attributes.index_width, two_byte_inst);
switch (BranchChooserTable->chooser[index])
{
case strongly_gshare:
Expand All @@ -36,13 +36,13 @@ Predictor BCT_Predict(BCT* BranchChooserTable, uint32_t addr)
}
}

void BCT_Update(BCT* BranchChooserTable, uint32_t addr, Result result)
void BCT_Update(BCT* BranchChooserTable, uint32_t addr, uint8_t two_byte_inst, Result result)
{
if (result.actual_taken == result.predict_taken[BIMODAL] && result.actual_taken == result.predict_taken[GSHARE])
return;
if (result.actual_taken != result.predict_taken[BIMODAL] && result.actual_taken != result.predict_taken[GSHARE])
return;
uint32_t index = Get_Index(addr, BranchChooserTable->attributes.index_width);
uint32_t index = Get_Index(addr, BranchChooserTable->attributes.index_width, two_byte_inst);
if (result.actual_taken == result.predict_taken[GSHARE])
{
switch (BranchChooserTable->chooser[index])
Expand Down Expand Up @@ -74,4 +74,4 @@ void BCT_fprintf(BCT* BranchChooserTable, FILE *fp)
uint32_t i;
for (i = 0; i < BranchChooserTable->attributes.chooser_num; i++)
fprintf(fp, "Choice table[%u]: %u\n", i, BranchChooserTable->chooser[i]);
}
}
8 changes: 5 additions & 3 deletions src/bct.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ void BCT_Initial(BCT* BranchChooserTable, uint32_t index_width);
* Search the BranchChooserTable for PC "addr" and make prediction
* input :
* addr : PC
* two_byte_instr: Are instructions a mix of 4 and 2 byte length. Modifies index function.
* return :
* the prediction on which predictor is chosen
*/
Predictor BCT_Predict(BCT* BranchChooserTable, uint32_t addr);
Predictor BCT_Predict(BCT* BranchChooserTable, uint32_t addr, uint8_t two_byte_inst);

/*
* Update the BranchChooserTable
* input :
* addr : PC
* two_byte_instr: Are instructions a mix of 4 and 2 byte length. Modifies index function.
* result : struct "Result", the prediction and actual result
*/
void BCT_Update(BCT* BranchChooserTable, uint32_t addr, Result result);
void BCT_Update(BCT* BranchChooserTable, uint32_t addr, uint8_t two_byte_inst, Result result);

/*
* Print the content of BranchChooserTable to file *fp
*/
void BCT_fprintf(BCT* BranchChooserTable, FILE *fp);

#endif
#endif
8 changes: 4 additions & 4 deletions src/bht.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ void BHT_Initial(BHT *BranchHistoryTable, uint32_t index_width, uint32_t history
memset(BranchHistoryTable->history, 0, sizeof(uint64_t) * BranchHistoryTable->attributes.history_num);
}

uint64_t BHT_Search(BHT *BranchHistoryTable, uint32_t addr)
uint64_t BHT_Search(BHT *BranchHistoryTable, uint32_t addr, uint8_t two_byte_inst)
{
uint32_t index = Get_Index(addr, BranchHistoryTable->attributes.index_width);
uint32_t index = Get_Index(addr, BranchHistoryTable->attributes.index_width, two_byte_inst);
return BranchHistoryTable->history[index];
}


void BHT_Update(BHT *BranchHistoryTable, uint32_t addr, Result result)
void BHT_Update(BHT *BranchHistoryTable, uint32_t addr, uint8_t two_byte_inst, Result result)
{
uint32_t index = Get_Index(addr, BranchHistoryTable->attributes.index_width);
uint32_t index = Get_Index(addr, BranchHistoryTable->attributes.index_width, two_byte_inst);
uint64_t old_history = BranchHistoryTable->history[index];
old_history = old_history >> 1;
if (result.actual_taken == TAKEN)
Expand Down
9 changes: 5 additions & 4 deletions src/bht.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ void BHT_Initial(BHT *BranchHistoryTable, uint32_t index_width, uint32_t history
* Search the BranchPredictionTable for PC "addr" and make prediction
* input :
* addr : PC
* return :
* two_byte_instr: Are instructions a mix of 4 and 2 byte length. Modifies index function. * return :
* the history pattern of correspounding entry (at most 64 bits, hence uint64_t)
*/
uint64_t BHT_Search(BHT *BranchHistoryTable, uint32_t addr);
uint64_t BHT_Search(BHT *BranchHistoryTable, uint32_t addr, uint8_t two_byte_inst);

/*
* Update the BranchPredictionTable
* input :
* addr : PC
* two_byte_instr: Are instructions a mix of 4 and 2 byte length. Modifies index function.
* result : struct "Result", the prediction and actual result
*/
void BHT_Update(BHT *BranchHistoryTable, uint32_t addr, Result result);
void BHT_Update(BHT *BranchHistoryTable, uint32_t addr, uint8_t two_byte_inst, Result result);

/*
* Print the content of BranchHistoryTable to file *fp
*/
void BHT_fprintf(BHT *BranchHistoryTable, FILE *fp);

#endif
#endif
46 changes: 23 additions & 23 deletions src/bp.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ void Predictor_Init(Predictor type, uint32_t* width)
}
}

Taken_Result Bimodal_Predict(BP_Bimodal *predictor, uint32_t addr)
Taken_Result Bimodal_Predict(BP_Bimodal *predictor, uint32_t addr, uint8_t two_byte_inst)
{
uint32_t index = Get_Index(addr, ((BPT *)predictor)->attributes.index_width);
uint32_t index = Get_Index(addr, ((BPT *)predictor)->attributes.index_width, two_byte_inst);
return BPT_Predict((BPT *)predictor, index);
}

Taken_Result Gshare_Predict(BP_Gshare *predictor, uint32_t addr)
Taken_Result Gshare_Predict(BP_Gshare *predictor, uint32_t addr, uint8_t two_byte_inst)
{
uint32_t h = predictor->global_history_register->attributes.history_width;
uint32_t i = predictor->branch_prediction_table->attributes.index_width;
uint32_t index = Get_Index(addr, i);
uint32_t index = Get_Index(addr, i, two_byte_inst);
uint32_t history = predictor->global_history_register->history;
uint32_t mask = (1 <<((i-h)))-1;
uint32_t index_tail = index & mask;
Expand All @@ -108,28 +108,28 @@ Taken_Result Gshare_Predict(BP_Gshare *predictor, uint32_t addr)
return BPT_Predict(predictor->branch_prediction_table, index);
}

Result Predictor_Predict(uint32_t addr)
Result Predictor_Predict(uint32_t addr, uint8_t two_byte_inst)
{
Result result;
result.predict_predictor = branch_predictor->predictor_type;
switch (branch_predictor->predictor_type)
{
case bimodal:
{
result.predict_taken[BIMODAL] = Bimodal_Predict((BP_Bimodal *)branch_predictor->predictor, addr);
result.predict_taken[BIMODAL] = Bimodal_Predict((BP_Bimodal *)branch_predictor->predictor, addr, two_byte_inst);
return result;
}
case gshare:
{
result.predict_taken[GSHARE] = Gshare_Predict((BP_Gshare *)branch_predictor->predictor, addr);
result.predict_taken[GSHARE] = Gshare_Predict((BP_Gshare *)branch_predictor->predictor, addr, two_byte_inst);
return result;
}
case hybrid:
{
BP_Hybrid *predictor = branch_predictor->predictor;
result.predict_taken[BIMODAL] = Bimodal_Predict(predictor->bp_bimodal, addr);
result.predict_taken[GSHARE] = Gshare_Predict(predictor->bp_gshare, addr);
result.predict_predictor = BCT_Predict(predictor->branch_chooser_table, addr);
result.predict_taken[BIMODAL] = Bimodal_Predict(predictor->bp_bimodal, addr, two_byte_inst);
result.predict_taken[GSHARE] = Gshare_Predict(predictor->bp_gshare, addr, two_byte_inst);
result.predict_predictor = BCT_Predict(predictor->branch_chooser_table, addr, two_byte_inst);
if (result.predict_predictor == bimodal)
result.predict_taken[HYBRID] = result.predict_taken[BIMODAL];
else
Expand All @@ -139,7 +139,7 @@ Result Predictor_Predict(uint32_t addr)
case yeh_patt:
{
BP_Yeh_Patt *predictor = branch_predictor->predictor;
uint64_t history = BHT_Search(predictor->branch_histroy_table, addr);
uint64_t history = BHT_Search(predictor->branch_histroy_table, addr, two_byte_inst);
result.predict_taken[YEH_PATT] = BPT_Predict(predictor->branch_predition_table, history);
return result;
}
Expand All @@ -148,17 +148,17 @@ Result Predictor_Predict(uint32_t addr)
}
}

void Bimodal_Update(BP_Bimodal *predictor, uint32_t addr, Result result)
void Bimodal_Update(BP_Bimodal *predictor, uint32_t addr, uint8_t two_byte_inst, Result result)
{
uint32_t index = Get_Index(addr, ((BPT *)predictor)->attributes.index_width);
uint32_t index = Get_Index(addr, ((BPT *)predictor)->attributes.index_width, two_byte_inst);
BPT_Update(predictor, index, result);
}

void Gshare_Update(BP_Gshare *predictor, uint32_t addr, Result result)
void Gshare_Update(BP_Gshare *predictor, uint32_t addr, uint8_t two_byte_inst, Result result)
{
uint32_t h = predictor->global_history_register->attributes.history_width;
uint32_t i = predictor->branch_prediction_table->attributes.index_width;
uint32_t index = Get_Index(addr, i);
uint32_t index = Get_Index(addr, i, two_byte_inst);
uint32_t history = predictor->global_history_register->history;
uint32_t mask = (1 <<((i-h)))-1;
uint32_t index_tail = index & mask;
Expand All @@ -167,39 +167,39 @@ void Gshare_Update(BP_Gshare *predictor, uint32_t addr, Result result)
BPT_Update(predictor->branch_prediction_table, index, result);
}

void Predictor_Update(uint32_t addr, Result result)
void Predictor_Update(uint32_t addr, uint8_t two_byte_inst, Result result)
{
switch (branch_predictor->predictor_type)
{
case bimodal:
{
Bimodal_Update((BP_Bimodal *)branch_predictor->predictor, addr, result);
Bimodal_Update((BP_Bimodal *)branch_predictor->predictor, addr, two_byte_inst, result);
return;
}
case gshare:
{
BP_Gshare *predictor = branch_predictor->predictor;
Gshare_Update(predictor, addr, result);
Gshare_Update(predictor, addr, two_byte_inst, result);
GHR_Update(predictor->global_history_register, result);
return;
}
case hybrid:
{
BP_Hybrid *predictor = branch_predictor->predictor;
if (result.predict_predictor == bimodal)
Bimodal_Update(predictor->bp_bimodal, addr, result);
Bimodal_Update(predictor->bp_bimodal, addr, two_byte_inst, result);
else
Gshare_Update(predictor->bp_gshare, addr, result);
Gshare_Update(predictor->bp_gshare, addr, two_byte_inst, result);
GHR_Update(predictor->bp_gshare->global_history_register, result);
BCT_Update(predictor->branch_chooser_table, addr, result);
BCT_Update(predictor->branch_chooser_table, addr, two_byte_inst, result);
return;
}
case yeh_patt:
{
BP_Yeh_Patt *predictor = branch_predictor->predictor;
uint64_t history = BHT_Search(predictor->branch_histroy_table, addr);
uint64_t history = BHT_Search(predictor->branch_histroy_table, addr, two_byte_inst);
BPT_Update(predictor->branch_predition_table, history, result);
BHT_Update(predictor->branch_histroy_table, addr, result);
BHT_Update(predictor->branch_histroy_table, addr, two_byte_inst, result);
return;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/bp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,36 @@ void Predictor_Init(Predictor name, uint32_t* width);
/*
* Prediction on taken_or_not of bimodal predictor
*/
Taken_Result Bimodal_Predict(BP_Bimodal *predictor, uint32_t addr);
Taken_Result Bimodal_Predict(BP_Bimodal *predictor, uint32_t addr, uint8_t two_byte_inst);

/*
* Prediction on taken_or_not of gshare predictor
*/
Taken_Result Gshare_Predict(BP_Gshare *predictor, uint32_t addr);
Taken_Result Gshare_Predict(BP_Gshare *predictor, uint32_t addr, uint8_t two_byte_inst);

/*
* Prediction of bimodal predictor
*/
Result Predictor_Predict(uint32_t addr);
Result Predictor_Predict(uint32_t addr, uint8_t two_byte_inst);

/*
* Update bimodal prediction table
*/
void Bimodal_Update(BP_Bimodal *predictor, uint32_t addr, Result result);
void Bimodal_Update(BP_Bimodal *predictor, uint32_t addr, uint8_t two_byte_inst, Result result);

/*
* Update gshare prediction table
*/
void Gshare_Update(BP_Gshare *predictor, uint32_t addr, Result result);
void Gshare_Update(BP_Gshare *predictor, uint32_t addr, uint8_t two_byte_inst, Result result);

/*
* Update predictior
*/
void Predictor_Update(uint32_t addr, Result result);
void Predictor_Update(uint32_t addr, uint8_t two_byte_inst, Result result);

/*
* Print the content of branch_predictor to file *fp
*/
void BP_fprintf(FILE *fp);

#endif
#endif
56 changes: 34 additions & 22 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ char *trace_file;

int main(int argc, char *argv[])
{
uint8_t two_byte_inst;
#ifdef DBG
debug_fp = fopen("debug.txt", "w");
if (debug_fp == NULL)
Expand All @@ -26,7 +27,7 @@ int main(int argc, char *argv[])

Predictor type;
uint32_t width[9];
parse_arguments(argc, argv, &type, width);
parse_arguments(argc, argv, &type, width, &two_byte_inst);

branch_target_buffer = NULL;
branch_predictor = NULL;
Expand Down Expand Up @@ -59,31 +60,42 @@ int main(int argc, char *argv[])
/* read the trace */
uint8_t take_or_not, line;
uint32_t addr;
int rr = fscanf(trace_file_fp, "%x %c%c", &addr, &take_or_not, &line);
char buf[256];
char * rr = fgets(buf, 256, trace_file_fp);
trace_count++;
if (rr == EOF)
if (rr == NULL)
break;

/* make branch prediction */
Result result = Predictor_Predict(addr);
if (branch_target_buffer == NULL)
result.predict_branch = branch;
else
result.predict_branch = BTB_Predict(addr);

result.actual_branch = branch;
if (take_or_not == 't')
result.actual_taken = taken;
// check for the begining of the branch trace. Remove results from stats
// as the earlier section as bp training.
if(!strncmp(buf, "BEGIN", 5)) {
printf("Reset stats\n");
Stat_Init();
}
else
result.actual_taken = not_taken;

/* update the predictor and statistic data */
Update_Stat(result);
if (result.predict_branch == branch)
Predictor_Update(addr, result);
if (branch_target_buffer != NULL)
BTB_Update(addr, result, trace_count);

{
sscanf(buf, "%x %c%c", &addr, &take_or_not, &line);

/* make branch prediction */
Result result = Predictor_Predict(addr, two_byte_inst);
if (branch_target_buffer == NULL)
result.predict_branch = branch;
else
result.predict_branch = BTB_Predict(addr);

result.actual_branch = branch;
if (take_or_not == 't')
result.actual_taken = taken;
else
result.actual_taken = not_taken;

/* update the predictor and statistic data */
Update_Stat(result);
if (result.predict_branch == branch)
Predictor_Update(addr, two_byte_inst, result);
if (branch_target_buffer != NULL)
BTB_Update(addr, result, trace_count);
}
}

FILE *fp = stdout;
Expand Down
Loading