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

compiler: improve boundary analysis and optimizations #110

Merged
merged 14 commits into from
Aug 21, 2024
Merged

Conversation

mertcandav
Copy link
Member

This PR implements better boundary analysis to optimize memory accesses.

In current implementation, boundary analysis was only performed for iterations, to do this the semantic analyzer associated each iteration variable with the iteration expression and had some strict conditions: for example optimization was mostly applied when the index value was immutable, but it could not optimize the cost of boundary checking where it was mutable and could be used safely before being mutated.

In addition, compiler optimizes array indexing with constant expressions. However, that's a simple optimization. Just check whether expression is constant and fits size of array.

New Boundary Analysis Implementation
The new boundary analysis tracks constant values ​​and expressions that can be predicted at runtime. If the expressions are safe until they are mutated or being risky, it optimizes boundary access checking and provides faster access to memory at runtime without compromising safety.

For example:

len(x) == 3 && x[0] == 'f' && x[1] == 'o' && x[2] == 'o'

In the code example above, the compiler knows at compile time that the length of x is 3, so it skips the runtime boundary checking analysis of subsequent accesses.

@mertcandav mertcandav added the compiler Related with compiler/compile-time label Aug 21, 2024
@mertcandav mertcandav self-assigned this Aug 21, 2024
@mertcandav mertcandav merged commit d7dc0e4 into master Aug 21, 2024
42 checks passed
@mertcandav mertcandav deleted the boundary branch August 21, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Related with compiler/compile-time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant