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: Optimize k*n and k+n when types are known #1529

Merged
merged 2 commits into from
Nov 19, 2024
Merged

Conversation

zeux
Copy link
Collaborator

@zeux zeux commented Nov 16, 2024

When type information is specified, we can compile k*n and k+n into MULK/ADDK forms that are faster to execute, as long as we think n is a number. Since we generally restrict type aware optimizations to O2, this does that as well.

This makes trig benchmark ~4% faster on Apple M2 in VM, and also a tiny improvement on scimark (~0.1%) can be observed. The optimization only affects interpreted execution, as NCG already can synthesize optimal code here.

If the type information is not truthful (e.g. user annotates type as a number and it's not), the worst case scenario is flipped arguments to metamethods like __add/__mul for constant left hand side.

Fixes #626 (the fix requires type information or NCG but I doubt any further work on this is warranted)

When type information is specified, we can compile k*n and k+n
into MULK/ADDK forms that are faster to execute, as long as we think
n is a number. Since we generally restrict type aware optimizations
to O2, this does that as well.

This makes trig benchmark ~4% faster on Apple M2 in VM, and also a
tiny improvment on scimark (~0.1%) can be observed. The optimization
only affects interpreted execution, as NCG already can synthesize
optimal code here.

If the type information is not truthful (e.g. user annotates type as
a number and it's not), the worst case scenario is flipped arguments
to metamethods like __add/__mul for constant left hand side.
@vegorov-rbx vegorov-rbx merged commit b1b21f3 into master Nov 19, 2024
8 checks passed
@vegorov-rbx vegorov-rbx deleted the opt-revaddmul branch November 19, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent performance with order of operations with constant
2 participants