系列文章 : [gem5] 從零開始的 gem5 學習筆記
在 Inside the Minor CPU model 這一篇的 Fetch1 stage 章節已經講述的很清楚了,這邊簡單做個中文的筆記!
fetch1 stage 是 gem5 minor CPU 的 pipeline 的第一個 stage。負責發出 instruction fetch 的請求,於是 class Fetch1 有一個 IcachePort,負責從外部抓資料進來。
class Fetch1 的 evaluate method 每一個 cycle 都會執行一次,就如同用 RTL 實作的 5 stage in-order CPU,假如沒有 bubble 或 stall 的話,每一個 cycle 都會把資料丟到往下一個 stage 傳遞的 pipeline register。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L576
從 Fetch2 Stage 會傳來 branch prediction 的資訊,在 Fetch2 Stage 遇到 branch 的話,會先在 Fetch2 Stage 預測跳或不跳。
從 Execute Stage 會傳來 branch 的資訊 ( Execute Stage 會知道實際上 branch 要跳或不跳 )
從 Fetch2 Stage 跟 Execute Stage 傳來的 Branch 資訊會讓 Fetch1 Stage 改變要 fetch 的 address ( change of stream )。
當 Execute Stage 跟 Fetch2 Stage 同時傳來 Branch 資訊時,會以 Execute Stage 的資訊優先。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L588-L646
每個在 Fetch1 stage 經由 IcachePort 取得的 line,會被賦予一個獨特的 lineSeqNum,方便我們可以 debug。
僅有當 Fetch1 成功在 Fetch2 input buffer 中保留一個空位的時候,才會去 IcachePort 發出一個 memory 請求。
Fetch1 有兩個 queue
當 Fetch1 新增一個請求的時候,會把一個 request 壓進 requests queue 裡面,並且將這個 request 送去給 MMU,以便取得 address。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L191-L200
當 Fetch1 Stage 接收到來自 MMU ( or iTLB ) 的 response,我們會把 request 從 requests queue 搬到 transfers queue,並將該 request 從 IcachePort 發出去給 memory。
當收到來自於 memory 的 reponse 之後,會將 request 的狀態改成 Complate。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L432
在每一個 Cycle 都會執行的 Fetch1::evaluate 裡面,會把已經變為 Complete 的 request ( 也就是來自 memory 的 reponse ) 放進 ForwardLineData 裡面,並把這個資訊交給 f1ToF2 的 Latch,讓 Fetch2 在下一個 Cycle 可以收到這個資料。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L670-L692
假如我們將 Fetch2 的 input buffer size 設為 1,表示我們不會進行 prefetching。
每個 Cycle 最多丟給 Fetch2 一個 ForwardLineData。
https://github.com/gem5/gem5/blob/develop/src/cpu/minor/BaseMinorCPU.py#L341
當一個 change of stream 發生的時候 ( 例如我們依照 Fetch2 Stage 傳來的 Branch Prediction 進行跳躍,或是依照 Execute Stage 傳來的 Branch 資訊進行跳躍 ),我們會將 transfers queue 裡面已經標示為 Complete 的 FetchRequest 丟棄。
https://github.com/gem5/gem5/blob/v25.1.0.0/src/cpu/minor/fetch1.cc#L675-L683