Lambert
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: rv32emu --- # rv32emu 改進方案 # issues # 過多 block 每個 block 紀錄到的指令數量有多有少,執行頻率也不一樣,最遭的情況是指令多的執行1次,指令少的執行很多次。 解法: 合併 block 以 c2mir 的 API 來看只有兩種做法,合併 function body 或是合併 insn 再重新 codegen。 MIR 的文件關於 module 有提到 MIR_new_import 的函式,我看不懂怎麼用,因為參數只要丟 ctx 跟字串就好了,但是我追蹤 MIR_link 使用 import_resolver 時會用到這個函式。 從 MIR 輸出的 IR 可以看到 import_solver 引入的函式(假設是 printf)會變成 `import printf`,我不確定 MIR 的編譯器會不會去優化 printf 還是單純 import,看起來後者比較有可能發生。 # MIR module 沒有清理 * 以 rv32emu 的現況來看,編譯出 JIT 函式就可以刪掉,因為 module 之後不會用到 * ravi 沒看到有刪除 module 的操作,但是會去用 [GC 回收編譯後的程式碼](https://the-ravi-programming-language.readthedocs.io/en/latest/ravi-jit-status.html#the-architecture-of-ravi-s-jit-compilation) > The compiled code is garbage collected as normal by Lua --- # Control flow graph 紀錄 Hot path > 有學過 graph,不過目前還不會寫 CFG 但是想到怎麼用 CFG 來 codegen 跟處理 branch 的方式。 使用 CFG 紀錄 hot path 直到超過預期的數量或是回到迴圈的開頭,也可以像 ravi 設定一個指令數量的 range 來判斷是否要 codegen,這能夠比過往單存紀錄 branch, jal/jalr 還要多的指令,也可以改善目前執行 block by block 遇到的問題。 步驟如下 * 建立 CFG 並開始紀錄指令,重複的指令就不要放進 CFG,如此便能夠紀錄迴圈而不紀錄重複的 insn 到 CFG。 * 走訪 CFG 來 codegen * 編譯 codegen 的 C 程式碼 * 快取 JIT 函式 ## 分支 codegen 的處理方式 分支的 codegen 用 next 紀錄跳躍的地址也就能夠檢查是否要跳躍,再去生成 true 跟 false 的對應程式碼。 先把 true 的 blocks 都生成完才生成 `} else {` 跟之後的 blocks,因此 traversal 的方式很重要。 ```c void emit_branch(rv, insn) { bool taken = false; // .... uint32_t next = 0; if(taken) { rv->PC += imm; next = pc; } else { rv->PC += 4; } } if(rv->PC == next) { // true } else { // false } ``` 以下圖的 CFG 為例,其中 A 跟 B 有分支,true 用紅色表示,false 則用藍色,黑色表示下一個 ```graphviz digraph { rankdir="TD"; block_a[label="A",shape="circle", style="bold, filled", fillcolor="#ffffff"] block_b[label="B",shape="circle", style="bold, filled", fillcolor="#ffffff"] block_c[label="C",shape="circle", style="bold, filled", fillcolor="#ffffff"] block_d[label="D",shape="circle", style="bold, filled", fillcolor="#ffffff"] node [ shape="circle", style="bold, filled", fillcolor="#dddddd",fixedsize=true,width=0.53 ]; block_a -> block_b[color=red] block_a -> block_c[color=blue] block_b -> block_c[color=red] block_b -> block_d[color=blue] block_c -> block_b block_c -> block_d[style=invis] } ``` 預期生成的 C code 大致會像下面這樣,為了避免重複生成一樣的 block 可以用 goto 來處理 ```c [block A] if(rv->PC == next_A) { block_B: [block B] } else { block_C: [block C] goto block_B } if(rv->PC == next_B) { goto block_C; } else { [block D] } ``` * 因為有 goto 跟 if 的緣故,不能保證執行指令就跟紀錄到的指令長度一樣,因此要在 codegen 時寫入更新虛擬機 cycle 的程式碼 * 或許能用 predict 的方式去合併下一塊 block,如下範例,剛開始會先生成 i < 500 的 block 之後才生成 else 區塊的 block,兩者可以做合併 ```c int sum = 0; for(int i = 0; i < 1000; i++) { if(i < 500) { sum += 2; } else { sum += 1 } } ```

    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