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

Optimizing chain assignments #151

Open
cardillan opened this issue Sep 25, 2024 · 1 comment
Open

Optimizing chain assignments #151

cardillan opened this issue Sep 25, 2024 · 1 comment
Labels
enhancement New feature or request optimizer Related to the code optimizer

Comments

@cardillan
Copy link
Owner

The Data Flow optimization needs to be expanded to handle the following construct:

i = rand(10)
a = i
i = rand(20)
b = i
print(a, b)

Currently, this code produces:

op rand i 10 0
set a i
op rand i 20 0
print a
print i
end

Of course, the source code typically doesn't contain such constructs, but this essentially is what some other optimizations, such as inlining function calls or unrolling a list iteration loop with modification may produce. We'd want to replace op rand i 10 0 with op rand a 10 0 and drop the set a i.

@cardillan cardillan added enhancement New feature or request optimizer Related to the code optimizer labels Sep 25, 2024
@cardillan
Copy link
Owner Author

A new optimization - "backpropagation" - was added to the Data Flow Optimization.

Up to now, Data Flow Optimization was only able to modify instruction arguments based on the data flow analyzed up to the instruction being inspected. A special mode was added which allows to modify instructions prior to the one being inspected: when a set instruction assigning a source variable to a target variable is encountered, the original instruction producing ("defining") the source variable is found. If the target variable's value is not needed between the assignments, and the source variable is not needed elsewhere, the set is dropped and the source variable is replaced with the target variable in the defining instruction.

The code above now produces (although in this particular example the compiled variable names match better the "spirit" of the source code, in general this is not guaranteed):

op rand :a 10 0
op rand :b 20 0
print :a
print :b

Will be included in version 3.1.0 as an experimental feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request optimizer Related to the code optimizer
Projects
None yet
Development

No branches or pull requests

1 participant