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

Simplify the return path analysis #6541

Merged
merged 19 commits into from
Oct 24, 2024
Merged

Conversation

jjcnn
Copy link
Contributor

@jjcnn jjcnn commented Sep 12, 2024

Description

The return path analysis ensures that if a method must return a value then the spine of the method returns a value. The analysis traverses a linearized version of the CFG (one which ignores branching expressions such as if-then-else and loops, and simply treats them as a single node) of each method from the (unique) entry point to the (unique) exit point. If there is ever a node in the graph that does not have outgoing edges, then an error is thrown.

When constructing such a linearized CFG we have so far treated local impls as branches, meaning that the spine was in fact not linear:

fn main() -> u64 {
   // This point has two outgoing edges

   // One goes to here
    impl core::ops::Eq for X {
        fn eq(self, other: Self) -> bool {
            asm(r1: self, r2: other, r3) {
                eq r3 r2 r1;
                r3: bool
            }
        }
    }

    // The other goes to here
    if X::Y(true) == X::Y(true) {
        a
    } else {
        a
    }
}

This has been the case even though the edge into a local impl does not in fact represent legal control flow.

This incorrect construction has made the traversal of the spine-CFG very convoluted and difficult to follow.

This PR fixes the incorrect construction, and simplifies the traversal of the spine so that it is clear what is going on. I have also added an additional test to show that methods inside local impls are in fact analyzed separately, even though there is no longer an edge going into the impl from the surrounding function.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@jjcnn jjcnn requested a review from a team as a code owner September 12, 2024 20:04
Copy link

codspeed-hq bot commented Sep 12, 2024

CodSpeed Performance Report

Merging #6541 will not alter performance

Comparing jjcnn/return_path_analysis_exit_fix (2285dcf) with master (be70774)

Summary

✅ 22 untouched benchmarks

Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

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

Can we test this to validate some previously unhandled state at least?

@jjcnn
Copy link
Contributor Author

jjcnn commented Sep 13, 2024

Can we test this to validate some previously unhandled state at least?

I haven't been able to find an example where the old algorithm gave an incorrect result. I can keep looking if you want?

When I removed the rovers[0] != exit_point check I got an error in this test case (as well as in a number of others):

https://github.com/FuelLabs/sway/tree/master/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_in_fn_decl

That's the one I used to debug my algorithm.

@jjcnn jjcnn requested a review from a team October 9, 2024 15:07
@jjcnn
Copy link
Contributor Author

jjcnn commented Oct 9, 2024

@IGI-111 : I managed to fix the linearization problem, and the traversal algorithm is much cleaner now. Take a look at your convenience.

@jjcnn jjcnn changed the title Ensure the return path analysis traverses the entire CFG Simplify the return path analysis Oct 14, 2024
@IGI-111 IGI-111 requested a review from a team October 17, 2024 23:54
@tritao tritao merged commit c374a11 into master Oct 24, 2024
40 checks passed
@tritao tritao deleted the jjcnn/return_path_analysis_exit_fix branch October 24, 2024 12:56
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.

4 participants