Finish RISC-V TODO backlog: switch lowering, result returns, optimizer tweaks#41
Merged
mengzhuo merged 2 commits intoplctlab:mainfrom Nov 7, 2025
DzmingLi:main
Merged
Finish RISC-V TODO backlog: switch lowering, result returns, optimizer tweaks#41mengzhuo merged 2 commits intoplctlab:mainfrom DzmingLi:main
mengzhuo merged 2 commits intoplctlab:mainfrom
DzmingLi:main
Conversation
This commit resolves 5 TODO items in the MoonBit compiler, improving code quality, optimization capabilities, and error handling. All changes are original contributions not present in bmwangmh/workspace branch. ## 1. Pure Function Call Elimination (src/riscv_opt_peephole.ml:171) - Implement dead code elimination for pure functions with unused results - Improve is_pure function to support local alloca storage - Add comprehensive test suite (test/src/pure_elim/) - Optimization allows removal of side-effect-free calls, reducing code size ## 2. Refactor inst_convert Function (src/riscv_virtasm.ml:289) - Reduce code duplication using typed helper functions - Add explicit type annotations to prevent OCaml type inference issues - Improve code structure with 10 conversion helpers for different instruction types - Reduce repetitive pattern matching from 88 lines to organized, maintainable code ## 3. Extend Switch Statement Type Support (src/riscv_generate.ml:1311) - Add support for C_byte, C_int64, C_uint, C_uint64 in switch statements - Add clear TODO markers for unsupported types (float, double, bigint, string, bytes) - Include test suite with future test cases documented (test/src/switch_types/) - Prepare compiler for extended type support when MoonBit language adds these features ## 4. Optimize JumpIndirect (src/riscv_virtasm_generate.ml:377) - Add validation for empty possibility list (fail fast with clear error) - Preserve single-target optimization (convert to direct jump) - Restructure to match expression for better clarity - Document future optimization direction (conditional branches for 2-3 targets) ## 5. Handle default_expr Conflicts (src/ast_derive.ml:1996) - Properly handle case where both positional and named layouts specify defaults - Check for equality: accept if same, error with clear message if different - Improve error reporting with parameter name/index in conflict messages - Replace generic "TODO" failwith with semantic validation ## Test Coverage - test/src/pure_elim/: Validates pure function elimination optimization - test/src/switch_types/: Tests existing switch types, documents future additions - All existing tests continue to pass
This commit implements 6 TODO items and adds comprehensive test coverage: 1. Error_result return handling (riscv_generate.ml:1171) - Wrap return values in Result<T, E> constructor - Handle both Ok (tag=1) and Err (tag=0) cases - Properly allocate and store tagged values 2. String constant switch support (riscv_generate.ml:1336) - Generate if-else chain with proper UTF-16 string comparison - Use memcmp for byte-by-byte comparison of UTF-16 LE encoded strings - First compare lengths, then compare content 3. Bytes constant switch support (riscv_generate.ml:1424) - Similar to string switch but for byte arrays - Each byte is 1 byte (not UTF-16 like strings) - Use memcmp for content comparison 4. Register allocation optimization (riscv_reg_alloc.ml:123) - Implement least-used register selection strategy - Replace arbitrary choice with usage-based heuristic - Reduce register pressure through better allocation 5. JumpIndirect optimization for 2 targets (riscv_virtasm_generate.ml:388) - Convert 2-target indirect jumps to conditional branches - Load target address and compare with jump register - Avoid indirect jump pipeline flush 6. Interpreter memcmp support (test/interpreter.cpp:365) - Add memcmp function to SSA interpreter - Required for string/bytes comparison to work in tests Tests added: - test/src/error_result: Tests Result<T,E> wrapping for error returns - test/src/string_switch: Tests string pattern matching - test/src/bytes_switch: Tests bytes pattern matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
lowering backed by interpreter
memcmp, and improve register allocation plus JumpIndirect2-target lowering
inst_convert,extend integer switch support, and guard against conflicting default expressions
elimination, and wider switch type coverage