MoonChin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Quiz 6 Problem X - Oral Test :::info **AI Tools Usage** Use Gemini for grammar correction ::: ## Question 9 ### Why is branch resolution often moved earlier in modern pipelines? #### Answers 1. Branch Misprediction Penalty In this slide from the lecture materials, the updated PC is only written back during the MEM stage. * When a branch is taken, or a jump occurs, the next instructions already in the pipeline are incorrect. * Under the default design we'll lose 3 cycles to no-ops because by the time the updated PC is known in the MEM stage, three unwanted instructinos have already entered the pipeline. * To fix this, the processor must convert these incorrect instructions into no-ops. * Moving resolution earlier, e.g., to the ID stage can reduce the number of instructions that need to be flushed, thereby decreasing the penalty or wasted cycles when a misprediction occurs. 2. Reduce the cost of control hazard * The later a branch is resolved, the longer the pipeline must stall or the more no-ops must be inserted to ensure correct results. * Every control hazard that requires a stall or a flush increases the average CPI above the ideal value of 1. * Increasing pipeline depth, for example, 10 or 15 stages, increases the potential for all types of hazards. In such pipelines, resolving branches early is critical to prevennt the CPI from rising significantly due to long control hazard stalls. #### Discussion using [MyCPU (3-pipeline)](https://github.com/moonchin819/ca2025-mycpu/blob/main/3-pipeline/src/main/scala/riscv/core/fivestage_final/Control.scala) `Control.scala` in `fivestage_final` from MyCPU provides the evidence for this question. It shows the hardware logic of moving branch resolution earlier to ID stage. Traditional design execute branch resolution in EX stage, which needs to flush 2 instruction when branching. But this design moves branch resolution to ID stage so we only need to flush 1 instruction. #### Examples I did some experiments on Ripes and look into the 5-Stage RISC-V Processor. When executing branch instruction in pipeline cpu, we'll know whether to branch very late, but pipeline has already started fetching the following instructions. In the case like this : ``` beq x1, x2, target # branch instruction add x3, x4, x5 # the instruction which might be executed incorrectly target: sub x6, x7, x8 ``` `add` instruction was already fetched in last stage and we only know the branch result now. I load immediate to registers x1 and x2, so instruction `beq` will branch. ![image](https://hackmd.io/_uploads/rJpPBRMEZe.png) Here, `beq` at EX stage has decided to branch, so the next instruction `add` will be flushed. ![image](https://hackmd.io/_uploads/rkrxU0MNZg.png) And PC will be updated to the address of target, then instruction `sub` will be fetched. The total cycles from `beq` to `sub` inside `target` is 8 because of flush. ![image](https://hackmd.io/_uploads/rJaCURfE-e.png =50%x) (Minus 2 cycles for loading immediate into x1 and x2) Due to the penalty cost in this case, branch resolution is moved earlier in modern pipelines to predict whether to branch. Based On : * Exercise 19. * Exercise 21. Q4 #### Lecture Materials [Lectures 21-23: RISC-V 5-Stage Pipeline](https://docs.google.com/presentation/d/1v-Squx8lK-oOrflFOwBZh-ue94seVHZudqDOgJmFf5Q/edit?slide=id.g2fa69f884ef_0_2234#slide=id.g2fa69f884ef_0_2234) Section. Control Hazards P.71 - P.78 Section. Superscala Processors P.81 - P.84 ## Question 10 ### Why can full forwarding still fail to eliminate stalls? #### Answer The main reason to this question is that **data cannot be forwarded backward in time**. In other words, when the data required by an instruction has not been calculated or retrieved at the moment it must be used, no amount of forwarding paths can solve the problem. 1. Load-Use Hazard * This is because the data of `LW` will be read from memory after MEM stage finished. * If the following instruction needs this data when `LW` is just starting at EX stage, the data was not generated. * Resulting even if with forwarding, the data generated later than it was used, the processor must forced insertion 1 cycle of stall. 2. Load-Branch Hazard This hazard would get worse if branch resolution was moved earlier into ID stage. * This is because the branch instruction needs to compare the value in the registers in ID stage. * If the previous instruction is `LW`, the data will be generated at the end of MEM, they are 2 stages away. * This will result in 1 or 2 cycles of stall because forwarding cannot forward the future data to the ID stage in the past. 3. Jump Register Dependency * Assume a `jalr` needs the value of `x1` to jump to the target address * If `x1` here is generated by previous instruction and it's not ready. * This could result stall since the jump address must be calculated in ID stage. So, based on all three scenarios above, my conclusion is : Although full forwarding solves most of the dependency like `add` following `sub`, it still can't overcome the inherent latency for `LW` instruction. #### Discussion using MyCPU (3-pipeline) This question can also be discussed with the program file mentioned earlier. In the example scenarios in the program file, including the last examples which we have already answered in detail. They all emphasize that `LW` instruction might be the major blind spot of full forwarding. Based on : * Exercise 19. * Exercise 21. Q3 #### Examples Here are some experiments also be done on Ripes to illustrate the pipeline issues in Question 10. **For Load-use hazard** ``` lw x1, 0(x2) # load data to x1 add x3, x1, x4 # next instruction immediately use x1, results in a stall ``` We can see when `lw` arrives EX stage, and `add` arrives ID stage, everything is same as normal. ![image](https://hackmd.io/_uploads/ryCAG6f4bg.png) But `add` actually needs the data generated by `lw` at ID stage, and the data will be generated after `lw` pass MEM stage. ![image](https://hackmd.io/_uploads/SJLeXpM4Zg.png) So we can see that there's a nop inserted between `add` and `lw`, namely, a stall. This results in 7 cycles. ![image](https://hackmd.io/_uploads/SkUh7TzNZl.png =50%x) **For Load-branch hazard** Here's the state when the first instruction (`lw`) arrives at EX stage. ![image](https://hackmd.io/_uploads/Hy9vSazEbe.png) There's no stall so far, but `beq` needs the value generated by `lw` to determine whether to branch or not. ``` lw x1, 0(x2) # load data beq x1, x0, target # Branch depends on loaded value target: # target function li t0, 0 # the content inside target function does not matter here. ``` Same as the case in Load-use hazard, the value generated by `lw` will be available after passing MEM stage. ![image](https://hackmd.io/_uploads/rJiJ_CM4Zx.png) Therefore, we still need a stall to deal with the hazard in this case. #### Lecture Materials [Lectures 21-23: RISC-V 5-Stage Pipeline](https://docs.google.com/presentation/d/1v-Squx8lK-oOrflFOwBZh-ue94seVHZudqDOgJmFf5Q/edit?slide=id.g2fa69f884ef_0_2234#slide=id.g2fa69f884ef_0_2234) Section. Fixing Data Hazards: Forwarding P.63 - P.65 [MIT 6.004 L16: Processor Pipelining ](https://www.youtube.com/watch?v=TMpjvAvQCWA) Section. Load-to-Use Stalls

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully