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

Implement optimization step to replace use of upvalues with constants where upvalue references a constant #77

Open
dibyendumajumdar opened this issue Jan 23, 2022 · 3 comments
Labels
linearizer Component: Linearizer

Comments

@dibyendumajumdar
Copy link
Owner

Motivation:

local NAME =1

function foo() 
   local t = {}
   t[NAME] = 'Dibyendu'
end

Here NAME is an up-value but we can see it refers to a constant that is never amended. So we can rewrite the code as follows:

function foo() 
   local t = {}
   t[1] = 'Dibyendu'
end
@dibyendumajumdar dibyendumajumdar added the linearizer Component: Linearizer label Jan 23, 2022
@dibyendumajumdar
Copy link
Owner Author

dibyendumajumdar commented Jan 23, 2022

Here is one way to do this:

  • Everytime a store is emitted against an up-value mark the local being references as 'updated'
  • In a separate pass after IR generation, revisit all up-value references that are constant literals and have not been updated; and replace these with constants

p.s. Need to know that all upvalue access occurs post initialization of local. One option is to insist on statements like:

local NAME = 1

That is, initialized in the local statement, and also all RHS values are constants.

@dibyendumajumdar
Copy link
Owner Author

A second more sophisticated approach might be to implement a constant propagation data flow step; but unsure how to handle upvalues there.

@dibyendumajumdar
Copy link
Owner Author

IR opcodes that may update a local (directly or via upvalue) are - MOV, GET*, LOADGLOBAL, CLOSURE.
We need to detect if a local was updated post initialization either directly or indirectly via upvalue; a simple way is to do two things: a) mark a variable that is initialized with literal, b) count assignments to the variable

dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 19, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
dibyendumajumdar added a commit that referenced this issue Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linearizer Component: Linearizer
Projects
None yet
Development

No branches or pull requests

1 participant