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

Shorten the time spent checking instruction length #92

Merged

Conversation

qwe661234
Copy link
Collaborator

I think we can pass the value of instruction length in IR to the exception handler rather than verifying it each time the emulate function is called, because we only need the value of rv->compressed in the exception handler.

@jserv
Copy link
Contributor

jserv commented Dec 7, 2022

Once we complete the instruction decoding, we can definitely know the length of instruction(s) pointed by specific address. Would it make sense?

@qwe661234
Copy link
Collaborator Author

Once we complete the instruction decoding, we can definitely know the length of instruction(s) pointed by specific address. Would it make sense?

Yes, we can know the length of instruction once instruction decoding completes. However, we should send a value to the exception handler if we want to reduce the number of times we check the instruction length.

@jserv
Copy link
Contributor

jserv commented Dec 7, 2022

Yes, we can know the length of instruction once instruction decoding completes. However, we should send a value to the exception handler if we want to reduce the number of times we check the instruction length.

If we can look up the instruction length (RV32 vs. RV32C) from a specific address, there is no need to preserve extra parameter in exception handlers. Let's keep it simple and stupid (KISS principle).

@qwe661234
Copy link
Collaborator Author

Yes, we can know the length of instruction once instruction decoding completes. However, we should send a value to the exception handler if we want to reduce the number of times we check the instruction length.

If we can look up the instruction length (RV32 vs. RV32C) from a specific address, there is no need to preserve extra parameter in exception handlers. Let's keep it simple and stupid (KISS principle).

Sorry, I miss the important information that we get instruction length by parsing instruction opcode, instead of specific address. If we don't want to pass more parameter to exception handlers, we need to save more information in struct riscv_t.

@jserv
Copy link
Contributor

jserv commented Dec 7, 2022

Since preliminary basic block was introduced, speculated execution can take place without frequent instruction length checks. That is, the explicit transition from 32-bit ISA to 16-bit counterpart (vice versa) can be lazily evaluated in a basic block.

@qwe661234
Copy link
Collaborator Author

qwe661234 commented Dec 7, 2022

Since preliminary basic block was introduced, speculated execution can take place without frequent instruction length checks. That is, the explicit transition from 32-bit ISA to 16-bit counterpart (vice versa) can be lazily evaluated in a basic block.

I think the idea above is checking instruction length in a basic block. However, there is a situation I am unsure whether it will occur or not. If a 32-bit instruction and a 16-bit compressed instruction coexist in the same basic block, it is unsafe to determine whether the instruction is compressed in basic block.

@Risheng1128
Copy link
Collaborator

We need to know the instruction is compressed or not when the exception occurred. So, I think that rv->compressed can be determined until the exception occurred.

For example, rewrite instruction jal as following:

case rv_insn_jal: { /* JAL: Jump and Link */
        ...
        /* check instruction misaligned */
        if (insn_is_misaligned(rv->PC)) {
+           rv->compressed = false;
            rv_except_insn_misaligned(rv, pc);
            return false;
        }
        /* can branch */
        return true;
    }

Therefore, we don't need to pass instruction length to exception handler and avoid to check rv->compressed frequently.

@qwe661234
Copy link
Collaborator Author

We need to know the instruction is compressed or not when the exception occurred. So, I think that rv->compressed can be determined until the exception occurred.

For example, rewrite instruction jal as following:

case rv_insn_jal: { /* JAL: Jump and Link */
        ...
        /* check instruction misaligned */
        if (insn_is_misaligned(rv->PC)) {
+           rv->compressed = false;
            rv_except_insn_misaligned(rv, pc);
            return false;
        }
        /* can branch */
        return true;
    }

Therefore, we don't need to pass instruction length to exception handler and avoid to check rv->compressed frequently.

Ok, I think it is better idea, I will follow this idea.

@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from 55f30c9 to 3a6c4ff Compare December 8, 2022 16:31
@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from 3a6c4ff to 3d36c67 Compare December 9, 2022 11:32
@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch 2 times, most recently from b99b203 to 1f36f8b Compare December 9, 2022 12:30
@jserv jserv requested a review from Risheng1128 December 9, 2022 12:54
@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from 87e36b9 to 0c39344 Compare December 9, 2022 16:43
@Risheng1128
Copy link
Collaborator

The commit Move instruction length checking should merge into the commit Reduce instruction length checking.

@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from 0c39344 to 7f43836 Compare December 9, 2022 17:08
@jserv jserv requested a review from Risheng1128 December 9, 2022 21:30
@jserv jserv changed the title Refactor exception handler Shorten the time spent checking instruction length Dec 9, 2022
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read https://cbea.ms/git-commit/ carefully for writing informative git commit messages.
You should explain the purpose of the proposed changes as well as their advantages.

@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch 3 times, most recently from 0f7858d to f202c3c Compare December 11, 2022 13:23
@qwe661234 qwe661234 requested review from jserv and removed request for jserv December 11, 2022 13:24
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, read https://cbea.ms/git-commit/ carefully.

Last but not least, clearly state how the proposed changes would be beneficial.

qwe661234 added a commit to qwe661234/rv32emu that referenced this pull request Dec 11, 2022
Move instruction length checking into exception block to avoid frequently checking.

In the past, each time the function "emulate" was called, we had to check the instruction length. Actually, we only need to check the instruction length when the exception occurs; otherwise, we can simply set "rv->compressed" to the appropriate value before invoking the exception handler. By doing this, we can save the overhead of checking instruction length if exception doesn't occur.

Resolves: sysprog21#92
@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from f202c3c to cd8ebae Compare December 11, 2022 14:37
@qwe661234 qwe661234 requested a review from jserv December 11, 2022 14:37
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you understand what Wrap the body at 72 characters means?

@jserv
Copy link
Contributor

jserv commented Dec 11, 2022

By the way, use QuillBot to improve your writing.

@qwe661234
Copy link
Collaborator Author

Do you understand what Wrap the body at 72 characters means?

I see, I need to change line when characters in a line more than 72.

To avoid frequent instruction length checking, move checking into
exception handler inoking block.

Previously, we had to check the instruction length each time the
function 'emulate' was called. Actually, we only need to check it when
the exception occurs; otherwise, we can simply set 'rv->compressed' to
the appropriate value before running the exception handler.

By doing so, we can avoid the overhead of checking instruction length if
an exception does not happen.

Resolves: sysprog21#92
@qwe661234 qwe661234 force-pushed the wip/instruction-decode branch from cd8ebae to 4bbb2c3 Compare December 11, 2022 15:18
@qwe661234 qwe661234 requested a review from jserv December 11, 2022 15:18
@jserv jserv merged commit e885b22 into sysprog21:wip/instruction-decode Dec 11, 2022
@qwe661234 qwe661234 deleted the wip/instruction-decode branch March 21, 2023 07:50
vestata pushed a commit to vestata/rv32emu that referenced this pull request Jan 24, 2025
To avoid frequent instruction length checking, move checking into
exception handler block.

Previously, we had to check the instruction length each time the
function 'emulate' was called. Actually, we only need to check it when
the exception occurs; otherwise, we can simply set rv->compressed to
the appropriate value before running the exception handler.

By doing so, we can avoid the overhead of checking instruction length if
an exception does not happen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants