Stop analysis at illegal instructions - #13
Conversation
Unsupported JALR and SYSTEM encodings are decoded as K_ILL. K_ILL does not end a basic block, so constant propagation can cross an unreachable illegal word and resolve a later JALR. Make K_ILL end the block and stop CFG traversal. Add jump-over cases for a reserved JALR and a CSR word. Also stop the encoding test when PACK_WORDS fails. make check-all passes. Signed-off-by: thc1006 <hctsai1006@cs.nctu.edu.tw>
9f281a4 to
15ca056
Compare
jserv
left a comment
There was a problem hiding this comment.
Reviewed the control-flow reasoning and the hand-assembled encodings. The change is correct, is a strict improvement (it removes a spurious rejection of valid jump-over-illegal programs), and the two new tests fail without it. One adjacent observation, not related to this PR: the dump command path runs analyze_syscalls/mark_reachable without the resolve_jalr that the emit path runs first, so its ; promotions: diagnostic output can diverge from emit for static-target JALR programs (diagnostic only, no effect on a compiled image).
| const bool link = rd == 1 && (nd->kind == K_JAL || | ||
| (nd->kind == K_JALR && nd->target != NONE)); | ||
| const bool terminates = (nd->kind == K_JAL && !link) || | ||
| const bool terminates = nd->kind == K_ILL || (nd->kind == K_JAL && !link) || |
There was a problem hiding this comment.
This fixes the illegal-word instance of a more general issue rather than the root cause. The underlying trigger is that resolve_jalr marks the target of any resolved JALR as a leader without checking that the JALR is reachable, and analyze_syscalls then clears cprop at that leader. So an unreachable ret with a constant base can corrupt a reachable node's cprop by leaking a constant across any intervening straight-line instruction, not only an illegal one: replace the illegal word in the new test with a plain NOP (00000013) in the same layout and rvopt still rejects the reachable exit path, with and without this change. This PR is a good, targeted fix for the illegal-word case; a follow-up could gate JALR-target leader marking on reachability (iterate resolve_jalr and mark_reachable to a fixpoint) to close the general case.
There was a problem hiding this comment.
Confirmed. Replacing the illegal word with either 00000013 (nop) or 00000033 (add x0, x0, x0) reproduces the same rejection on both main and this branch, confirming that leader marking from unreachable JALRs is the general issue.
I'll keep this PR scoped to treating K_ILL as a control-flow barrier and address reachability-gated JALR target leader marking in a separate follow-up.
| jump-over-reserved-JALR:0100006f,01400093,00009067,00008067,05d00893,00000073 \ | ||
| jump-over-CSR:0100006f,01400093,00001073,00008067,05d00893,00000073 |
There was a problem hiding this comment.
Good regression coverage: both cases reject without the rvopt.c change (unsupported op at pc 20) and accept with it, so they pin the behavior.
|
Thank you prof for the adjacent observation. I confirmed that the I will keep this PR scoped to the illegal-instruction control-flow barrier and cover the dump/emit analysis mismatch in the follow-up, with a regression test for the diagnostic path. UPDATE(UTC+8 PM 4:16): UPDATE(UTC+8 PM 7:39): The broader fix builds on the |
Follow-up to #12: decoding unsupported JALR and SYSTEM encodings as
K_ILLexposed a pre-existing CFG gap where illegal instructions did not terminate analysis, allowing constant propagation to cross unreachable words and resolve a later JALR.Unsupported JALR and SYSTEM encodings are decoded as
K_ILL.K_ILLdoes not end a basic block, so constant propagation can cross an unreachable illegal word and resolve a later JALR.Make
K_ILLend the block and stop CFG traversal. Add jump-over cases for a reserved JALR and a CSR word. Also stop the encoding test whenPACK_WORDSfails.make check-allpasses.Summary by cubic
Stop CFG traversal at illegal instructions to prevent constant propagation from crossing unreachable words and wrongly resolving later
JALR. TreatK_ILLas a basic-block end and a terminator; add jump-over tests and fail early on encoding errors.is_block_end()now treatsK_ILLas a block terminator.successors()stops traversal atK_ILL.JALRandCSRso unreachable illegal words don’t affect reachable paths.verify-muxnow fails ifPACK_WORDScannot encode input.Written for commit 15ca056. Summary will update on new commits.